-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA.py
39 lines (31 loc) · 884 Bytes
/
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
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 21 22:02:05 2019
@author: Administrator
"""
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
#load data
data = load_iris()
y = data.target
X = data.data
pca = PCA(n_components = 2)
reduced_X = pca.fit_transform(X)
red_x, red_y = [],[]
blue_x, blue_y = [],[]
green_x, green_y = [],[]
for i in range(len(reduced_X)):
if y[i] == 0:
red_x.append(reduced_X[i][0])
red_y.append(reduced_X[i][1])
elif y[i] == 1:
blue_x.append(reduced_X[i][0])
blue_y.append(reduced_X[i][1])
else:
green_x.append(reduced_X[i][0])
green_y.append(reduced_X[i][1])
plt.scatter(red_x, red_y, c = 'r', marker = 'x')
plt.scatter(blue_x, blue_y, c = 'b', marker = 'D')
plt.scatter(green_x, green_y, c = 'g', marker = '.')
plt.show()