-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
170 lines (128 loc) · 5.87 KB
/
train.py
File metadata and controls
170 lines (128 loc) · 5.87 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
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
165
166
167
168
169
170
from __future__ import print_function
import os
import warnings
import argparse
import joblib
import pingouin as pg
import numpy as np
import requests
import pandas as pd
import azureml.core
import lightgbm as lgb
from io import BytesIO
from boruta import BorutaPy
from azureml.core.run import Run
from urllib.request import urlopen
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
warnings.filterwarnings(action='ignore', category=UserWarning, module='lightgbm')
def corr_drop_cols(df, corr_val = 0.85):
df_copy = df.copy() # create a copy
corrmat = pg.pairwise_corr(df_copy, method='pearson')[['X', 'Y', 'r']]
df_corr = corrmat.sort_values(by='r', ascending=0)[(corrmat['r'] >= corr_val) | (corrmat['r'] <= -1*corr_val)]
setcols = set(df_corr.Y.to_list())
# Drop columns high correlation values
df_copy = df_copy.drop(list(setcols), axis=1)
return df_copy
def create_label(df):
df_copy = df.copy() # create a copy
# Encoding target variable
df_copy['label'] = [1 if x >= 1400 else 0 for x in df_copy['shares']]
df_copy = df_copy.drop(['shares', 'timedelta'], axis=1)
y = df_copy['label'].values
labelencoder = LabelEncoder()
df_copy['label'] = labelencoder.fit_transform(y)
# Encoding all categorical variables
col_list = [s for s in df_copy.columns if 'is' in s]
df_copy[col_list] = df_copy[col_list].apply(lambda x: labelencoder.fit_transform(x))
return df_copy
def scaling_num(df):
df_copy = df.copy() # create a copy
# Scaling numerical data
from sklearn.preprocessing import MinMaxScaler
col_list = [s for s in df_copy.columns if 'is' in s] + ['label']
num_cols = [m for m in df_copy if m not in col_list]
scale = MinMaxScaler()
df_copy[num_cols] = pd.DataFrame(scale.fit_transform(df_copy[num_cols].values), columns=[num_cols], index=df_copy.index)
return df_copy
def feature_selection(df, OUT_LOC):
df_copy = df.copy() # create a copy
mfile = BytesIO(requests.get(OUT_LOC).content) # BytesIO create a file object out of the response from GitHub
feat_selector = joblib.load(mfile)
X = df_copy.drop(['label'], axis=1)
keep_cols = list(X.columns[feat_selector.support_]) + ['label']
df_copy = df_copy[keep_cols]
return df_copy
def split_train_test(df):
df_copy = df.copy() # create a copy
X = df_copy.drop('label', axis=1)
y = df_copy.pop('label')
# Train-test split 80/20
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, stratify = y, random_state = 100)
return X_train, X_test, y_train, y_test
if __name__ == "__main__":
print('azureml.core.VERSION={}'.format(azureml.core.VERSION))
# Parse ainput arguments
parser = argparse.ArgumentParser()
parser.add_argument("--num-leaves", type=int, dest="num_leaves", default=64, help="# of leaves of the tree")
parser.add_argument("--min-data-in-leaf", type=int, dest="min_data_in_leaf", default=50, help="minimum # of samples in each leaf")
parser.add_argument("--learning-rate", type=float, dest="learning_rate", default=0.001, help="learning rate")
parser.add_argument("--feature-fraction",type=float,dest="feature_fraction",default=1.0,help="ratio of features used in each iteration")
parser.add_argument("--bagging-fraction",type=float,dest="bagging_fraction",default=1.0,help="ratio of samples used in each iteration")
parser.add_argument("--bagging-freq", type=int, dest="bagging_freq", default=1, help="bagging frequency")
parser.add_argument("--max-depth", type=int, dest="max_depth", default=20, help="To limit the tree depth explicitly")
args = parser.parse_args()
args.feature_fraction = round(args.feature_fraction, 2)
args.bagging_fraction = round(args.bagging_fraction, 2)
print(args)
# Start an Azure ML run
run = Run.get_context()
# Data location
DATA_LOC = "https://github.com/franckess/AzureML_Capstone/blob/main/data/OnlineNewsPopularity.csv?raw=true"
# Boruta model location
BORUTA_LOC = "https://github.com/franckess/AzureML_Capstone/releases/download/1.1/boruta_model_final.pkl"
# Parameters of GBM model
params = {
"objective": "binary",
"num_leaves": args.num_leaves,
"min_data_in_leaf": args.min_data_in_leaf,
"learning_rate": args.learning_rate,
"feature_fraction": args.feature_fraction,
"bagging_fraction": args.bagging_fraction,
"bagging_freq": args.bagging_freq,
"num_threads": 16,
"max_depth": args.max_depth
}
print(params)
# Load training data
print('Loading data from {}'.format(DATA_LOC))
df = pd.read_csv(DATA_LOC)
# Removing space character in the feature names
df.columns = df.columns.str.replace(' ','')
# Drop URL column
df = df.drop(['url'], axis=1)
# Perform Data pre-processing
print('Running pre-processing steps')
df = corr_drop_cols(df)
df = create_label(df)
df = scaling_num(df)
print('Loading Boruta model for feature selection from {}'.format(BORUTA_LOC))
df = feature_selection(df, BORUTA_LOC)
# Split train data into train & test
X_train, X_test, y_train, y_test = split_train_test(df)
# Train LightGBM model
print('Running modeling step')
clf = lgb.LGBMClassifier(**params)
clf.fit(X_train, y_train)
#prediction on the test set
y_pred=clf.predict(X_test)
# view accuracy
accuracy=accuracy_score(y_pred, y_test)
print('LightGBM Model accuracy score: {0:0.4f}'.format(accuracy))
# Log the validation loss (NRMSE - normalized root mean squared error)
run.log("Accuracy", np.float(accuracy))
#Dump the model using joblib
os.makedirs("outputs", exist_ok=True)
joblib.dump(value=clf, filename="outputs/lgb_model.pkl")