liveBook · Manning

Variance

As explained in Outlier Detection in Python

Variance is a statistical measure of dispersion equal to the average squared deviation from the mean.

Variance measures dispersion: the average squared deviation from the mean. In multivariate analysis, it indicates how much variation each transformed component captures and helps reveal structure in data. section 6.9

PCA uses variance to rotate correlated features into components ordered by the variation they capture. The first component preserves the dominant pattern, while later components represent remaining variance. section 6.9

Interpret variance operationally rather than treating it as an automatic anomaly signal:

  • High variance in a component can expose extreme observations, including points that follow the main pattern but lie far from it. section 6.9
  • Low-variance components may still expose observations that have ordinary values in each original feature but violate feature relationships. section 6.9
  • Detector variance can also describe instability: very small neighbor counts produce high-variance, potentially arbitrary evaluations. section 8.1

PCA on correlated two-dimensional data

A synthetic dataset forms a diagonal pattern because one feature is built from another plus small noise. PCA transforms it into two components: the first captures most variance, while the second captures the residual variation. Two outliers become conspicuous in the transformed space. section 6.9

Listing 6.9 Example using scikit-learn’s PCA class to transform 2D data
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
 
x_data = np.random.random(100)                                    #1
y_data = np.random.random(100) / 10.0
 
data = pd.DataFrame({'A': x_data, 'B': x_data + y_data})          #2
data= pd.concat([data, 
   pd.DataFrame([[1.8, 1.8], [0.5, 0.1]], columns=['A', 'B'])])
 
pca = PCA(n_components=2)                                         #3
pca.fit(data)
print(pca.explained_variance_ratio_)
 
new_data = pd.DataFrame(pca.transform(data), columns=['0', '1'])  #4

The listing shows how to fit scikit-learn's PCA to a pandas DataFrame, inspect explained variance ratios, and transform observations into component coordinates. section 6.9

How Outlier Detection in Python covers it

How deep it goes
Explains variance through component transformation and explained-variance inspection, without developing a standalone statistical derivation.
Examples
Uses correlated synthetic two-dimensional data, two contrasting outliers, PCA components, and a scikit-learn implementation.
What it assumes
Assumes familiarity with features, correlations, components, and basic tabular data handling.
What you can do after
Apply scikit-learn PCA to transform tabular data and inspect component variance when investigating outliers.
How it connects
Links variance to PCA, dimensionality reduction, feature correlations, Mahalanobis distance, and outlier detection.

Also explained in

Compare how 19 books explain variance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.