-
Notifications
You must be signed in to change notification settings - Fork 5
/
TPS-LDA-Catboost.py
165 lines (130 loc) · 5.47 KB
/
TPS-LDA-Catboost.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
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
# TPS-LDA-Catboost
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder, StandardScaler, MinMaxScaler
from sklearn.preprocessing import OneHotEncoder
import category_encoders as ce
from sklearn.model_selection import train_test_split
from sklearn.metrics import *
from sklearn.model_selection import StratifiedKFold
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn import model_selection
import lightgbm as lgbm
import xgboost as xgb
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor
import optuna
import tqdm
# Exploratory Data Analysis
train=pd.read_csv("../input/tabular-playground-series-aug-2021/train.csv")
test=pd.read_csv("../input/tabular-playground-series-aug-2021/test.csv")
train.head()
# make sure there are no null values
# if there are nulls, impute them
train.isnull().sum()
y = train['loss']
train.drop(['id','loss'],axis=1,inplace=True)
test.drop(['id'],axis=1,inplace=True)
# Feature Distribution
fig = plt.figure(figsize = (15, 60))
for i in range(len(train.columns.tolist()[:100])):
plt.subplot(20,5,i+1)
sns.set_style("white")
plt.title(train.columns.tolist()[:100][i], size = 12, fontname = 'monospace')
a = sns.kdeplot(train[train.columns.tolist()[:100][i]], color = '#34675c', shade = True, alpha = 0.9, linewidth = 1.5, edgecolor = 'black')
plt.ylabel('')
plt.xlabel('')
plt.xticks(fontname = 'monospace')
plt.yticks([])
for j in ['right', 'left', 'top']:
a.spines[j].set_visible(False)
a.spines['bottom'].set_linewidth(1.2)
fig.tight_layout(h_pad = 3)
plt.show()
# Scaling and LDA
not_features = ['id', 'loss']
features = []
for feat in train.columns:
if feat not in not_features:
features.append(feat)
scaler = StandardScaler()
train[features] = scaler.fit_transform(train[features])
test[features] = scaler.transform(test[features])
x = train
# show variance of components
lda = LDA(n_components=42, solver='svd')
X_lda = lda.fit_transform(x, y)
EVR = lda.explained_variance_ratio_
for idx, R in enumerate(EVR):
print("Component {}: {}% var".format(idx+1, np.round(R*100,2)))
# Model Tuning and Training
def objective(trial,data=x,target=y):
lda = LDA(n_components=42, solver='svd')
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.25,random_state=42)
X_train = lda.fit_transform(X_train, y_train)
X_test = lda.fit_transform(X_test, y_test)
params = {'iterations':trial.suggest_int("iterations", 1000, 20000),
'od_wait':trial.suggest_int('od_wait', 500, 2000),
'loss_function':'RMSE',
'task_type':"GPU",
'eval_metric':'RMSE',
'leaf_estimation_method':'Newton',
'bootstrap_type': 'Bernoulli',
'learning_rate' : trial.suggest_uniform('learning_rate',0.02,1),
'reg_lambda': trial.suggest_uniform('reg_lambda',1e-5,100),
'subsample': trial.suggest_uniform('subsample',0,1),
'random_strength': trial.suggest_uniform('random_strength',10,50),
'depth': trial.suggest_int('depth',1,15),
'min_data_in_leaf': trial.suggest_int('min_data_in_leaf',1,30),
'leaf_estimation_iterations': trial.suggest_int('leaf_estimation_iterations',1,15),
}
model = CatBoostRegressor(**params)
model.fit(X_train,y_train,eval_set=[(X_test,y_test)],early_stopping_rounds=100,verbose=False)
y_preds = model.predict(X_test)
loss = np.sqrt(mean_squared_error(y_test, y_preds))
return loss
# use optima to get best parameters
OPTUNA_OPTIMIZATION = True
study = optuna.create_study(direction='minimize')
study.optimize(objective, n_trials=50)
print('Number of finished trials:', len(study.trials))
print('Best trial: score {}, params {}'.format(study.best_trial.value, study.best_trial.params))
if OPTUNA_OPTIMIZATION:
display(optuna.visualization.plot_optimization_history(study))
display(optuna.visualization.plot_slice(study))
display(optuna.visualization.plot_parallel_coordinate(study))
# Training using kfold
cat_params = study.best_trial.params
cat_params['loss_function'] = 'RMSE'
cat_params['eval_metric'] = 'RMSE'
cat_params['bootstrap_type']= 'Bernoulli'
cat_params['leaf_estimation_method'] = 'Newton'
cat_params['random_state'] = 42
cat_params['task_type']='GPU'
test_preds=None
print("\033[93mTraining........")
kf = StratifiedKFold(n_splits = 15 , shuffle = True , random_state = 42)
for fold, (tr_index , val_index) in enumerate(kf.split(x.values , y.values)):
print("⁙" * 15)
print(f"Fold {fold + 1}")
x_train,x_val = x.values[tr_index] , x.values[val_index]
y_train,y_val = y.values[tr_index] , y.values[val_index]
eval_set = [(x_val, y_val)]
model =CatBoostRegressor(**cat_params)
model.fit(x_train, y_train, eval_set = eval_set, verbose = False)
train_preds = model.predict(x_train)
val_preds = model.predict(x_val)
print(np.sqrt(mean_squared_error(y_val, val_preds)))
if test_preds is None:
test_preds = model.predict(test.values)
else:
test_preds += model.predict(test.values)
print("-" * 50)
print("\033[95mTraining Done")
test_preds /= 15
# Preparing Submission
submission = pd.read_csv("../input/tabular-playground-series-aug-2021/sample_submission.csv")
submission['loss']=test_preds
submission.to_csv("lda.csv",index=False)