如何解决ordinalencoder中的密钥错误?

发布于 2025-02-08 03:39:31 字数 548 浏览 3 评论 0原文

大家好。嗨,大家好。正如您在图片中看到的那样,当我尝试使用目标列时,我遇到了一个键错误。我尝试了不同的方式,但它行不通。当我输入时: y = train ['level']y = train [['level'] 或者 print(train ['level']) 或者 enc = ordinalencoder()enc.fit(train ['Level'])

以及有关级别列的任何其他内容,发生了此错误。我如何解决?请帮助我” | 在此处输入图像描述

enter image description here
hello guys. Hi guys. As you can see in the picture, when I tried to use the target column, I encountered a key error. I tried different ways but it did not work. When I type:
y= train['Level'] or y= train[['Level']]
or
print (Train['Level'])
or
enc = OrdinalEncoder() enc.fit(Train['Level'])

and any other things about Level column this Error occured. how I can fix it? please help me "|
enter image description here

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

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

发布评论

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

评论(1

梦旅人picnic 2025-02-15 03:39:31

当dataframe中有keyError时,您应该始终通过train.columns检查列,因为有时它可能包含眼睛看不到的空格或其他字符如果您始终检查一下,那就很好。

我还想将建议添加到您的代码中,因为您正在尝试编码目标变量,为什么不使用labElencoder而不是ordinalencoder

labElencoder的输入是(n_samples,),似乎是您的情况和ordinalencoder的输入, is (n_samples,n_features)。所以我认为您应该使用第一个。

祝你好运

When there is KeyError in dataframe you should always check for the columns by Train.columns because sometimes it may contain spaces or other characters that can not be seen by the eye so it would be good if you always checked for that.

I also want to add suggestions to your code, because you are trying to encode the target variable why don't use LabelEncoder instead of OrdinalEncoder?

the input for LabelEncoder is (n_samples,) that seems the case for you and the input for OrdinalEncoder is (n_samples, n_features). So I think you should use the first one.

Good Luck

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