NMF 细分的均值
最后,您将以可视化方式探索由 NMF 构建的 3 段解决方案的平均值,并将其与 K-means 的结果进行比较。在这里,您将提取特征矩阵 W。我们将使用它来提取硬分配结果:对每位客户,在该矩阵中选择关联值最大的列(细分)作为其所属细分。
我们已将 pandas 库加载为 pd,将 seaborn 库加载为 sns。已导入原始 wholesale 数据集,以及已经拟合好的 3 段 NMF 实例 nmf。components 数据集已作为 pandas 的 DataFrame 加载。
本练习是课程的一部分
Python 营销中的机器学习
练习说明
- 通过传入转换后的值作为数据,并以 components 的索引作为列名,创建 W 矩阵。
- 通过选择对应值最大的列名来赋予细分标签。
- 按细分计算各列的平均值。
- 将这些平均值绘制为热力图。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create the W matrix
W = pd.DataFrame(data=nmf.___(wholesale), columns=components.index)
W.index = wholesale.index
# Assign the column name where the corresponding value is the largest
wholesale_nmf3 = wholesale.___(segment = W.idxmax(axis=1))
# Calculate the average column values per each segment
nmf3_averages = wholesale_nmf3.___('___').mean().round(0)
# Plot the average values as heatmap
sns.___(___.T, cmap='YlGnBu')
# Display the chart
plt.show()