Python的numpy庫中將矩陣轉(zhuǎn)換為列表等函數(shù)的方法
這篇文章主要介紹Python的numpy庫中的一些函數(shù),做備份,以便查找。
(1)將矩陣轉(zhuǎn)換為列表的函數(shù):numpy.matrix.tolist()
返回list列表
Examples
>>>
>>> x = np.matrix(np.arange(12).reshape((3,4))); x matrix([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.tolist() [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
(2)將數(shù)組轉(zhuǎn)換為列表的函數(shù):numpy.ndarray.tolist()
Notes:(數(shù)組能夠被重新構(gòu)造)
The array may be recreated, a=np.array(a.tolist()).
Examples
>>>
>>> a = np.array([1, 2]) >>> a.tolist() [1, 2] >>> a = np.array([[1, 2], [3, 4]]) >>> list(a) [array([1, 2]), array([3, 4])] >>> a.tolist() [[1, 2], [3, 4]]
(3)numpy.mean()計算矩陣或數(shù)組的均值:
Examples
>>>
>>> a = np.array([[1, 2], [3, 4]]) #對所有元素求均值 >>> np.mean(a) 2.5 >>> np.mean(a, axis=0) #對每一列求均值 array([ 2., 3.]) >>> np.mean(a, axis=1) #對每一行求均值 array([ 1.5, 3.5])
(4)numpy.std()計算矩陣或數(shù)組的標準差:
Examples
>>>
>>> a = np.array([[1, 2], [3, 4]]) #對所有元素求標準差 >>> np.std(a) 1.1180339887498949 >>> np.std(a, axis=0) #對每一列求標準差 array([ 1., 1.]) >>> np.std(a, axis=1) #對每一行求標準差 array([ 0.5, 0.5])
(5)numpy.newaxis為數(shù)組增加一個維度:
Examples:
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]]) #先輸入3行2列的數(shù)組a >>> b=a[:,:2] >>> b.shape #當數(shù)組的行與列都大于1時,不需增加維度 (3, 2) >>> c=a[:,2] >>> c.shape #可以看到,當數(shù)組只有一列時,缺少列的維度 (3,) >>> c array([3, 6, 9])
>>> d=a[:,2,np.newaxis] #np.newaxis實現(xiàn)增加列的維度 >>> d array([[3], [6], [9]]) >>> d.shape #d的維度成了3行1列(3,1) (3, 1) >>> e=a[:,2,None] #None與np.newaxis實現(xiàn)相同的功能 >>> e array([[3], [6], [9]]) >>> e.shape (3, 1)
(6)numpy.random.shuffle(index): 打亂數(shù)據(jù)集(數(shù)組)的順序:
Examples:
>>> index = [i for i in range(10)] >>> index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> np.random.shuffle(index) >>> index [7, 9, 3, 0, 4, 1, 5, 2, 8, 6]
(7)計算二維數(shù)組某一行或某一列的最大值最小值:
>>> import numpy as np >>> a = np.arange(15).reshape(5,3) #構(gòu)造一個5行3列的二維數(shù)組 >>> a array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14]]) >>> b = a[:,0].min() ##取第0列的最小值,其他列同理 >>> b 0 >>> c = a[0,:].max() ##取第0行的最大值,其他行同理 >>> c 2
(8)向數(shù)組中添加列:np.hstack()
n = np.array(np.random.randn(4,2)) n Out[153]: array([[ 0.17234 , -0.01480043], [-0.33356669, -1.33565616], [-1.11680009, 0.64230761], [-0.51233174, -0.10359941]]) l = np.array([1,2,3,4]) l Out[155]: array([1, 2, 3, 4]) l.shape Out[156]: (4,)
可以看到,n是二維的,l是一維的,如果直接調(diào)用np.hstack()會出錯:維度不同。
n = np.hstack((n,l)) ValueError: all the input arrays must have same number of dimensions
解決方法是將l變?yōu)槎S的,可以用(5)中的方法:
n = np.hstack((n,l[:,np.newaxis])) ##注意:在使用np.hstack()時必須用()把變量括起來,因為它只接受一個變量 n Out[161]: array([[ 0.17234 , -0.01480043, 1. ], [-0.33356669, -1.33565616, 2. ], [-1.11680009, 0.64230761, 3. ], [-0.51233174, -0.10359941, 4. ]])
下面講一下如何按列往一個空列表添加值:
n = np.array([[1,2,3,4,5,6],[11,22,33,44,55,66],[111,222,333,444,555,666]]) ##產(chǎn)生一個三行六列容易區(qū)分的數(shù)組 n Out[166]: array([[ 1, 2, 3, 4, 5, 6], [ 11, 22, 33, 44, 55, 66], [111, 222, 333, 444, 555, 666]]) sample = [[]for i in range(3)] ##產(chǎn)生三行一列的空列表 Out[172]: [[], [], []] for i in range(0,6,2): ##每間隔一列便添加到sample中 sample = np.hstack((sample,n[:,i,np.newaxis])) sample Out[170]: array([[ 1., 3., 5.], [ 11., 33., 55.], [ 111., 333., 555.]])
持續(xù)更新中……
以上這篇Python的numpy庫中將矩陣轉(zhuǎn)換為列表等函數(shù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python算法之求n個節(jié)點不同二叉樹個數(shù)
本文先向大家分享了建立二叉樹的簡單代碼,其次介紹了Python計算n個節(jié)點不同二叉樹個數(shù)的問題及實現(xiàn)代碼示例,具有一定參考價值,需要的朋友可以了解下。2017-10-10python ansible自動化運維工具執(zhí)行流程
ansible是基于 paramiko 開發(fā)的,并且基于模塊化工作,本身沒有批量部署的能力,接下來通過本文給大家分享python ansible自動化運維工具的特點及執(zhí)行流程,感興趣的朋友跟隨小編一起看看吧2021-06-06python 統(tǒng)計list中各個元素出現(xiàn)的次數(shù)的幾種方法
這篇文章主要介紹了python 統(tǒng)計list中各個元素出現(xiàn)的次數(shù)的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Python面向?qū)ο蟪绦蛟O(shè)計之私有屬性及私有方法示例
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計之私有屬性及私有方法,結(jié)合實例形式分析了Python私有屬性及私有方法的相關(guān)使用方法及操作注意事項,需要的朋友可以參考下2019-04-04Python不使用int()函數(shù)把字符串轉(zhuǎn)換為數(shù)字的方法
今天小編就為大家分享一篇Python不使用int()函數(shù)把字符串轉(zhuǎn)換為數(shù)字的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python數(shù)據(jù)類型之Tuple元組實例詳解
這篇文章主要介紹了Python數(shù)據(jù)類型之Tuple元組,結(jié)合實例形式分析了Python元組類型的概念、定義、讀取、連接、判斷等常見操作技巧與相關(guān)注意事項,需要的朋友可以參考下2019-05-05