深度学习入门:手写字识别最简单全过程

深度学习入门:手写字识别最简单全过程

一.将数据集变成.csv格式

def convert(imgf, labelf, outf, n):
    f = open(imgf, "rb")
    o = open(outf, "w")
    l = open(labelf, "rb")
 
    f.read(16)
    l.read(8)
    images = []
 
    for i in range(n):
        image = [ord(l.read(1))]
        for j in range(28 * 28):
            image.append(ord(f.read(1)))
        images.append(image)
 
    for image in images:
        o.write(",".join(str(pix) for pix in image) + "\n")
    f.close()
    o.close()
    l.close()
 
 
convert("F:\\MNIST_data\\train-images.idx3-ubyte", "F:\\MNIST_data\\train-labels.idx1-ubyte",
        "mnist_train.csv", 60000)
convert("F:\\MNIST_data\\t10k-images.idx3-ubyte", "F:\\MNIST_data\\t10k-labels.idx1-ubyte",
        "mnist_test.csv", 10000)
 
print("Convert Finished!")

深度学习入门:手写字识别最简单全过程_第1张图片
转换结果

二. 训练模型

1.导入相关包和模块

import pandas as pd
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import Dense

2.读取训练集

data=pd.read_csv('mnist_train.csv')
x=data.values[:,1:]
y=data.values[:,0]

3.对数据进行预处理

scaler=StandardScaler()
scaler.fit(x)
x=scaler.transform(x)
x_train,x_test,y_train,y_test=train_test_split(x,y,shuffle=False)

4.建立网络

nn_clf=MLPClassifier(hidden_layer_sizes(200,100),learning_rate_init=0.01)
nn_clf.fit(x_train,y_train)
y_pred=nn_clf.predict(x_test)

5.打印正确率

precision=precision_score(y_test,y_pred,average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average='weighted')
cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(cm)
print("Precision:%f"  % precision)
print("Recall:%f" % recall)
print("F1 score:%f" % f1)

我的结果:
深度学习入门:手写字识别最简单全过程_第2张图片

你可能感兴趣的:(神经网络,计算机视觉,深度学习,目标检测)