Skip to Content
40 Algorithms Every Programmer Should Know
book

40 Algorithms Every Programmer Should Know

by Imran Ahmad
June 2020
Intermediate to advanced
382 pages
11h 39m
English
Packt Publishing
Content preview from 40 Algorithms Every Programmer Should Know

Principal component analysis

PCA is an unsupervised machine learning technique that can be used to reduce dimensions using linear transformation. In the following figure, we can see two principle components, PC1 and PC2, which show the shape of the spread of the data points. PC1 and PC2 can be used to summarize the data points with appropriate coefficients:

Let's consider the following code:

from sklearn.decomposition import PCAiris = pd.read_csv('iris.csv')X = iris.drop('Species', axis=1)pca = PCA(n_components=4)pca.fit(X)

Now let's print the coefficients of our PCA model:

Note that the original DataFrame has four features, Sepal.Length ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

50 Algorithms Every Programmer Should Know - Second Edition

50 Algorithms Every Programmer Should Know - Second Edition

Imran Ahmad
Grokking Algorithms

Grokking Algorithms

Aditya Bhargava

Publisher Resources

ISBN: 9781789801217Supplemental Content