Get startedGet started for free

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.

This exercise is part of the course

Machine Learning for Marketing in Python

View Course

Exercise 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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()
Edit and Run Code