PCA for materials data · Part 4 of four ·

From five measurements to a PCA picture

We can now connect the pictures to our five measurement columns. We will find combinations of those columns and check how much variation a simpler picture retains.

First, we examine projection

We begin with two of our five columns. The horizontal axis is standardized yield strength and the vertical axis is standardized tensile strength. Each blue point is one of our 300 specimens.

The blue points stay fixed. Each gold point is the nearest position on the chosen line to a blue point. That nearest position is its orthogonal projection. At 0°, the line lies along the yield-strength axis, so the spread of the gold points is simply the variance of standardized yield strength: 1. In Part 3, we found an eigenvalue of 1.967 for this pair of columns. Turn the line and try to find a direction with that much spread before revealing PC1.

Which direction gives the greatest spread?

Blue: our specimens. Gold: projected positions. A few connecting lines show how projection works. All 300 specimens are projected.

The strip uses the same scale at every angle. PCA measures spread using variance. A larger value means greater spread around the mean.

Make your prediction first.

Optional: inspect a three-dimensional example

Here you can rotate the view of a filled volume of points. Viewing rotation helps you inspect its shape. It does not change the data or perform PCA.

Inspect the points in three dimensions

Loading the observations...

The optional 3D example contains 1,155 grid points, or 1,200 random points using seed 714.

Where do eigenvectors enter?

For centered data, the covariance matrix's unit eigenvectors give principal directions. The direction with the largest eigenvalue gives the greatest variance of projected observations. We call it the first principal direction. The next direction is perpendicular to it and captures the greatest variance available under that restriction.

We saw this in the activity. The largest spread we could find, 1.967, was the largest eigenvalue from Part 3, and it occurred along the eigenvector (1, 1). This is always true: the direction of greatest variance is the eigenvector with the largest eigenvalue, and the variance along it equals that eigenvalue. For two correlated standardized columns, PC1 always lies at 45° or 135°. With five columns, the directions are not so easy to guess, which is why we calculate them.

Optional: why does the eigenvector give the greatest variance?

Let \(\mathbf u\) be a unit direction and \(C\) the covariance matrix of the centered data. Each specimen's score along \(\mathbf u\) is its dot product with \(\mathbf u\). The variance of these scores is

\[\operatorname{Var}=\mathbf u^{\mathsf T}C\,\mathbf u.\]

For our two strengths, with \(\mathbf u=(\cos\theta,\sin\theta)\), this becomes \(1+0.967\sin 2\theta\). It is largest at \(\theta=45^\circ\), where it equals 1.967.

In general, if \(C\mathbf u=\lambda\mathbf u\), then \(\mathbf u^{\mathsf T}C\mathbf u=\lambda\,\mathbf u^{\mathsf T}\mathbf u=\lambda\), because a unit vector has \(\mathbf u^{\mathsf T}\mathbf u=1\). Among all unit directions, \(\mathbf u^{\mathsf T}C\mathbf u\) is largest for the eigenvector with the largest eigenvalue. This result is known as the Rayleigh quotient property of symmetric matrices.

When we project each specimen onto these directions, we obtain its component scores. A direction specifies the combination of features; a score is the value of that combination for one specimen. These are different things.

In five dimensions, each direction has five coefficients. All five measurements participate in calculating the new coordinates. PC1 and PC2 are not two selected spreadsheet columns.

We calculate one step at a time

Download the CSV and save it beside the notebook or Python script. Download the complete Python example. We use Python and NumPy. Run each section before moving to the next. In a notebook, put each section in its own cell.

1. Read and inspect the data

import numpy as np

# We read the five measurements, leaving out the specimen label.
data = np.loadtxt("materials-pca-synthetic.csv",
                  delimiter=",", skiprows=1, usecols=(1, 2, 3, 4, 5))
print(data.shape)
print(data[:3])
assert data.shape == (300, 5)
assert np.isfinite(data).all()

We should see 300 rows and five columns. The first three rows should match the spreadsheet. We check for missing or nonfinite numbers before calculating.

2. Center and scale each measurement column

# We calculate each mean and sample standard deviation across specimens.
means = data.mean(axis=0)
standard_deviations = data.std(axis=0, ddof=1)
assert (standard_deviations > 0).all()
scaled = (data - means) / standard_deviations
print(scaled.mean(axis=0))
print(scaled.var(axis=0, ddof=1))

The means should be close to zero, allowing for rounding in computer arithmetic. The variances should be one. Here axis=0 means calculate down each column; ddof=1 uses the sample convention with divisor n−1.

