-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_evaluate_features.py
More file actions
98 lines (81 loc) · 2.16 KB
/
03_evaluate_features.py
File metadata and controls
98 lines (81 loc) · 2.16 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
"""
Python 3.6
This script will evaluate the features extracted from feature_extract.py
Specifically it will:
- Generate box plots for all features (by classification)
- Calculate correlations across all features to check for colinearity
Libraries:
- Pandas
- NumPy
- seaborn
by MAS 06.2019
"""
import os
import pandas as pd
import seaborn as sns
import time
import pylab as plt
# Timer
start_time = time.time()
def mk_dir():
"""
Function to make "plots" directory to put plots in.
:return: None
"""
path = os.getcwd()
try:
os.mkdir(path+"/plots/")
# In case folder already made
except OSError:
pass
def fetch_csv():
"""
Fetch csv data file from working directory.
:return: dataframe if exits, None otherwise
"""
# Read in sequence data with classifications
try:
df = pd.read_csv("./ecoli_proteome_features.csv")
return df
except FileNotFoundError:
print("File not found.")
def gen_boxplots(df):
"""
Generate boxplots by classification for each feature.
Save box plots in "plots" directory
:param df: dataframe of IDs, sequences, classifications, features
:return: None
"""
# Features start at column 3
for feature in list(df)[3:]:
sns.boxplot(x="Local", y=feature, data=df)
plt.savefig("./plots/" + feature + ".png")
plt.clf()
def check_colinearity(df):
"""
Generate correlation matrix for all features. Save plot in
"plots directory".
:param df: dataframe of IDs, sequences, classifications, features
:return: None
"""
# Check for multi-colinearity
# Features start at column 3
plt.figure(figsize=(12, 10))
cor = df[df.columns[3:]].corr()
sns.heatmap(cor, annot=True, cmap=plt.cm.Reds)
plt.savefig("./plots/correlation_matrix.png")
def run():
"""
Run functions if data exist
:return:
"""
df = fetch_csv()
if df.all:
df = df.dropna(how="any")
gen_boxplots(df)
check_colinearity(df)
else:
print("No Data!")
if __name__ == '__main__':
run()
print('run time = ' + str(time.time() - start_time) + ' s')