我不知道問題是什么以及為什么我會收到此錯誤:
ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets
任何人都可以幫我處理這段代碼嗎?
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:分類指標無法處理多標簽指標和連續多輸出目標的混合
uj5u.com熱心網友回復:
錯誤的原因是您將 one-hot 標簽y_test與模型估計的類概率標簽進行比較predictions。一個快速的解決方法是將兩者都轉換為簡單的分類標簽,如下所示:
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))
uj5u.com熱心網友回復:
y_test_arg=np.argmax(y_test,axis=1)
Y_pred = np.argmax(predictions,axis=1)
print(confusion_matrix(y_test_arg, Y_pred))
它將解決問題,Inshallah。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/351704.html
標籤:Python 张量流 凯拉斯 scikit 学习 分类
上一篇:如何重用keras函式模型的層
