import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.neural_network import MLPClassifier import pandas as pd from sklearn.model_selection import train_test_split import joblib iris = datasets.load_iris()
X = iris.data[:, :2] y = iris.target
X_train,X_test, y_train,y_test = train_test_split( X,y,train_size = 0.8,random_state = 42) hidden_n,hidden_m=10,6 clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(hidden_n,hidden_m), random_state=1) clf.fit(X_train, y_train) print('构建神经网络模型的参数为:\n',clf)
plt.rc('font', size=14) plt.rcParams['font.sans-serif'] = 'SimHei' plt.rcParams['axes.unicode_minus']=False
x_min =np.min(X[:,0])-1 x_max = np.max(X[:,0])+1 y_min =np.min(X[:,1])-1 y_max = np.max(X[:,1])+1 p = plt.figure(figsize=(12,8))
ax1 = p.add_subplot(2,2,1) plt.scatter(X[:,0],X[:,1],c=y) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.xlabel('sepal length') plt.ylabel('sepal width') plt.title('全部样本')
ax1 = p.add_subplot(2,2,2) plt.scatter(X_train[:,0],X_train[:,1],c=y_train) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.xlabel('sepal length') plt.ylabel('sepal width') plt.title('训练集样本')
XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
Z = np.c_[XX.ravel(), YY.ravel()] Z_predict=clf.predict(Z)
Z_predict = Z_predict.reshape(XX.shape) ax1 = p.add_subplot(2,2,3) plt.pcolormesh(XX, YY, Z_predict/2, cmap=plt.cm.Paired) plt.scatter(X[:,0],X[:,1],c=y) plt.title('训练集得到的模型分类预测结果')
plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.xlabel('sepal length') plt.ylabel('sepal width') p.tight_layout() plt.show()
joblib.dump(clf,'iris_2features_ann.m') clf_nn = joblib.load('iris_2features_ann.m') print('加载的神经网络模型的参数为:\n',clf_nn)
y_pred = clf_nn.predict(X_test) for i in [0,1,2,3,4]: print('测试集第',i,'个样本',X_test[i,0:2],'的分类预测结果为:',y_pred[i])
plt.rc('font', size=14) plt.rcParams['font.sans-serif'] = 'SimHei' plt.rcParams['axes.unicode_minus']=False
x_min =np.min(X[:,0])-1 x_max = np.max(X[:,0])+1 y_min =np.min(X[:,1])-1 y_max = np.max(X[:,1])+1
y0,y1,y2=np.where(y_test==0),np.where(y_test==1),\ np.where(y_test==2)
y0_pred,y1_pred,y2_pred=np.where(y_pred==0),\ np.where(y_pred==1),np.where(y_pred==2)
p=plt.figure(figsize=(12,4)) ax1 = p.add_subplot(1,2,1) plt.scatter(X_test[y0,0],X_test[y0,1],marker='o') plt.scatter(X_test[y1,0],X_test[y1,1],marker='*') plt.scatter(X_test[y2,0],X_test[y2,1],marker='D') plt.title('iris测试集及类别') labels=['类0','类1','类2'] plt.legend(labels) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) ax1 = p.add_subplot(1,2,2)
plt.scatter(X_test[y0_pred,0],X_test[y0_pred,1],marker='o') plt.scatter(X_test[y1_pred,0],X_test[y1_pred,1],marker='*') plt.scatter(X_test[y2_pred,0],X_test[y2_pred,1],marker='D') plt.title('使用属性0和1训练模型对iris测试集预测的类别') plt.legend(labels) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.show()
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.neural_network import MLPClassifier import pandas as pd from sklearn.model_selection import train_test_split from sklearn.externals import joblib
iris = datasets.load_iris()
X = iris.data y = iris.target
X_train,X_test, y_train,y_test = train_test_split( X,y,train_size = 0.8,random_state = 42) hidden_n,hidden_m=5,4 clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(hidden_n,hidden_m), random_state=1) clf.fit(X_train, y_train) joblib.dump(clf,'iris_4features_ann.m') print('构建的模型为:\n',clf)
clf_nn = joblib.load('iris_4features_ann.m') y_pred = clf_nn.predict(X_test) for i in [0,1,2,3,4]: print('测试集第',i,'个样本',X_test[i,0:2],'的分类预测结果为:',y_pred[i])
plt.rc('font', size=14) plt.rcParams['font.sans-serif'] = 'SimHei' plt.rcParams['axes.unicode_minus']=False
x_min =np.min(X[:,0])-1 x_max = np.max(X[:,0])+1 y_min =np.min(X[:,1])-1 y_max = np.max(X[:,1])+1
y0,y1,y2=np.where(y_test==0),np.where(y_test==1),\ np.where(y_test==2)
y0_pred,y1_pred,y2_pred=np.where(y_pred==0),\ np.where(y_pred==1),np.where(y_pred==2)
p=plt.figure(figsize=(12,4)) ax1 = p.add_subplot(1,2,1) plt.scatter(X_test[y0,0],X_test[y0,1],marker='o') plt.scatter(X_test[y1,0],X_test[y1,1],marker='*') plt.scatter(X_test[y2,0],X_test[y2,1],marker='D')
plt.title('iris测试集类别') labels=['类0','类1','类2'] plt.legend(labels) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) ax1 = p.add_subplot(1,2,2)
plt.scatter(X_test[y0_pred,0],X_test[y0_pred,1],marker='o') plt.scatter(X_test[y1_pred,0],X_test[y1_pred,1],marker='*') plt.scatter(X_test[y2_pred,0],X_test[y2_pred,1],marker='D')
plt.title('使用全部4个属性训练模型对iris测试集预测的结果') plt.legend(labels) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.show()
|