-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
102 lines (72 loc) · 3 KB
/
models.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
# Import sk learn libraries for logistic regression, decision tree and neural network
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, roc_curve, auc
# Import ensemble for majority voting scheme
from sklearn.ensemble import VotingClassifier
# Import numpy for array manipulation
import numpy as np
import pandas as pd
# Import pickle to load the dataframe
import pickle
# Import matplotlib for plotting
import matplotlib.pyplot as plt
# Load the dataframe in a function
def load_df():
with open('data/master_df.pickle', 'rb') as f:
master_df = pickle.load(f)
return master_df
# Functions for each model
def logistic_regression(X_train, y_train, X_test, y_test):
# Create a logistic regression model with parameters
log_reg = LogisticRegression(C=0.01, random_state=42)
# Train the model
log_reg.fit(X_train, y_train)
# Make predictions on the test set
y_pred = log_reg.predict(X_test)
# Return the predictions
return y_pred
def decision_tree(X_train, y_train, X_test, y_test):
# Create a decision tree model with parameters
dt = DecisionTreeClassifier(max_depth=5, min_samples_leaf=0.16, random_state=42)
# Train the model
dt.fit(X_train, y_train)
# Make predictions on the test set
y_pred = dt.predict(X_test)
# Return the predictions
return y_pred
def neural_network(X_train, y_train, X_test, y_test):
# Create a neural network model with parameters
nn = MLPClassifier(hidden_layer_sizes=(100, 100), max_iter=400, random_state=42)
# Train the model
nn.fit(X_train, y_train)
# Make predictions on the test set
y_pred = nn.predict(X_test)
# Return the predictions
return y_pred
def majority_classifier(X_train, y_train, X_test, y_test):
# Create a logistic regression model with parameters
log_reg = LogisticRegression(C=0.01, random_state=42)
# Create a decision tree model with parameters
dt = DecisionTreeClassifier(max_depth=5, min_samples_leaf=0.16, random_state=42)
# Create a neural network model with parameters
nn = MLPClassifier(hidden_layer_sizes=(100, 100), max_iter=400, random_state=42)
# Create a voting classifier
voting_classifier = VotingClassifier(estimators=[('lr', log_reg), ('dt', dt), ('nn', nn)], voting='hard')
# Train the model
voting_classifier.fit(X_train, y_train)
# Make predictions on the test set
y_pred = voting_classifier.predict(X_test)
# Return the predictions
return y_pred
# Function to calculate the metrics
def calculate_metrics(y_test, y_pred):
# Calculate the accuracy
accuracy = accuracy_score(y_test, y_pred)
# Calculate the precision
precision = precision_score(y_test, y_pred)
# Calculate the recall
recall = recall_score(y_test, y_pred)
# Return the metrics
return accuracy, precision, recall