numpy展平數(shù)組ndarray.flatten()詳解
numpy展平數(shù)組ndarray.flatten()
ndarray.flatten(order=‘C')
復(fù)制原數(shù)組,并將其展平成一維數(shù)組返回。
Params:
order : {‘C’, ‘F’, ‘A’, ‘K’},可選任意一個,默認(rèn)是‘C’。
- C:行為主要順序,從左至右,從上至下
- F:列為主要順序,從上至下,從左至右

舉例,原始數(shù)組:
arr = np.arange(9).reshape(3,3)
arr
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])按‘C’風(fēng)格展平:
arr.flatten() array([0, 1, 2, 3, 4, 5, 6, 7, 8])
按’F’風(fēng)格展平:
arr.flatten('F')
array([0, 3, 6, 1, 4, 7, 2, 5, 8])numpy.ndarray實現(xiàn)扁平化numpy.ndarray.flatten
numpy.ndarray.flatten(order=‘C')
官方鏈接
把多維數(shù)組"扁平化"為一個一維向量,其過程是把該數(shù)組按照order指定的順序遍歷一遍,并把結(jié)果儲存為一維向量.
Parameters
order {‘C', ‘F', ‘A', ‘K'}, optional
‘C' (Default) means to flatten in row-major (C-style) order.
‘F' means to flatten in column-major (Fortran- style) order.
‘A' means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise.
‘K' means to flatten a in the order the elements occur in memory. The default is ‘C'.示例
a = np.array([[1,2], [3,4]]) print(a.flatten())
結(jié)果
[1 2 3 4]
相當(dāng)于
print(a.reshape(1,a.size))
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實現(xiàn)字符串連接的三種方法及其效率、適用場景詳解
本篇文章主要介紹了python實現(xiàn)字符串連接的三種方法及其效率、適用場景詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01
TensorFlow進(jìn)階學(xué)習(xí)定制模型和訓(xùn)練算法
本文將為你提供關(guān)于 TensorFlow 的中級知識,你將學(xué)習(xí)如何通過子類化構(gòu)建自定義的神經(jīng)網(wǎng)絡(luò)層,以及如何自定義訓(xùn)練算法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
對Python 多線程統(tǒng)計所有csv文件的行數(shù)方法詳解
今天小編就為大家分享一篇對Python 多線程統(tǒng)計所有csv文件的行數(shù)方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
使用Python爬了4400條淘寶商品數(shù)據(jù),竟發(fā)現(xiàn)了這些“潛規(guī)則”
這篇文章主要介紹了使用Python爬了4400條淘寶商品數(shù)據(jù),竟發(fā)現(xiàn)了這些“潛規(guī)則”,筆者用 Python 爬取淘寶某商品的全過程,并對商品數(shù)據(jù)進(jìn)行了挖掘與分析,最終得出結(jié)論。需要的朋友可以參考下2018-03-03

