numpy下的flatten()函數(shù)用法詳解
flatten是numpy.ndarray.flatten的一個(gè)函數(shù),其官方文檔是這樣描述的:
ndarray.flatten(order='C')
Return a copy of the array collapsed into one dimension.
Parameters:
|
order : {‘C', ‘F', ‘A', ‘K'}, optional ‘C' 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'. |
|
| Returns: |
y : ndarray A copy of the input array, flattened to one dimension. |
即返回一個(gè)折疊成一維的數(shù)組。但是該函數(shù)只能適用于numpy對(duì)象,即array或者mat,普通的list列表是不行的。
例子:
1、用于array對(duì)象
from numpy import * >>>a=array([[1,2],[3,4],[5,6]]) ###此時(shí)a是一個(gè)array對(duì)象 >>>a array([[1,2],[3,4],[5,6]]) >>>a.flatten() array([1,2,3,4,5,6])
2、用于mat對(duì)象
>>> a=mat([[1,2,3],[4,5,6]]) >>> a matrix([[1, 2, 3], [4, 5, 6]])<br>>>> a.flatten()<br>matrix([[1, 2, 3, 4, 5, 6]])<br>
3、但是該方法不能用于list對(duì)象
>>> a=[[1,2,3],[4,5,6],['a','b']] [[1, 2, 3], [4, 5, 6], ['a', 'b']] >>> a.flatten() ###報(bào)錯(cuò) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'list' object has no attribute 'flatten'
想要list達(dá)到同樣的效果可以使用列表表達(dá)式:
>>> [y for x in a for y in x] [1, 2, 3, 4, 5, 6, 'a', 'b']
4、用在矩陣
>>> a = [[1,3],[2,4],[3,5]] >>> a = mat(a) >>> y = a.flatten() >>> y matrix([[1, 3, 2, 4, 3, 5]]) >>> y = a.flatten().A >>> y array([[1, 3, 2, 4, 3, 5]]) >>> shape(y) (1, 6) >>> shape(y[0]) (6,) >>> y = a.flatten().A[0] >>> y array([1, 3, 2, 4, 3, 5])
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
運(yùn)籌學(xué)-Python實(shí)現(xiàn)圖論與最短距離
需要求解任意兩個(gè)節(jié)點(diǎn)之間的最短距離,使用?Floyd?算法,只要求解單源最短路徑問題,有負(fù)權(quán)邊時(shí)使用?Bellman-Ford?算法,沒有負(fù)權(quán)邊時(shí)使用?Dijkstra?算法,本節(jié)我們只討論Dijkstra?算法,需要的朋友可以參考一下2022-01-01
Pandas 解決dataframe的一列進(jìn)行向下順移問題
今天小編就為大家分享一篇Pandas 解決dataframe的一列進(jìn)行向下順移問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python連接PostgreSQL數(shù)據(jù)庫并查詢數(shù)據(jù)的詳細(xì)指南
在現(xiàn)代軟件開發(fā)中,數(shù)據(jù)庫是存儲(chǔ)和檢索數(shù)據(jù)的核心組件,PostgreSQ是一個(gè)功能強(qiáng)大的開源對(duì)象關(guān)系數(shù)據(jù)庫系統(tǒng),它以其穩(wěn)定性、強(qiáng)大的功能和靈活性而聞名,Python作為一種流行的編程語言,與PostgreSQL的結(jié)合使用非常廣泛,本文介紹了Python連接PostgreSQL數(shù)據(jù)庫并查詢數(shù)據(jù)2024-12-12
Python pygame繪制文字制作滾動(dòng)文字過程解析
這篇文章主要介紹了Python pygame繪制文字制作滾動(dòng)文字過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
python實(shí)現(xiàn)全盤掃描搜索功能的方法
今天小編就為大家分享一篇python實(shí)現(xiàn)全盤掃描搜索功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02
python 爬取華為應(yīng)用市場(chǎng)評(píng)論
項(xiàng)目需要爬取評(píng)論數(shù)據(jù),在此做一個(gè)記錄,這里爬取的是web端的數(shù)據(jù),以后可能會(huì)考慮爬取android app中的數(shù)據(jù)。2021-05-05
Pandas DataFrame操作數(shù)據(jù)增刪查改
我們?cè)谟?nbsp;pandas 處理數(shù)據(jù)的時(shí)候,經(jīng)常會(huì)遇到用其中一列數(shù)據(jù)替換另一列數(shù)據(jù)的場(chǎng)景。這一類的需求估計(jì)很多人都遇到,當(dāng)然還有其它更復(fù)雜的。解決這類需求的辦法有很多,這里我們來推薦幾個(gè),這篇文章主要介紹了Pandas DataFrame操作數(shù)據(jù)的增刪查改2022-10-10

