对数变换
在前面的练习中,您对数据做了线性缩放,这不会改变数据的形状。如果您的数据服从(或接近服从)正态分布,这种方法很有效,而许多机器学习模型都假设如此。有时您会遇到接近正态的变量,例如一组人群的身高或体重。然而,很多真实世界中的变量并不符合这种模式,例如工资或人群的年龄。在本练习中,您将对 so_numeric_df 数据表中的 ConvertedSalary 列进行对数变换,因为该列有大量数据集中在较小的数值附近,但同时也包含非常高的数值。这类分布通常被称为具有长右尾。
本练习是课程的一部分
Python 中的机器学习特征工程
练习说明
- 从
sklearn的preprocessing模块导入PowerTransformer。 - 将
PowerTransformer()实例化为pow_trans。 - 在
so_numeric_df的ConvertedSalary列上拟合PowerTransformer。 - 使用刚刚拟合好的变换器对同一列进行变换。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import PowerTransformer
from sklearn.preprocessing import ____
# Instantiate PowerTransformer
pow_trans = ____
# Train the transform on the data
____
# Apply the power transform to the data
so_numeric_df['ConvertedSalary_LG'] = ____(so_numeric_df[['ConvertedSalary']])
# Plot the data before and after the transformation
so_numeric_df[['ConvertedSalary', 'ConvertedSalary_LG']].hist()
plt.show()