Repository navigation
Expand file tree
/
Copy pathPCAVisualisation.py
More file actions
126 lines (73 loc) · 2.75 KB
/
Copy pathPCAVisualisation.py
File metadata and controls
126 lines (73 loc) · 2.75 KB
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
#!/bin/python3
# -*- coding:utf-8 -*-
""" Importing librairies """
import os
import pandas as pd
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
"""Creating filename of the file"""
dataFile = "../output/CompleteData.csv"
""" Creating a counter Line"""
counterLine = 0
"""Reading the file"""
openingDataFile = open(dataFile, 'r')
"""Counting the lines"""
while(openingDataFile.readline()):
counterLine += 1
"""Closing the file"""
openingDataFile.close()
"""ReOpening the file"""
openingDataFile = open(dataFile, 'r')
"""Creating a dictionnary of data"""
dictionnaryData = {}
""" Reading header line and creating items of dictionnary"""
line = openingDataFile.readline().replace('\n', '')
lineSplitted = line.split(',')
lineSplitted[0] = "X"
listOfHeaders = lineSplitted
listOfData = []
"""Reading file and assigning data to item"""
for i in range(0, counterLine - 1):
line = openingDataFile.readline().replace('\n', '')
lineSplitted = line.split(',')
listOfData.append(lineSplitted)
openingDataFile.close()
"""Creating dataframe from data"""
dataFrameOfData = pd.DataFrame(listOfData, columns= listOfHeaders)
"""Deleting unused variables"""
del(listOfData)
del(listOfHeaders)
"""Creating a list of labels"""
columnLabels = dataFrameOfData["Labels"]
uniqColumnLabels = list(columnLabels.drop_duplicates())
"""Selecting data from dataframe"""
dataFrameOfData = dataFrameOfData.iloc[:, 1:len(dataFrameOfData.columns) - 1]
"""Standardise data"""
dataFrameOfData = StandardScaler().fit_transform(dataFrameOfData)
"""Making PCA, Selecting the 10 first Principal Components"""
pca = PCA(n_components = 10)
principalComponents = pca.fit_transform(dataFrameOfData)
pcaColumns = []
for i in range(0, 10):
pcaColumns.append("PCA{}".format(i))
"""Creating a new dataframe with PCA values"""
principalComponents = pd.DataFrame(data = principalComponents, columns=pcaColumns)
"""Concatening PCA values with Patients labels"""
principalComponents = pd.concat([principalComponents, columnLabels], axis = 1)
"""Drawing figure"""
listOfColors = ['b', 'g', 'r', 'c', 'm']
plt.style.use("ggplot")
plt.xlabel('Principal Component 0', fontsize = 15)
plt.ylabel('Principal Component 1', fontsize = 15)
plt.title('PCA', fontsize = 20)
for target, color in zip(uniqColumnLabels,listOfColors):
indicesToKeep = principalComponents['Labels'] == target
plt.scatter(principalComponents.loc[indicesToKeep, "PCA0"],
principalComponents.loc[indicesToKeep, "PCA1"],
c = color,
s = 50)
plt.legend(uniqColumnLabels)
plt.grid()
"""Saving figure"""
plt.savefig("../figures/PCAFigure.eps", dpi = 600, format="eps", bbox_inches="tight")