DAY 8
0

# NumPy操作

## 安裝

`pip install numpy`

## 載入NumPy

``````import numpy as np
``````

np是世俗公認的numpy簡稱，如果要使用其他numpy的別名，請三思而後行

## NumPy陣列

### 基本操作

``````# 載入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
# [ True  True  True False False False]
# [12 14  0]
``````

# 矩陣

## 基本運算

``````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]]
``````