Tuesday, April 1, 2025

Data Science and Analytics Tools - Customer Segmentation using K-Means Clustering

 Notes:

  • What problem does it solve?
    Helps businesses identify distinct customer segments based on purchasing behavior or demographic features, allowing targeted marketing.

  • How can businesses or users benefit from customizing the code?
    Businesses can adjust the model for different customer attributes, clustering methods, and target numbers of segments.

  • How can businesses or users adopt the solution further, if needed?
    Custom segmentation can be used for marketing campaigns, personalized promotions, and improving customer retention strategies.

Actual Python Code:


import pandas as pd

from sklearn.cluster import KMeans

import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler


# Load customer data (assumed to have columns like 'Age', 'Annual Income', 'Spending Score')

data = pd.read_csv('customer_data.csv')


# Normalize data

scaler = StandardScaler()

scaled_data = scaler.fit_transform(data[['Age', 'Annual Income', 'Spending Score']])


# Apply KMeans clustering

kmeans = KMeans(n_clusters=4, random_state=42)

clusters = kmeans.fit_predict(scaled_data)


# Add cluster labels to the data

data['Cluster'] = clusters


# Visualize the clusters

plt.scatter(data['Annual Income'], data['Spending Score'], c=data['Cluster'], cmap='viridis')

plt.xlabel('Annual Income')

plt.ylabel('Spending Score')

plt.title('Customer Segmentation using K-Means')

plt.show()


# Display cluster centers

print("Cluster Centers:")

print(kmeans.cluster_centers_)


No comments:

Post a Comment

IoT (Internet of Things) Automation - Smart Energy Usage Tracker

  Notes: Problem Solved: Logs and analyzes power usage from smart meters. Customization Benefits: Track per-device energy and set ale...