我创建了一个多类分类模型,其中输出变量有 6 个类。当我尝试获取准确度分数时出现错误。我尝试过其他答案,但答案没有帮助。代码#Converting Target Variable to Numericlang = {'US':1, 'UK':2, 'GE':3, 'IT':4, 'FR':5, 'ES':6} df.language = [lang[item] for item in df.language] #Creating Input Features and Target VariablesX= df.iloc[:,1:13]y= df.iloc[:,0]#Standardizing the Input Featuresfrom sklearn.preprocessing import StandardScalerscaler = StandardScaler()X = scaler.fit_transform(X)#Train Test SplitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)#Modelmodel = Sequential()model.add(Dense(12, activation='relu', kernel_initializer='random_normal', input_dim=12))model.add(Dense(10, activation='relu', kernel_initializer='random_normal'))model.add(Dense(8, activation='relu', kernel_initializer='random_normal'))#Output Layermodel.add(Dense(7, activation = 'softmax', kernel_initializer='random_normal'))#Compiling the neural networkmodel.compile(optimizer ='adam',loss='sparse_categorical_crossentropy', metrics =['accuracy'])#Fitting the data to the training datasetmodel.fit(X_train,y_train, batch_size=5, epochs=100)#Make predictionspred_train = model.predict(X_train)pred_test = model.predict(X_test) print('Train Accuracy = ',accuracy_score(y_train,pred_train.round()))print('Test Accuracy = ',accuracy_score(y_test,pred_test.round()))错误ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets变量保存的值 我正在添加所需变量保存的值。我相信我收到的输出变量的数量不正确,因为 1 个值有多个输出。
1 回答
大话西游666
TA贡献1817条经验 获得超14个赞
predict
返回样本属于每个类的概率,但accuracy_score
需要类标签。您必须从预测中获取类别标签。使用
accuracy_score(y, np.argmax(pred_train, axis=1))
np.argmax
返回概率最高的类的标签,因为您对一批数据而不是单个样本进行了预测,所以您必须使用axis=1
。
添加回答
举报
0/150
提交
取消