简体   繁体   English

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

[英]ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets

I don't know what the problem is and why I'm getting this error:我不知道问题是什么以及为什么我会收到此错误:

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

Can anyone please help me with this code?任何人都可以帮我处理这段代码吗?

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.datasets import make_classification
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler
from sklearn.model_selection import train_test_split
tf.random.set_seed(0)

# generate the data
X, y = make_classification(n_classes=6, n_samples=1000, n_features=10, n_informative=10, n_redundant=0, random_state=42)
print(y.shape)
# (1000, )

# split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# one-hot encode the target
enc = OneHotEncoder(sparse=False, handle_unknown='ignore')
enc.fit(y_train.reshape(-1, 1))
y_train = enc.transform(y_train.reshape(-1, 1))
y_test = enc.transform(y_test.reshape(-1, 1))
print(y_train.shape, y_test.shape)
# (750, 6) (250, 6)

# scale the features
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

# define the model
model = Sequential()
model.add(Dense(units=30, activation='relu'))
model.add(Dense(units=15, activation='relu'))
model.add(Dense(6, activation='softmax'))

# fit the model
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=X_train, y=y_train, epochs=3, batch_size=10, validation_data=(X_test, y_test))

predictions = model.predict(X_test)

confusion_matrix(y_test,predictions)

print(classification_report(y_lab,predictions))

ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets ValueError:分类指标无法处理多标签指标和连续多输出目标的混合

The reason for the error is you're comparing a one-hot label y_test with a class probability label which is estimated by the model predictions .错误的原因是您将单热标签y_test与由模型predictions估计的类概率标签进行比较。 A quick fix would be converting both to simple categorical labels like below:一个快速的解决方法是将两者都转换为简单的分类标签,如下所示:

confusion_matrix(np.argmax(y_test, axis=1), np.argmax(predictions, axis=1))
classification_report(np.argmax(y_test, axis=1), np.argmax(predictions, axis=1))
y_test_arg=np.argmax(y_test,axis=1)
Y_pred = np.argmax(predictions,axis=1)
print(confusion_matrix(y_test_arg, Y_pred))

It will solve the problem, Inshallah.它将解决问题,Inshallah。

暂无
暂无

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

相关问题 ValueError:分类指标无法处理多标签指标和连续多输出目标错误的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets error ValueError:分类指标无法处理多标签指标和连续多输出目标 sklearn 的混合 - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets sklearn f-score:ValueError:分类指标无法处理多标签指标和连续多输出目标的混合 - f-score: ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets ValueError:无法处理多标签指示器和连续多输出的precision_score()的混合 - ValueError: Can't handle mix of multilabel-indicator and continuous-multioutput accuracy_score() 错误:分类指标无法处理多类多输出和多标记指标目标的混合 - Error: Classification metrics can't handle a mix of multiclass-multioutput and multilabel-indicator targets 分类指标无法处理连续多输出和多标签指标目标的混合 - classification metrics can't handle a mix of continuous-multioutput and multi-label-indicator targets Confusion_matrix ValueError:分类指标无法处理二进制和连续多输出目标的混合 - Confusion_matrix ValueError: Classification metrics can't handle a mix of binary and continuous-multioutput targets 如何处理 ValueError:分类指标无法处理多标签指标和多类目标错误的混合 - how to handle ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets error 混淆矩阵:ValueError:分类指标无法处理多类和连续多输出目标的混合 - Confusion matrix: ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets 如何修复 ValueError:分类指标无法处理模型的多类和多标签指标目标的混合? - How to fix ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets for model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM