Decorrelating the grain measurements with PCA
You observed in the previous exercise that the width and length measurements of the grain are correlated. Now, you'll use PCA to decorrelate these measurements, then plot the decorrelated points and measure their Pearson correlation.
This exercise is part of the course
Unsupervised Learning in Python
Exercise instructions
- Import
PCAfromsklearn.decomposition. - Create an instance of
PCAcalledmodel. - Use the
.fit_transform()method ofmodelto apply the PCA transformation tograins. Assign the result topca_features. - The subsequent code to extract, plot, and compute the Pearson correlation of the first two columns
pca_featureshas been written for you, so hit submit to see the result!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import PCA
____
# Create PCA instance: model
model = ____
# Apply the fit_transform method of model to grains: pca_features
pca_features = ____
# Assign 0th column of pca_features: xs
xs = pca_features[:,0]
# Assign 1st column of pca_features: ys
ys = pca_features[:,1]
# Scatter plot xs vs ys
plt.scatter(xs, ys)
plt.axis('equal')
plt.show()
# Calculate the Pearson correlation of xs and ys
correlation, pvalue = pearsonr(xs, ys)
# Display the correlation
print(correlation)