请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

predict_classes没有,改用老师你分享的公众号的方法对原始结果标签做one-hot处理,但最后可视化时,原始数据点显示不出来

由于predict_classes(x_train)失败,看了您分享的方法,按照步骤在分离数据时对原始结果标签做one-hot处理,即y=to_categorical(y)
#分离数据
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
y=to_categorical(y)#对原始结果标签做one-hot处理
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.33,random_state=10)
print(x.shape,y.shape,x_train.shape,x_test.shape,y_train.shape,y_test.shape)

#build the mlp model
from keras.models import Sequential
from keras.layers import Dense,Activation

mlp=Sequential()
mlp.add(Dense(units=20,input_dim=2,activation=‘sigmoid’))
mlp.add(Dense(units=2,activation=‘sigmoid’))#输出单位数对应的结果标签数
mlp.summary()

#compile the model
mlp.compile(optimizer=‘adam’,loss=‘binary_crossentropy’)

#train the model
mlp.fit(x_train,y_train,epochs=3000)

#make a prediction of training data
y_train_predict=np.argmax(mlp.predict(x_train),axis=-1)
print(y_train_predict)

#calculate the training accuracy
from sklearn.metrics import accuracy_score
accuracy_train=accuracy_score(np.argmax(y_train,axis=-1),y_train_predict)
print(‘training accuracy:’,accuracy_train)
#算得结果为0.9672727272727273

#make a prediction of test data
y_test_predict=np.argmax(mlp.predict(x_test),axis=-1)
print(y_test_predict)

#calculate the test accuracy
accuracy_test=accuracy_score(np.argmax(y_test,axis=-1),y_test_predict)
print(‘testing accuracy:’,accuracy_test)
#算得结果为0.9705882352941176

#generate new data for plot
xx,yy=np.meshgrid(np.arange(0,1,0.01),np.arange(0,1,0.01))
x_range=np.c_[xx.ravel(),yy.ravel()]
y_range_predict=np.argmax(mlp.predict(x_range),axis=-1)
print(type(y_range_predict),y_range_predict)
<class ‘numpy.ndarray’> [1 1 1 … 1 1 1]

然后我再将结果转化给列向量形式
y_range_predict=np.array(y_range_predict)
y_range_predict=y_range_predict.reshape(-1,1)
print(type(y_range_predict),y_range_predict.shape,y_range_predict)
<class ‘numpy.ndarray’> (10000, 1) [[1]
[1]
[1]

[1]
[1]
[1]]

然后我再跟着视频步骤将其格式转换
y_range_predict_form=pd.Series(i[0] for i in y_range_predict)
print(y_range_predict_form)

#visualize the predict result
fig2=plt.figure(figsize=(6,6))

pass_mlp=plt.scatter(x_range[:,0][y_range_predict_form1],x_range[:,1][y_range_predict_form1])
fail_mlp=plt.scatter(x_range[:,0][y_range_predict_form0],x_range[:,1][y_range_predict_form0])

passed=plt.scatter(x.loc[:,‘x1’][y1],x.loc[:,‘x2’][y1])
failed=plt.scatter(x.loc[:,‘x1’][y0],x.loc[:,‘x2’][y0])

plt.xlabel(‘x1’)
plt.ylabel(‘x2’)
plt.legend((passed,failed,pass_mlp,fail_mlp),(‘passed’,‘failed’,‘pass_mlp’,‘fail_mlp’))
plt.title(‘predict result’)
plt.show()

运行程序就会报错,显示x.loc[:,‘x1’][y==1] 错了。too many indices for array: array is 1-dimensional, but 6 were indexed。是不是因为我对原始结果标签做one-hot处理,导致y的标签1和0没有了,所以点的图显示不出来。请问一下老师。要怎么处理呢?谢谢。

正在回答 回答被采纳积分+3

1回答

慕仔2404423 2022-06-07 10:35:18

把y做了to_categorical处理之后y就不能直接这么用了, 最后预览的时候可以先 y = np.argmax(y,axis=-1) 转换回来再用,   我是 分离数据的时候进行 one hot 处理的, 这样就不改变y的原值了

X_train,X_test,y_train,y_test = train_test_split(X,to_categorical(y),test_size=0.33,random_state=10)

1 回复 有任何疑惑可以回复我~
  • 提问者 Tseng_zh_h #1
    谢谢你的答复。我是在最后可视化前又加载了y,让它有0,1之分。
    回复 有任何疑惑可以回复我~ 2022-06-07 15:55:07
  • 为啥我做to_categorical 处理之后 模型都没法训练呢?
    回复 有任何疑惑可以回复我~ 2022-11-10 17:11:01
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信