Reference: https://docs.scipy.org/doc/numpy-1.13.0/user/index.html
NumPy is powerful package for scientific computing with Python. It contains among other things:
In this class, we will mainly introduce the NumPy "array" features, plus a few mathematical functions.
Before we use the NumPy array in the python code, we need to first "import" the NumPy toolbox
NumPy是一個針對科學運算開發的python工具庫,提供方便的陣列功能與數學運算工具。
要使用NumPy相關的指令前,要先引入(啟動)NumPy
import numpy
A=numpy.arange(4) # 引進numpy,可以開始用numpy相關的指令
print(A)
[0 1 2 3]
import numpy as np # import...as...(用 np 來取代 numpy這個工具庫的全名)
B=np.arange(5)
print(B)
[0 1 2 3 4]
(也可以從檔案讀入資料到陣列中,另外在NumPyFileIO介紹)
#np.array 創造新陣列
A = np.array([1,2,3]) # 1-D陣列
print(A)
print(A.shape) # checking the size in each dimension of the array
[1 2 3] (3,)
A = np.array([[1,2,3],[4,5,6],[7,8,9]]) # 2-D陣列
print(A)
print(A.shape) # checking the size in each dimension of the array
[[1 2 3] [4 5 6] [7 8 9]] (3, 3)
# show only a sub-set of A
print(A)
print(A[:,2]) # the entire third column of A
print(A[0:2,0:2]) # first two column of the top two rows
[[1 2 3] [4 5 6] [7 8 9]] [3 6 9] [[1 2] [4 5]]
#np.zeros 創造任意形狀的陣列,所有元素為0.0 or integer 0
A=np.zeros(3) # 創造1x3的陣列
print(A)
[ 0. 0. 0.]
B=np.zeros((2,3)) # 創造2x3的陣列
print(B)
[[ 0. 0. 0.] [ 0. 0. 0.]]
c=np.zeros(3,dtype=int) # 創造1x3的陣列,所有元素為整數0
print(c)
[0 0 0]
#np.ones 創造任意形狀的陣列,所有元素為1.0 or integer 1
B=np.ones((2,3))
print(B)
c=np.ones(3,dtype=int) # 創造1x3的陣列,所有元素為整數1
print(c)
[[ 1. 1. 1.] [ 1. 1. 1.]] [1 1 1]
# np.arange(end) or np.arange(start,end,interval) (default start=0, interval=1)
A=np.arange(10) # 只給一個數值的話,代表從0開始,到給定的數值之間、間隔為1的一串整數
print(A)
B=np.arange(1,10) # 開始與結束之間、間隔為1的一串整數
print(B)
C=np.arange(1,10,2) # 開始與結束之間、用指定的間隔產生一串整數
print(C)
[0 1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9] [1 3 5 7 9]
A=np.arange(2, 10, dtype=np.float) # 開始與結束之間、間隔為1.0的一串實數
print(A)
B=np.arange(2, 3, 0.1) # 開始與結束之間、用指定的間隔產生一串實數
print(B)
[ 2. 3. 4. 5. 6. 7. 8. 9.] [ 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9]
# np.linspace(start,end,n) # 在起始與結束的數值之間,均分成n等分的一串數值
A=np.linspace(1,10) # default n=50
print(A)
[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735 2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816 3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898 4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898 5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061 6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143 7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224 8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306 9.81632653 10. ]
B=np.linspace(1,10,10) # 自訂 n=10
print(B)
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
# np.asarray 將list轉換成NumPy array
A=[1,3,5.]
print(A)
B=np.asarray(A) # 將list A轉成Numpy Array (所有的元素要是同一種類型,所以都轉成實數)
print(B)
[1, 3, 5.0] [ 1. 3. 5.]
A=[1,3,'test']
print(A)
B=np.asarray(A) # 將list A轉成Numpy Array (所有的元素要是同一種類型,所以都轉成字串)
print(B)
[1, 3, 'test'] ['1' '3' 'test']
陣列的加減乘除(+ - * /)、內建基本數學函數(sine, cosine, exponential, log...)
Note:
a=np.array([1,2,3])
b=np.array([4,5,6])
A=a+b # 陣列相加
print(A)
A=a-b # 陣列相減
print(A)
[5 7 9] [-3 -3 -3]
A=a/b # 相同位置的元素相除 element-wise division
print(A)
[ 0.25 0.4 0.5 ]
A=a*b # 相同位置的元素相乘 element-wise multiplication
print(A)
[ 4 10 18]
A=np.dot(a,b) # matrix multiplication of two 1D arrays
print(A)
32
c=np.array([[1, 0], [0, 1]])
print(c)
d=np.array([[4, 1], [2, 2]])
print(d)
B=np.dot(c,d) # matrix multiplication of two 2D arrays
print(B)
[[1 0] [0 1]] [[4 1] [2 2]] [[4 1] [2 2]]
# Example of basic math functions for numpy arrays
A=np.linspace(0,np.pi,6) # generage a 6x1 array between 0 and pi
print(A)
B=np.cos(A) # cosine (similar for sin, tan...)
print(B)
C=np.exp(A) # exponential
print(C)
D=np.log(C) # natural log
print(D)
[ 0. 0.62831853 1.25663706 1.88495559 2.51327412 3.14159265] [ 1. 0.80901699 0.30901699 -0.30901699 -0.80901699 -1. ] [ 1. 1.87445609 3.51358562 6.58606196 12.34528394 23.14069263] [ 0. 0.62831853 1.25663706 1.88495559 2.51327412 3.14159265]
#np.reshape 改變"形狀" (將 ixj的陣列變形為mxn, 但必須滿足 ixj = mxn)
A=np.arange(4)
print(A)
B= np.arange(4).reshape((2,2)) #將 1x4 陣列變成 2x2
print(B)
[0 1 2 3] [[0 1] [2 3]]
#np.transpose 轉置 (維度交換)
C=np.transpose(B)
print(C)
[[0 2] [1 3]]
# np.tile(array,n) or np.tile(array,(n,m)) # 陣列自我複製
D=np.tile(A,4) # 陣列自我複製 1xn次
print(D)
[0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3]
D=np.tile(A,(4,2)) # 陣列自我複製 nxm 次 (n是垂直方向重複次數,m是水平方向重複次數)
print(D)
[[0 1 2 3 0 1 2 3] [0 1 2 3 0 1 2 3] [0 1 2 3 0 1 2 3] [0 1 2 3 0 1 2 3]]
# np.hstack np.vstack # 不同陣列堆疊(維度大小要相合)
a = np.array([1,2,3])
b = np.array([4,5,6])
c= np.hstack((a,b))
print(c)
[1 2 3 4 5 6]
d= np.vstack((a,b))
print(d)
[[1 2 3] [4 5 6]]
# 維度大小不合的陣列無法堆疊
d= np.vstack((a,c))
print(d)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-53-b2b485830342> in <module>() ----> 1 d= np.vstack((a,c)) # 維度大小不合的陣列無法堆疊 2 print(d) ~\Anaconda3\lib\site-packages\numpy\core\shape_base.py in vstack(tup) 235 236 """ --> 237 return _nx.concatenate([atleast_2d(_m) for _m in tup], 0) 238 239 def hstack(tup): ValueError: all the input array dimensions except for the concatenation axis must match exactly
np.column_stack # 將數個1D陣列以直行排列成二維 combine multiple 1-D arrays as columns to form a 2-D array
e= np.column_stack((a,b,a,b))
print(e)
[[1 4 1 4] [2 5 2 5] [3 6 3 6]]