值错误:pos_label=1不是一个有效的标签。它应该是 [0] 之一
在执行这段代码时:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import LearningCurveDisplay, train_test_split
from imblearn.datasets import fetch_datasets
def load_dataset(dataset):
data = fetch_datasets()[dataset]
data.target = np.where(data.target < 0, 0, 1)
return data.data, data.target
X, y = load_dataset("webpage")
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.3,
random_state=0,
)
model = RandomForestClassifier(max_depth=3, random_state=0)
train_sizes = [0.55, 0.78, 1.0]
LearningCurveDisplay.from_estimator(
model,
X, y,
train_sizes=train_sizes,
cv=10,
scoring='average_precision',
line_kw = {"marker": "o"},
)
我得到以下错误:
ValueError: pos_label=1 is not a valid label: It should be one of [0]
我知道这是因为数据集不平衡,可能在验证折中没有足够的少数类样本?不过我随后尝试了带cv=5或 cv=10的分层交叉验证,结果仍然是同样的错误,因此我有点困惑。
它到底从哪里来,如何防止它?
解决方案
在 LearningCurveDisplay.from_estimator 调用中直接添加 shuffle=True 和一个 random_state:
LearningCurveDisplay.from_estimator(
model,
X, y,
train_sizes=train_sizes,
cv=10,
scoring='average_precision',
shuffle=True, # <--- shuffles the training folds before slicing
random_state=0, # <--- Guarantees reproducibility of the shuffles
line_kw={"marker": "o"},
)
另外,在你的代码中:
# Change this:
LearningCurveDisplay.from_estimator(model, X, y, ...)
# To this:
LearningCurveDisplay.from_estimator(model, X_train, y_train, ...)
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。