Python中flatten( )函數及函數用法詳解


flatten()函數用法

flatten是numpy.ndarray.flatten的一個函數,即返回一個一維數組。

flatten只能適用於numpy對象,即array或者mat,普通的list列表不適用!。

a.flatten():a是個數組,a.flatten()就是把a降到一維,默認是按行的方向降 。
a.flatten().A:a是個矩陣,降維后還是個矩陣,矩陣.A(等效於矩陣.getA())變成了數組。具體看下面的例子:

1、用於array(數組)對象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
>>> from numpy import *
>>> a = array([[ 1 , 2 ],[ 3 , 4 ],[ 5 , 6 ]])
>>> a
array([[ 1 , 2 ],
     [ 3 , 4 ],
     [ 5 , 6 ]])
>>> a.flatten() #默認按行的方向降維
array([ 1 , 2 , 3 , 4 , 5 , 6 ])
>>> a.flatten( 'F' ) #按列降維
array([ 1 , 3 , 5 , 2 , 4 , 6 ])
>>> a.flatten( 'A' ) #按行降維
array([ 1 , 2 , 3 , 4 , 5 , 6 ])
>>>

2、用於mat(矩陣)對象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> a = mat([[ 1 , 2 , 3 ],[ 4 , 5 , 6 ]])
>>> a
matrix([[ 1 , 2 , 3 ],
     [ 4 , 5 , 6 ]])
>>> a.flatten()
matrix([[ 1 , 2 , 3 , 4 , 5 , 6 ]])
>>> a = mat([[ 1 , 2 , 3 ],[ 4 , 5 , 6 ]])
>>> a
matrix([[ 1 , 2 , 3 ],
     [ 4 , 5 , 6 ]])
>>> a.flatten()
matrix([[ 1 , 2 , 3 , 4 , 5 , 6 ]])
>>> y = a.flatten().A
>>> shape(y)
( 1L , 6L )
>>> shape(y[ 0 ])
( 6L ,)
>>> a.flatten().A[ 0 ]
array([ 1 , 2 , 3 , 4 , 5 , 6 ])
>>>

從中可以看出matrix.A的用法和矩陣發生的變化。

3、但是該方法不能用於list對象,想要list達到同樣的效果可以使用列表表達式:

?
1
2
3
4
5
>>> a = array([[ 1 , 2 ],[ 3 , 4 ],[ 5 , 6 ]])
>>> [y for x in a for y in x]
[ 1 , 2 , 3 , 4 , 5 , 6 ]
>>>

下面看下Python中flatten用法

一、用在數組

?
1
2
3
4
>>> a = [[ 1 , 3 ],[ 2 , 4 ],[ 3 , 5 ]]
>>> a = array(a)
>>> a.flatten()
array([ 1 , 3 , 2 , 4 , 3 , 5 ])

二、用在列表

如果直接用flatten函數會出錯

?
1
2
3
4
5
6
7
>>> a = [[ 1 , 3 ],[ 2 , 4 ],[ 3 , 5 ]]
>>> a.flatten()
 
Traceback (most recent call last):
  File "<pyshell#10>" , line 1 , in <module>
   a.flatten()
AttributeError: 'list' object has no attribute 'flatten'

正確的用法

?
1
2
3
4
>>> a = [[ 1 , 3 ],[ 2 , 4 ],[ 3 , 5 ],[ "abc" , "def" ]]
>>> a1 = [y for x in a for y in x]
>>> a1
[ 1 , 3 , 2 , 4 , 3 , 5 , 'abc' , 'def' ]

或者(不理解)

?
1
2
3
4
>>> a = [[ 1 , 3 ],[ 2 , 4 ],[ 3 , 5 ],[ "abc" , "def" ]]
>>> flatten = lambda x: [y for l in x for y in flatten(l)] if type (x) is list else [x]
>>> flatten(a)
[ 1 , 3 , 2 , 4 , 3 , 5 , 'abc' , 'def' ]

三、用在矩陣

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> 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 ])
 
 
 
 
2017年老男孩全棧python第2期視頻教程 attach_img
55G老男孩python全棧開發全套視頻教程 包含:基礎篇,前端篇,web框架篇,項目實戰篇 attach_img
2018全棧開發Flask Python Web網站編程價值2400
2018Python Flask打造一個視頻網站實戰視頻教程 attach_img  ...2
2018小象學院《分布式爬蟲實戰》第二期視頻教程
Python高級編程技巧實戰 基於Python項目與面試題講解  ...2
Python分布式爬蟲打造搜索引擎網站(價值388元)
小象最新Python機器學習升級版視頻學習教程
麥子學院Python自動化開發 類Zabbix監控項目開發與實戰視頻教程
2017AI人工智能時代基礎實戰python機器學習深度學習算法全套視頻教程
AI人工智能時代基礎實戰python機器學習深度學習算法視頻教程  ...2
2017年最新Python3.6網絡爬蟲實戰案例基礎+實戰+框架+分布式高清視頻教程
Python數據分析基礎與實踐 Python數據分析實踐課程 Python視頻教程
2017AI人工智能基礎實戰python機器深度學習算法視頻教程  ...2
Python Matplotlib實戰視頻教程 莫煩老師Python課程+麻省理工MIT Python大牛公開課 attach_img
Python自動化開發-類Zabbix監控項目開發與實戰 老男孩教學總監Alex主講 attach_img
2017最新python數據分析入門與實戰 attach_img  ...2
2017最新python入門+進階+實戰課堂教學管理系統開發全套完整版  ...2
2017年最新Python3.6網絡爬蟲實戰案例基礎+實戰+框架+分布式高清視頻教程
高清中文PDF:數據處理、分析、科學計算、自然語言處理  ...2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM