IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Unsupervised Learning in Python

Visualizza il corso

Istruzioni dell'esercizio

  • Import PCA from sklearn.decomposition.
  • Create an instance of PCA called model.
  • Use the .fit_transform() method of model to apply the PCA transformation to grains. Assign the result to pca_features.
  • The subsequent code to extract, plot, and compute the Pearson correlation of the first two columns pca_features has been written for you, so hit submit to see the result!

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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)
Modifica ed esegui il codice