PythiloLesson 11 of 12 · progress kept in this browser

PCA by Power Iteration

Centre a two-dimensional cloud, build its covariance matrix and repeatedly multiply a direction. The final unit vector points along the greatest variance; choose a consistent sign.

import math
vector = [2, 2]
norm = math.hypot(*vector)
print([x/norm for x in vector])
Your task

principal_axis(points, steps) returns the first principal unit direction for 2D points using power iteration starting at (1,1)/sqrt(2). Centre points, use population covariance, take steps iterations; fall back to the covariance column with the greatest norm if the starting direction is orthogonal; return (0,0) for zero variance and orient x >= 0. Inputs are nonempty.