import numpy as np
import matplotlib.pyplot as plt
x = np.array([[9], [4], [8], [7]]) #一維陣列建立
x
array([[9], [4], [8], [7]])
x.shape
(4, 1)
x = np.array([[9, 4, 8 , 7], [8, 7, 4, 5]]) #二維陣列建立
x
array([[9, 4, 8, 7], [8, 7, 4, 5]])
x.shape
(2, 4)
# 矩陣
a = x[0]
b = x[1]
a
array([9, 4, 8, 7])
a.reshape(2,2)
array([[9, 4], [8, 7]])
a = a.reshape(2,2)
b = b.reshape(2,2)
x = np.array([a,b])
x
array([[[9, 4], [8, 7]], [[8, 7], [4, 5]]])
x.shape
(2, 2, 2)
#one hot encoding
from tensorflow.keras.utils import to_categorical
x=[0,1,2,3,4,5]
# 類別個數:6
x_1 = to_categorical(x,6)
x_1
array([[1., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0.], [0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 1.]], dtype=float32)