k-means clustering

The k-means clustering algorithm is a frequently used algorithm for drawing insights into the formations and separations within data. In marketing, it is often used to build customer segments and understand the behaviors of these different segments. Let's dive into building clustering models in Python.

In order to use the k-means clustering algorithm in the scikit-learn package, we need to import the kmeans module, as shown in the following code:

from sklearn.cluster import KMeans

Then, you can build and fit a k-means clustering model, using the following code:

kmeans = KMeans(n_clusters=4).fit(normalized_df[['TotalSales', 'OrderCount', 'AvgOrderValue']])

As you can see from this code, we are building a clustering model ...

Get Hands-On Data Science for Marketing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.