Numpy 基礎學習筆記
阿新 • • 發佈:2019-02-07
Numpy對矩陣的操作更加便捷,前一段時間的使用中,總會因為一些基礎的東西模糊不清而出錯,所以今天看了一遍莫煩 的numpy 教程。順便做了筆記供之後查閱。
Numpy 使用
基本使用
列表轉numpy
矩陣
import numpy as np
array = np.array([[1,2,3],
[4,5,6]])
輸出
[[1 2 3]
[4 5 6]]
矩陣的資訊
print('number of dim:' ,array.ndim)
print('shape:',array.shape)
print('size:' ,array.size)
輸出
number of dim: 2
shape: (2, 3)
size: 6
指定元素資料的型別
a = np.array([1,2,3],dtype=np.int)
print(a.dtype)
b = np.array([1,2,3],dtype=np.int64)
print(b.dtype)
輸出
int32
int64
建立一些特殊的矩陣
c = np.zeros((2,4))
print(c)
d = np.ones((3,5))
print(d)
e = np.arange(0,20,2).reshape((2 ,5))
print(e)
f = np.linspace(1,10,4)
print(f)
輸出
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]]
[[ 0 2 4 6 8]
[10 12 14 16 18]]
[ 1. 4. 7. 10.]
矩陣簡單運算
a = np.array([10,20,30,40])
b = np.arange(4)
print (a,b)
print ('a-b:',a-b)
print ('a+b:',a+b)
print ('b^2:',b**2)
print ('b<3:',b<3)
輸出
[10 20 30 40] [0 1 2 3]
a-b: [10 19 28 37]
a+b: [10 21 32 43]
b^2: [0 1 4 9]
b<3: [ True True True False]
兩種乘法運算
a = np.array([[1,1],
[0,1]])
b = np.arange(4).reshape((2,2))
print(a)
print(b)
print('a*b: \n',a*b)
print('a.dot(b): \n',a.dot(b))
輸出
[[1 1]
[0 1]]
[[0 1]
[2 3]]
a*b:
[[0 1]
[0 3]]
a.dot(b):
[[2 4]
[2 3]]
求和求大小
a = np.random.random((2,4))
print(a)
print(np.sum(a))
print(np.max(a))
print(np.min(a))
print(np.sum(a,axis = 1)) # 行求和
輸出
[[ 0.16971606 0.67080715 0.57505605 0.59053659]
[ 0.94514567 0.24781893 0.28458866 0.82784495]]
sum: 4.31151405263
max: 0.945145668017
min: 0.169716059936
行求和: [ 2.00611585 2.3053982 ]
其他運算
A = np.arange(2,14).reshape((3,4))
print(A)
print('min index: ',np.argmin(A))
print('max index: ',np.argmax(A))
print('mean: ',np.mean(A))
print('median: ',np.median(A))
print('cumsum: ',np.cumsum(A)) # 累加求和
print('diff: \n',np.diff(A)) # 相鄰求差
輸出
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
min index: 0
max index: 11
mean: 7.5
median: 7.5
cumsum: [ 2 5 9 14 20 27 35 44 54 65 77 90]
diff:
[[1 1 1]
[1 1 1]
[1 1 1]]
排序 轉置 擷取
B = np.random.random((2,4))
print('sort:\n',np.sort(B)) #預設行排序
print('A.T:\n',A.T)
print('A.transpose():\n',A.transpose())
print('np.clip(A,4,9):\n',np.clip(A,4,9))
輸出
sort:
[[ 0.00157543 0.15291127 0.65574215 0.73835591]
[ 0.08381171 0.1036112 0.25088152 0.48191754]]
A.T:
[[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]]
A.transpose():
[[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]]
np.clip(A,4,9):
[[4 4 4 5]
[6 7 8 9]
[9 9 9 9]]
陣列索引操作
import numpy as np
A = np.arange(3,15)
print('A:\n',A)
print('a[0]:',A[0])
B = np.arange(3,15).reshape((3,4))
print('B:\n',B)
print('B[1]:',B[1])
print('B[1][2]:',B[1][2])
print('B[1,2]:',B[1,2]) # B[1,2]與B[1][2]等價
print('B[1,:]:',B[1,:])
print('B[1,1:3]:',B[1,1:3])
print('-------------------')
for row in B: # 迭代行,迭代列可以轉置後迭代行
print(row)
輸出
A:
[ 3 4 5 6 7 8 9 10 11 12 13 14]
a[0]: 3
B:
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
B[1]: [ 7 8 9 10]
B[1][2]: 9
B[1,2]: 9
B[1,:]: [ 7 8 9 10]
B[1,1:3]: [8 9]
-------------------
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
矩陣合併
主要包含vstack
,hstack
,concatenate
三個函式
import numpy as np
A = np.array([1,1,1])
B = np.array([2,2,2])
C = np.vstack((A,B))
D = np.hstack((A,B))
print('A:\n',A)
print('B:\n',B)
print('C:\n',C)
print('D:\n',D)
"""---------------"""
print('A:',A)
print('A.T:',A.T)
print('A.shape:',A.shape)
print('A.T.shape:',A.T.shape)
# 無法通過轉置把序列別換維度
bb = A[np.newaxis,:]
print('bb: \n',bb)
print('bb.shape:',np.shape(bb))
cc = A[:,np.newaxis]
print('cc: \n',cc)
print('cc.shape:',np.shape(cc))
"""-----------------"""
A = np.array([[1,1,1,1]])
B = np.array([[2,2,2,2]])
E = np.concatenate((A,B),axis=0)
print(E)
輸出
A:
[1 1 1]
B:
[2 2 2]
C:
[[1 1 1]
[2 2 2]]
D:
[1 1 1 2 2 2]
A: [1 1 1]
A.T: [1 1 1]
A.shape: (3,)
A.T.shape: (3,)
bb:
[[1 1 1]]
bb.shape: (1, 3)
cc:
[[1]
[1]
[1]]
cc.shape: (3, 1)
[[1 1 1 1]
[2 2 2 2]]
分割
import numpy as np
A = np.arange(12).reshape((3,4))
print('A:\n',A)
print('------------------')
print(np.split(A,2,axis=1))
print('------------------')
print(np.vsplit(A,3))
print('------------------')
print(np.hsplit(A,2))split(A,2))
輸出
A:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
------------------
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
------------------
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
------------------
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
陣列拷貝
簡單的=
,是把兩個變數指向同一個地址,變還其中的一個內容會影響另一個矩陣的內容。
import numpy as np
a = np.array([0,1,2,3])
b = a
a[0] = 100
print('b:\n',b)
print('b is a?',b is a)
print('----------------')
c = a.copy()
print('c before a change:',c)
a[1] = 111
print('c after a change:',c)
輸出
b:
[100 1 2 3]
b is a? True
----------------
c before a change: [100 1 2 3]
c after a change: [100 1 2 3]