-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train.py
139 lines (126 loc) · 4.3 KB
/
Train.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
__author__ = 'Xteeven'
import numpy as np
import neurolab as nl
import mlpy
import matplotlib.pyplot as plt
import time
from sklearn.lda import LDA
from sklearn.qda import QDA
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.svm import SVC
from sklearn import neighbors, datasets
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
datos = np.loadtxt('datasetnew.txt', delimiter=',')
x, y = datos[:, :10], datos[:, 10].astype(np.int)
# x = x - np.mean(x, axis=0)
xn = x / np.sqrt(np.sum(x**2, axis=0))
kfold = mlpy.cv_kfold(len(xn), len(xn))
#-----------------------------------------------------------
# percent = []
# # 0.722916666667
# result = []
# classifier = LDA()
# classifier.fit(xn, y)
# for crossval in range(len(kfold)):
# trainindex = kfold[crossval][0].tolist()
# evaluateindex = kfold[crossval][1].tolist()
# trainx = xn[trainindex]
# trainy = y[trainindex]
# evaluatex = xn[evaluateindex]
# evaluatey = y[evaluateindex]
#
# classifier.fit(trainx, trainy)
# result.append(classifier.predict(evaluatex) == evaluatey)
#
# print float(sum(result))/len(result)
#
#-----------------------------------------------------------
# X_train, X_test, y_train, y_test = train_test_split(
# xn, y, test_size=0.1, random_state=0)
#
# gam = np.arange(1, 10).tolist()
# tuned_parameters = [{'weights': ['uniform', 'distance'], 'n_neighbors': gam}]
# clfa = neighbors.KNeighborsClassifier()
# clf = GridSearchCV(clfa, tuned_parameters, cv=5)
# clf.fit(X_train, y_train)
# print(clf.best_params_)
#
# result = []
# 0.735416666667 {'n_neighbors': 4, 'weights': 'distance'}
# classifier = neighbors.KNeighborsClassifier(weights='distance', n_neighbors=4)
# classifier.fit(xn ,y)
# for crossval in range(len(kfold)):
# trainindex = kfold[crossval][0].tolist()
# evaluateindex = kfold[crossval][1].tolist()
# trainx = xn[trainindex]
# trainy = y[trainindex]
# evaluatex = xn[evaluateindex]
# evaluatey = y[evaluateindex]
#
# classifier.fit(trainx, trainy)
# result.append(classifier.predict(evaluatex) == evaluatey)
#
# print float(sum(result))/len(result)
#
#
#-----------------------------------------------------------
# X_train, X_test, y_train, y_test = train_test_split(
# xn, y, test_size=0.1, random_state=0)
# lim = np.arange(900, 1200, 300/100).tolist()
# gam = np.arange(1, 2, 1.0/100).tolist()
# tuned_parameters = [{'kernel': ['rbf'], 'gamma': gam,
# 'C': lim}]
#
# clf = GridSearchCV(SVC(C=1), tuned_parameters, cv=5)
# clf.fit(X_train, y_train)
# print(clf.best_params_)
# y_true, y_pred = y_test, clf.predict(X_test)
# print(classification_report(y_true, y_pred))
#
# #{'kernel': 'rbf', 'C': 972, 'gamma': 1.3300000000000003} #0.7625
# classifier = SVC(kernel='rbf', C=972, gamma=1.33)
# classifier.fit(xn,y)
# result = []
# for crossval in range(len(kfold)):
# trainindex = kfold[crossval][0].tolist()
# evaluateindex = kfold[crossval][1].tolist()
# trainx = xn[trainindex]
# trainy = y[trainindex]
# evaluatex = xn[evaluateindex]
# evaluatey = y[evaluateindex]
# clf.fit(trainx, trainy)
# result.append(clf.predict(evaluatex) == evaluatey)
# print float(sum(result))/float(len(result))
#
#----------------------------------------------------------------
# X_train, X_test, y_train, y_test = train_test_split(
# xn, y, test_size=0.1, random_state=0)
# lim = np.arange(1, 37).tolist()
# tuned_parameters = [{'n_estimators': lim}]
# cla = RandomForestClassifier()
# clf = GridSearchCV(cla, tuned_parameters, cv=3)
# clf.fit(X_train, y_train)
# print(clf.best_params_)
#
# result = []
#{'n_estimators': 36} 0.789583333333
# classifier = RandomForestClassifier(n_estimators=36)
# classifier.fit(xn, y)
# for crossval in range(len(kfold)):
# trainindex = kfold[crossval][0].tolist()
# evaluateindex = kfold[crossval][1].tolist()
# trainx = xn[trainindex]
# trainy = y[trainindex]
# evaluatex = xn[evaluateindex]
# evaluatey = y[evaluateindex]
#
# classifier.fit(trainx, trainy)
# result.append(classifier.predict(evaluatex) == evaluatey)
#
# print float(sum(result))/len(result)
#print confusion_matrix(classifier.predict(xn), y)
plt.show()