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

來源:互聯網
上載者:User
下面就為大家分享一篇Python的numpy庫中將矩陣轉換為列表等函數的方法,具有很好的參考價值,希望對大家有所協助。一起過來看看吧

這篇文章主要介紹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.]])

持續更新中……

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.