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

伊尚杜塔

我为多类分类创建了一个模型,其中输出变量具有6个类。尝试获取准确性得分时出现错误。我尝试了其他SO答案,但是答案没有帮助。

#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()))

错误

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

变量持有的值我要添加所需变量持有的值。我相信我收到的输出变量数量不正确,因为1值有多个输出。

y_train

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

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

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返回样本属于每个类别的概率,但是accuracy_score需要类别标签。您必须从预测中获取类标签。采用

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

np.argmax以最高的概率返回类的标签,因为您对一批数据进行了预测,而后对一个样本进行了预测axis=1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

混淆矩阵:ValueError:分类指标无法处理未知和多类目标的混合

ValueError:分类指标无法处理多标签指标和二进制目标的混合

分类指标无法处理连续多输出和多标签指标目标的混合情况

f分数:ValueError:分类指标无法处理多标签指标和连续多输出目标的混合情况

Python Sklearn“ ValueError:分类指标无法处理多类多输出和二进制目标的混合”错误

ValueError:分类指标无法处理未知目标和二进制目标的混合

混淆矩阵错误“分类指标无法同时处理多标签指标和多类别目标”

遇到错误:分类指标无法处理多类多输出和二进制目标的混合

ValueError:分类指标无法处理预训练 CNN 上的多标签指标混合

错误:分类指标无法处理multiclass-multioutput和multilabel-indicator目标的混合

一类svm分类错误:分类指标无法处理未知目标和二进制目标的混合

多标签分类Keras指标

如何处理ValueError:分类指标无法同时处理multilabel-indicator和multiclass target错误

分类指标不能同时处理二进制目标和连续目标

准确性得分ValueError:无法处理二进制目标和连续目标的混合

如何修复OneClassSVM和GridSearchCV的错误“用于多指标评分”

多类分类中性能指标的计算

在LightGBM中消除eval,obj(目标)和指标的歧义

用于多标签分类的 keras 自定义指标

使用 AUC 指标进行多标签分类的预测

用于多类分类的sklearn指标

使用 OneVsRestClassifier 的分类目标和 one-hot 编码目标之间的概率差异

大熊猫重命名多指标的单列的元组

根据多指标的水平过滤选择值

Pandas 多指标比较和相对差异

多类和多标签问题的 ROC 曲线和 AUC。ValueError:目标分数需要是多类 roc_auc 的概率

熊猫-多指标均值

无法选择Firebase AB测试激活事件或目标指标事件

使用 Keras ValueError 进行多类分类:检查目标时出错: