-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightgbm_funcs.py
More file actions
60 lines (55 loc) · 2.34 KB
/
Copy pathlightgbm_funcs.py
File metadata and controls
60 lines (55 loc) · 2.34 KB
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
from re import UNICODE
from numpy.lib import utils
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import seaborn as sns
import funcs
import mlfuncs
import eda_plots
import utils
import dlfuncs
# import machine learning modules
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
from sklearn.metrics import accuracy_score, f1_score
from sklearn.impute import SimpleImputer
from sklearn.compose import make_column_transformer
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
import xgboost as xgb
from lightgbm import LGBMClassifier
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
SEED = 42
params = {}
params['learning_rate']=0.03
params['boosting_type']='gbdt' #GradientBoostingDecisionTree
params['objective']='multiclass' #Multi-class target feature
params['metric']='multi_logloss' #metric for multi-class
# params['max_depth']=10
params['num_class']=3
def lightgbm_model(df, target_var, feats_to_exclude, text_feats, stratify = True, test_size = 0.25):
# extract train and test set
# split data
X_train, X_test, y_train, y_test = mlfuncs.split_data(df, target_var=target_var, stratify = stratify, test_size = test_size)
# extract cat_feats, num_feats, text_feats
cat_feats, num_feats, text_feats = dlfuncs.extract_cat_num_text_feats_for_keras(X_train, feats_to_exclude=feats_to_exclude, text_feats = text_feats)
# extract processor pipeline
preprocessor = dlfuncs.preprocess_col_transformer_for_keras(cat_feats, num_feats, text_feats = text_feats)
# instantiate XGBoost Classifier
clf_name = 'LightGBM Classifier'
clf = LGBMClassifier(n_estimators=1500, random_state=SEED)
print("Creating pipeline for {}.".format(clf_name))
pipe = make_pipeline(preprocessor, clf)
# fit training data to pipe
print("Fitting training data to pipeline for {}.".format(clf_name))
pipe.fit(X_train, y_train)
# get predictions
print("Predicting test values for {}.".format(clf_name))
y_pred = pipe.predict(X_test)
# get accuracy score
print("Calculating accuracy score for {}.".format(clf_name))
clf_f1_scr = accuracy_score(y_test, y_pred)
print("Accuracy Score for {}: {}".format(clf_name, clf_f1_scr))
return clf_f1_scr, pipe