CommencerCommencer gratuitement

NMF segmentation averages

Finally, you will visually explore the average values of the 3-segment solution built by NMF and can compare it to the K-means one. Here you will extract the features matrix W which we will use to extract the hard segment assignment by choosing the column value (segment) with highest associated value in this matrix for each customer.

We have loaded pandas library as pd and seaborn library as sns. The raw wholesale dataset has been imported, and the already fitted 3-segment NMF instance as nmf. The components dataset has been loaded as pandas DataFrame.

Cet exercice fait partie du cours

Machine Learning for Marketing in Python

Afficher le cours

Instructions

  • Create the W matrix by passing the transformed values as data, and components index as column values.
  • Assign the segment value by selecting the column name where the corresponding value is the largest.
  • Calculate the average column values per each segment.
  • Plot the average values as a heatmap.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# 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()
Modifier et exécuter le code