3. Find the directions and their variances

# We form the covariance matrix of the five standardized features.
covariance = scaled.T @ scaled / (len(scaled) - 1)
values, directions = np.linalg.eigh(covariance)

# We place the largest eigenvalue and its direction first.
order = np.argsort(values)[::-1]
values = values[order]
directions = directions[:, order]

# We choose a sign that makes the yield-strength coefficient positive.
if directions[1, 0] < 0:
    directions[:, 0] *= -1
# We fix PC2's sign as well, so that our numbers match the plot below.
# With this choice, PC2's conductivity coefficient is negative.
if directions[4, 1] > 0:
    directions[:, 1] *= -1

fractions = values / values.sum()
scores = scaled @ directions
print(100 * fractions)
print(directions[:, 0])
print(directions[:, 1])

T transposes rows and columns; @ performs matrix multiplication. eigh is NumPy's solver for real symmetric matrices. Each column of directions is one unit eigenvector. The sign of an eigenvector can be reversed without changing the PCA result: (1, 1) and (−1, −1) describe the same line. Different computers or libraries may return either sign, so we fix the signs of PC1 and PC2 to match this article.

What does our first combination contain?

These are the calculated PC1 and PC2 weights for the standardized features. We have reached this table only after explaining what the calculation does.

Standardized featurePC1 weightPC2 weight
Grain size-0.482+0.140
Yield strength+0.484+0.059
Tensile strength+0.477-0.146
Elongation-0.452+0.390
Conductivity-0.318-0.896

Positive weights on both strengths and a negative weight on elongation mean that this component describes a tendency toward higher strengths and lower elongation. Grain size and conductivity also contribute. We should interpret the weights together and remember that they were calculated after standardization.

PC2 is dominated by conductivity, with weight −0.896. Elongation contributes +0.390, and the other weights are small. PC2 therefore describes differences in conductivity that the strength trend of PC1 does not explain. Because the conductivity weight is negative, a large positive PC2 score means lower conductivity. S005 has PC2 score 0.979 and conductivity 26.1 MS/m, below the dataset mean of 27.48 MS/m. S006 has PC2 score −1.398 and conductivity 28.4 MS/m, above the mean. This is the extra relationship involving conductivity that we noticed in Part 3.

S003 has PC1 score 2.835, S005 has −0.195 and S006 has 2.413. These are dimensionless calculated values, not strengths or grain sizes. One score alone does not fully describe a specimen. A PC1-versus-PC2 plot uses the first two scores for every specimen.

