国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python的numpy庫中將矩陣轉換為列表等函數的方法

2020-01-04 15:32:44
字體:
來源:轉載
供稿:網友

這篇文章主要介紹Python的numpy庫中的一些函數,做備份,以便查找。

(1)將矩陣轉換為列表的函數:numpy.matrix.tolist()

返回list列表

Examples

>>>

>>> x = np.matrix(np.arange(12).reshape((3,4))); xmatrix([[ 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)將數組轉換為列表的函數:numpy.ndarray.tolist()

Notes:(數組能夠被重新構造)

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()計算矩陣或數組的均值:

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()計算矩陣或數組的標準差:

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為數組增加一個維度:

Examples:

>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]]) #先輸入3行2列的數組a>>> b=a[:,:2] >>> b.shape #當數組的行與列都大于1時,不需增加維度(3, 2)>>> c=a[:,2] >>> c.shape #可以看到,當數組只有一列時,缺少列的維度(3,)>>> carray([3, 6, 9])
>>> d=a[:,2,np.newaxis] #np.newaxis實現增加列的維度>>> darray([[3],  [6],  [9]])>>> d.shape  #d的維度成了3行1列(3,1)(3, 1)>>> e=a[:,2,None] #None與np.newaxis實現相同的功能>>> earray([[3],  [6],  [9]])>>> e.shape(3, 1)

(6)numpy.random.shuffle(index): 打亂數據集(數組)的順序:

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)計算二維數組某一行或某一列的最大值最小值:

>>> import numpy as np >>> a = np.arange(15).reshape(5,3) #構造一個5行3列的二維數組 >>> 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)向數組中添加列: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是一維的,如果直接調用np.hstack()會出錯:維度不同。

n = np.hstack((n,l)) ValueError: all the input arrays must have same number of dimensions 

解決方法是將l變為二維的,可以用(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]]) ##產生一個三行六列容易區分的數組  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)] ##產生三行一列的空列表 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.]]) 

持續更新中……

以上這篇Python的numpy庫中將矩陣轉換為列表等函數的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台州市| 海安县| 赣州市| 沧州市| 东港市| 施秉县| 藁城市| 东宁县| 获嘉县| 秦安县| 新龙县| 宁阳县| 曲阳县| 维西| 凉山| 福泉市| 若羌县| 黎平县| 开封县| 水富县| 姚安县| 庆安县| 咸阳市| 定边县| 扎鲁特旗| 嘉禾县| 本溪| 米易县| 连平县| 巴青县| 都昌县| 平泉县| 鄂伦春自治旗| 都江堰市| 阿拉善盟| 达孜县| 青浦区| 七台河市| 桃江县| 河西区| 陆河县|