简体   繁体   English

如何修复 ValueError:分类指标无法处理模型的多类和多标签指标目标的混合?

[英]How to fix ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets for model?

I have created a model for multiclass classification where output variable has 6 classes.我创建了一个多类分类模型,其中输出变量有 6 个类。 I am getting an error when I try to obtain the accuracy score.当我尝试获得准确度分数时出现错误。 I have tried other SO answers but the answers did not help.我尝试过其他 SO 答案,但答案没有帮助。

Code代码

#Converting Target Variable to Numeric
lang = {'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 Variables
X= df.iloc[:,1:13]
y= df.iloc[:,0]

#Standardizing the Input Features
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X = scaler.fit_transform(X)

#Train Test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

#Model
model = 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 Layer
model.add(Dense(7, activation = 'softmax', kernel_initializer='random_normal'))

#Compiling the neural network
model.compile(optimizer ='adam',loss='sparse_categorical_crossentropy', metrics =['accuracy'])

#Fitting the data to the training dataset
model.fit(X_train,y_train, batch_size=5, epochs=100)

#Make predictions
pred_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()))

Error错误

ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets

Values held by variables I am adding the values held by required variables.变量持有的值我正在添加所需变量持有的值。 I believe the number of output variables that I am receiving in incorrect as there are multiple outputs for 1 value.我相信我收到的输出变量的数量不正确,因为 1 个值有多个输出。

y_train y_train

101    4
250    1
130    2
277    1
157    2
      ..
18     6
47     5
180    1
131    2
104    4

pred_train pred_train

array([[0.13525778, 0.15400752, 0.14303789, ..., 0.14364597, 0.14196989,
        0.14313765],
       ...,
       [0.13389133, 0.15622397, 0.14272076, ..., 0.14345258, 0.142379  ,
        0.14322434]], dtype=float32)

y_test y_test

57     5
283    1
162    2
237    1
107    4
      ..
182    1
173    1
75     3
251    1
55     5

pred_test预测试

array([[0.13440262, 0.15538406, 0.14284912, 0.13841757, 0.14352694,
        0.14221355, 0.14320615],
       .....,
       [0.13503768, 0.1543666 , 0.14298101, 0.13881107, 0.14361957,
        0.14203095, 0.14315312]], dtype=float32)

predict returns the probability of the sample belonging of each class, but accuracy_score required the class labels. predict返回样本属于每个类的概率,但accuracy_score需要类标签。 You have to get the class labels from the predictions.您必须从预测中获取类别标签。 Use

accuracy_score(y, np.argmax(pred_train, axis=1))

np.argmax returns the label of the class with the highest probability and since you made a prediction on a batch of data rather then a single sample you will have to use axis=1 . np.argmax以最高概率返回类的标签,并且由于您对一批数据而不是单个样本进行了预测,因此您必须使用axis=1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理 ValueError:分类指标无法处理多标签指标和多类目标错误的混合 - how to handle ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets error ValueError:分类指标无法在 ROC 曲线计算中处理多类和多标签指标目标的混合 - ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets in ROC curve calculation ValueError:分类指标无法处理多类和多标记指标目标的混合 - ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets 错误:分类指标无法处理多类多输出和多标记指标目标的混合 - Error: Classification metrics can't handle a mix of multiclass-multioutput and multilabel-indicator targets 混淆矩阵错误“分类指标无法处理多标签指标和多类目标的混合” - confusion matrix error "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets" 分类指标无法处理多类和多标签指标目标的混合 - Classification metrics can't handle a mix of multiclass and multilabel-indicator targets ValueError:分类指标无法处理多标签指标和连续多输出目标错误的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets error ValueError:分类指标无法处理多标签指标和连续多输出目标的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets ValueError:分类指标无法处理多标签指标和连续多输出目标 sklearn 的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets sklearn ValueError:分类指标无法处理多标签指标和二进制目标的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM