作业14 手写数字识别-小数据集

1.手写数字数据集及预处理

# 1、手写数字数据集及预处理
from sklearn.datasets import load_digits
digits = load_digits()  # 读取手写数字数据集
X_data = digits.data.astype(np.float32)
Y_data = digits.target.astype(np.float32).reshape(-1,1)

# 对X_data进行归一化MinMaxScaler
scaler = MinMaxScaler()
X_data = scaler.fit_transform(X_data)
print("X_data归一化后:",X_data)
# 对Y进行独热编码OneHotEncoder
Y = OneHotEncoder().fit_transform(Y_data).todense()
print("Y独热编码后:",Y)

X = X_data.reshape(-1,8,8,1) # 转换为图片的格式
# 划分训练集和测试集
x_train,x_test,y_train,y_test = train_test_split(X,Y,test_size=0.2,random_state=0,stratify=Y)
print("x_train.shape:",x_train.shape) # 查看维度
print("x_test.shape:",x_test.shape)  # 查看维度
print("y_train.shape:",y_train.shape) # 查看维度
print("y_test.shape:",y_test.shape)  # 查看维度

运行结果: 

2.设计卷积神经网络结构

模型结构图:

# 2、设计卷积神经网络结构
model = Sequential()
ks = (3,3)  # 设置卷积核的大小
input_shape = x_train.shape[1:]
# 第一层卷积
model.add(Conv2D(filters=16,kernel_size=ks,padding="same",input_shape=input_shape,activation="relu"))
# 池化层1
model.add(MaxPool2D(pool_size=(2,2)))
# 防止过拟合
model.add(Dropout(0.25))
# 二层卷积
model.add(Conv2D(filters=32,kernel_size=ks,padding="same",activation="relu"))
# 池化层2
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))
# 三层卷积
model.add(Conv2D(filters=64,kernel_size=ks,padding="same",activation="relu"))
# 四层卷积
model.add(Conv2D(filters=128,kernel_size=ks,padding="same",activation="relu"))
# 池化层3
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))
# 平坦层
model.add(Flatten())
# 全连接层
model.add(Dense(128,activation="relu"))
model.add(Dropout(0.25))
# 激活函数softmax
model.add(Dense(10,activation="softmax"))
print(model.summary())

运行结果:

3.模型训练

# 3、模型训练
# 画Train History图
plt.rcParams['font.sans-serif'] = ['FangSong'] # 指定字体
def show_train_history(train_history, train, validation):
    plt.plot(train_history.history[train])
    plt.plot(train_history.history[validation])
    plt.ylabel('train')
    plt.xlabel('epoch')
    plt.legend(['train', 'validation'], loc='upper left')
    plt.show()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
train_history = model.fit(x=x_train,y=y_train,validation_split=0.2,batch_size=300,epochs=10,verbose=2)
show_train_history(train_history,"accuracy","val_accuracy")  # 准确率
show_train_history(train_history,"loss","val_loss")  # 损失率

运行结果:

4.模型评价

# 4、模型评价
score = model.evaluate(x_test,y_test)
print("score:",score)
# 预测值
pre = model.predict_classes(x_test)
print("预测值为:",pre[:10])
# 交差表与交叉矩阵
y_test1 = np.argmax(y_test,axis=1).reshape(-1)
y_true = np.array(y_test1)[0]
# 交叉表查看预测数据与原数据对比
pd.crosstab(y_true,pre,rownames=['true'],colnames=['predict'])
# 交叉矩阵
y_test1 = y_test1.tolist()[0]
a = pd.crosstab(np.array(y_test1),pre,rownames=['Lables'],colnames=['Predict'])
# 转换成dataframe
df = pd.DataFrame(a)
sns.heatmap(df,annot=True,cmap="Oranges",linewidths=0.2,linecolor="G")

运行结果:

 

posted on 2020-06-09 11:08  carmen-  阅读(284)  评论(0编辑  收藏  举报