檢查是否已安裝NumPy
import numpy
numpy.version.full_version
輸出結果
'1.18.2'
也可以使用別名
import numpy as np
np.version.full_version
輸出結果一樣
'1.18.2'
array( [ 陣列元素 ] ):陣列元素用逗號隔開
arange( 起始值, 結束值, 間隔 , 資料型別 ):也是產生一維陣列,元素內容根據間隔大小自動化產生
linspace( 起始值, 結束值, 元素個數 ):常用畫線,給定陣列的區間起始值與結束值),就自動產生幾個元素
IntArr = np.array([1, 2, 3, 4, 5])
print("IntArr=>{}".format(IntArr))
#print("IntArr.dtype=>{}".format(IntArr.dtype))
FltArr = np.array([1.5, 2.5, 3.5, 4.5, 5.5])
print("FltArr=>{}".format(FltArr))
IntArg = np.arange(1, 6, 1)
print("IntArg=>{}".format(IntArg))
FltArg = np.arange(1, 6, 0.5)
print("FltArg=>{}".format(FltArg))
Lin = np.linspace(3, 5, 9)
print("Lin=>{}".format(Lin))
輸出結果
IntArr=>[1 2 3 4 5]
FltArr=>[1.5 2.5 3.5 4.5 5.5]
IntArg=>[1 2 3 4 5]
FltArg=>[1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5]
Lin=>[3. 3.25 3.5 3.75 4. 4.25 4.5 4.75 5. ]
array( [[ 陣列1元素 ], [ 陣列2元素 ]] ):
# 2維陣列 2 by 3 array
TwoByThree = np.array([[1.1, 1.2, 1.3], [2.1, 2.2, 2.3]])
print("2 by 3=>{}".format(TwoByThree))
TwoByThree = np.array([[1.1, 1.2, 1.3], [2.1, 2.2, 2.3]], dtype=np.int16)
print("2 by 3=>{0}".format(TwoByThree))
輸出結果
2 by 3=>[[1.1 1.2 1.3]
[2.1 2.2 2.3]]
2 by 3=>[[1 1 1]
[2 2 2]]
empty( (陣列各維度大小用逗號區分) ):建立全為空的陣列 ,內容為系統給定隨機值
zeros( (陣列各維度大小用逗號區分) ):建立全為0的陣列
ones( (陣列各維度大小用逗號區分) ):建立全為1的陣列
empty = np.empty( (2, 5) )
print("empty=>{}".format(empty))
zeros = np.zeros( (2, 5) )
print("zeros=>{}".format(zeros))
ones = np.ones( (2, 5) )
print("ones=>{}".format(ones))
輸出結果
empty=>[[0.01 0.02 0.025 0.05 0.1 ]
[0.2 0.25 0.5 1. 2. ]]
zeros=>[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
ones=>[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]