Numpy 學習筆記

初步認識numpy的屬性 ndim shape size

# 創(chuàng)建numpy數(shù)組
import numpy as np

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

print(array)
print("number of dim", array.ndim) # 維度
print("shape", array.shape) # 2 * 3
print("size", array.size) # 6
[[1 2 3]
 [4 5 6]]
number of dim 2
shape (2, 3)
size 6

認識numpy的數(shù)據(jù)類型

a = np.array([2, 23, 4], dtype=np.int64)
print(a)
print(a.dtype)
a2 = np.array([5, 6,7 ], dtype = np.float32)
print(a2.dtype)
[ 2 23  4]
int64
float32

常見的生成array的方式

a_zero = np.zeros( (3, 4) ) # 全0, 形狀要用()括起來
a_zero
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
a_ones = np.ones( (3, 4), dtype = np.int16);
a_ones
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]], dtype=int16)
a_empty = np.empty( (3,4) )
a_empty
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
a_range = np.arange(10, 20, 2);
a_range
array([10, 12, 14, 16, 18])
a_reshape = np.arange(12).reshape( (3, 4) )
a_reshape
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
a_line = np.linspace(0, 10, 5).reshape( (5,1) )
a_line
array([[  0. ],
       [  2.5],
       [  5. ],
       [  7.5],
       [ 10. ]])

常見的numpy運算

# numpy的運算
a = np.array([10, 20, 30, 40]);
b = np.arange(4);
print(a, b)
print(a + b)
print(a * b)
print(b ** 2) #平方
print(10 * np.sin(a)) #sin  等數(shù)學運算
[10 20 30 40] [0 1 2 3]
[10 21 32 43]
[  0  20  60 120]
[0 1 4 9]
[-5.44021111  9.12945251 -9.88031624  7.4511316 ]
print(b < 3) # 判斷mask
[ True  True  True False]
print(b == 2) #判斷mask 是否相等
[False False  True False]
#矩陣計算
a_mat = np.array([[1, 1],[0, 1]]);
b_mat = np.arange(4).reshape((2,2));
print(a_mat)
print(b_mat)
print(a_mat * b_mat)
print(np.dot(a_mat, b_mat))  #矩陣計算
print(a_mat.dot(b_mat))
[[1 1]
 [0 1]]
[[0 1]
 [2 3]]
[[0 1]
 [0 3]]
[[2 4]
 [2 3]]
[[2 4]
 [2 3]]

常見的匯總作用的函數(shù)

a = np.random.random( (2, 4) ) #隨機
print(a)
print(np.sum(a))
print(np.max(a))
print(np.min(a))
[[ 0.74260727  0.20295659  0.00618381  0.47263097]
 [ 0.43585519  0.73091641  0.09287091  0.59677618]]
3.28079732304
0.742607271838
0.00618381462723
# axis 按軸計算
print(np.sum(a, axis = 0)) #沿著y軸 求和
print(np.max(a, axis = 1)) # 沿著x軸  求最大

[ 1.17846246  0.933873    0.09905472  1.06940714]
[ 0.74260727  0.73091641]
A = np.arange(2, 14).reshape( (2, 6))
print(A)
[[ 2  3  4  5  6  7]
 [ 8  9 10 11 12 13]]
np.argmin(A) #最小值索引
np.argmax(A) #最大值索引
11
np.mean(A) #平均值
7.5
A.mean()
7.5
np.median(A) #中位數(shù)
7.5
np.cumsum(A) #累加
array([ 2,  5,  9, 14, 20, 27, 35, 44, 54, 65, 77, 90], dtype=int32)
np.diff(A) #累差
array([[1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])
np.nonzero(A) #非零的數(shù)字
(array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], dtype=int64),
 array([0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5], dtype=int64))
a_withzero = np.arange(-3, 5).reshape( (2, 4))
print(a_withzero)
np.nonzero(a_withzero) # 返回非0的位置 用兩個array的形式返回
[[-3 -2 -1  0]
 [ 1  2  3  4]]





(array([0, 0, 0, 1, 1, 1, 1], dtype=int64),
 array([0, 1, 2, 0, 1, 2, 3], dtype=int64))
print(np.sort(A))
[[ 2  3  4  5  6  7]
 [ 8  9 10 11 12 13]]
b_desc = np.arange(9, -1, -1); # 指定步長 linspace是指定步數(shù)
print(b_desc)
print(np.sort(b_desc))
[9 8 7 6 5 4 3 2 1 0]
[0 1 2 3 4 5 6 7 8 9]
print(np.transpose(b_desc))
[9 8 7 6 5 4 3 2 1 0]
print(b_desc)
[9 8 7 6 5 4 3 2 1 0]
a_2d = np.arange(2, 14).reshape( (6, 2))
print(a_2d)
print(a_2d.T)
[[ 2  3]
 [ 4  5]
 [ 6  7]
 [ 8  9]
 [10 11]
 [12 13]]
[[ 2  4  6  8 10 12]
 [ 3  5  7  9 11 13]]
a_ran = np.random.random( (3,4))
print(a_ran)
print(np.sort(a_ran))
print(np.sort(a_ran, axis = 0))
print(np.sort(a_ran, axis=1))
[[ 0.77974552  0.87391665  0.42413729  0.54764486]
 [ 0.70079738  0.73673138  0.11842954  0.40184359]
 [ 0.52657421  0.85092361  0.06880996  0.76002899]]
[[ 0.42413729  0.54764486  0.77974552  0.87391665]
 [ 0.11842954  0.40184359  0.70079738  0.73673138]
 [ 0.06880996  0.52657421  0.76002899  0.85092361]]
[[ 0.52657421  0.73673138  0.06880996  0.40184359]
 [ 0.70079738  0.85092361  0.11842954  0.54764486]
 [ 0.77974552  0.87391665  0.42413729  0.76002899]]
[[ 0.42413729  0.54764486  0.77974552  0.87391665]
 [ 0.11842954  0.40184359  0.70079738  0.73673138]
 [ 0.06880996  0.52657421  0.76002899  0.85092361]]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容