代码如下,解析见注释
from numpy.core.fromnumeric import shape
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_digits
import os
from sklearn.model_selection import train_test_split
import numpy as np
iris = load_digits()
x = iris['data']
y = iris['target']#导入sklearn 自带的手写体数据
import pylab as pl
a=np.reshape(x[0],(8,8))#把矩阵转换成能显示的图片
pl.matshow(a)#预览图片
pl.show()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2,stratify=y)
#生成训练集与测试集
clf = MLPClassifier()#导入分类器
clf.fit(x_train, y_train)#拟合
result = clf.score(x_test, y_test)#计算正确率
print("模型评分:%10f"%result)
手写体数字预览图如下
模型正确率如下