-
Notifications
You must be signed in to change notification settings - Fork 38
/
model.py
205 lines (181 loc) · 9.19 KB
/
model.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import pandas as pd
import pandas_profiling as pp
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
import os
import plotly.graph_objects as go
import plotly.io as pio
import pickle
from sklearn.utils import resample
# Metrics
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, auc, roc_curve
# Validation
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from sklearn.pipeline import Pipeline, make_pipeline
# Tuning
from sklearn.model_selection import GridSearchCV
# Feature Extraction
from sklearn.feature_selection import RFE
# Preprocessing
from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer, Binarizer, LabelEncoder
# Models
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
# Ensembles
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import ExtraTreesClassifier
warnings.filterwarnings('ignore')
sns.set_style("whitegrid", {'axes.grid' : False})
pio.templates.default = "plotly_white"
################################################################################
# #
# Analyze Data #
# #
################################################################################
def explore_data(df):
print("Number of Instances and Attributes:", df.shape)
print('\n')
print('Dataset columns:',df.columns)
print('\n')
print('Data types of each columns: ', df.info())
################################################################################
# #
# Checking for Duplicates #
# #
################################################################################
def checking_removing_duplicates(df):
count_dups = df.duplicated().sum()
print("Number of Duplicates: ", count_dups)
if count_dups >= 1:
df.drop_duplicates(inplace=True)
print('Duplicate values removed!')
else:
print('No Duplicate values')
################################################################################
# #
# Split Data to Training and Validation set #
# #
################################################################################
def read_in_and_split_data(data, target):
X = data.drop(target, axis=1)
y = data[target]
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=0)
return X_train, X_test, y_train, y_test
################################################################################
# #
# Spot-Check Algorithms #
# #
################################################################################
def GetModel():
Models = []
Models.append(('LR' , LogisticRegression()))
Models.append(('LDA' , LinearDiscriminantAnalysis()))
Models.append(('KNN' , KNeighborsClassifier()))
Models.append(('CART' , DecisionTreeClassifier()))
Models.append(('NB' , GaussianNB()))
Models.append(('SVM' , SVC(probability=True)))
return Models
def ensemblemodels():
ensembles = []
ensembles.append(('AB' , AdaBoostClassifier()))
ensembles.append(('GBM' , GradientBoostingClassifier()))
ensembles.append(('RF' , RandomForestClassifier()))
ensembles.append(( 'Bagging' , BaggingClassifier()))
ensembles.append(('ET', ExtraTreesClassifier()))
return ensembles
################################################################################
# #
# Spot-Check Normalized Models #
# #
################################################################################
def NormalizedModel(nameOfScaler):
if nameOfScaler == 'standard':
scaler = StandardScaler()
elif nameOfScaler =='minmax':
scaler = MinMaxScaler()
elif nameOfScaler == 'normalizer':
scaler = Normalizer()
elif nameOfScaler == 'binarizer':
scaler = Binarizer()
pipelines = []
pipelines.append((nameOfScaler+'LR' , Pipeline([('Scaler', scaler),('LR' , LogisticRegression())])))
pipelines.append((nameOfScaler+'LDA' , Pipeline([('Scaler', scaler),('LDA' , LinearDiscriminantAnalysis())])))
pipelines.append((nameOfScaler+'KNN' , Pipeline([('Scaler', scaler),('KNN' , KNeighborsClassifier())])))
pipelines.append((nameOfScaler+'CART', Pipeline([('Scaler', scaler),('CART', DecisionTreeClassifier())])))
pipelines.append((nameOfScaler+'NB' , Pipeline([('Scaler', scaler),('NB' , GaussianNB())])))
pipelines.append((nameOfScaler+'SVM' , Pipeline([('Scaler', scaler),('SVM' , SVC())])))
pipelines.append((nameOfScaler+'AB' , Pipeline([('Scaler', scaler),('AB' , AdaBoostClassifier())]) ))
pipelines.append((nameOfScaler+'GBM' , Pipeline([('Scaler', scaler),('GMB' , GradientBoostingClassifier())]) ))
pipelines.append((nameOfScaler+'RF' , Pipeline([('Scaler', scaler),('RF' , RandomForestClassifier())]) ))
pipelines.append((nameOfScaler+'ET' , Pipeline([('Scaler', scaler),('ET' , ExtraTreesClassifier())]) ))
return pipelines
################################################################################
# #
# Train Model #
# #
################################################################################
def fit_model(X_train, y_train,models):
# Test options and evaluation metric
num_folds = 10
scoring = 'accuracy'
results = []
names = []
for name, model in models:
kfold = KFold(n_splits=num_folds, shuffle=True, random_state=0)
cv_results = cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
return names, results
################################################################################
# #
# Save Trained Model #
# #
################################################################################
def save_model(model,filename):
pickle.dump(model, open(filename, 'wb'))
################################################################################
# #
# Performance Measure #
# #
################################################################################
def classification_metrics(model, conf_matrix):
print(f"Training Accuracy Score: {model.score(X_train, y_train) * 100:.1f}%")
print(f"Validation Accuracy Score: {model.score(X_test, y_test) * 100:.1f}%")
fig,ax = plt.subplots(figsize=(8,6))
sns.heatmap(pd.DataFrame(conf_matrix), annot = True, cmap = 'YlGnBu',fmt = 'g')
ax.xaxis.set_label_position('top')
plt.tight_layout()
plt.title('Confusion Matrix', fontsize=20, y=1.1)
plt.ylabel('Actual label', fontsize=15)
plt.xlabel('Predicted label', fontsize=15)
plt.show()
print(classification_report(y_test, y_pred))
# Load Dataset
df = pd.read_csv('Crop_recommendation.csv')
# Remove Outliers
Q1 = df.quantile(0.25)
Q3 = df.quantile(0.75)
IQR = Q3 - Q1
df_out = df[~((df < (Q1 - 1.5 * IQR)) |(df > (Q3 + 1.5 * IQR))).any(axis=1)]
# Split Data to Training and Validation set
target ='label'
X_train, X_test, y_train, y_test = read_in_and_split_data(df, target)
# Train model
pipeline = make_pipeline(StandardScaler(), GaussianNB())
model = pipeline.fit(X_train, y_train)
y_pred = model.predict(X_test)
conf_matrix = confusion_matrix(y_test,y_pred)
classification_metrics(pipeline, conf_matrix)
# save model
save_model(model, 'model.pkl')