Starbucks Customers Segmentation
By: Saleh G
Tags: Clustering, Machine Learning, K Means, Segmentation, Unsupervised Learning

With data overflowing in every direction, businesses are smartly rushing to utilize them to make data-driven decisions that create business value.

For this reason, Starbucks has published sets of simulated data that mimics customer behavior on the Starbucks Rewards mobile app. Starbucks operates more than 30,000 retail stores in 80 markets around the world [1] making them a big player in a vigorously competitive industry. In a way that creates competitive advantage, the company is looking at the prospect of using data collected on customer behaviors to better engage them with promotions.

In this blog, I will give an overview of the project in which I attempted to propose a solution that helps the company in forming better understanding of their customer base using the published data. This work is to be submitted as Udacity’s Data Scientist capstone project.

1. Project Overview

An unsupervised learning technique will be used in this project to segment customers and measure how each segment behave. In particular, K-Means clustering algorithm is used. The provided dataset of courses will go through different phases at first before being ready for modeling. Starting with cleaning and ending with the final dataset grouped by customer profiles with their data aggreated.

Here’s a high-level overview of the workflow followed in this project:

Define the problem in-hands and measurement criterias. Explore, understand, and clean the provided datasets. Prepare final dataset grouped by customers. Transform final dataset appropriately before fitting model. Fit model on final dataset and predict cluster labels. Explore distributions among formed clusters. Discuss outcomes and make recommendations.

1. Problem Statement

The company is assumed to currently promote products to customers with no prior knowledge of their segments. It is possible hence to analyse the data to find patterns in customer behaviors. This helps the company in aiming and tailoring their marketing efforts and resources to consumers who exhibit similar characteristics and are considered most likely to opt in for business’s offerings. This form of target marketing is important because it helps the company by maximizing revenue while maintaining promotional campaigns cost smartly low. To do this, an unsupervised learning technique will be used to cluster customers into groups that can be be investigated separately to better understand their qualities and engage them accordingly.

2. Measurement Criterias

When speaking about measurement criterias, two categories come to mind; business metrics which assist in making decisions from a business point of view, and technical metrics which assist in making assessment of the implementation of the algorithm used to cluster data.

2.1. Business Metrics

All features will be investigated as much as possible, but there are two main themes used to drive decisions made in this regard:

1 Distinguish the difference between how customers behave during non-promotional periods and how they behave when they are prompted to do so during promotionals periods. To measure this, Recency-Frequency-Monetary Analysis for examining customer value is used. In this model, recency, frequency and monetary values are assigned a score from one to five based on quantiles partition dividing range of values into equal-sized intervals. The RFM score is then obtained by taking the average of all three scores.

2 Identify which customers react preferably to which types of offers. To measure this, offer view and completion rates will be used where the cardinalities of corresponding sets are divided by cardinalities of relevant sets.

2.2. Technical Metrics

For the algorithm chosen in this problem, K-Means, two appropriate methods will be used find the optimal number of clusters 𝑘 and to assess the model implementations; silhouette coefficient (SC) analysis and within-cluster Sum of Squared Errors (SSE) analysis.

1 Silhouette Coefficient (SC) analysis is a way to measure how close each point in a cluster is to the points in its neighboring clusters. Its a neat way to find out the optimum value for k during K-Means clustering. Silhouette values lies in the range of [-1, 1]. A value of +1 indicates that the sample is far away from its neighboring cluster and very close to the cluster its assigned. Similarly, value of -1 indicates that the point is close to its neighboring cluster than to the cluster its assigned. And, a value of 0 means at the boundary of the distance between the two cluster. Value of +1 is ideal and -1 is least preferred. Hence, higher the value better is the cluster configuration. [2]

Mathematically, for one data point 𝑖 where 𝑎(𝑖) represents the calculated mean distance of point 𝑖 with regard to to all other points in the cluster it was assigned and 𝑏(𝑖) represents the calculated mean distance of point 𝑖 with regard to to all points in its closest neighboring cluster, silhouette is defined:

Therefore, it is clear from the above equation that

Then, for one value representing the silhouette score that used for assessment of clustering outcomes, where 𝑠¯(𝑘) represents the mean 𝑠(𝑖) over all data of the entire dataset for a specific number of clusters 𝑘, silhouette coefficient is defined:

2 Sum of Squared Errors (SSE) analysis is a way to measure variability (dispersion) within a cluster by taking the sum of the squared differences between each data point and its cluster's mean, and therefore defined where 𝑥𝑖 is the 𝑖th data point and 𝑛 is the total number of data points: [3]

3. Raw Data Understanding & Cleaning

Published dataset contains simulated data that mimics customer behaviors on the Starbucks Rewards mobile app. Once every few days, Starbucks sends out an offer to users of the mobile app. An offer can be merely an informational advertisement or an actual offer such as a discount or buy-one-get-one-free. This dataset is a simplified version of the real Starbucks app because the underlying simulator only has one product whereas Starbucks actually sells dozens of products.

Data is contained in three files:

portfolio.json containing meta data about the promotions offered by the company to their customer base. profile.json containing demographic data about the customers enrolled in the reward program via the mobile app. transcript.json containing time-stamped records for transactions, offers received, offers viewed, and offers completed by customers.

3.1. portfolio Dataset

This dataset contains meta data about promotions offered by the company to their customer base as per the following schema:

id (str) - offer id

(str) - offer id offer_type (str) - type of offer; bogo, discount, or informational

(str) - type of offer; bogo, discount, or informational difficulty (int) - minimum required to be spent to complete an offer

