Scikit-learn for PCA
We figured out the implementation of the PCA algorithm using the numpy library. Scikit-learn can let us start using this method with just one line of code:
from sklearn.decomposition import PCA
pca_model = PCA(n_components = 2)
PCA is a scikit-learn library class. It contains more than 5 arguments, but we are most interested in only one - n_components. This argument is responsible for the number of main components that we want to get. The only condition is that the number of components must, of course, be equal to or less than the variables in the dataset.
The PCA class contains 2 main methods that we will use: fit and transform. The fit() method loads the data into the class, and the transform() method transforms it, and we get the result of the PCA algorithm. If we want to combine these 2 operations, use the fit_transform() method:
pca_model = PCA(n_components = 2)
# fit() and transform()
pca_model.fit(X)
X_reduced = pca_model.transform(X)
# fit_transform()
X_reduced = pca_model.fit_transform(X)
If we want to get the components that the algorithm has calculated, call the .components_ attribute:
print(pca_model.components_)
Swipe to start coding
Import the PCA class from the scikit-learn library and create a PCA model for the iris dataset with 2 components.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 5.26
Scikit-learn for PCA
Svep för att visa menyn
We figured out the implementation of the PCA algorithm using the numpy library. Scikit-learn can let us start using this method with just one line of code:
from sklearn.decomposition import PCA
pca_model = PCA(n_components = 2)
PCA is a scikit-learn library class. It contains more than 5 arguments, but we are most interested in only one - n_components. This argument is responsible for the number of main components that we want to get. The only condition is that the number of components must, of course, be equal to or less than the variables in the dataset.
The PCA class contains 2 main methods that we will use: fit and transform. The fit() method loads the data into the class, and the transform() method transforms it, and we get the result of the PCA algorithm. If we want to combine these 2 operations, use the fit_transform() method:
pca_model = PCA(n_components = 2)
# fit() and transform()
pca_model.fit(X)
X_reduced = pca_model.transform(X)
# fit_transform()
X_reduced = pca_model.fit_transform(X)
If we want to get the components that the algorithm has calculated, call the .components_ attribute:
print(pca_model.components_)
Swipe to start coding
Import the PCA class from the scikit-learn library and create a PCA model for the iris dataset with 2 components.
Lösning
Tack för dina kommentarer!
single