Simple Python Clustering Example based on the high school students' score

CK CK 3788 
Created at  
340 0 0

What is Clustering?

Clustering is a machine learning technique used to group similar data points together into clusters. The goal is to find inherent structure in data by identifying groups where the points within each cluster are more similar to each other than to points in other clusters. This allows for data summarization, anomaly detection, and improved understanding of the underlying data relationships.

Simple Python Clustering Example based on the high school students' score

 

Example Code

Here’s an example of using Python to perform clustering on an array containing high school score data. The example uses the KMeans algorithm from the sklearn library.

import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler

# Example high school score data (e.g., [Math Score, English Score, Science Score])
data = np.array([
    [85, 78, 90],
    [72, 88, 85],
    [95, 92, 98],
    [60, 65, 70],
    [75, 80, 79],
    [90, 85, 88],
    [55, 60, 58],
    [80, 83, 85],
    [98, 99, 100],
    [65, 70, 68]
])

# Standardize the data for better clustering performance
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)

# Perform K-Means clustering
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(data_scaled)

# Get cluster labels and centroids
labels = kmeans.labels_
centroids = kmeans.cluster_centers_

# Display results
print("Cluster labels:", labels)
print("Centroids:", scaler.inverse_transform(centroids))

# Plot the data (2D visualization for simplicity)
plt.scatter(data_scaled[:, 0], data_scaled[:, 1], c=labels, cmap='viridis', marker='o', s=100, label="Students")
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', marker='x', s=200, label="Centroids")
plt.title("High School Score Clustering")
plt.xlabel("Standardized Math Score")
plt.ylabel("Standardized English Score")
plt.legend()
plt.show()

Explanation

  1. Data Input: The data array simulates high school scores in subjects like Math, English, and Science for various students.
  2. Standardization: Data is scaled using StandardScaler to ensure all features contribute equally to the clustering process.
  3. Clustering: The KMeans algorithm is applied to partition the data into clusters. Adjust n_clusters as needed.
  4. Results: The cluster labels and centroids are printed. The centroids are transformed back to the original scale for better interpretation.
  5. Visualization: A 2D scatter plot shows clusters and their centroids, focusing on the first two features for simplicity.

You can extend this to more features or use other clustering methods like DBSCAN or Agglomerative Clustering based on the data characteristics.

 

DBSCAN Clustering

Here’s an example using the DBSCAN clustering algorithm to process high school score data. DBSCAN (Density-Based Spatial Clustering of Applications with Noise) identifies clusters based on the density of data points and can detect outliers.

Example Code

import numpy as np
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Example high school score data (e.g., [Math Score, English Score, Science Score])
data = np.array([
    [85, 78, 90],
    [72, 88, 85],
    [95, 92, 98],
    [60, 65, 70],
    [75, 80, 79],
    [90, 85, 88],
    [55, 60, 58],
    [80, 83, 85],
    [98, 99, 100],
    [65, 70, 68]
])

# Standardize the data for better clustering performance
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)

# Perform DBSCAN clustering
dbscan = DBSCAN(eps=1.5, min_samples=2)  # Adjust `eps` and `min_samples` as needed
dbscan.fit(data_scaled)

# Get cluster labels
labels = dbscan.labels_

# Display results
print("Cluster labels:", labels)  # -1 indicates noise (outliers)

# Visualize the clustering results in 2D space
plt.scatter(data_scaled[:, 0], data_scaled[:, 1], c=labels, cmap='viridis', marker='o', s=100)
plt.title("High School Score Clustering with DBSCAN")
plt.xlabel("Standardized Math Score")
plt.ylabel("Standardized English Score")
plt.colorbar(label="Cluster")
plt.show()

Explanation

  1. Data Input: The data array contains high school scores in Math, English, and Science.
  2. Standardization: Scores are standardized with StandardScaler to make the features comparable.
  3. DBSCAN Parameters:
    • eps: The maximum distance between two points to be considered part of the same neighborhood.
    • min_samples: The minimum number of points required to form a dense region.
  4. Labels:
    • A cluster label (0, 1, 2, ...) is assigned to each data point.
    • A label of -1 indicates that the point is considered noise or an outlier.
  5. Visualization: Only the first two features are plotted for clarity. Use tools like PCA or t-SNE for better visual representation in higher dimensions.

