-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprinciple-component-analysis-lab.py
140 lines (63 loc) · 1.92 KB
/
principle-component-analysis-lab.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
126
127
128
129
130
131
132
133
134
135
136
137
# coding: utf-8
# In[1]:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
get_ipython().magic('matplotlib inline')
# In[2]:
from sklearn.datasets import load_breast_cancer
# In[3]:
cancer = load_breast_cancer()
# In[4]:
cancer.keys()
# In[5]:
print(cancer['DESCR'])
# In[6]:
print(cancer['feature_names'])
# In[8]:
#with principle component analysis we are looking for what components explain the most variance with the dataset
df = pd.DataFrame(cancer['data'],columnss=cancer['feature_names'])
# In[9]:
#with principle component analysis we are looking for what components explain the most variance with the dataset
df = pd.DataFrame(cancer['data'],columnss=cancer['feature_names'])
# In[10]:
#with principle component analysis we are looking for what components explain the most variance with the dataset
df = pd.DataFrame(cancer['data'],columns=cancer['feature_names'])
#(['DESCR', 'data', 'feature_names', 'target_names', 'target'])
# In[11]:
df.head()
# In[12]:
from sklearn.preprocessing import StandardScaler
# In[13]:
scaler = StandardScaler()
scaler.fit(df)
# In[14]:
scaled_data = scaler.transform(df)
# In[15]:
from sklearn.decomposition import PCA
# In[16]:
pca = PCA(n_components=2)
# In[17]:
pca.fit(scaled_data)
# In[18]:
x_pca = pca.transform(scaled_data)
# In[19]:
scaled_data.shape
# In[20]:
x_pca.shape
# In[21]:
plt.figure(figsize=(8,6))
plt.scatter(x_pca[:,0],x_pca[:,1],c=cancer['target'],cmap='plasma')
plt.xlabel('First principal component')
plt.ylabel('Second Principal Component')
# In[22]:
pca.components_
# In[23]:
df_comp = pd.DataFrame(pca.components_,columns=cancer['feature_names'])
# In[24]:
plt.figure(figsize=(12,6))
sns.heatmap(df_comp,cmap='plasma',)
# In[25]:
#so now what you could do is a logistic regression model on the x_pca data now that it is just two components, or even use a SVM model.
# In[ ]: