ValueError:从Y”的唯一值推断出的无效类。预期:[0 1 2 ... 1387 1388 1389],得到[0 1 2 ... 18609 24127 41850]

发布于 2025-02-03 03:55:10 字数 1324 浏览 4 评论 0原文

情况:我正在尝试使用XGBoost分类器,但是此错误弹出了我: “ valueerror:无效类是从y的唯一值推断出的。 /em>。

与此解决的一个从'y”的唯一值推断出无效的类。预期:[0 1 2 3 4 5],得到[1 2 3 4 5 6] ,看来我有一个不同的场景,该场景从0开始。

代码:

X = data_concat
y = data_concat[['forward_count','comment_count','like_count']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=72)

#Train, test split
print ('Train set:', X_train.shape,  y_train.shape)     #Check the size after split
print ('Test set:', X_test.shape,  y_test.shape)

xgb = XGBClassifier()
clf = xgb.fit(X_train, y_train, eval_metric='auc')  #HERE IS WHERE GET THE ERROR

dataFRME和框架信息就是这样: dataframe

dataFrame信息。

我采用了不同的y,这意味着当y的列或更多列时,列表” [0 1 2 ... 1387 1388 1388 1389] “将同时收缩或扩展。

如果您需要更多信息,请告诉我。感谢您的帮助:)

Situation: I am trying to use XGBoost classifier, however this error pops up to me:
"ValueError: Invalid classes inferred from unique values of y. Expected: [0 1 2 ... 1387 1388 1389], got [0 1 2 ... 18609 24127 41850]".

Unlike this solved one: Invalid classes inferred from unique values of `y`. Expected: [0 1 2 3 4 5], got [1 2 3 4 5 6], it seems that I have a different scenario which is about not starting from 0.

Code:

X = data_concat
y = data_concat[['forward_count','comment_count','like_count']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=72)

#Train, test split
print ('Train set:', X_train.shape,  y_train.shape)     #Check the size after split
print ('Test set:', X_test.shape,  y_test.shape)

xgb = XGBClassifier()
clf = xgb.fit(X_train, y_train, eval_metric='auc')  #HERE IS WHERE GET THE ERROR

The Datafrme and frame info is like this:
DataFrame

DataFrame Info.

I have adopted different y, meaning when y has less or more columns, the list "[0 1 2 ... 1387 1388 1389]" will simultaneously shrink or expand.

If you need further info, please let me know. Appreciate your help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怕倦 2025-02-10 03:55:10

需要转换y_train值以适合XGBoost,从0但不是1开始。
这是代码:

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train = le.fit_transform(y_train)

Need to transform the y_train value to fit xgboost, it starts from 0 but not 1.
Here is the code:

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train = le.fit_transform(y_train)
心意如水 2025-02-10 03:55:10

也许您的数据具有相同的大小,但是这些类的标签具有不同的值,例如,它必须为[1,2,3,4] IT返回[1,2,5,6]。如果您首先编码数据,然后出于某种原因将一些属于某些类中的编码值放下一些编码值,则可能发生这种情况。

Maybe your data have same size, but labels of those classes have different values, for example it must be [1,2,3,4] an it returns [1,2,5,6]. It can occur if you firstly encode your data and then drop some those encoded values that belong in some class for some reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文