变量标准化
现在进入数据准备的最后一步。您将把去偏后的数据集 wholesale_boxcox 变换到相同量纲,也就是说,使所有列的均值为 0、标准差为 1。您将使用 sklearn.preprocessing 模块中的 StandardScaler 函数。
您在上一个练习中变换得到的去偏数据集 wholesale_coxbox 已作为一个 pandas DataFrame 导入。同时,StandardScaler() 实例已初始化为 scaler。
本练习是课程的一部分
Python 营销中的机器学习
练习说明
- 在 Box-Cox 变换后的数据集上拟合已初始化的
scaler实例。 - 变换并将缩放后的数据集存为
wholesale_scaled。 - 由缩放后的数据集创建一个
pandasDataFrame。 - 打印所有列的均值和标准差。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Fit the initialized `scaler` instance on the Box-Cox transformed dataset
scaler.___(wholesale_boxcox)
# Transform and store the scaled dataset as `wholesale_scaled`
wholesale_scaled = scaler.___(wholesale_boxcox)
# Create a `pandas` DataFrame from the scaled dataset
wholesale_scaled_df = pd.DataFrame(data=___,
index=wholesale_boxcox.___,
columns=wholesale_boxcox.columns)
# Print the mean and standard deviation for all columns
print(wholesale_scaled_df.agg(['___','std']).round())