Skillversitas

Unsupervised Learning

Unsupervised learning works on data without labels. Instead of predicting a known answer, the algorithms search for structure: clusters of similar points, compact lower-dimensional representations, or the boundary of what "normal" looks like. Typical applications are customer segmentation, exploratory analysis, compression, and anomaly detection.

K-Means Clustering

K-means partitions data into k clusters by alternating two steps: assign each point to its nearest centroid, then move each centroid to the mean of its members. It is fast and scalable, but you must choose k, scale the features first, and be aware it prefers round, similarly sized clusters.

import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

X = StandardScaler().fit_transform(df[["recency", "frequency", "monetary"]])

inertia = []
for k in range(2, 9):
    km = KMeans(n_clusters=k, n_init=10, random_state=42).fit(X)
    inertia.append(km.inertia_)
print(inertia)  # look for the "elbow" where adding clusters stops helping

kmeans = KMeans(n_clusters=4, n_init=10, random_state=42).fit(X)
df["cluster"] = kmeans.labels_
print(df.groupby("cluster").mean())  # interpret each segment

Choosing k and Measuring Fit

Because clusters have no ground truth, quality is measured geometrically. The silhouette score is the standard choice: it compares a point's cohesion to its own cluster against its separation from the nearest other cluster, ranging from -1 (misclassified) to +1 (well placed). The highest average silhouette over candidate k values picks a defensible cluster count.

from sklearn.metrics import silhouette_score, silhouette_samples

for k in (3, 4, 5, 6):
    labels = KMeans(n_clusters=k, n_init=10, random_state=42).fit_predict(X)
    print(k, "silhouette:", silhouette_score(X, labels))
sample_scores = silhouette_samples(X, kmeans.labels_)
print("worst-fit points:", sample_scores.argsort()[:5])

When clusters have irregular shapes, hierarchical clustering with AgglomerativeClustering or density-based DBSCAN are better fits; DBSCAN also marks sparse points as noise, which directly supports outlier flagging.

PCA Dimensionality Reduction

Principal Component Analysis rotates the feature space so each new axis captures as much remaining variance as possible. It decorrelates features, compresses data, and removes noise. Inspect the cumulative explained variance to decide how many components to keep — commonly enough for 90–95% of the total.

from sklearn.decomposition import PCA

pca = PCA(n_components=0.95)  # keep components covering 95% of variance
X_reduced = pca.fit_transform(X)
print(X.shape, "-", X_reduced.shape, "dimensions")
print("explained variance:", pca.explained_variance_ratio_)

Two-dimensional PCA projections are also the quickest way to eyeball whether clusters the algorithm found actually look separated on a plot. For visualization with nonlinear structure, UMAP or t-SNE are common follow-ups in notebooks.

Anomaly Detection

Anomalies are points that fit the learned structure poorly. IsolationForest isolates each point with random cuts and scores how few cuts were needed; fraud and failure detection use this heavily. A fitted autoencoder from Keras is an alternative: reconstruction error spikes on inputs the network cannot compress well.

from sklearn.ensemble import IsolationForest

iso = IsolationForest(contamination=0.02, random_state=42)
flags = iso.fit_predict(X)  # -1 = anomaly, 1 = normal
print("anomalies:", (flags == -1).sum())

Summary

Unsupervised learning finds structure without labels: k-means for segmentation with silhouette-score validation and an elbow scan for k, PCA for compressing correlated features while keeping most variance, and isolation-based methods for flagging anomalies. Scale your features, visualize the clusters, and interpret each segment by its average profile to turn clusters into business decisions.