請在終端機底下輸入這個,謝謝pip install numpy
像這樣
import numpy as np
np是世俗公認的numpy簡稱,如果要使用其他numpy的別名,請三思而後行
相較於python的list,他們最大的差別就是list能夠儲存不同資料型態的資料,而NumPy陣列只能儲存相同的資料型態,但效能帶來的極致體驗,還是讓NumPy在處理大量資料中獨霸一方,陣列操作可以說是NumPy招牌,其特色為同質且支援多重維度的ndarray
物件,這個物件的三個重要屬性分別為:
維度(ndim)
形狀(shape)
資料型態(dtype)
一般我們稱一維陣列為vector而二維陣列為matrix,使用方法如下
# 載入numpy模組
import numpy as np
if __name__ == "__main__":
# 以列表來建立numpy陣列
l1 = [12, 14, 0, 5, 23]
a1 = np.array(l1)
l2 = [17, -5, 28, 5, 6]
a2 = np.array(l2)
# 陣列相加
print(a1 + a2)
# [29 9 28 10 29]
# 顯示重要屬性
print(a1.ndim)
# 1 => 一維陣列
print(a1.shape)
# (5,) => 五個元素
print(a1.dtype)
# int64 => 資料型態
# 存取資料
print(a1[3])
# 5 # 載入numpy模組
import numpy as np
if __name__ == "__main__":
# 以列表來建立numpy陣列
l1 = [12, 14, 0, 5, 23]
a1 = np.array(l1)
l2 = [17, -5, 28, 5, 6]
a2 = np.array(l2)
# 陣列相加
print(a1 + a2)
# [29 9 28 10 29]
# 顯示重要屬性
print(a1.ndim)
# 1 => 一維陣列
print(a1.shape)
# (5,) => 五個元素
print(a1.dtype)
# int64 => 資料型態
import numpy as np
if __name__ == "__main__":
# 以列表來建立numpy陣列
l1 = [12, 14, 0, 5, 23, 3]
a1 = np.array(l1)
print(a1)
# [12, 14, 0, 5, 23, 3]
# 顯示重要屬性
print(a1.ndim)
# 1 => 一維陣列
print(a1.shape)
# (6,) => 六個元素
print(a1.dtype)
# int64 => 資料型態
# 維度轉換
a1 = a1.reshape([2, 3])
print(a1)
# [[12 14 0] [ 5 23 3]]
# 顯示重要屬性
print(a1.ndim)
# 2 => 二維陣列
print(a1.shape)
# (2, 3) => 2*3個元素
print(a1.dtype)
# int64 => 資料型態
import numpy as np
if __name__ == "__main__":
# 以列表來建立numpy陣列
l1 = [12, 14, 0, 5, 23, 3]
a1 = np.array(l1)
print(a1)
# [12, 14, 0, 5, 23, 3]
print(a1.dtype)
# int64 => 資料型態
# 轉換資料型態
a1 = a1.astype("float32")
print(a1)
# [12. 14. 0. 5. 23. 3.]
print(a1.dtype)
# float32 => 資料型態
import numpy as np
if __name__ == "__main__":
# 以列表來建立numpy陣列
l1 = [12, 14, 0, 5, 23, 3]
a1 = np.array(l1)
print(a1)
# [12, 14, 0, 5, 23, 3]
print(a1[0:3])
# 從第0個開始取,取到第3個之後不取
# [12 14 0]
print(a1[4:6])
# 從第4個開始取,取到第6個之後不取
# [23 3]
import numpy as np
if __name__ == "__main__":
# 以列表來建立numpy陣列
l1 = [12, 14, 0, 5, 23, 3]
a1 = np.array(l1)
print(a1)
# [12, 14, 0, 5, 23, 3]
# 用布林遮罩來過濾資料
mask = a1 % 2 == 0
print(mask)
# [ True True True False False False]
print(a1[mask])
# [12 14 0]
今天我們要來研究一下NumPy多維陣列,先從一點簡單的開始,二維陣列,俗稱矩陣(matrix),
import numpy as np
if __name__ == "__main__":
# 以二維列表來建立numpy二維陣列
l1 = [[12, 14, 0], [5, 23, 3]]
a1 = np.array(l1)
l2 = [[34, 17, 97], [83, 18, 34]]
a2 = np.array(l2)
print(a1)
# [[12 14 0] [ 5 23 3]]
print(a2)
# [[34 17 97] [83 18 34]]
#矩陣加法
print(a1 + a2)
#[[46 31 97] [88 41 37]]
#矩陣減法
print(a1 - a2)
#[[-22 -3 -97] [-78 5 -31]]
#元素乘積(不是矩陣乘法)
print(a1 * a2)
#[[408 238 0] [415 414 102]]
#矩陣除法
print(a1 / a2)
#[[0.35294118 0.82352941 0. ] [0.06024096 1.27777778 0.08823529]]
矩陣轉置會將欄和列互換
import numpy as np
if __name__ == "__main__":
# 以二維列表來建立numpy二維陣列
l1 = [[12, 14, 0], [5, 23, 3]]
a1 = np.array(l1)
print(a1)
# [[12 14 0]
# [ 5 23 3]]
# 矩陣轉置
print(a1.T)
# [[12 5]
# [14 23]
# [ 0 3]]
注意矩陣點積需要第一個矩陣的欄數等於第二個矩陣的列數喔
import numpy as np
if __name__ == "__main__":
# 以二維列表來建立numpy二維陣列
l1 = [[12, 14, 0], [5, 23, 3]]
a1 = np.array(l1)
l2 = [[34, 17, 97], [83, 18, 34], [44, 19, 22]]
a2 = np.array(l2)
print(a1)
# [[12 14 0]
# [ 5 23 3]]
print(a2)
# [[34 17 97]
# [83 18 34]
# [44 19 22]]
# 矩陣點積
print(a1.dot(a2))
# [[1570 456 1640]
# [2211 556 1333]]
print(a2.dot(a1))
# 報錯 a2的欄數是3 a1的列數是2
import numpy as np
if __name__ == "__main__":
# 以二維列表來建立numpy二維陣列
l1 = [[12, 14, 0], [5, 23, 3]]
a1 = np.array(l1)
l2 = [[34, 17, 97], [83, 18, 34], [44, 19, 22]]
a2 = np.array(l2)
print(a1)
# [[12 14 0]
# [ 5 23 3]]
print(a2)
# [[34 17 97]
# [83 18 34]
# [44 19 22]]
# 矩陣內積
print(np.inner(a1,a2))
# [[ 646 1248 794]
# [ 852 931 723]]
import numpy as np
if __name__ == "__main__":
# 以二維列表來建立numpy二維陣列
l1 = [[12, 14, 0], [5, 23, 3]]
a1 = np.array(l1)
l2 = [[34, 17, 97], [83, 18, 34], [44, 19, 22]]
a2 = np.array(l2)
print(a1)
# [[12 14 0]
# [ 5 23 3]]
print(a2)
# [[34 17 97]
# [83 18 34]
# [44 19 22]]
# 矩陣外積
print(np.outer(a1,a2))
# [[ 408 204 1164 996 216 408 528 228 264]
# [ 476 238 1358 1162 252 476 616 266 308]
# [ 0 0 0 0 0 0 0 0 0]
# [ 170 85 485 415 90 170 220 95 110]
# [ 782 391 2231 1909 414 782 1012 437 506]
# [ 102 51 291 249 54 102 132 57 66]]