-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.py
134 lines (116 loc) · 4.12 KB
/
classifier.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
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
import numpy as np
import pandas as pd
from sklearn.preprocessing import OrdinalEncoder
from sklearn.svm import SVC
from imblearn.over_sampling import SMOTE
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import BaggingClassifier
def scale_data(X, scaler=None):
if not scaler:
scaler = StandardScaler().fit(X)
return scaler.transform(X), scaler
# Baseline
def svc_baseline(xtrain, ytrain, xtest, ytest):
Baseline_model = SVC(kernel="rbf")
Baseline_model.fit(xtrain, ytrain)
ypred_train = Baseline_model.predict(xtrain)
train_accuracy = np.mean(ypred_train == ytrain)
ypred_test = Baseline_model.predict(xtest)
test_accuracy = np.mean(ypred_test == ytest)
return train_accuracy, test_accuracy
# Final Model
def ensemble_model(xtrain, ytrain, xtest, ytest):
ens_model = BaggingClassifier(n_estimators=20, random_state=30)
ens_model.fit(xtrain, ytrain)
ypred_train = ens_model.predict(xtrain)
train_accuracy = np.mean(ypred_train == ytrain)
ypred_test = ens_model.predict(xtest)
test_accuracy = np.mean(ypred_test == ytest)
return train_accuracy, test_accuracy
if __name__ == "__main__":
# Config
DATA_DIR = "input"
res = pd.DataFrame(
{},
columns=[
"No. of Datapoints",
"Bad Deal Percentage",
"Train Accuracy",
"Test Accuracy",
],
)
# Data Loading
train_data = pd.read_csv(f"{DATA_DIR}/train_data.csv")
test_data = pd.read_csv(f"{DATA_DIR}/test_data.csv")
test_data = test_data.set_index("Deal_num")
test_labels = pd.read_csv(f"{DATA_DIR}/test_labels.csv")
test_labels = test_labels.set_index("Deal_num")
# Categorical variable Encoder
x_encoder = OrdinalEncoder(
categories=[
["low", "med", "high", "vhigh"],
["low", "med", "high", "vhigh"],
["2", "3", "4", "5more"],
["2", "4", "more"],
["small", "med", "big"],
["low", "med", "high"],
]
)
y_encoder = OrdinalEncoder(categories=[["Bad_deal", "Nice_deal"]])
# Data splitting into X and y
xtrain, ytrain = (
train_data.drop("How_is_the_deal", axis=1),
train_data[["How_is_the_deal"]],
)
# Encode the categorical data
xtrain = pd.DataFrame(
x_encoder.fit_transform(xtrain), columns=train_data.columns[:-1]
)
xtest = pd.DataFrame(x_encoder.fit_transform(test_data), columns=test_data.columns)
ytrain = pd.DataFrame(
y_encoder.fit_transform(ytrain), columns=[train_data.columns[-1]]
)
ytest = pd.DataFrame(
y_encoder.fit_transform(test_labels), columns=test_labels.columns
)
# Standard Normalization
xtrain_sc, tr_scaler = scale_data(xtrain)
xtest_sc, _ = scale_data(xtest, tr_scaler)
svc_train_ac, svc_test_ac = svc_baseline(
xtrain_sc, ytrain.values.reshape(-1), xtest_sc, ytest.values.reshape(-1)
)
bad_deal_perc = ytrain["How_is_the_deal"].value_counts(normalize=True)[1]
res.loc["SVC"] = [
xtrain.shape[0],
bad_deal_perc,
svc_train_ac,
svc_test_ac,
]
# SMOT
smot = SMOTE(sampling_strategy="minority", k_neighbors=3)
xsmot, ysmot = smot.fit_resample(xtrain.values, ytrain.values.reshape(-1))
xsmot = pd.DataFrame(xsmot, columns=xtrain.columns)
ysmot = pd.DataFrame(ysmot, columns=ytrain.columns)
xsmot_sc, sc_scaler = scale_data(xsmot)
xtest_sc, _ = scale_data(xtest, sc_scaler)
svc_train_ac_smot, svc_test_ac_smot = svc_baseline(
xsmot_sc, ysmot.values.reshape(-1), xtest_sc, ytest.values.reshape(-1)
)
print(type(ysmot))
bad_deal_perc = ysmot["How_is_the_deal"].value_counts(normalize=True)[1]
res.loc["SVC (SMOT)"] = [
xsmot.shape[0],
bad_deal_perc,
svc_train_ac_smot,
svc_test_ac_smot,
]
ens_train_ac_smot, ens_test_ac_smot = ensemble_model(
xsmot_sc, ysmot.values.reshape(-1), xtest_sc, ytest.values.reshape(-1)
)
res.loc["Ensemble (SMOT)"] = [
xsmot.shape[0],
bad_deal_perc,
ens_train_ac_smot,
ens_test_ac_smot,
]
print(res)