【发布时间】:2019-12-17 18:02:31
【问题描述】:
我正在尝试评估 2 种数据插补方法。
我的数据集:https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data
我的目标标签是LotFrontage。
首先,我使用 OneHotEncoding 对所有类别特征进行编码,然后使用相关矩阵过滤-0.3 或0.3 以上的任何内容。
encoded_df = pd.get_dummies(train_df, prefix_sep="_", columns=['MSZoning', 'Street', 'Alley',
'LotShape', 'LandContour', 'Utilities',
'LotConfig', 'LandSlope', 'Neighborhood',
'Condition1', 'Condition2', 'BldgType', 'HouseStyle'])
corrmat = encoded_df.corr()
corrmat[(corrmat > 0.3) | (corrmat < -0.3)]
# filtering out based on corrmat output...
encoded_df = encoded_df[['SalePrice', 'MSSubClass', 'LotFrontage', 'LotArea',
'BldgType_1Fam', 'BldgType_2fmCon', 'BldgType_Duplex', 'BldgType_Twnhs', 'BldgType_TwnhsE',
'MSZoning_C (all)', 'MSZoning_FV', 'MSZoning_RH', 'MSZoning_RL', 'MSZoning_RM']]
然后我尝试了两种插补方法:
- 使用
LotFrontage的平均值(使用此方法是因为我发现离群率较低) - 尝试用
DecisionTreeRegressor预测LotFrontage
# imputate LotFrontage with the mean value (we saw low outliers ratio so we gonna use this)
encoded_df1 = encoded_df.copy()
encoded_df1['LotFrontage'].fillna(encoded_df['LotFrontage'].mean(), inplace=True)
X1 = encoded_df1.drop('LotFrontage', axis=1)
y1 = encoded_df1['LotFrontage']
X1_train, X1_test, y1_train, y1_test = train_test_split(X1, y1)
classifier1 = DecisionTreeRegressor()
classifier1.fit(X1_train, y1_train)
y1_pred = classifier1.predict(X1_test)
print('score1: ', classifier1.score(X1_test, y1_test))
# imputate LotFrontage with by preditcing it using DecisionTreeRegressor
encoded_df2 = encoded_df.copy()
X2 = encoded_df2[~encoded_df2['LotFrontage'].isnull()].drop('LotFrontage', axis=1)
y2 = encoded_df2[~encoded_df2['LotFrontage'].isnull()]['LotFrontage']
X2_train, X2_test, y2_train, y2_test = train_test_split(X2, y2)
classifier2 = DecisionTreeRegressor()
classifier2.fit(X2_train, y2_train)
y2_pred = classifier2.predict(encoded_df2[encoded_df2['LotFrontage'].isnull()].drop('LotFrontage', axis=1))
imputated_encoded_df2 = encoded_df2[encoded_df2['LotFrontage'].isnull()].assign(LotFrontage=y2_pred)
X3 = imputated_encoded_df2.drop('LotFrontage', axis=1)
y3 = imputated_encoded_df2['LotFrontage']
X3_train, X3_test, y3_train, y3_test = train_test_split(X3, y3)
classifier2.fit(X3_train, y3_train)
y3_pred = classifier2.predict(X3_test)
print('score2: ', classifier2.score(X3_test, y3_test))
我的问题是:
- 我首先使用
fillna的平均值然后拆分训练和测试并检查分数是否正确?因为如果我在拟合模型之前填充值,它不会在插补数据上拟合模型,从而给我带来有偏见的结果吗?第二种方法也一样 - 我做错了什么,因为我无法确定最佳的插补方法,因为这两种方法的得分都不好,而且是随机的
【问题讨论】:
-
我投票结束这个问题,因为它与 help center 中定义的编程无关,而是关于 ML 理论和/或方法 - 请参阅 stackoverflow.com/tags/machine-learning/info 中的介绍和注意事项跨度>
-
@Turing85 在技术上是正确的,但可以说这里不是适当的密切原因:如果 OP 删除了他们的第二个问题(因此使问题成为焦点),这会成为主题吗?可能不会……
标签: python machine-learning regression decision-tree imputation