Use DBSCAN to Handle Noise
Advanced · Write the code · from K-Means Clustering: Find Patterns in Data Without Labels
From the lesson: K-Means Clustering: Find Patterns in Data Without Labels
A streaming service groups users into "action fans," "comedy lovers," and "documentary watchers" - without anyone filling out a survey. A retailer segments customers into "big spenders," "bargain hunters," and "window shoppers" based on purchase behavior. Nobody labeled these groups. The algorithm discovered them.
Read the lessonFit a DBSCAN(eps=0.5, min_samples=5) on the scaled data. Print the number of clusters found (excluding noise), then the number of noise points on a new line. Noise points have label -1.
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
import numpy as np
X, _ = make_blobs(n_samples=150, centers=3,
cluster_std=0.8, random_state=42)
noise = np.random.RandomState(42).uniform(-10, 10, (20, 2))
X_full = np.vstack([X, noise])
X_scaled = StandardScaler().fit_transform(X_full)
# Fit DBSCAN and print number of clusters + noise points
More K-Means Clustering exercises
- Run Your First K-MeansAdvanced
- Predict the Inertia PatternAdvanced
- Find the Best K with SilhouetteAdvanced
- Complete Clustering PipelineAdvanced
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz