首页 > 解决方案 > 如何防止python中的KeyErrors?

问题描述

在机器学习项目中使用 MinMaxScaler 时,我遇到了 KeyError 问题。这是我的相关代码:

df = pd.read_csv(io.BytesIO(uploaded['Root_Work_Sample.csv']))
print(df.shape)
print(df.columns)
display(df.head(5))
print(df.dtypes)
train_cols = ["feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11, feature12, feature13, feature14, y"]
df_train, df_test = train_test_split(df, train_size=1000, test_size=876, shuffle=False)
print("Train--Test size", len(df_train), len(df_test))
print(df_train)
print(df_test)
 
# scale the feature MinMax, build array
x = df_train.loc[:,train_cols].values  #THE ERROR IS ON THIS LINE
min_max_scaler = MinMaxScaler()
x_train = min_max_scaler.fit_transform(x)
x_test = min_max_scaler.transform(df_test.loc[:,train_cols])

这是我得到的错误:

KeyError: "None of [Index(['feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11, feature12, feature13, feature14, y'], dtype='object')] are in the [columns]"

有没有关于如何解决这个问题的建议以及关于像我这样的新手如何避免这类错误的一般做法?

标签: pythonpandasdataframescikit-learnkeyerror

解决方案


df_train不是数据框,它是 2D numpy 数组,因此您不能loc在其上使用方法。我猜你train_test_split以错误的方式使用函数。而且您指定train_cols错误,您应该将每个功能用引号括起来,如下所示:

train_cols = ["feature", "feature2",....]

尝试这个:

X, y = df[train_cols], df["y"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=876, shuffle=False)

scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

推荐阅读