|  | 
 
| 
#MLPClassifier在鸢尾花数据测试-逻辑回归
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  from sklearn.neural_network import MLPClassifier
 from sklearn.datasets import load_iris
 from sklearn.model_selection import train_test_split
 
 iris=load_iris()
 x=iris['data']
 y=iris['target']
 x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)#
 clf=MLPClassifier(solver='adam',alpha=1e-5,hidden_layer_sizes=(3,3),random_state=1,max_iter=100000,)
 clf.fit(x,y)
 clf.score(x_test,y_test)
 
 
 以上是完全正确的代码,我的问题是,我想用import matplotlib.pyplot as plt  #画图展示展示出来,所以加了以下代码:但是跑不出来?
 
 
 import matplotlib.pyplot as plt  #画图展示
 h=.02 #设置网格步长,为作图准备
 #为作图准备
 x_min,x_max=x[:,0].min()-.5,x[:,0].max()+.5#第一维度网格数据预备
 y_min,y_max=x[:,1].min()-.5,x[:,1].max()+.5#第二维度
 #做面积
 xx,yy=np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))#创建网格数据
 #预测模型
 z=logreg.predict(np.c_[xx.ravel(),yy.ravel()])#预测
 z=z.reshape(xx.shape)#将z矩阵转换为与xx相同的形状
 #绘制图像,绘制模型分类器的结果图像
 plt.figure(figsize=(4,4))#设置画板
 plt.pcolormesh(xx,yy,z,cmap=plt.cm.Paired)
 #绘制模型图像以及样本点图像
 plt.figure(figsize=(4,4))#设置画板
 plt.pcolormesh(xx,yy,z,cmap=plt.cm.Paired)#作网格图
 plt.scatter(x_test[:,0],x_test[:,1],c=y_test,edgecolors='k',cmap=plt.cm.Paired)#画出预测的结果
 plt.xlabel('Sepal length')#作x轴标签
 plt.ylabel('Sepal width')#作y轴标签
 plt.xlim(xx.min(),xx.max())#设置x轴范围
 plt.ylim(yy.min(),yy.max())#设置y轴范围
 plt.show()
 
 
 | 
 |