forked from aslakey/CBM_Encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
road_safety.py
99 lines (78 loc) · 3.87 KB
/
road_safety.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
import pandas as pd
import numpy as np
import io
import requests
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn import metrics
from beta_encoder import BetaEncoder
import category_encoders as ce
from utils import *
import csv
def run_rs_experiments():
print("Loading Data")
df = load_data()
#columns:
continuous = []
categorical = ['make','model']
X = df[continuous+categorical]
y = df[['Sex_of_Driver']]
successes = y.sum()[0]
alpha_prior = float(successes / len(y))
models = [LogisticRegression(solver='lbfgs'),
RandomForestClassifier(n_estimators=100),
GradientBoostingClassifier(),
MLPClassifier()]
results = [['model','Encoder','Accuracy','STD','Training Time','Sparsity','Dimensions']]
for model in models:
print("")
print("----------------------")
print("Testing Algorithm: ")
print(type(model))
print("----------------------")
#TargetEncoder
print("TargetEncoder Results:")
acc, std, time, sparsity, dimensions = cv_binary_classification(model, X, y, continuous, categorical, encoder=ce.TargetEncoder(return_df=False))
results.append([type(model), 'TargetEncoder', acc, std, time, sparsity, dimensions])
#OrdinalEncoder
print("OrdinalEncoder Results:")
acc, std, time, sparsity, dimensions = cv_binary_classification(model, X, y, continuous, categorical, encoder=ce.OrdinalEncoder(return_df=False))
results.append([type(model), 'OrdinalEncoder', acc, std, time, sparsity, dimensions])
#BinaryEncoder
print("BinaryEncoder Results:")
acc, std, time, sparsity, dimensions = cv_binary_classification(model, X, y, continuous, categorical, encoder=ce.BinaryEncoder(return_df=False))
results.append([type(model), 'BinaryEncoder', acc, std, time, sparsity, dimensions])
#HashingEncoder
print("HashingEncoder Results:")
acc, std, time, sparsity, dimensions = cv_binary_classification(model, X, y, continuous, categorical, encoder=ce.HashingEncoder(return_df=False))
results.append([type(model), 'HashingEncoder', acc, std, time, sparsity, dimensions])
#OneHotEncoder
print("OneHotEncoder Results:")
acc, std, time, sparsity, dimensions = cv_binary_classification(model, X, y, continuous, categorical, encoder=OneHotEncoder(handle_unknown='ignore', sparse=False))
results.append([type(model), 'OneHotEncoder', acc, std, time, sparsity, dimensions])
#BetaEncoder (mean)
print("Beta Encoder (mean) Results:")
acc, std, time, sparsity, dimensions = cv_binary_classification(model, X, y, continuous, categorical, encoder=BetaEncoder(alpha=alpha_prior, beta=1-alpha_prior))
results.append([type(model), 'BetaEncoder (m)', acc, std, time, sparsity, dimensions])
#BetaEncoder (mean, variance)
print("Beta Encoder (mean and variance Results:")
acc, std, time, sparsity, dimensions = cv_binary_classification(model, X, y, continuous, categorical, encoder=BetaEncoder(alpha=alpha_prior, beta=1-alpha_prior), moments='mv')
results.append([type(model), 'BetaEncoder (mv)', acc, std, time, sparsity, dimensions])
file = 'road_safety_experiments.csv'
with open(file, "w") as output:
writer = csv.writer(output, lineterminator='\n')
writer.writerows(results)
try:
upload_file(file)
except:
print("File Not Uploaded")
def load_data():
df = pd.read_csv('road_safety_raw.csv')
df = df.sample(10000)
df.to_csv('road_safety_raw.csv', index=False)
return df
if __name__ == '__main__':
run_rs_experiments()