S001: PC1 1.875; PC2 0.375S002: PC1 0.726; PC2 -0.161S003: PC1 2.835; PC2 -1.071S004: PC1 0.861; PC2 0.576S005: PC1 -0.195; PC2 0.979S006: PC1 2.413; PC2 -1.398S007: PC1 2.969; PC2 1.435S008: PC1 0.327; PC2 0.149S009: PC1 -0.535; PC2 0.146S010: PC1 -0.451; PC2 0.481S011: PC1 -3.179; PC2 0.265S012: PC1 0.815; PC2 -0.922S013: PC1 -2.522; PC2 1.212S014: PC1 -1.588; PC2 0.217S015: PC1 -1.705; PC2 0.321S016: PC1 2.483; PC2 0.384S017: PC1 1.276; PC2 0.575S018: PC1 2.103; PC2 -1.113S019: PC1 1.722; PC2 -0.333S020: PC1 -1.127; PC2 0.075S021: PC1 0.121; PC2 -0.133S022: PC1 -0.895; PC2 -0.335S023: PC1 1.992; PC2 -0.318S024: PC1 -0.313; PC2 1.299S025: PC1 2.267; PC2 1.277S026: PC1 -1.088; PC2 0.877S027: PC1 0.061; PC2 1.153S028: PC1 -1.002; PC2 1.371S029: PC1 -3.296; PC2 -0.156S030: PC1 -0.018; PC2 0.019S031: PC1 -0.335; PC2 0.874S032: PC1 3.156; PC2 1.075S033: PC1 -2.918; PC2 0.503S034: PC1 -1.911; PC2 -0.026S035: PC1 -2.697; PC2 0.967S036: PC1 2.984; PC2 0.687S037: PC1 -3.129; PC2 0.482S038: PC1 -0.812; PC2 1.232S039: PC1 2.994; PC2 0.994S040: PC1 -2.068; PC2 -1.079S041: PC1 -0.335; PC2 0.634S042: PC1 -3.074; PC2 0.249S043: PC1 -0.934; PC2 -0.462S044: PC1 -2.617; PC2 -0.413S045: PC1 0.265; PC2 0.223S046: PC1 -3.071; PC2 1.255S047: PC1 -2.644; PC2 0.574S048: PC1 2.898; PC2 -0.708S049: PC1 2.101; PC2 1.272S050: PC1 3.828; PC2 0.180S051: PC1 -1.600; PC2 0.702S052: PC1 2.078; PC2 -0.877S053: PC1 -0.296; PC2 0.955S054: PC1 -1.014; PC2 1.142S055: PC1 1.744; PC2 0.376S056: PC1 2.820; PC2 -0.169S057: PC1 2.776; PC2 -0.638S058: PC1 -0.300; PC2 -1.030S059: PC1 -2.795; PC2 0.401S060: PC1 -2.088; PC2 1.253S061: PC1 -0.583; PC2 0.741S062: PC1 0.195; PC2 -0.363S063: PC1 0.792; PC2 -0.850S064: PC1 -0.869; PC2 0.894S065: PC1 0.413; PC2 0.490S066: PC1 1.867; PC2 -0.794S067: PC1 0.185; PC2 -0.736S068: PC1 -0.868; PC2 -0.146S069: PC1 -3.056; PC2 -0.847S070: PC1 2.494; PC2 0.093S071: PC1 -1.248; PC2 0.872S072: PC1 1.132; PC2 0.796S073: PC1 -2.716; PC2 -0.189S074: PC1 0.339; PC2 -0.277S075: PC1 2.750; PC2 0.872S076: PC1 1.200; PC2 0.842S077: PC1 0.715; PC2 0.685S078: PC1 0.873; PC2 0.952S079: PC1 -0.300; PC2 0.620S080: PC1 -3.401; PC2 -0.249S081: PC1 1.542; PC2 0.515S082: PC1 -1.351; PC2 0.394S083: PC1 -0.062; PC2 -0.268S084: PC1 1.238; PC2 1.215S085: PC1 -2.249; PC2 -1.210S086: PC1 1.290; PC2 0.975S087: PC1 -2.379; PC2 -1.510S088: PC1 2.692; PC2 -1.172S089: PC1 -1.549; PC2 -0.360S090: PC1 -2.883; PC2 0.730S091: PC1 0.548; PC2 0.378S092: PC1 2.675; PC2 1.039S093: PC1 1.365; PC2 0.093S094: PC1 -2.893; PC2 -0.313S095: PC1 0.990; PC2 0.261S096: PC1 -1.403; PC2 1.113S097: PC1 0.832; PC2 0.628S098: PC1 -0.010; PC2 0.179S099: PC1 -0.345; PC2 -0.707S100: PC1 2.483; PC2 -1.089S101: PC1 -2.261; PC2 0.196S102: PC1 1.170; PC2 -0.620S103: PC1 1.598; PC2 0.923S104: PC1 1.331; PC2 -0.418S105: PC1 3.900; PC2 0.550S106: PC1 -0.088; PC2 -0.617S107: PC1 -0.548; PC2 0.582S108: PC1 -0.805; PC2 -0.137S109: PC1 -0.924; PC2 1.789S110: PC1 -1.588; PC2 0.846S111: PC1 3.158; PC2 -1.371S112: PC1 -1.697; PC2 0.358S113: PC1 3.334; PC2 1.208S114: PC1 3.655; PC2 1.326S115: PC1 -1.233; PC2 0.460S116: PC1 -0.804; PC2 0.889S117: PC1 1.590; PC2 0.886S118: PC1 -2.200; PC2 -0.320S119: PC1 1.577; PC2 -1.318S120: PC1 -1.162; PC2 0.806S121: PC1 3.470; PC2 -0.106S122: PC1 2.297; PC2 -1.338S123: PC1 3.418; PC2 -0.370S124: PC1 -3.109; PC2 -0.398S125: PC1 -2.550; PC2 0.434S126: PC1 3.919; PC2 0.971S127: PC1 1.780; PC2 -0.811S128: PC1 2.213; PC2 -0.946S129: PC1 -3.241; PC2 -1.059S130: PC1 -3.371; PC2 -0.471S131: PC1 2.855; PC2 -0.809S132: PC1 0.519; PC2 -0.107S133: PC1 -1.852; PC2 -1.160S134: PC1 2.938; PC2 -0.325S135: PC1 -2.431; PC2 0.370S136: PC1 -2.452; PC2 -1.004S137: PC1 -0.924; PC2 -0.776S138: PC1 -1.676; PC2 -0.468S139: PC1 -0.320; PC2 1.322S140: PC1 2.337; PC2 -0.623S141: PC1 2.349; PC2 0.784S142: PC1 -0.092; PC2 -1.856S143: PC1 2.455; PC2 0.497S144: PC1 2.547; PC2 -1.116S145: PC1 -0.929; PC2 -0.163S146: PC1 0.242; PC2 -0.557S147: PC1 0.610; PC2 -1.214S148: PC1 -3.405; PC2 -0.649S149: PC1 2.952; PC2 1.082S150: PC1 -1.250; PC2 -0.808S151: PC1 -2.466; PC2 -0.852S152: PC1 -0.333; PC2 1.424S153: PC1 -2.771; PC2 -1.104S154: PC1 -2.388; PC2 -0.082S155: PC1 1.326; PC2 0.115S156: PC1 0.983; PC2 1.216S157: PC1 -2.784; PC2 0.410S158: PC1 0.071; PC2 -0.456S159: PC1 0.115; PC2 -1.144S160: PC1 -0.930; PC2 1.539S161: PC1 -2.731; PC2 -0.170S162: PC1 0.377; PC2 0.077S163: PC1 -2.969; PC2 -0.970S164: PC1 -2.540; PC2 0.610S165: PC1 -3.049; PC2 -0.285S166: PC1 3.160; PC2 -0.271S167: PC1 0.298; PC2 1.525S168: PC1 2.757; PC2 -0.868S169: PC1 -1.083; PC2 -1.082S170: PC1 3.221; PC2 0.612S171: PC1 2.648; PC2 -0.910S172: PC1 -0.108; PC2 0.651S173: PC1 -3.010; PC2 0.825S174: PC1 1.800; PC2 -1.322S175: PC1 -3.770; PC2 -1.255S176: PC1 0.401; PC2 0.631S177: PC1 -2.309; PC2 0.893S178: PC1 -2.252; PC2 1.006S179: PC1 -1.321; PC2 -1.004S180: PC1 -0.087; PC2 0.399S181: PC1 -0.918; PC2 -1.060S182: PC1 0.272; PC2 -0.330S183: PC1 -0.717; PC2 -1.563S184: PC1 -1.062; PC2 0.983S185: PC1 -2.387; PC2 0.959S186: PC1 -0.063; PC2 -1.209S187: PC1 0.011; PC2 0.230S188: PC1 1.958; PC2 -0.543S189: PC1 3.618; PC2 -0.220S190: PC1 3.397; PC2 -1.449S191: PC1 -3.270; PC2 -0.356S192: PC1 2.435; PC2 -0.501S193: PC1 1.309; PC2 1.273S194: PC1 -1.052; PC2 -0.699S195: PC1 -3.823; PC2 -0.616S196: PC1 0.700; PC2 1.200S197: PC1 3.073; PC2 -0.644S198: PC1 2.767; PC2 0.379S199: PC1 -0.783; PC2 0.820S200: PC1 3.066; PC2 0.038S201: PC1 2.959; PC2 -0.080S202: PC1 0.123; PC2 -0.618S203: PC1 -2.425; PC2 0.041S204: PC1 -0.895; PC2 0.357S205: PC1 0.080; PC2 1.114S206: PC1 0.616; PC2 -1.339S207: PC1 -1.023; PC2 -0.983S208: PC1 2.567; PC2 -0.591S209: PC1 1.522; PC2 -1.604S210: PC1 1.836; PC2 -0.314S211: PC1 -1.881; PC2 0.169S212: PC1 0.397; PC2 -0.218S213: PC1 1.398; PC2 0.581S214: PC1 -3.316; PC2 0.905S215: PC1 1.853; PC2 0.034S216: PC1 2.142; PC2 -0.796S217: PC1 -2.122; PC2 -1.044S218: PC1 3.632; PC2 -0.672S219: PC1 3.365; PC2 -0.204S220: PC1 2.660; PC2 -0.902S221: PC1 -0.208; PC2 0.961S222: PC1 -0.395; PC2 -1.725S223: PC1 -2.168; PC2 -0.711S224: PC1 -2.507; PC2 -0.042S225: PC1 -0.666; PC2 1.425S226: PC1 2.322; PC2 -0.713S227: PC1 2.115; PC2 0.307S228: PC1 -1.047; PC2 0.047S229: PC1 0.779; PC2 -0.946S230: PC1 -1.835; PC2 0.704S231: PC1 -1.595; PC2 0.833S232: PC1 3.155; PC2 -1.519S233: PC1 -3.083; PC2 -0.562S234: PC1 2.284; PC2 1.631S235: PC1 -1.517; PC2 1.184S236: PC1 1.325; PC2 0.360S237: PC1 -1.547; PC2 -0.676S238: PC1 -0.710; PC2 0.170S239: PC1 -1.431; PC2 0.660S240: PC1 0.357; PC2 -0.138S241: PC1 -2.141; PC2 1.586S242: PC1 -2.928; PC2 1.251S243: PC1 -2.562; PC2 -0.152S244: PC1 3.126; PC2 0.023S245: PC1 1.707; PC2 1.018S246: PC1 2.831; PC2 0.112S247: PC1 0.991; PC2 0.615S248: PC1 -0.252; PC2 -0.176S249: PC1 -0.426; PC2 0.282S250: PC1 -2.344; PC2 -0.867S251: PC1 2.419; PC2 -1.253S252: PC1 0.866; PC2 -1.054S253: PC1 -3.714; PC2 -1.380S254: PC1 0.521; PC2 1.429S255: PC1 0.408; PC2 -0.822S256: PC1 1.198; PC2 -0.575S257: PC1 0.689; PC2 -1.736S258: PC1 -1.973; PC2 0.145S259: PC1 -0.220; PC2 -0.141S260: PC1 0.388; PC2 0.184S261: PC1 2.565; PC2 0.699S262: PC1 -1.859; PC2 0.240S263: PC1 2.754; PC2 -1.545S264: PC1 1.781; PC2 0.404S265: PC1 -1.068; PC2 -0.833S266: PC1 -2.807; PC2 -0.953S267: PC1 -1.213; PC2 -1.376S268: PC1 -0.603; PC2 0.701S269: PC1 -3.350; PC2 -0.814S270: PC1 1.639; PC2 -0.523S271: PC1 2.447; PC2 0.098S272: PC1 2.147; PC2 -0.610S273: PC1 0.211; PC2 0.089S274: PC1 1.686; PC2 -0.458S275: PC1 2.835; PC2 1.389S276: PC1 -1.815; PC2 0.728S277: PC1 2.898; PC2 0.324S278: PC1 -3.725; PC2 -0.710S279: PC1 1.809; PC2 1.029S280: PC1 -2.074; PC2 0.177S281: PC1 0.734; PC2 -0.458S282: PC1 -3.203; PC2 -0.997S283: PC1 1.269; PC2 -0.746S284: PC1 -3.067; PC2 -0.037S285: PC1 0.284; PC2 -0.917S286: PC1 -0.844; PC2 -0.531S287: PC1 -0.426; PC2 0.108S288: PC1 0.037; PC2 0.294S289: PC1 -1.779; PC2 -1.573S290: PC1 -0.761; PC2 -0.963S291: PC1 -1.990; PC2 1.006S292: PC1 -2.717; PC2 -0.953S293: PC1 -0.385; PC2 -1.054S294: PC1 0.186; PC2 0.124S295: PC1 0.790; PC2 0.581S296: PC1 -1.061; PC2 0.473S297: PC1 -0.838; PC2 1.341S298: PC1 -3.839; PC2 -0.689S299: PC1 -0.179; PC2 -1.480S300: PC1 1.358; PC2 1.471S003S005S006-4-3-2-101234-2-1012PC1 score (dimensionless)PC2 score (dimensionless)
All 300 specimens. Each axis combines the five standardized measurements. These are not grain-size and strength axes. Hover over a point to see its label and scores.

