【发布时间】:2022-01-01 19:34:55
【问题描述】:
我正在尝试执行以下代码:
heart_df = pd.read_csv(r"location")
X = heart_df.iloc[:, :-1].values
y = heart_df.iloc[:, 11].values
new_df = X[["Sex", "ChestPainType", "RestingECG", "ExerciseAngina", "ST_Slope"]].values() #this is line 17
cat_cols = new_df.copy()
并得到 IndexError 如下:
File "***location***", line 17, in <module>
new_df = X[["Sex", "ChestPainType", "RestingECG", "ExerciseAngina", "ST_Slope"]].values()
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
据我所知,当我们使用浮点数作为索引时会出现这个 IndexError,但不明白为什么会出现这种情况。
在这里,通过创建 new_df 和 cat_cols,我想将分类列分开以在稍后阶段应用 OneHotEncoding。
数据集在这里:https://www.kaggle.com/fedesoriano/heart-failure-prediction。
【问题讨论】:
标签: python-3.x one-hot-encoding index-error