1. 程式人生 > >python科學計算庫numpy基礎

python科學計算庫numpy基礎

Numpy是什麼?

NumPyNumericalPython的縮寫)是一個開源的Python科學計算庫。使用NumPy,就可以很自然地使用陣列和矩陣。NumPy包含很多實用的數學函式,涵蓋線性代數運算、傅立葉變換和隨機數生成等功能

Numpy基礎

NumPy的主要物件是同種元素的多維陣列。這是一個所有的元素都是一種型別。在NumPy中維度(dimensions)叫做軸(axis),軸的個數叫做秩(rank)NumPy的陣列類被稱作 ndarray(矩陣也叫陣列)通常被稱作陣列。

常用的ndarray物件屬性有:ndarray.ndim(陣列軸的個數,軸的個數被稱作秩

)

ndarray.shape(陣列的維度。這是一個指示陣列在每個維度上大小的整數元組。例如一個nm列的矩陣,它的shape屬性將是(2,3),這個元組的長度顯然是秩,即維度或者ndim屬性)

ndarray.size陣列元素的總個數,等於shape屬性中元組元素的乘積

ndarray.dtype一個用來描述陣列中元素型別的物件,可以通過創造或指定dtype使用標準Python型別。另外NumPy提供它自己的資料型別)。

Numpy的資料型別:

Numpy內建的特徵碼:

#int8, int16, int32,int64 可以由字串’i1’, ‘i2’,’i4’, ‘i8’代替

建立陣列並檢視其屬性

兩種方法建立

a = np.array([[1,2,3], [4, 5, 6]], dtype=int)
print(a.shape)       #  a.ndim, a.size, a.dtype
print(a.ndim)       #  a.ndim, a.size, a.dtype
print(a.dtype)       #  a.ndim, a.size, a.dtype
a = np.arange(10).reshape(2, 5) # 建立2行5列的二維陣列,
# 也可以建立三維陣列,
a = np.arange(12).reshape(2,3,2)
print(a)

 Numpy 隨機數模組np.random.random, np.random.randn, np.random.rand的比較

  1. rand 生成均勻分佈的偽隨機數。分佈在(0~1)之間
  2. randn 生成標準正態分佈的偽隨機數(均值為0,方差為1)
#=====隨機函式=======
a=np.random.random(6)
b=np.random.rand(6)
c=np.random.randn(6)

 

常用函式

 

索引、切片和迭代

#一維陣列索引、迭代、遍歷
a = np.arange(10)**3
print(a)
#[開始:末尾:步長]
print(a[2:5])
#小於6以下的步長為2的變為-1000
a[:6:2] = -1000
print(a)
#逆序
print(a[ : :-1])
#遍歷元素
for i in a:
    print(i)
print(a)

#二維陣列索引、迭代、遍歷
b=np.arange(20).reshape(5,4)
print(b)
print(b[2,3])#下面等價
print(b[2][3])
print(b[:,1])#下面等價
print(b[0:,1])#下面等價
print(b[0:5,1])
print(b[1:3,:])
print(b[-1])#下面等價
print(b[-1,:])#下面等價
print(b[-1,...])

# b[i] 中括號中的表示式被當作 i 和一系列 : ,來代表剩下的軸。NumPy也允許你使用“點”像 b[i,...] 。
# 點 (…)代表許多產生一個完整的索引元組必要的分號。如果x是
# 秩為5的陣列(即它有5個軸),那麼:x[1,2,…] 等同於 x[1,2,:,:,:],x[…,3] 等同於 x[:,:,:,:,3],x[4,…,5,:] 等同 x[4,:,:,5,:].


# 三維陣列操作
c = np.arange(12).reshape(2,3,2)

print(c)
print(c[1])
print(c[0,1])
#c[2,1]    # 等價於c[2][1]
#c[2,1,1]  # 等價於c[2][1][1]
d = np.arange(10)**2
print(d)
e = np.array([3, 5, 6])
print(e)
print(d)
#花哨的索引NumPy比普通Python序列提供更多的索引功能。陣列可以被整數陣列和布林陣列索引。
print(d[e])


f = np.arange(12).reshape(3, 4)
g = f>4
print(g)
print(f[g])

#  np.flatten()返回一個摺疊成一維的陣列。但是該函式只能適用於numpy物件,即array或者mat,普通的list列表是不行的。
a = np.array([[1,2], [3, 4], [5, 6]])
print(a.flatten())
b = np.mat([[1,2,3], [4, 5, 6]])
print(b.flatten())

靈活切片

c = np.arange(20).reshape(4,5)
print(c[0,0])
print(c[0:3:2])
print(c[:-1])
#取第一行
print(c[0])
#取第一列
print(c[:,0])
print(c)
#取第一、四行
print(c[0:4:3])
print(c[[0,3]])
#取第一、三列
print(c[0:,0:3:2])
print(c[:,[0,2]])
#取二三行二三列
print(c[[1,2],1:3])

 

形狀操作:

ravel(), vstack(),hstack(),column_stack,row_stack, stack, split, hsplit, vsplit

#合併
# a = np.arange(10).reshape(2,5)
# print(a.resize(5,2))
b = np.arange(6).reshape(2,3)
c = np.ones((2,3))
d = np.hstack((b,c))              # hstack:horizontal stack 左右合併
print(d)
e = np.vstack((b,c))              # vstack: vertical stack 上下合併
print(e)
f = np.column_stack((b,c))
print(f)
g = np.row_stack((b,c))
print(g)
h = np.stack((b, c), axis=1)      # 按行合併
print(h)
i = np.stack((b,c), axis=0)       # 按列合併
print(i)
j = np.concatenate ((b, c, c, b), axis=0)   #多個合併
print(j)
#分割
k = np.hsplit(i, 2)
print(k)
l = np.vsplit(i, 2)
print(l)
m = np.split(i, 2, axis=0)
print(m)
n = np.split(i, 2,axis=1)
print(n)
o = np.array_split(np.arange(10),3)   #不等量分割
print(o)