-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifiers.py
executable file
·153 lines (110 loc) · 4.13 KB
/
classifiers.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 11 23:11:07 2018
@author: juliocesar
"""
# Libraries and Dependencies
# -----------------------------------------------------------------------
from sklearn.svm import LinearSVC #,SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
#from xgboost.sklearn import XGBClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
# Global matrix variable
X = None
# Classifiers
# -----------------------------------------------------------------------
# SVM classifier
def svm(data, c):
# Building the training and test sets
trx = data['training_data'].toarray()
trl = data['training_labels'].reshape(X.shape[0],1)
tsx = data['test_data'].toarray()
# Creating and training classifier
classifier = LinearSVC(C = c).fit(trx, trl.ravel())
# Making prediction on test set
prediction = classifier.predict(tsx)
return prediction
# Random Forest Classifier
def rndForest(data, ntrees):
# Building the training and test sets
trx = data['training_data'].toarray()
trl = data['training_labels'].reshape(X.shape[0],1)
tsx = data['test_data'].toarray()
# Creating and training classifier
clf = RandomForestClassifier(n_estimators = ntrees).fit(trx, trl.ravel())
# Making prediction on test set
prediction = clf.predict(tsx)
return prediction
# Logistic Regression Classifier
def logReg(data, c):
# Building the training and test sets
trx = data['training_data'].toarray()
trl = data['training_labels'].reshape(X.shape[0],1)
tsx = data['test_data'].toarray()
# Creating and training classifier
clf = LogisticRegression(C = c).fit(trx, trl.ravel())
# Making prediction on test set
prediction = clf.predict(tsx)
return prediction
# Extreme Gradient Boost Classifier
def xgboost(data, md, ss, cs):
# Building the training and test sets
trx = data['training_data'].toarray()
trl = data['training_labels'].reshape(X.shape[0],1)
tsx = data['test_data'].toarray()
# Creating and training classifier
clf = XGBClassifier( max_depth = md, subsample = ss,
colsample_bytree = cs).fit(trx, trl.ravel())
# Making prediction on test set
prediction = clf.predict(tsx)
return prediction
# Linear Discriminant Analysis Classifier
def lda(data):
# Building the training and test sets
trx = data['training_data'].toarray()
trl = data['training_labels'].reshape(X.shape[0],1)
tsx = data['test_data'].toarray()
# Creating and training classifier
clf = LinearDiscriminantAnalysis().fit(trx, trl.ravel())
# Making prediction on test set
prediction = clf.predict(tsx)
return prediction
# Quadratic Discriminant Analysis Classifier
def qda(data):
# Building the training and test sets
trx = data['training_data'].toarray()
trl = data['training_labels'].reshape(X.shape[0],1)
tsx = data['test_data'].toarray()
# Creating and training classifier
clf = QuadraticDiscriminantAnalysis().fit(trx, trl.ravel())
# Making prediction on test set
prediction = clf.predict(tsx)
return prediction
# Ada Boost Classifier
def adaboost(data, ne):
# Building the training and test sets
trx = data['training_data'].toarray()
trl = data['training_labels'].reshape(X.shape[0],1)
tsx = data['test_data'].toarray()
# Creating and training classifier
dt = DecisionTreeClassifier()
clf = AdaBoostClassifier(n_estimators = ne,
base_estimator = dt).fit(trx, trl.ravel())
# Making prediction on test set
prediction = clf.predict(tsx)
return prediction
# Functions
# -----------------------------------------------------------------------
# Getter for global matrix
def getX():
global X
return X
# Setter for global matrix
def setX(value):
global X
X = value