forked from pepesan/machine-learning-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_04_pca.py
42 lines (32 loc) · 1022 Bytes
/
04_04_pca.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
import mglearn
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
mglearn.plots.plot_pca_illustration()
cancer = load_breast_cancer()
print(cancer.target_names)
pca = PCA(n_components=2)
pca.fit(cancer.data)
transformada = pca.transform(cancer.data)
print(cancer.data.shape)
print(transformada.data.shape)
mglearn.discrete_scatter(transformada[:, 0], transformada[:, 1], cancer.target)
plt.legend(cancer.target_names, loc="best")
plt.xlabel("PC1")
plt.ylabel("PC2")
# enseña los datos y su procesado
plt.show()
escala = MinMaxScaler()
escala.fit(cancer.data)
escalada = escala.transform(cancer.data)
pca.fit(escalada)
transformada = pca.transform(escalada)
mglearn.discrete_scatter(transformada[:, 0], transformada[:, 1], cancer.target)
plt.legend(cancer.target_names, loc="best")
plt.gca()
plt.xlabel("PC1")
plt.ylabel("PC2")
# enseña los datos y su procesado
plt.show()