【问题标题】:I can't understand what I am doing wrong with my training and my test data for a competition on Kaggle我无法理解我在 Kaggle 比赛中的训练和测试数据做错了什么
【发布时间】:2020-12-01 22:25:57
【问题描述】:

在机器学习 kaggle 微课程中,您可以找到这些数据集和代码来帮助您为比赛制作预测模型:https://www.kaggle.com/[put your user name here]/exercise-categorical-variables/edit

它为您提供两个数据集:1 个训练数据集和 1 个测试数据集,您将使用它们进行预测并提交以查看您在比赛中的排名

所以在:

第 5 步:生成测试预测并提交结果

我写了这段代码: EDITED

# Read the data
X = pd.read_csv('../input/train.csv', index_col='Id') 
X_test = pd.read_csv('../input/test.csv', index_col='Id')

# Remove rows with missing target, separate target from predictors
X.dropna(axis=0, subset=['SalePrice'], inplace=True)
y = X.SalePrice
X.drop(['SalePrice'], axis=1, inplace=True)

# To keep things simple, we'll drop columns with missing values
cols_with_missing = [col for col in X.columns if X[col].isnull().any()] 
X.drop(cols_with_missing, axis=1, inplace=True)
X_test.drop(cols_with_missing, axis=1, inplace=True)

#print(X_test.shape, X.shape)

X_test.head()

# Break off validation set from training data
X_train, X_valid, y_train, y_valid = train_test_split(X, y,
                                                      train_size=0.8, test_size=0.2,
                                                      random_state=0)

X_train.head()

#Asses Viability of method ONE-HOT
# Get number of unique entries in each column with categorical data
object_nunique = list(map(lambda col: X_train[col].nunique(), object_cols))
d = dict(zip(object_cols, object_nunique))

# Print number of unique entries by column, in ascending order
sorted(d.items(), key=lambda x: x[1])


# Columns that will be one-hot encoded   ####<<<<I THINK THAT THE PROBLEM STARTS HERE>>>>#####
low_cardinality_cols = [col for col in object_cols if X_train[col].nunique() < 10]

##############For X_train

# Apply one-hot encoder to each column with categorical data
OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(X_train[low_cardinality_cols]))
OH_cols_valid = pd.DataFrame(OH_encoder.transform(X_valid[low_cardinality_cols]))

# One-hot encoding removed index; put it back
OH_cols_train.index = X_train.index
OH_cols_valid.index = X_valid.index

# Remove categorical columns (will replace with one-hot encoding)
num_X_train = X_train.drop(object_cols, axis=1)
num_X_valid = X_valid.drop(object_cols, axis=1)

# Add one-hot encoded columns to numerical features
OH_X_train = pd.concat([num_X_train, OH_cols_train], axis=1)
OH_X_valid = pd.concat([num_X_valid, OH_cols_valid], axis=1)

##############For X_test
low_cardinality_cols = [col for col in object_cols if X_test[col].nunique() < 10]

# Apply one-hot encoder to each column with categorical data
OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
#Se não retirar os NAs a linha abaixo dá erro
X_test.dropna(axis = 0, inplace=True)
OH_cols_test = pd.DataFrame(OH_encoder.fit_transform(X_test[low_cardinality_cols]))
#print(OH_cols_test.shape, OH_cols_train.shape)

# One-hot encoding removed index; put it back
OH_cols_test.index = X_test.index


# Remove categorical columns (will replace with one-hot encoding)
num_X_test = X_test.drop(object_cols, axis=1)


# Add one-hot encoded columns to numerical features
OH_X_test = pd.concat([num_X_test, OH_cols_test], axis=1)

#print(OH_X_test.shape ,OH_X_valid.shape)

# Define and fit model
model = RandomForestRegressor(n_estimators=100, criterion='mae', random_state=0)
model.fit(OH_X_train, y_train)

# Get validation predictions and MAE
preds_test = model.predict(OH_X_test)


# Save predictions in format used for competition scoring
output = pd.DataFrame({'Id': X_test.index,
                       'SalePrice': preds_test})
output.to_csv('submission.csv', index=False)

当我尝试预处理数据集时,我得到不同的行用于训练数据和测试数据。然后我无法拟合模型并做出预测。

我认为我应该只拆分测试数据集来完成所有操作,但 y 比 X_test 多 1 行,然后我无法拆分。

所以我认为我必须使用训练数据集进行拆分,然后将其拟合用于测试数据集的预测

【问题讨论】:

  • 你能修复比赛的链接吗?它目前返回 404 错误。
  • 我更改了链接格式。 kaggle.com`` [把你的用户名放在这里]`` /exercise-categorical-variables/edit 你必须登录你的 kaggle 帐户,复制你的用户名,然后将其粘贴到链接中的 [把你的用户名放在这里]我提供了

标签: python machine-learning random-forest kaggle


【解决方案1】:

我相信你的问题发生在这一行:

low_cardinality_cols = [col for col in object_cols if X_test[col].nunique() < 10]

您正在为您的独特列引用 X_test。按照 Kaggle 教程,您应该引用 X_train,如下所示:

# Columns that will be one-hot encoded
low_cardinality_cols = [col for col in object_cols if X_train[col].nunique() < 10]

你似乎在这一行的后面也犯了同样的错误:

OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(X_test[low_cardinality_cols]))

您已将其标记为 one-hot 编码训练列,但您使用的是 X_test 而不是 X_train。您正在混淆您的训练和测试集处理,这不是一个好主意。这一行应该是:

OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(X_train[low_cardinality_cols]))

我建议您再次查看本教程中的代码块,并确保您的所有数据集和变量都正确匹配,以便您处理正确的训练和测试数据。

【讨论】:

  • 我认为low_cardinality_cols 就在前面。我改变了它试图使训练和测试数据集的形状相等。否则我无法做出预测。正如你所说,我再次更改它并收到此错误ValueError: Number of features of the model must match the input. Model n_features is 155 and input n_features is 148 我认为这是因为OH_X_train(用于拟合模型)中的特征形状与OH_X_test(用于预测)中的形状不同跨度>
  • 我不应该拆分测试数据集以使其工作吗?我认为问题在于我无法为X_test 正确运行此代码:# Break off validation set from training data X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=0),因为行数与y 中的行数不同
  • 如果您的功能在 OH_X_train 和 OH_X_test 之间有所不同,您是否检查过您从 OH_X_train 中删除的内容是从 OH_X_test 中删除的内容?这些数字表明您从测试数据中删除了未从训练数据中删除的列。这就是为什么它期望 155 并在得到 148 时抛出错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-31
  • 2021-02-16
  • 2018-02-25
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2019-05-25
相关资源
最近更新 更多