-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-Baseline-Classification-Model.py
More file actions
200 lines (145 loc) · 5.95 KB
/
3-Baseline-Classification-Model.py
File metadata and controls
200 lines (145 loc) · 5.95 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 6 12:12:29 2023
@author: Jfitz
"""
#
# The following code (when run on the 24 hour data )will produce the
# baseline results of the classifiers as described in Journal 2:
# Logistic Regression 46%
# Random Forest 63%
# XG Boost 63%
# Light GBM 64%
# Decision tree 54%
# Keras Neural Network 66%
#
# 4 hourly features:
#
# Logistic Regression 43%
# Random Forest 53%
# XG Boost 53%
# Light GBM 54%
# Decision tree 48%
# Keras Neural Network 55%
#
import keras
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
import xgboost as xgb
import lightgbm as lgb
import pandas as pd
import seaborn as sns
color = sns.color_palette()
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import classification_report
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
file = '24HrFeatures.csv'
#file = '4HrFeatures.csv'
df = pd.read_csv(file)
#Prepare the data for modeling
X = df.copy().drop(['id','class','date','Category','counter','patientID'],axis=1)
y = df['class'].copy()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
# Scale the continuous variables
scaler = StandardScaler()
X_train[['f.mean', 'f.sd', 'f.propZeros']] = scaler.fit_transform(X_train[['f.mean', 'f.sd', 'f.propZeros']])
X_test[['f.mean', 'f.sd', 'f.propZeros']] = scaler.transform(X_test[['f.mean', 'f.sd', 'f.propZeros']])
#Create the models:
#----------------------Logistic Regression
logReg = LogisticRegression()
model = logReg
trainingScores = []
cvScores = []
predictionsBasedOnKFolds = pd.DataFrame(data=[],
index=y_train.index,columns=[0,1,2])
# Trainingset 10-fold cross validation
k_fold = StratifiedKFold(n_splits=10,shuffle=True,random_state=2018)
# Fit the logistic regression model on the training data
model.fit(X_train, y_train)
# Evaluate the model's performance on the testing data
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
confusion_mat = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)
print(f" Logistic Regression Accuracy: {accuracy}")
print(f"LR Confusion Matrix: \n{confusion_mat}")
print(f"LR Classification Report:\n{class_report}")
#--------------------- Random Forest
# create a random forest classifier object
rfc = RandomForestClassifier()
# fit the model on the training data
rfc.fit(X_train, y_train)
# predict on the test data
y_pred = rfc.predict(X_test)
# evaluate the model's accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Random Forest Accuracy: {accuracy}")
confusionRF = confusion_matrix(y_test, y_pred)
print(f"RanDom Forest Confusion Matrix: \n{confusionRF}")
class_report = classification_report(y_test, y_pred)
print(f"Random Forest Classification Report:\n{class_report}")
#----------------- XG Boost
# Define the model
xgb_model = xgb.XGBClassifier()
xgb_model.fit(X_train, y_train)
# Evaluate the model
y_pred = xgb_model.predict(X_test)
acc_score = accuracy_score(y_test, y_pred)
conf_mat = confusion_matrix(y_test, y_pred)
print("XGB Accuracy score:", acc_score)
print("XGB Confusion matrix:\n", conf_mat)
class_report = classification_report(y_test, y_pred)
print(f"XGB Classification Report:\n{class_report}")
feat_imp = pd.Series(xgb_model.feature_importances_, index=X.columns).sort_values(ascending=False)
plt.barh(feat_imp.index, feat_imp.values)
plt.title("XGB Feature importances")
plt.show()
#-----------------------Light GBM
# Define the model
lgb_model = lgb.LGBMClassifier()
lgb_model.fit(X_train, y_train)
# Evaluate the model
y_pred = lgb_model.predict(X_test)
acc_score = accuracy_score(y_test, y_pred)
conf_mat = confusion_matrix(y_test, y_pred)
print("Light GBM Accuracy score:", acc_score)
print("Light GBM Confusion matrix:\n", conf_mat)
class_report = classification_report(y_test, y_pred)
print(f"Light GBM Classification Report:\n{class_report}")
#feat_imp = pd.Series(lgb_model.feature_importances_, index=X.columns).sort_values(ascending=False)
#plt.barh(feat_imp.index, feat_imp.values)
#plt.title("Light GBM Feature importances")
#plt.show()
#------------------Decision Tree
# Define the decision tree model
dt_model = DecisionTreeClassifier()
dt_model.fit(X_train, y_train)
# Evaluate the model
y_pred = dt_model.predict(X_test)
acc_score = accuracy_score(y_test, y_pred)
conf_mat = confusion_matrix(y_test, y_pred)
print("Decision Tree Accuracy score:", acc_score)
print("Decision Tree Confusion matrix:\n", conf_mat)
class_report = classification_report(y_test, y_pred)
print(f"Decision Tree Classification Report:\n{class_report}")
#plt.figure(figsize=(10,6))
#plot_tree(dt_model, filled=True, feature_names=X.columns, class_names=['Depressive', 'Control', 'Schizophrenic'])
#plt.savefig('DecisionTreeClassifier.pdf', dpi=300)
#plt.show()
#----------------KERAS
# Step 2: Define the neural network model
model = keras.Sequential([
keras.layers.Dense(64, activation='relu', input_shape=[X_train.shape[1]]),
keras.layers.Dense(32, activation='relu'),
keras.layers.Dense(3, activation='softmax')
])
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Step 3: Train the model
model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test))
# Evaluate the model
test_loss, test_acc = model.evaluate(X_test, y_test)
print(" Neural Network Test accuracy:", test_acc)