IIT Madras Zanzibar · Teaching

← Z3003 Data Mining

Tutorial 2: Feature Selection & PCA

numpy · sklearn · matplotlib  ·  Submit as dm02t[RR].py

Submission

Name your file dm02t[RR].py where RR = last two digits of your roll number. All tasks in one file with # Task N comments. Submit by the deadline announced in class.

Background

Real datasets often have many features, most of which add noise rather than signal. Feature selection identifies which features are most informative for a classification task. PCA goes further: it constructs new axes (principal components) that record maximum variance using fewer dimensions. Both are essential preprocessing steps before any large-scale mining pipeline.

We use the Iris dataset (4 features, 3 classes, 150 samples): small enough to understand fully.

Tasks

Task 1 Load and Explore the Dataset

  1. Import: numpy as np, from sklearn.datasets import load_iris, from sklearn.preprocessing import StandardScaler
  2. Load: data = load_iris(). Store features as X and labels as y
  3. Print the shape of X
  4. Print the feature names: data.feature_names
  5. Print the mean and standard deviation of each feature using np.mean(X, axis=0) and np.std(X, axis=0)
X shape: (150, 4) Features: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'] Means: [5.843 3.057 3.758 1.199] Stds: [0.828 0.436 1.765 0.762]

Task 2 Standardise the Features

PCA is sensitive to feature scale. Standardise each feature to have mean 0 and standard deviation 1 before applying PCA.

  1. Create a StandardScaler and fit-transform: X_scaled = scaler.fit_transform(X)
  2. Print the mean and std of each column of X_scaled to verify (means ≈ 0, stds ≈ 1)
After scaling: Means: [~0, ~0, ~0, ~0] Stds: [~1, ~1, ~1, ~1]
Without standardisation, features measured in large units would dominate PCA simply because of their scale.

Task 3 Compute the Covariance Matrix

The covariance matrix records how features vary together and is the foundation of PCA.

  1. Compute: cov_matrix = np.cov(X_scaled.T)
  2. Print the 4×4 covariance matrix
  3. Identify the two features with the highest covariance (look at off-diagonal values)
  4. Write your answer as a comment: # Feature X and Feature Y have the highest covariance: Z
Covariance matrix (4x4): [[ 1.006 -0.117 0.872 0.818] [-0.117 1.006 -0.428 -0.366] [ 0.872 -0.428 1.006 0.969] [ 0.818 -0.366 0.969 1.006]] # petal length and petal width have the highest covariance: 0.969

Task 4 Apply PCA: Reduce to 2 Components

  1. Import: from sklearn.decomposition import PCA
  2. Create pca = PCA(n_components=2) and fit-transform X_scaled
  3. Print the shape of the result X_pca
  4. Print pca.explained_variance_ratio_
  5. Print the total variance retained (sum of explained variance ratios) as a percentage
X_pca shape: (150, 2) Explained variance ratio: [0.7296 0.2285] Total variance retained: 95.81%
4 features reduced to 2, yet 95.8% of information is kept. This is the power of dimensionality reduction.

Task 5 Information Gain: Which Feature is Most Informative?

Information Gain (mutual information) measures how much a feature reduces uncertainty about the class label.

  1. Import: from sklearn.feature_selection import mutual_info_classif
  2. Compute: ig = mutual_info_classif(X, y, random_state=5)
  3. Print each feature name and its score, sorted from highest to lowest
  4. State which feature is most informative as a comment in your code
Feature importance (Information Gain): petal length (cm) : 0.9892 petal width (cm) : 0.9811 sepal length (cm) : 0.5249 sepal width (cm) : 0.2638 # Most informative: petal length (cm)

Task 6 Extension: How Many Components Do You Need?

Apply PCA with all 4 components. Print the cumulative explained variance. At what number of components do you reach 99% variance retained? Write your answer as a comment.

Use np.cumsum(pca.explained_variance_ratio_) on a PCA fitted with all components.