【发布时间】:2018-10-30 16:28:40
【问题描述】:
我正在尝试在我的数据集上同时应用插补和热门编码。我知道在应用插补时,数据的维度可能会发生变化,所以我手动处理了它。该模型运行良好,但后来我决定应用热编码。现在,该程序无法编译。我收到尺寸不匹配错误。
test_X = pd.get_dummies(test)
train_X = pd.get_dummies(train)
col_with_missingVal = (col for col in train_X.columns if train_X[col].isnull().any())
for col in col_with_missingVal:
train_X[col + 'is_missing'] = train_X[col].isnull()
test_X[col + 'is_missing'] = test_X[col].isnull()
#impute the data
imputer = Imputer()
imp_train_X = pd.DataFrame(imputer.fit_transform(train_X))
imp_test_X = pd.DataFrame(imputer.fit_transform(test_X))
imp_train_X.columns = train_X.columns
imp_test_X.columns = test_X.columns
#Fit the model
my_model = RandomForestRegressor()
my_model.fit(imp_train_X, train_y)
# Use the model to make predictions
predicted_prices = my_model.predict(imp_test_X)
我在最后一行代码中收到以下错误:
ValueError: Number of features of the model must match the input. Model n_features is 293 and input n_features is 274
此错误的原因是什么?如何解决?
【问题讨论】:
标签: python pandas machine-learning scikit-learn