python擴充庫1—numpy,python擴充numpy

來源:互聯網
上載者:User

python擴充庫1—numpy,python擴充numpy


1 數組對象

建立數組

import numpy as npa = np.arange(10)b = np.arange(2,10,1) #[2,10)步長為1c = np.linspace(0,10,20) #[0,10]共20個d = np.array([range(5)]) #用list/tuple建立數組

快速產生x*y的全零數組

a = np.zeros((3,4))
0~1的隨機數
a = np.random.rand(5)

一維數組轉化為二維數組

a = np.arange(20)a = a.reshape(4,5)

ps:使用reshape(-1,5)得到一樣的結果,會根據列自動適應行

構造更高維的

a = a.reshape(2,2,5)

二維數組轉化為一維數組

a = np.array([[1,2,3],[4,5,6]])a = np.ravel(a)

查看數組屬性

a.ndim查看維度,a.shape查看各維度大小,a.size查看元素個數,a.dtype查看元素類型

數組切割vsplit()和hsplit()

vsplit()來進行分行,而hsplit分列,np.vsplit(arr,indices)

a = np.arange(18).reshape(-1,3)print aprint np.vsplit(a,3)  print np.hsplit(a,3)

輸出為

array([[ 0,  1,  2],       [ 3,  4,  5],       [ 6,  7,  8],       [ 9, 10, 11],       [12, 13, 14],       [15, 16, 17]])[array([[0, 1, 2],       [3, 4, 5]]), array([[ 6,  7,  8],       [ 9, 10, 11]]), array([[12, 13, 14],       [15, 16, 17]])][array([[ 0],       [ 3],       [ 6],       [ 9],       [12],       [15]]), array([[ 1],       [ 4],       [ 7],       [10],       [13],       [16]]), array([[ 2],       [ 5],       [ 8],       [11],       [14],       [17]])]

按指定位置切分

將第二個參數改為list,來指定切分的位置

print np.vsplit(1,4) #以第1和第4行進行切分

數組操作

1) ‘+’,’-’,’*’,’/’ 加減乘除

2)開根號、指數操作

a = np.array([1,2])print np.array(a)print np.sqrt(a)print np.exp(a)print np.square(a)print np.power(a,5)print a**5  #和np.power(a,5)效果一樣

3)最大最小值

a.min()a.max()a.sum()a.min(axis=0) #minimun element in each column  a.min(axis=1) #minimun element in each rowa.max(axis=0)a.max(axis=1)

4)數組均值、中位元

np.mean(a)np.median(a)

數組取值

1)可以直接使用下標取值,直接賦值為淺拷貝(b=a,為b指向了a的記憶體位址),要真正拷貝,使用copy

a = np.array([[1,2],[3,4]])b = ac = a.copy()print a[0][0], b[0][0], c[0][0]  #1 1 1b[0][0] = 5print a[0][0], b[0][0], c[0][0]  # 5 5 1

2)利用’:’可以訪問某一維的全部資料

a = np.arange(20).reshape(4, 5)print a[:,[1,3]] #取出a的每一行的第2到4個元素

數組拼接

a = np.array([1,2,3])b = np.array([4,5,6])c = np.hstack([a,b]) #[1, 2, 3, 4, 5, 6]d = np.vstack([a,b]) #[[1, 2, 3],                                # [4, 5, 6]]


二 矩陣對象

建立矩陣

矩陣是二維的,而數組的可以是任意正整數維

a = np.arange(5)a = np.mat(a) #[[0, 1, 2, 3, 4]]b = np.mat('1 2;3 4') #[[1, 2],         #[3, 4]]

矩陣乘法

矩陣的’*’操作符進行的是矩陣乘法,乘號左側的矩陣列和乘號右側的矩陣行要相等,而在數組中’*’操作符進行的是每一元素的對應相乘,乘號兩側的數組每一維大小需要一致

矩陣轉置

a = np.array([[1, 2, 3], [4, 5, 6]])print np.transpose(a)   #數組用transposeprint np.matrix(a).T  #矩陣用T

矩陣求逆

a = np.mat('1.0 2.0;3.0 4.0')b = nlg.inv(a)print a*b #[[1.0000000e+00 0.0000000e+00]    #[8.8817842e-16 1.0000000e+00]]

特徵值與特徵向量

eig_value, eig_vector = nlg.eig(a)

3 其他

缺失值處理

用nan作為缺失值,用isnan判定

a = np.random.rand(2,2)a[0, 1] = np.nanprint np.isnan(a) #[[False  True] # [False False]]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.