【问题标题】:Passing list-likes to .loc or [] with any missing label will raise KeyError in the future, you can use .reindex() as an alternative将列表喜欢传递给 .loc 或带有任何缺失标签的 [] 将来会引发 KeyError,您可以使用 .reindex() 作为替代
【发布时间】:2021-12-14 15:51:45
【问题描述】:

我正在尝试使用以下方法将我的数据集拆分为训练集和测试集:

for train_set, test_set in stratified.split(complete_df, complete_df["loan_condition_int"]):
    stratified_train = complete_df.loc[train_set]
    stratified_test = complete_df.loc[test_set]

我的数据框 complete_df 没有任何 NaN 值。我通过使用返回0complete_df.isnull().sum().max() 来确保它。

但我仍然收到警告说:

Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.

这会导致稍后出现错误。我尝试使用我在网上找到的一些技术,但仍然无法修复它。

【问题讨论】:

  • train_settest_set 包含不在 complete_df 索引中的值。

标签: pandas dataframe machine-learning


【解决方案1】:

首先,您应该澄清stratified 是什么。我假设它是 sklearn 的 StratifiedShuffleSplit 对象。

我的数据集 complete_df 没有任何 NAN 值。

警告消息中的“缺失标签”不是指缺失值,即 NaN。错误是说train_set 和/或test_set 包含complete_df 的索引中不存在的值(标签)。这是因为.loc 基于行(和列)标签而不是行位置执行索引,而train_settest_set 表示行号。因此,如果您的 DataFrame 的索引与行的整数位置不一致(似乎是这种情况),则会引发警告。

要按行位置选择,请使用iloc。这应该工作

for train_set, test_set in stratified.split(complete_df, complete_df["loan_condition_int"]):
    stratified_train = complete_df.iloc[train_set]
    stratified_test = complete_df.iloc[test_set]

【讨论】:

    猜你喜欢
    • 2021-03-05
    • 2021-07-21
    • 2020-07-06
    • 2021-07-18
    • 2020-12-24
    • 2021-10-23
    • 2021-04-24
    • 1970-01-01
    相关资源
    最近更新 更多