我正在關注 Aurelien Geron 的《動手機器學習》一書,更具體地說,它開始進入分類器。我正在遵循書中的代碼,但我得到的錯誤是:
ValueError: The number of classes has to be greater than one; got 1 class
我的代碼:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDClassifier
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = .20, random_state = 42)
y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)
sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train_5)
當我在網上查找錯誤時,潛在的修復方法是使用np.unique(y_train_5),但這也不起作用。
uj5u.com熱心網友回復:
問題是你傳遞了 y_train_5 使得每個值都相同,如果你這樣做
print(set(y_train_5))
你只會看到一個值。考慮進行分層訓練測驗拆分,以確保每個類都在訓練和測驗中結束。或者,您的 y_train 根本不包含“5”,并且 y_train_5 和 y_test_5 中的所有值都是“假”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/373169.html
