我正在处理在 Kaggle 上举办的波士顿挑战赛,我仍在完善我的功能。查看数据集,我意识到有些列需要用二进制编码,有些用十进制编码(将它们排在 n 的范围之外),有些需要单热编码。我收集了这些列并将它们分类在不同的列表中(至少基于我对它们的数据应该如何编码的判断):
categorical_columns = ['MSSubClass', 'MSZoning', 'Alley', 'LandContour', 'Neighborhood', 'Condition1', 'Condition2',
'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'Foundation', 'Heating',
'Functional', 'GarageType', 'PavedDrive', 'SaleType', 'SaleCondition']
binary_columns = ['Street', 'CentralAir']
ranked_columns = ['LotShape', 'Utilities', 'LandSlope', 'ExterQual', 'ExterCond', 'BsmtQual', 'BsmtCond',
'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'HeatingQC', 'KitchenQual', 'FireplaceQu',
'GareFinish', 'GarageQual', 'GarageCond', 'PoolQC', 'Fence', 'MiscFeature']
一位 stackexchange 用户建议我使用pandas.get_dummies()
方法对分类变量进行一次热编码MSZoning
,并将其附加到如下变量:
OHE_MSZoning = pd.get_dummies(train['MSZoning'])
我想知道如何使用函数和控制流语句(如for-loop
.