Iris Data Analysis with Scikit-Learn: Standardization, Spectral Clustering, and Evaluation

Data Acquisition and Partitioning

The initial phase involves importing the Iris dataset and partitioning it into subsets for training and testing. This ensures that the model's performance can be evaluated on unseen data.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load the dataset
iris_bunch = load_iris()
X_features = iris_bunch.data
y_target = iris_bunch.target

# Split the data: 80% for training and 20% for testing
X_train, X_test, y_train, y_test = train_test_split(
    X_features, y_target, test_size=0.2, random_state=42
)

The train_test_split function utilizes several key parameters:

  • arrays: Accepts the data matrices and corresponding labels to be divided.
  • test_size: Defines the proportion of the dataset allocated for testing (e.g., 0.2 for 20%).
  • train_size: Specifies the training set proportion; optional if test_size is set.
  • random_state: An integer seed controlling the random number generator for reproducible splits.
  • shuffle: Boolean determining whether to shuffle data before splitting.
  • stratify: If not None, ensures the split maintains the proportion of samples for each class.

Feature Scaling

To improve algorithm convergence, feature standardization is applied. This process rescales features to have a mean of zero and a standard deviation of one (Z-score normalization).

from sklearn.preprocessing import StandardScaler

# Initialize the standard scaler
scaler = StandardScaler()

# Fit the scaler to training data and transform both sets
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

While StandardScaler removes the mean and scales to unit variance, other methods exist such as MinMaxScaler for scaling to a fixed range, Normalizer for individual sample normalization, and OneHotEncoder for categorical features.

Implementing Spectral Clustering

Spectral Clustering is utilized here to group the data points. This technique is effective for identifying clusters with complex geometric structures based on graph theory.

from sklearn.cluster import SpectralClustering

# Define the spectral clustering model with 3 clusters
cluster_algo = SpectralClustering(n_clusters=3, affinity='nearest_neighbors', random_state=42)

# Fit the model to the scaled training data
cluster_algo.fit(X_train_scaled)
predicted_labels = cluster_algo.labels_

Alternative clustering algorithms within Scikit-Learn include KMeans (for large datasets), DBSCAN (density-based), and Agglomerative Clustering (hierarchical).

Cluster Visualization

Visualizing the results provides intuition about the model's performance. We can plot the standardized features directly or apply dimensionality reduction using t-SNE for clearer separation.

import matplotlib.pyplot as plt
import pandas as pd
from sklearn.manifold import TSNE

# Visualization using the first two features
plt.figure(figsize=(8, 5))
for i in range(3):
    plt.scatter(
        X_train_scaled[predicted_labels == i, 0],
        X_train_scaled[predicted_labels == i, 1],
        label=f'Cluster {i}'
    )
plt.title('Cluster Visualization on Standardized Features')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.show()

# Visualization using t-SNE
tsne_transformer = TSNE(n_components=2, init='random', random_state=123)
X_tsne = tsne_transformer.fit_transform(X_train_scaled)

tsne_df = pd.DataFrame(X_tsne, columns=['Dim1', 'Dim2'])
tsne_df['label'] = predicted_labels

plt.figure(figsize=(8, 6))
for i in range(3):
    subset = tsne_df[tsne_df['label'] == i]
    plt.scatter(subset['Dim1'], subset['Dim2'], label=f'Cluster {i}')
plt.title('t-SNE Projection of Clusters')
plt.legend()
plt.show()

Model Evaluation

To assess clustering quality without ground truth labels, the Silhouette Score is calculated. This metric measures how close each point in one cluster is to points in the neighboring clusters.

from sklearn.metrics import silhouette_score

# Iterate through different cluster counts to find the optimal configuration
for k in range(3, 6):
    temp_model = SpectralClustering(n_clusters=k, random_state=42)
    temp_preds = temp_model.fit_predict(X_train_scaled)
    score = silhouette_score(X_train_scaled, temp_preds)
    print(f"Clusters: {k} | Silhouette Score: {score:.4f}")

Other evaluation metrics in sklearn.metrics include the Adjusted Rand Index (ARI) and V-measure, which require true labels, while Calinski-Harabasz and Silhouette scores are unsupervised metrics.

Tags: python scikit-learn Machine Learning Data Science Clustering

Posted on Tue, 14 Jul 2026 17:19:14 +0000 by renegade888