How much does a simpler picture retain?

Each standardized column has variance 1, so our five columns have a total variance of 5. The five eigenvalues add up to the same total, allowing for rounding: 4.165, 0.713, 0.084, 0.024 and 0.015. PC1's share is 4.165 / 5 = 83.3%. PC2 adds 14.3%, so the first two components together account for 97.6%. The remaining three share 2.4%.

0255075100PC1: 83.3% of the variance; first 1 together: 83.3%83.3%PC1PC2: 14.3% of the variance; first 2 together: 97.6%14.3%PC2PC3: 1.7% of the variance; first 3 together: 99.2%1.7%PC3PC4: 0.5% of the variance; first 4 together: 99.7%0.5%PC4PC5: 0.3% of the variance; first 5 together: 100.0%0.3%PC5Share of total variance (%)
Each bar is one principal component's share of the total standardized variance. PC1 and PC2 together hold 97.6%. Hover over a bar to see the running total.

These percentages describe variance retained, not the percentage of physical knowledge retained.

# We reconstruct all five standardized measurements using two components.
kept = 2
reconstructed_scaled = scores[:, :kept] @ directions[:, :kept].T
reconstructed = reconstructed_scaled * standard_deviations + means
print("Variance retained (%):", 100 * fractions[:kept].sum())
print("RMS error in standardized values:",
      np.sqrt(np.mean((scaled - reconstructed_scaled)**2)))
