-
Notifications
You must be signed in to change notification settings - Fork 0
/
votingClassification_70427.py
92 lines (73 loc) · 2.92 KB
/
votingClassification_70427.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
import random
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
import load_data
from preprocessing import OnehotEncoder, RemoveEmptyColumn, RemoveStdZeroColumn, ConcatProdLine
from model import HardVotingClassifier
import pandas as pd
import numpy as np
from imblearn.over_sampling import BorderlineSMOTE
from sklearn.metrics import f1_score
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from sklearn.ensemble import BaggingClassifier, GradientBoostingClassifier
from sklearn.tree import DecisionTreeClassifier
from xgboost import XGBClassifier
seed = 42
def seed_everything(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
def preprocess(train_X, train_y, test):
train_X, test = RemoveEmptyColumn.preprocess(train_X, test)
train_X, test = RemoveStdZeroColumn.preprocess(train_X, test)
train_X, test, new_col = ConcatProdLine.preprocess(train_X, test)
train_X = train_X.fillna(0)
test = test.fillna(0)
train_X, test, prod_dum = OnehotEncoder.preprocess(train_X, test, [new_col])
return train_X, train_y, test
def training(train_X, train_y, test):
models = [
LGBMClassifier(objective='multiclass', random_state=seed),
CatBoostClassifier(objective='MultiClass',
task_type='GPU',
one_hot_max_size=2, random_seed=42,
iterations=4000, early_stopping_rounds=50,
learning_rate=0.05, verbose=False
),
XGBClassifier(random_state=seed),
]
[x.fit(train_X, train_y) for x in models]
return models
def predict(models, test, mode, weights):
if mode == "hard":
preds = np.asarray([x.predict(test).reshape(-1) for x in models]).T
res = np.apply_along_axis(
lambda x: np.argmax(np.bincount(x, weights=weights)),
axis=1,
arr=preds
)
if mode == "soft":
preds = np.asarray([x.predict_proba(test) for x in models])
res = np.zeros(preds[0].shape)
for pred, weight in zip(preds, weights):
res = res + pred*weight
res = np.argmax(preds, axis=0)
return res
def submission(preds, basepath, filename):
submit = pd.read_csv('{}/sample_submission.csv'.format(basepath))
submit['Y_Class'] = preds
submit.to_csv('{}.csv'.format(filename), index=False)
def main():
seed_everything(42) # Seed 고정
datapath = 'open'
train, test = load_data.load_data(datapath)
train_X, train_y = load_data.split_data_label(train)
test, _ = load_data.split_data_label(test, False)
train_X, train_y, test = preprocess(train_X, train_y, test)
models = training(train_X, train_y, test)
preds = predict(models, test, "hard", [2,2,1])
submission(preds, datapath, 'hard')
if __name__ == "__main__":
main()