在建模的过程中离散型的字符变量是不能直接用于建模,需要对这些变量进行重编码。重编码的方法有多种,字符型的值转换为整数型的值、哑变量处理(0-1变量)、One-Hot热编码(类似于哑变量)。
1.字符型转数值的方法
处理前的数据集:
for feature in income.columns:
if income[feature].dtype == 'object':
income[feature] = pd.Categorical(income[feature]).codes
income.head()