-
Notifications
You must be signed in to change notification settings - Fork 4
/
v1.0-classify.py
328 lines (212 loc) · 10.7 KB
/
v1.0-classify.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
######################################################################################## Remove warnings
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
######################################################################################## Importing Modules
from sklearn.model_selection import train_test_split as tts
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
import numpy
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import Pipeline
from sklearn.pipeline import make_pipeline
from sklearn.pipeline import FeatureUnion
from sklearn.preprocessing import StandardScaler
from sklearn.base import TransformerMixin, BaseEstimator
from transformer import DFFunctionTransformer
from transformer import DFFeatureUnion
from transformer import DFStandardScaler
import re
from sklearn.model_selection import GridSearchCV
######################################################################################## Loading data & Preprocessing
# Load the dataset - CSV input
data = pd.read_csv('data/class_full.csv',
encoding='latin1',
error_bad_lines=False,
delimiter=';',
decimal=',')
# Define column names & Change label to Pandas "Category"
data.columns = ['desc', 'value', 'label']
# Define features, numeric_features and target(label).
features = [c for c in data.columns.values if c not in ['label']]
numeric_features = [c for c in data.columns.values if c not in ['desc','label']]
target = 'label'
# Split data into training and testing
train_features, test_features, train_labels, test_labels = tts(data[features], data[target], test_size=0.05, random_state=42)
# X_Train, X_Test, Y_Train, Y_Test
# Transformer to select a single column from the data frame to perform additional transformations on.
# Use on text columns in the data
class TextSelector(BaseEstimator, TransformerMixin):
def __init__(self, key):
self.key = key
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.key]
# Transformer to select a single column from the data frame to perform additional transformations on.
# Use on numeric columns in the data
class NumberSelector(BaseEstimator, TransformerMixin):
def __init__(self, key):
self.key = key
def fit(self, X, y=None):
return self
def transform(self, X):
return X[[self.key]]
desc = Pipeline([
('selector', TextSelector(key='desc')),
('tfidf', TfidfVectorizer(stop_words='english'))
])
desc.fit_transform(train_features)
value = Pipeline([
('selector', NumberSelector(key='value')),
# ('standard', StandardScaler())
])
value.fit_transform(train_features)
feats = FeatureUnion([
('desc', desc),
('value', value)])
feature_processing = Pipeline([('feats', feats)])
feature_processing.fit_transform(train_features)
pipeline = Pipeline([
('features', feats),
('classifier', SVC(kernel='linear', random_state = 42)),
])
pipeline.fit(train_features, train_labels)
predictions = pipeline.predict(test_features)
print(numpy.mean(predictions == test_labels))
pipeline.get_params().keys()
# hyperparameters = { 'features__desc__tfidf__max_df': [0.9, 0.95],
# 'features__desc__tfidf__ngram_range': [(1,1), (1,2)],
# 'classifier__max_depth': [50, 70],
# 'classifier__min_samples_leaf': [1,2]
# }
# clf = GridSearchCV(pipeline, hyperparameters, cv=5)
# # Fit and tune model
# clf.fit(train_features, train_labels)
# clf.best_params_
# #refitting on entire training data using best settings
# clf.refit
# predictions = clf.predict(test_features)
# probs = clf.predict_proba(test_features)
# print(numpy.mean(predictions == test_labels))
# submission = pd.read_csv('data/test.csv')
# #preprocessing
# submission = processing(submission)
# predictions = clf.predict_proba(submission)
# preds = pd.DataFrame(data=predictions, columns=clf.best_estimator_.named_steps['classifier'].classes_)
# #generating a submission file
# result = pd.concat([submission[['id']], preds], axis=1)
# result.set_index('id', inplace = True)
# result.head()
######################################################################################## Fitting the models
# #Test
# train_features, test_features, train_labels, test_labels = tts(vectors, labels, test_size=0.05)
# # Random Forest Classifier
# print('\nEstimating score with Random Forest Classifier...')
# forest_model = RandomForestClassifier(random_state=42)
# forest_model.fit(train_features, train_labels)
# predictions_forest = forest_model.predict(vectors_)
# print('Score: {}'.format(forest_model.score(test_features, test_labels)) + ' Random Forest Classifier')
# # Decision Tree Classifier
# print('\nEstimating score with Decision Tree Classifier...')
# tree_model = tree.DecisionTreeClassifier(random_state=42)
# tree_model.fit(train_features, train_labels)
# predictions_tree = tree_model.predict(vectors_)
# print('Score: {}'.format(tree_model.score(test_features, test_labels)) + ' Decistion Tree Classifier')
# # SVC Linear Classifier
# print('\nEstimating score with SVC Linear Classifier...')
# svc_model = svm.SVC(kernel='linear', random_state=42, probability=True)
# svc_model.fit(train_features, train_labels)
# predictions_svc = svc_model.predict(vectors_)
# print('Score: {}'.format(svc_model.score(test_features, test_labels)) + ' SVC Linear Classifier')
# # ExtraTree Classifier
# print('\nEstimating score with ExtraTree Classifier...')
# extra_model = ExtraTreesClassifier(n_estimators=100, max_depth=None, min_samples_split=10, random_state=42)
# extra_model.fit(train_features, train_labels)
# predictions_extra = extra_model.predict(vectors_)
# print('Score: {}'.format(extra_model.score(test_features, test_labels)) + ' ExtraTree Classifier')
# ######################################################################################## Print consolidated results and accuracy
# # Print of all results
# print('\nScore: {}'.format(forest_model.score(test_features, test_labels)) + ' Random Forest Classifier')
# print('Score: {}'.format(tree_model.score(test_features, test_labels)) + ' Decistion Tree Classifier')
# print('Score: {}'.format(svc_model.score(test_features, test_labels)) + ' SVC Linear Classifier')
# print('Score: {}'.format(extra_model.score(test_features, test_labels)) + ' ExtraTree Classifier\n')
# # Voting Classifier
# eclf = VotingClassifier(estimators=[
# ('lr', forest_model),
# ('rf', tree_model),
# ('svc', svc_model),
# ('extra', extra_model)
# ], voting='soft')
# for clf, label in zip( [forest_model, tree_model, svc_model, extra_model, eclf],
# ['Random Forest', 'Decision Tree', 'SVC', 'Extra']):
# scores = cross_val_score(clf, test_features, test_labels, cv=5, scoring='accuracy')
# print("Accuracy: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))
# ######################################################################################## Make Predictions
# # Statement to be classified
# statement = pd.read_csv('data/extrato.csv',
# encoding='latin1',
# error_bad_lines=False,
# delimiter=',')
# # Defining the column and vectorizing it
# newFeatures = statement['memo'].values.astype(str)
# newVectorizer = TfidfVectorizer()
# newVector = newVectorizer.fit_transform(newFeatures)
# newVector_ = newVectorizer.transform(newFeatures)
# # Predictions with Random Forest, Decision Tree & SVC models
# predictions_forest = forest_model.predict(vectorizer.transform(newFeatures))
# predictions_forest = numpy.asarray(predictions_forest)
# predictions_forest = pd.DataFrame(predictions_forest)
# predictions_tree = tree_model.predict(vectorizer.transform(newFeatures))
# predictions_tree = numpy.asarray(predictions_tree)
# predictions_tree = pd.DataFrame(predictions_tree)
# predictions_svc = svc_model.predict(vectorizer.transform(newFeatures))
# predictions_svc = numpy.asarray(predictions_svc)
# predictions_svc = pd.DataFrame(predictions_svc)
# predictions_extra = extra_model.predict(vectorizer.transform(newFeatures))
# predictions_extra = numpy.asarray(predictions_extra)
# predictions_extra = pd.DataFrame(predictions_extra)
# # Finding probabilities for each of the assigned categories
# forest_prob = forest_model.predict_proba(vectorizer.transform(newFeatures))
# forest_prob = numpy.asarray(forest_prob)
# forest_prob = pd.DataFrame(forest_prob)
# forest_prob = forest_prob.max(axis=1)
# tree_prob = tree_model.predict_proba(vectorizer.transform(newFeatures))
# tree_prob = numpy.asarray(tree_prob)
# tree_prob = pd.DataFrame(tree_prob)
# tree_prob = tree_prob.max(axis=1)
# svc_prob = svc_model.predict_proba(vectorizer.transform(newFeatures))
# svc_prob = numpy.asarray(svc_prob)
# svc_prob = pd.DataFrame(svc_prob)
# svc_prob = svc_prob.max(axis=1)
# extra_prob = extra_model.predict_proba(vectorizer.transform(newFeatures))
# extra_prob = numpy.asarray(extra_prob)
# extra_prob = pd.DataFrame(extra_prob)
# extra_prob = extra_prob.max(axis=1)
# ######################################################################################## Export and consolidate predictions
# csv_pred = pd.concat([ predictions_forest, forest_prob,
# predictions_tree, tree_prob,
# predictions_svc, svc_prob,
# predictions_extra, extra_prob],
# axis=1)
# csv_pred.to_csv("data/predictions.csv")
# consolidated = pd.concat([statement, csv_pred], axis=1)
# consolidated.columns = ['type', 'date', 'amount', 'memo', 'id',
# 'forest_predict', 'forest_prob',
# 'tree_predict', 'tree_prob',
# 'svc_predict', 'svc_prob',
# 'extra_predict', 'extra_prob']
# consolidated.to_csv("data/consolidated.csv")
# ######################################################################################## END