PythonPlaza - Python & AI

A Gaussian Mixture Model (GMM) (Distribution-based Clustering)

A Gaussian Mixture Model (GMM) is a probabilistic model that assumes that the data is generated from a mixture of several Gaussian (normal) distributions, each with its own mean and variance. One of the most powerful aspects of GMMs is their capacity to compute the probability of each data point belonging to a particular cluster. This is achieved through a process known as ‘soft clustering’, as opposed to ‘hard clustering’ methods like K-Means. In soft clustering, instead of forcefully assigning a data point to a single cluster, GMM assigns probabilities that indicate the likelihood of that data point belonging to each of the Gaussian components.

K-Means Initialization: The model runs a quick K-Means algorithm to find centroid clusters and uses those stable centroids to initialize the Gaussian Means.
Multiple Random Restarts: You run the entire GMM process 10 to 100 times with different random starting seeds, and keep the model that yields the highest overall likelihood.
If you are using Python, this logic is built directly into libraries like scikit-learn. You can configure the GaussianMixture function's init_params argument to toggle between initialization methods.


USE CASE 1: Use GMM for customer segmentation in Market Basket Analysis. Instead of finding which products are purchased together (like Apriori or FP-Growth), use GMM to group customers based on their purchasing behavior. Once customers are clustered, you can create targeted promotions and personalized recommendations for each segment.


import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.mixture import GaussianMixture
import numpy as np
import matplotlib.pyplot as plt

# ----------------------------------
# Step 1: Sample Market Basket Data
# ----------------------------------
data = pd.DataFrame({
    'Customer': ['C001','C002','C003','C004','C005','C006','C007','C008'],
    'Bread': [12,10,11,1,0,2,6,5],
    'Milk': [10,8,9,2,1,1,5,6],
    'Eggs': [8,7,6,1,2,0,4,5],
    'Beer': [0,1,0,10,12,9,4,5],
    'Chips': [1,0,1,8,10,7,3,4]
})

#Load data
#You can also download from
#https://www.pythonplaza.com/sample_customer_shopping.html
data= pd.read_csv("customer_shopping.csv")


print("Original Data")
print(data)

# ----------------------------------
# Step 2: Select Features
# ----------------------------------
X = data[['Bread', 'Milk', 'Eggs', 'Beer', 'Chips']]

# ----------------------------------
# Step 3: Scale Features
# ----------------------------------
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# ----------------------------------
# Step 4: Train GMM Model
# ----------------------------------
gmm = GaussianMixture(n_components=3, random_state=42)
gmm.fit(X_scaled)

# Assign clusters
data['Cluster'] = gmm.predict(X_scaled)

print("\nCluster Assignments")
print(data[['Customer', 'Cluster']])

# ----------------------------------
# Step 5: Cluster Profiles (mean of original data)
# ----------------------------------
print("\nCluster Profiles (Original Scale Means)")

cluster_profiles = data.groupby('Cluster')[['Bread','Milk','Eggs','Beer','Chips']].mean()

print(cluster_profiles.round(2))

# ----------------------------------
# Step 6: Test New Customer
# ----------------------------------
new_customer = pd.DataFrame({
    'Bread': [11],
    'Milk': [9],
    'Eggs': [7],
    'Beer': [1],
    'Chips': [1]
})

new_customer_scaled = scaler.transform(new_customer)

# Direct prediction using GMM
predicted_cluster = gmm.predict(new_customer_scaled)[0]

print("\nNew Customer")
print(new_customer)
print(f"\nPredicted Cluster: {predicted_cluster}")

# ----------------------------------
# Step 7: Recommendation Logic
# ----------------------------------
if predicted_cluster == 0:
    print("Recommendation: Bread, Milk, Eggs promotions")
elif predicted_cluster == 1:
    print("Recommendation: Beer and Chips promotions")
else:
    print("Recommendation: Mixed basket offers")


USE CASE 2: Healthcare Patient Grouping using GMM: Hospitals often need to group patients with similar characteristics to: Identify high-risk patients, Personalize treatment plans, Optimize resource allocation, Improve healthcare management



import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.mixture import GaussianMixture
import numpy as np

# Load data
# download from:
# https://www.pythonplaza.com/healthcare_patient_dataset.html
df = pd.read_csv("patients_data.csv")

# Features used for clustering
X = df[['Age',
        'BMI',
        'Blood_Pressure',
        'Cholesterol',
        'Hospital_Visits']]

# Scale data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# ---------------------------------------
# Train Gaussian Mixture Model (GMM)
# ---------------------------------------
gmm = GaussianMixture(n_components=3, random_state=42)
gmm.fit(X_scaled)

# Assign clusters
df['Cluster'] = gmm.predict(X_scaled)

# New patient
new_patient = [[33, 28.0, 139, 210, 4]]

# Apply SAME scaling
new_patient_scaled = scaler.transform(new_patient)

# Predict cluster using GMM
cluster = gmm.predict(new_patient_scaled)

print("Patient belongs to Cluster:", cluster[0])

# ---------------------------------------
# Optional: cluster centers (means of Gaussians)
# ---------------------------------------
centers = scaler.inverse_transform(gmm.means_)

cluster_profiles = pd.DataFrame(
    centers,
    columns=['Age','BMI','Blood_Pressure','Cholesterol','Hospital_Visits']
)

print("\nCluster Profiles (Gaussian Means):")
print(cluster_profiles.round(2))




About Us  | Contact Us | Sitemap  | Privacy Policy