DAY 25
1
Software Development

## NumPy

### Datatype

Type 說明
`bool` 布林值
`int8` 8 位元有號整數
`int16` 16 位元有號整數
`int32` 32 位元有號整數
`int64` 64 位元有號整數
`uint8` 8 位元無號整數
`uint16` 16 位元無號整數
`uint32` 32 位元無號整數
`uint64` 64 位元無號整數
`float16` 16 位元浮點數
`float32` 32 位元浮點數
`float64` 64 位元浮點數
`complex64` 64 位元複數
`complex128` 128 位元複數

``````import numpy as np

# 用字串指定 Datatype
x = np.array([1, 2, 3], dtype = 'uint8')

# 通過 np.datatype 指定
x = np.array([4, 5, 6], dtype = np.int16)
``````

### Datatype 轉換

``````x = np.array([1, 2, 3], dtype = 'uint8')
test = x.astype('float8')
``````

### NumPy 運算

• 轉置 Transpose：透過 `array.T` 這個函式來轉置矩陣
``````array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(array1)
#[[1 2 3]
# [4 5 6]
# [7 8 9]]
array1_T = array1.T
print(array1_T)
#[[1 4 7]
# [2 5 8]
# [3 6 9]]
``````
• 加減乘除運算
``````array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
array2 = np.array([[10, 11, 12], [13, 14, 15], [16, 17, 18]])

print(array1 + array2)
# [[11 13 15]
#  [17 19 21]
#  [23 25 27]]
print(array1 - array2)
# [[-9 -9 -9]
#  [-9 -9 -9]
#  [-9 -9 -9]]
print(array1 * array2)
# [[ 10  22  36]
#  [ 52  70  90]
#  [112 136 162]]
print(array1 / array2)
# [[0.1        0.18181818 0.25      ]
#  [0.30769231 0.35714286 0.4       ]
#  [0.4375     0.47058824 0.5       ]]
``````

``````print(np.dot(array1, array2))
#[[ 84  90  96]
# [201 216 231]
# [318 342 366]]
``````

``````array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
array2 = array1 + 1
print(array2)
# [[ 2  3  4]
#  [ 5  6  7]
#  [ 8  9 10]]
``````

``````array1 + np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
``````

``````array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
array3 = np.array([1, 2, 3])
print(array1 + array3)
# [[ 2  4  6]
#  [ 5  7  9]
#  [ 8 10 12]]
``````