WRITE · TRACE · UNDERSTAND
PCA by Power Iteration
Centre a two-dimensional cloud, build its covariance matrix and repeatedly multiply a direction.
Your experiment
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.
Python runs in your browser. Your draft stays on this device.
What to look for
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.
Watch your code work
Ready to experimentWrite a solution or load the worked answer, then choose Trace & check.
Step through arrays, variables and training curves produced by your own Python.