from sklearn.model_selection import train_test_split import sklearn.neural_network as net import matplotlib.cm as cm file_path=r'D:\anaconda3\temp\邮政编码数据.txt' data=pd.read_csv(file_path,header=0) print(data.shape) data.head() X=data.iloc[:,1:-1] Y=data.iloc[:,0] X.shape np.random.seed(1) ids=np.random.choice(len(Y),25) plt.figure(figsize=(8,8)) for i,item in enumerate(ids): img=np.array(X.iloc[item]).reshape(16,16) plt.subplot(5,5,i+1) plt.imshow(img,cmap=cm.gray_r) plt.show X_train,X_test,Y_train,Y_test = train_test_split(X,Y,train_size = 0.60,random_state = 123) nodes = np.arange(1,20,2) acts = ['relu','logistic'] errTrain = np.zeros((len(nodes),2)) errTest = np.zeros((len(nodes),2)) for i,node in enumerate(nodes): for j ,act in enumerate(acts): NeuNet = net.MLPClassifier(hidden_layer_sizes = (node,),activation = act,random_state = 1,max_iter=300) nodes=np.arange(1,20,2) acts=['relu','logistic'] NeuNet.fit(X_train,Y_train) errTrain[i,j] = 1-NeuNet.score(X_train,Y_train) errTest[i,j] = 1-NeuNet.score(X_test,Y_test) plt.plot(nodes,errTest[:,0],label='relu_test',linestyle='-') plt.plot(nodes,errTest[:,1],label='logistic_test',linestyle='-.') plt.plot(nodes,errTrain[:,0],label='relu_train',linestyle='-',linewidth=0.5) plt.plot(nodes,errTrain[:,1],label='logistic_train',linestyle='-',linewidth=0.5) plt.title('2012010812') plt.xlabel('hidden node numbers') plt.ylabel('erros') plt.xticks(nodes) plt.legend()
时间: 2024-04-04 18:31:53 浏览: 93
这段代码看起来有一些问题。首先,第一行 `from sklearn.model_selection import train_test_split import sklearn.neural_network as net import matplotlib.cm as cm` 应该换行,每个语句单独占一行。其次,在这个代码块中,没有导入 pandas 库,但是调用了 pandas 中的函数 `read_csv`。在代码块的后半部分,您定义了变量 `nodes`,但是在循环中又重新定义了 `nodes`,这会导致变量被覆盖,可能会影响到后续的程序运行。此外,循环中的绘图部分也存在一些问题,因为在循环中重复定义了绘图的参数。根据您的问题描述,我无法确定您的具体问题在哪里,但是这些问题可能会影响到程序的正确性。因此,您需要检查一下代码并修复这些问题,才能正常运行程序。
阅读全文