-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA.py
129 lines (54 loc) · 1.26 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# coding: utf-8
# In[51]:
import pandas as pd
from sklearn.datasets import load_digits
# In[52]:
dataset = load_digits()
dataset.keys()
# In[53]:
datasets.data[0]
# In[54]:
datasets.data[0].reshape(8,8)
# In[55]:
from matplotlib import pyplot as plt
# In[56]:
plt.gray()
plt.matshow(dataset.data[50].reshape(8,8))
# In[57]:
datasets.target
# In[58]:
df = pd.DataFrame(dataset.data)
df.describe()
# In[59]:
X = df
y= dataset.target
# In[60]:
y
# In[61]:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_scaled = scaler.fit_transform(X)
x_scaled
# In[62]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x_scaled,y, test_size = 0.2)
# In[63]:
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
model.score(X_test, y_test)
# In[66]:
from sklearn.decomposition import PCA
pca = PCA(0.95)
x_pca = pca.fit_transform(X)
x_pca.shape
# In[65]:
X.shape
# In[67]:
X_train_pca, X_test_pca, y_train, y_test = train_test_split(x_pca,y, test_size = 0.2)
# In[68]:
model = LogisticRegression()
model.fit(X_train_pca, y_train)
model.score(X_test_pca, y_test)
# In[ ]: