As usual, scikit-learn saves the day by implementing this procedure in an easy to use transformer so that we don't have to go through that manual process each time we wish to use this powerful process:
- We can import it from scikit-learn's decomposition module:
# scikit-learn's version of PCA
from sklearn.decomposition import PCA
- To mimic the process we performed with the iris dataset, let's instantiate a PCA object with only two components:
# Like any other sklearn module, we first instantiate the class
pca = PCA(n_components=2)
- Now, we can fit our PCA to the data:
# fit the PCA to our data
pca.fit(iris_X)
- Let's take a look at some of the attributes of the PCA object to see if they match up with what we achieved in our manual process. Let's take a look at the components_ attribute of our object to see if this matches up...