Customization

  • Tune eps and min_samples to better fit the density of your dataset.
  • Consider visualizing the results in a 3D plot or reducing dimensions with PCA for more detailed datasets.

 

Agglomerative Clustering

Here’s an example of performing hierarchical clustering using the Agglomerative Clustering algorithm from the sklearn library. This example uses the same high school score dataset.

Example Code

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import StandardScaler
from scipy.cluster.hierarchy import dendrogram, linkage

# Example high school score data (e.g., [Math Score, English Score, Science Score])
data = np.array([
    [85, 78, 90],
    [72, 88, 85],
    [95, 92, 98],
    [60, 65, 70],
    [75, 80, 79],
    [90, 85, 88],
    [55, 60, 58],
    [80, 83, 85],
    [98, 99, 100],
    [65, 70, 68]
])

# Standardize the data
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)

# Perform hierarchical clustering
agg_clustering = AgglomerativeClustering(n_clusters=3, linkage='ward')
labels = agg_clustering.fit_predict(data_scaled)

# Display results
print("Cluster labels:", labels)

# Visualize dendrogram
linkage_matrix = linkage(data_scaled, method='ward')
plt.figure(figsize=(10, 7))
dendrogram(linkage_matrix, labels=np.arange(1, len(data) + 1), leaf_font_size=12, color_threshold=0.7 * np.max(linkage_matrix[:, 2]))
plt.title("Dendrogram of High School Scores")
plt.xlabel("Student Index")
plt.ylabel("Ward's Distance")
plt.axhline(y=7, color='r', linestyle='--', label='Cluster Threshold')
plt.legend()
plt.show()

# Visualize clusters in 2D (using first two features for simplicity)
plt.scatter(data_scaled[:, 0], data_scaled[:, 1], c=labels, cmap='viridis', s=100)
plt.title("Agglomerative Clustering of High School Scores")
plt.xlabel("Standardized Math Score")
plt.ylabel("Standardized English Score")
plt.show()

Explanation

  1. Data Scaling: Standardizes the data for better clustering.
  2. Agglomerative Clustering:
    • linkage='ward': Uses Ward's method to minimize the variance within clusters.
    • n_clusters=3: Specifies the number of clusters to form.
  3. Dendrogram:
    • Uses scipy.cluster.hierarchy.dendrogram and linkage to visualize how the clusters are formed hierarchically.
    • The red dashed line indicates the threshold at which clusters are cut.
  4. 2D Visualization:
    • Scatter plot of clusters (limited to the first two features for simplicity).

Customization

  • Change linkage to complete, average, or single for different hierarchical clustering strategies.
  • Adjust n_clusters or explore the dendrogram to decide an appropriate threshold.
Tags Agglomerative Clustering DBSCAN KMeans Python Clustering sklearn Facebook X
Comments 0
Similar posts
  1. Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python
    633
  2. Save clustering result (aka dictionary) in JSON and Reuse loaded result
    378
  1. Flink with Python: Core Concepts, Kafka Integration, and Architectural Trade-offs
    187
  2. How do I determine the client IP type in Python - IPv4 or IPv6
    761
  3. Predicting Buyer Preferences with PyTorch: A Deep Learning Approach
    1,233
  4. PyTorch example to Forcast Stock Price based on 10 days Dataset
    651
  5. Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
    1,071
  6. Dynamic DNS Made Easy: Building a Python-Based Solution
    795
  7. Milvus & FAISS: Decoding Their ML Synergy
    278
  8. Save clustering result (aka dictionary) in JSON and Reuse loaded result
    378
  9. Forecasting with Linear Regression and KNN Regression in Python
    925
  10. Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction
    1,434
  11. Uvicorn, Gunicorn: Proving Your Production Prowess
    397
  12. Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python
    1,156
  13. Harnessing the Power of Random Forest Algorithm in Python
    658
  14. Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python
    633
  15. Mastering Random Forest Regression: A Comprehensive Guide with Python Examples
    1,096
  16. Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network
    800
  17. Memory Problems with Python Arrays? Discover Dask Array
    323
  18. Python Implementation of Linear Regression
    840
  19. Mastering Supervised Machine Learning with Python: A Comprehensive Guide
    1,056
  20. Mastering AI: A Beginner's Guide to Python Programming and Beyond
    1,033
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    39
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    216
  30. Understanding Data Lineage
    260