[PYTHON] PCA with Scikit-learn

Play with PCA.

pca.py


from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

##Data reading
digits = load_digits()
X = digits.data
y = digits.target
target_names = digits.target_names

## PCA
pca = PCA(n_components=2)
X_r = pca.fit(X).transform(X)

## colors
colors = [plt.cm.nipy_spectral(i/10., 1) for i in range(10)]

## plot
plt.figure()
for c, target_name  in zip(colors, target_names):
    plt.scatter(X_r[y == target_name, 0], X_r[y == target_name, 1], c=c, label = target_name)
plt.legend()
plt.title('PCA')
plt.show()

Execution result.

Untitled.png

reference: Scikit-learn PCA documentation Scikit-learn PCA sample The University of Tokyo Tomioka-sensei HP PFI Blog

Recommended Posts

PCA with Scikit-learn
Isomap with Scikit-learn
DBSCAN with scikit-learn
Clustering with scikit-learn (1)
Clustering with scikit-learn (2)
kmeans ++ with scikit-learn
Cross Validation with scikit-learn
Clustering with scikit-learn + DBSCAN
Learn with chemoinformatics scikit-learn
DBSCAN (clustering) with scikit-learn
Install scikit.learn with pip
Calculate tf-idf with scikit-learn
Neural network with Python (scikit-learn)
Parallel processing with Parallel of scikit-learn
[Python] Linear regression with scikit-learn
Robust linear regression with scikit-learn
Grid search of hyperparameters with Scikit-learn
Moving object separation with Robust PCA
Creating a decision tree with scikit-learn
Image segmentation with scikit-image and scikit-learn
Identify outliers with RandomForestClassifier in scikit-learn
Laplacian eigenmaps with Scikit-learn (personal notes)
Non-negative Matrix Factorization (NMF) with scikit-learn
Try machine learning with scikit-learn SVM
Scikit-learn DecisionTreeClassifier with datetime type values
The most basic clustering analysis with scikit-learn
Revisited scikit-learn
[Scikit-learn] I played with the ROC curve
Try SVM with scikit-learn on Jupyter Notebook
Multi-label classification by random forest with scikit-learn
[Python] Use string data with scikit-learn SVM
Implement a minimal self-made estimator with scikit-learn
Fill in missing values with Scikit-learn impute
Visualize scikit-learn decision trees with Plotly's Treemap
I tried handwriting recognition of runes with scikit-learn
Predict the second round of summer 2016 with scikit-learn
Multivariable regression model with scikit-learn --SVR comparison verification