# We compare the reconstruction with the original measurements, column by column.
errors = np.sqrt(np.mean((data - reconstructed)**2, axis=0))
print("RMS error in original units:", errors.round(2))

# We check that using all five components recovers the input.
full = scores @ directions.T
assert np.allclose(full, scaled)

Reconstruction gives an approximation to the five original measurements, rather than selecting two of them. Using every component recovers the centered and scaled data up to numerical rounding. Using fewer components leaves some differences out.

What is left out, in physical units?

A percentage of variance is hard to judge. An error in MPa or MS/m is easier. The table compares each column's root-mean-square (RMS) reconstruction error with the column's standard deviation, its typical spread. To obtain the middle column, change kept = 2 to kept = 1.

MeasurementStandard deviationRMS error, PC1 onlyRMS error, PC1 and PC2
Grain size (μm)16.943.082.35
Yield strength (MPa)31.964.984.72
Tensile strength (MPa)32.357.366.19
Elongation (%)3.911.500.77
Conductivity (MS/m)1.491.130.11

With PC1 alone, the conductivity error, 1.13 MS/m, is almost as large as the column's own spread, 1.49 MS/m. PC1 tells us little about conductivity. Adding PC2 reduces the error to 0.11 MS/m, and the elongation error falls from 1.50% to 0.77%. Grain size and the two strengths improve less, because PC1 already described them well.

