英文:
"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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论