“None of [Index([‘PBT’, ‘Book_Preference’], dtype=’object’)] are in the [index]”

huangapple go评论96阅读模式
英文:

"None of [Index([('PBT', 'Book_Preference')], dtype='object')] are in the [index]"

问题

抱歉,由于你的请求,我不会再回答问题,只提供翻译。以下是你提供的内容的翻译:

Have this particular table based on Preferred Book Type(0-10 and Book Preference (0-4) and Target (0 = False, 1 = True):

根据首选图书类型(0-10)、图书偏好(0-4)和目标(0 = False,1 = True),有这个特定的表格:

PBT 图书偏好 目标
0 1 0
0 0 1

THIS WAS MY TARGET:

这是我的目标:

df["Target"] = (df['PBT']==0.0) & (df['Book_Preference']==0)
df["Target"] = df["Target"].astype(int)

MY SKLEARN:

我的Scikit-Learn代码:

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(n_estimators=100, min_samples_split=100, random_state=1)

train = (df['PBT']==6) & (df['Book_Preference']==1)
test = (df['PBT']==5) & (df['Book_Preference']==0)

predictors = ["PBT", "Voted_For"]
model.fit(train[predictors], train["Target"])

HOWEVER I GET THIS PROBLEM AFTER RUNNING THE SKLEARN:

然而,运行Scikit-Learn后,我遇到了以下问题:

KeyError: "None of [Index([('PBT', 'Book_Preference')], dtype='object')] are in the [index]"

WHAT IS THE ISSUE WITH MY CODE AND HOW DO I SOLVE THIS?

我的代码有什么问题,我应该如何解决这个问题?

英文:

Have this particular table based on Preferred Book Type(0-10 and Book Preference (0-4) and Target (0 = False, 1 = True):

PBT Book_Preference Target
0 1 0
0 0 1

THIS WAS MY TARGET:

df["Target"] = (df['PBT']==0.0) & (df['Book_Preference']==0)
df["Target"] = df["Target"].astype(int)

MY SKLEARN:

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(n_estimators=100, min_samples_split=100, random_state=1)

train = (df['PBT']==6) & (df['Book_Preference']==1)
test = (df['PBT']==5) & (df['Book_Preference']==0)

predictors = [["PBT", "Voted_For"]]
model.fit(train[predictors], train["Target"])

HOWEVER I GET THIS PROBLEM AFTER RUNNING THE SKLEARN:

KeyError: "None of [Index([('PBT', 'Book_Preference')], dtype='object')] are in the [index]"

WHAT IS THE ISSUE WITH MY CODE AND HOW DO I SOLVE THIS?

答案1

得分: 2

train = (df['PBT'] == 6) & (df['Book_Preference'] == 1)

在这里,train 是一个布尔系列,您需要使用它来对数据框进行布尔索引:

train = df[(df['PBT'] == 6) & (df['Book_Preference'] == 1)]

此外,您使用了 predictors = ["PBT", "Voted_For"] 并选择类似于 train[predictors] 的列。然而,要从数据帧中选择列,您需要使用列表而不是列表的列表:

predictors = ["PBT", "Voted_For"]
train[predictors]
英文:

You use

train = (df['PBT']==6) & (df['Book_Preference']==1)

train here is a boolean Series, you need use that to boolean indexing the dataframe

train = df[(df['PBT']==6) & (df['Book_Preference']==1)]

Also you use predictors = [["PBT", "Voted_For"]] and select columns like train[predictors]. However, to select columns from DataFrame, you need use list not list of list.

predictors = ["PBT", "Voted_For"]
train[predictors]

huangapple
  • 本文由 发表于 2023年3月12日 08:27:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定