ANN:MLP-algorithm

 1 from sklearn.neural_network import MLPClassifier
 2 from sklearn.datasets import load_wine
 3 from sklearn.model_selection import train_test_split
 4 wine=load_wine()
 5 X=wine.data[:,:2]
 6 y=wine.target
 7 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8)
 8 mlp=MLPClassifier(solver='lbfgs',max_iter=1000)
 9 mlp.fit(X_train,y_train)
10 print("the score of this model:{}".format(mlp.score(X_test,y_test)))
 1 import matplotlib.pyplot as plt
 2 from matplotlib.colors import ListedColormap
 3 import numpy as np
 4 cmap_light=ListedColormap(['#FFAAAA','#AAFFAA','#AAAAFF'])
 5 cmap_bold=ListedColormap(['#FF0000','#00FF00','0000FF'])
 6 x_min=X_train[:,0].min()-1
 7 x_max=X_train[:,0].max()+1
 8 y_min=X_train[:,1].min()-1
 9 y_max=X_train[:,1].max()+1
10 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02),
11                   np.arange(y_min,y_max,.02))
12 z=mlp.predict(np.c_[xx.ravel(),yy.ravel()])
13 z=z.reshape(xx.shape)
14 plt.figure()
15 plt.pcolormesh(xx,yy,z,cmap=cmap_light)
16 plt.scatter(X[:,0],X[:,1],c=y,edgecolors='k',s=60)
17 plt.xlim(xx.min(),xx.max())
18 plt.ylim(yy.min(),yy.max())
19 plt.title("MLPClassifier:solver=lbfgs")
20 plt.show()
1 mlp=MLPClassifier(solver='lbfgs',max_iter=10000,hidden_layer_sizes=[10,10],activation='tanh',alpha=1)
2 mlp.fit(X_train,y_train)
3 print(mlp.score(X_test,y_test))
1 z=mlp.predict(np.c_[xx.ravel(),yy.ravel()])
2 z=z.reshape(xx.shape)
3 plt.figure()
4 plt.pcolormesh(xx,yy,z,cmap=cmap_light)
5 plt.scatter(X[:,0],X[:,1],c=y,edgecolors='k',s=60)
6 plt.xlim(xx.min(),xx.max())
7 plt.ylim(yy.min(),yy.max())
8 plt.title("MLPClassifier:solver=lbfgs")
9 plt.show()
原文地址:https://www.cnblogs.com/St-Lovaer/p/12308352.html