-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
46 lines (38 loc) · 1.15 KB
/
helper.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
# import algorithms
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
# import decompositions
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.decomposition import PCA
from sklearn.metrics import confusion_matrix
# Start with Decompositions
def lda(X, y, n):
'''
Returns optimal projection of the data
LDA with n components
'''
selector = LinearDiscriminantAnalysis(n_components=n)
selector.fit(X, y)
return selector.transform(X), y
def pca(X, y, n):
'''
Returns optimal projection of the data
PCA with n components
'''
selector = PCA(n_components=n)
selector.fit(X, y)
return selector.transform(X), y
class svm():
def __init__(self, X_train, y_train):
self.classifier = SVC(kernel='rbf')
self.classifier.fit(X_train, y_train)
def classify(self, X_test, y_test):
pred = self.classifier.predict(X_test)
return confusion_matrix(y_test, pred)
class bayes():
def __init__(self, X_train, y_train):
self.classifier = GaussianNB()
self.classifier.fit(X_train, y_train)
def classify(self, X_test, y_test):
pred = self.classifier.predict(X_test)
return confusion_matrix(y_test, pred)