What can we conclude?

For this synthetic dataset and scaling choice, two combinations retain most of the measured variance. They provide a useful two-dimensional representation of five features. The table of reconstruction errors shows what the simpler picture leaves out, in physical units. We must decide whether those omitted differences matter for our purpose.

PCA does not establish causation. A low-variance component is not automatically noise. Important materials differences may occur in a component with relatively little variance. The simpler picture helps us examine the measurements; it does not replace scientific judgment.

How was the synthetic spreadsheet made?

Now we can reveal how the data were generated. The generator script builds every specimen from two random numbers plus small random noise. The first number changes grain size, both strengths and elongation together. The second mainly changes conductivity, with smaller effects on yield strength and elongation. Our five columns therefore carry only about two independent sources of variation. PCA found two components that hold 97.6% of the variance, without being told this. Real measurements are rarely so clean, so we should expect more components to matter.

The generator also simplifies one materials relationship. In it, yield strength falls along a straight line as grain size increases. In many real alloys, yield strength follows the Hall–Petch relation: it changes with one over the square root of grain size, which is a curve. PCA combines columns along straight lines, so a curved relationship can spread over more than one component. An extra component does not always mean an extra physical mechanism.

For readers who know some linear algebra

SVD. PCA can also be calculated from the singular value decomposition of the standardized data matrix, without forming the covariance matrix. The right singular vectors are our directions, and each eigenvalue equals a squared singular value divided by \(n-1\). Our article SVD, explained with a small picture introduces SVD.

scikit-learn. The same weights and percentages come from a library call:

from sklearn.decomposition import PCA

pca = PCA().fit(scaled)
print(pca.components_[:2])            # rows are directions
print(100 * pca.explained_variance_ratio_)

When we tested this, scikit-learn returned PC2 with the opposite sign. Both signs are correct.

Loadings. Many texts call our weights loadings. Others define loadings as the weights multiplied by the square root of the eigenvalue. For standardized data, these scaled loadings equal the correlations between each feature and the component scores. Check which definition a paper or program uses.

For further reading, see the Cross Validated discussion and projection animations. We can also explore PCA reconstruction of small pixel images.

Acknowledgment and further reading

We consulted Jonathon Shlens's A Tutorial on Principal Component Analysis (2005, Version 2) while preparing this series. It explains PCA through changes of basis, covariance and the relationship with singular value decomposition. A later version is available on arXiv.