Search Knowledge

© 2026 LIBREUNI PROJECT

Python for Scientific Computing / Scikit-Learn Deep Dive

Scikit-Learn: Unsupervised Learning

Learning Without Labels

Unsupervised learning is used when we have features (XX) but no target (yy). The goal is to find inherent patterns or structures within the data.

1. Clustering: Grouping Similar Samples

Clustering algorithms attempt to partition the data into groups (clusters) where samples in the same group are more similar to each other than to those in other groups.

K-Means Clustering

The most popular clustering algorithm. it partitions data into KK clusters by minimizing the distance between points and their cluster centroids.

python
1 
2from sklearn.cluster import KMeans
3from sklearn.datasets import make_blobs
4import matplotlib.pyplot as plt
5 
6# Create 3 distinct blobs of data
7X, _ = make_blobs(n_samples=300, centers=3, cluster_std=0.60, random_state=0)
8 
9# Instantiate and fit
10kmeans = KMeans(n_clusters=3)
11kmeans.fit(X)
12 
13# Predicted labels
14y_kmeans = kmeans.predict(X)
15print(f"Centroids:\n{kmeans.cluster_centers_}")
16 

2. Dimensionality Reduction

High-dimensional data (hundreds or thousands of features) is difficult to visualize and can lead to the “curse of dimensionality.” Dimensionality reduction seeks to represent data in a lower-dimensional space while preserving as much information as possible.

Principal Component Analysis (PCA)

PCA finds the “principal components”—the orthogonal axes along which the data varies the most.

python
1 
2from sklearn.decomposition import PCA
3from sklearn.datasets import load_iris
4 
5iris = load_iris()
6X = iris.data # 4 features
7 
8# Reduce from 4D to 2D
9pca = PCA(n_components=2)
10X_reduced = pca.fit_transform(X)
11 
12print(f"Original shape: {X.shape}")
13print(f"Reduced shape: {X_reduced.shape}")
14print(f"Explained variance ratio: {pca.explained_variance_ratio_}")
15 

3. Anomaly Detection

Identifying outliers or unusual patterns that do not conform to expected behavior.

Isolation Forest

Isolation Forest works by isolating anomalies using trees. Anomalies are easier to isolate and thus have shorter path lengths in the trees.

python
1 
2from sklearn.ensemble import IsolationForest
3import numpy as np
4 
5# Generate normal data
6X_train = 0.3 * np.random.randn(100, 2)
7X_train = np.r_[X_train + 2, X_train - 2]
8 
9# Generate some abnormal novel observations
10X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2))
11 
12clf = IsolationForest(contamination=0.1)
13clf.fit(X_train)
14 
15y_pred_outliers = clf.predict(X_outliers)
16print(f"Predictions for outliers (-1 is anomaly):\n{y_pred_outliers}")
17 

Summary of Unsupervised Techniques

Code
skinparam componentStyle rectangle

package "Unsupervised Tasks" {
component "Clustering" as CLUST
component "Dimensionality Reduction" as DIM
component "Density Estimation" as DENS
}

node "Algorithms" {
component "K-Means / DBSCAN / Agglomerative" as CALG
component "PCA / t-SNE / UMAP" as DALG
component "GMM / Kernel Density" as DEALG
}

CLUST --> CALG
DIM --> DALG
DENS --> DEALG
Unsupervised TasksAlgorithmsClusteringDimensionality ReductionDensity EstimationK-Means / DBSCAN /AgglomerativePCA / t-SNE / UMAPGMM / Kernel Density

In the next section, we will explore specialized scientific libraries like SciPy and Matplotlib to round out our scientific Python toolkit.