-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeatureImportance.py
More file actions
122 lines (97 loc) · 4.73 KB
/
featureImportance.py
File metadata and controls
122 lines (97 loc) · 4.73 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
# ===================== #
# Author: Jesse Wolf, jwolf@uoguelph.ca | Thomas Papp-Simon, tpappsim@uoguelph.ca
# Date: March 18, 2023
# How to run: python3 .\featureImportance.py -base .\scaled_training_sets\training2015-2021_outliers_removed_scaled.csv -rfe .\RFE_splits\RFE_training2015-2021_outliers_removed_scaled.csv -rfe9 .\RFE_splits\train2015_2021_RFEcommon.csv
# This script takes our baseline data (2015-2021 with no RFE performed), our RFE-selected data for 2015-2021, and our common RFE data (2015-2021 with only the 9 features that RFE identified as common between all training splits.)
# ================= #
# Import relevant libraries
from sklearn.linear_model import LogisticRegression
from sklearn.feature_selection import RFE
from matplotlib import pyplot
import pandas as pd
import argparse
import sys
import os
print ("\nBeginning featureImportance.py.\n")
# Make directory if does not exist
path = "featureImportance"
# Check whether the specified path exists or not
isExist = os.path.exists(path)
if not isExist:
# Create a new directory because it does not exist
os.makedirs(path)
parser = argparse.ArgumentParser(description='Feature importance for the 3 datasets')
parser.add_argument('--base_file', '-base', action="store", dest='base_file', required=True, help='Name of csv input file.')
parser.add_argument('--RFE_file', '-rfe', action="store", dest='rfe_file', required=True, help='Name of csv input file.')
parser.add_argument('--RFE9_file', '-rfe9', action="store", dest='rfe9_file', required=True, help='Name of csv input file.')
# handle user errors
try:
args = parser.parse_args()
except:
parser.print_help()
sys.exit(0)
# save arguments in separate variables
filename_base = args.base_file
filename_rfe_all = args.rfe_file
filename_rfe_common = args.rfe9_file
# load the datasets
df_base = pd.read_csv(filename_base)
df_RFE_all = pd.read_csv(filename_rfe_all)
df_RFE_common = pd.read_csv(filename_rfe_common)
# Remove non-numerical columns
df_base_features = df_base.drop(['team_abbreviation_home', 'team_abbreviation_away', 'game_date', 'game_yearEnd'],axis=1)
df_RFE_common_features = df_RFE_common.drop(['team_abbreviation_home', 'team_abbreviation_away', 'game_date', 'game_yearEnd'],axis=1)
df_RFE_all_features = df_RFE_all.drop(['team_abbreviation_home', 'team_abbreviation_away', 'game_date', 'game_yearEnd'],axis=1)
df_list = [df_base_features, df_RFE_common_features]
# for df in df_list:
fig, axs = pyplot.subplots(1,2, figsize = (10, 7))
for df, ax in zip(df_list, axs.ravel()):
X=df.values[:,]
Y=df.values[:,-1:].astype(int)
# Feature selectionn
model = LogisticRegression(solver='lbfgs', max_iter=1000)
model.fit(X,Y.ravel())
RFE_model = RFE(estimator=model, n_features_to_select=len(X))
# Create a data frame of importance and column names
importances = pd.DataFrame(data={
'Attribute': df.columns[0:],
'Importance': model.coef_[0]
})
# Sort in descending order
importances = importances.sort_values(by='Importance', ascending=False)
importances = importances.iloc[1:]
ax.bar(x=importances['Attribute'], height=importances['Importance'], color='#087E8B')
ax.set_ylabel('Importance')
ax.tick_params(axis='x', labelrotation=90)
name =[x for x in globals() if globals()[x] is df][0]
if name == 'df_base_features':
ax.set_title('Feature Importance (baseline)')
elif name == 'df_RFE_common_features':
ax.set_title('Feature Importance (RFE common features)')
pyplot.tight_layout()
pyplot.savefig(f"./featureImportance/feature_Importance_base_RFE.png")
# Plotting the feature importance plot for all features selected by RFE for 2015-2021 training set separately.
fig, axs = pyplot.subplots(1,1, figsize = (10, 7))
X=df_RFE_all_features.values[:,]
Y=df_RFE_all_features.values[:,-1:].astype(int)
# Feature selectionn
model = LogisticRegression(solver='lbfgs', max_iter=1000)
model.fit(X,Y.ravel())
RFE_model = RFE(estimator=model, n_features_to_select=len(X))
# Create a data frame of importance and column names
importances = pd.DataFrame(data={
'Attribute': df_RFE_all_features.columns[0:],
'Importance': model.coef_[0]
})
# Sort in descending order
importances = importances.sort_values(by='Importance', ascending=False)
importances = importances.iloc[1:]
axs.bar(x=importances['Attribute'], height=importances['Importance'], color='#087E8B')
axs.set_ylabel('Importance')
axs.tick_params(axis='x', labelrotation=90)
name =[x for x in globals() if globals()[x] is df_RFE_all_features][0]
if name == 'df_RFE_all_features':
axs.set_title("Feature Importance (RFE all features)")
pyplot.tight_layout()
pyplot.savefig(f"./featureImportance/feature_Importance_base_RFE_all.png")
print ("featureImportance.py has finished running, on to learningCurves.py.\n")