(int) - minimum required to be spent to complete an offer reward (int) - reward given for completing an offer

(int) - reward given for completing an offer duration (int) - time in days for an offer to be open

(int) - time in days for an offer to be open channels (list) - communication channels used to market an offer

First 5 Rows of “ portfolio" Raw Dataset

It is clear tha etvery offer has a minimum required to be spent by a customer to complete an offer represented by difficulty and therfore be eligible for the reward . Also, every offer has a validity period represented by duration after which it expires.

The above is true for offers with offer type “bogo” and “discount”. However, for “informational” offers while they have a duration , their difficulty and reward are zero since these offers are merely providing information about a product. This means they are not tracked for completion in transcript .

There are 10 rows (representing 10 types of offers) and 6 columns with no missing or null values. Cleaning process started by renaming columns id and reward to offer_id and offered_reward respectively. This serves readability. The latter in particular to make it distinguishable from a later added column redeemed_reward . Then, columns channels and offer_type were one-hot encoded. Lastly, offer_id was converted to integers for readability.

First 5 Rows of “ portfolio" Clean Dataset

3.2. profile Dataset

This dataset contains demographic data about the customers enrolled in the reward program via the mobile app as per the following schema:

age (int) - age of the customer

(int) - age of the customer became_member_on (int) - date when customer created an app account

(int) - date when customer created an app account gender (str) - gender of the customer; M, F, or O

(str) - gender of the customer; M, F, or O id (str) - customer id

(str) - customer id income (float) - income of customer

First 5 Rows of “ profile" Raw Dataset

Features of this dataset are self-explanatory. It is clear from the data above that they are some missing values, and indeed there are 2175 problematic rows with missing values and clear outliers. Cleaning process started by dropping them. Also, dropping 212 profiles with ‘other’ gender to make it easier to just remap ‘male’ and ‘female’ gender with binary values. Column became_member_on was converted to standard datetime object used to add calculated days_member column and then dropped. Lastly, renaming column id to customer_id and converting customer_id to integers.

First 5 Rows of “ profile" Clean Dataset

3.1. transcript Dataset

This dataset contains records for transactions, offers received, offers viewed, and offers completed by customers as per the following schema:

event (str) - record description; transaction or offer activity

(str) - record description; transaction or offer activity person (str) - customer id

(str) - customer id time (int) - time in hours since start of test. The data begins at time t=0

(int) - time in hours since start of test. The data begins at time t=0 value - (dict) - offer id or transaction amount depending on the record

First 5 Rows of “ transcript" Raw Dataset

Last 5 Rows of “ transcript" Raw Dataset

Records in “ transcript" Raw Dataset for Single Customer

Cleaning process started by dropping corresponding custmer profiles that was previously dropped in profile . The value column was separated into single columns. The dataset then was splitted into two separate datasets offers_df and transactions_df based on event type with each value of customer_id and offers_id mapped to the same integer value previously created.

First 5 Rows of “ offers_df" Clean Dataset

First 5 Rows of “ transactions_df" Clean Dataset

4. Final Data Preparation

This section covers the process of merging and aggregating previously cleaned datasets. This section is quite long, but an overview is as follows.

Dataset portfolio was merged to offers_df , and the latter splitted by offer activity type into; received_df , viewed_df , and completed_df . Auxiliary dataframe aux_df was created as a copy of received_df which will be the workspace for all later preprocessing steps. In aux_df , two columns are established; offer_start_time and offer_end_time . These columns are the bases for most logical tests later. For each offer, offer_viewed_time and offer_completed_time are obtained from from other relevant datasets. Here, the following was assumed:

• For all offer types, an offer is viewed only if done so after the time it has been received and in offer’s duration.

• For disc and bogo offers, an offer is completed only if done so after the time it has been viewed and in offer’s duration.

• For info offers, an offer is completed if the customer made any transaction after the time it has been viewed and in offer’s duration. Other features are added like for example response_score which measures how fast a customer responded to an offer once viewed. The final dataset coe (standing for Customer-Offer Engagement) grouped by customer profiles was created using appreciate and relevent selection of columns from aux_df aggregated by either sum or mean of values. Features important for business measurement criterias were added such as count and amount of transactions for each customer in overall, promo, and nonpromo periods. Recency, frequency, and monetary values were also obtained and used to calculate RFM Score for each customer during promo and nonpromo periods.

The final dataset when finished preprocessing has 39 features (columns) and 14,608 customer profiles (rows).

Final Dataset “coe” Ready for Modeling

Schema for Final Dataset “coe”

5. Modeling & Evaluation

K-Means was the choice for this problem, but first the data was transformed using PowerTransformer to make data more Gaussian-like while standardized such that its distribution has a mean of 0 and standard deviation of 1. This is important since K-Means clustering in particular uses Euclidean distance and thus treat the data as “isotropic” in all directions of space. That is why it tends to produce spherical (rather than elongated) clusters. In this situation leaving variances unequal is equivalent to putting more weight on variables with smaller variance. [4]

Another transformation, i.e. dimensionality reduction, was performed using Principal Component Analysis (PCA). Each principal component is a unit vector that points in the direction of highest variance (after accounting for the variance captured by earlier principal components). The further a weight is from zero, the more the principal component is in the direction of the corresponding feature. If two features have large weights of the same sign (both positive or both negative), then increases in one tend to be associated with increases in the other. To contrast, features with different signs can be expected to show a negative correlation: increases in one variable should result in a decrease in the other.