|
@@ -6,18 +6,27 @@ from sklearn.neighbors import KNeighborsClassifier
|
|
from sklearn.ensemble import GradientBoostingClassifier
|
|
from sklearn.ensemble import GradientBoostingClassifier
|
|
from sklearn.feature_selection import RFECV
|
|
from sklearn.feature_selection import RFECV
|
|
import sklearn.svm as svm
|
|
import sklearn.svm as svm
|
|
|
|
+from sklearn.metrics import accuracy_score
|
|
|
|
+from thundersvm import SVC
|
|
|
|
|
|
|
|
+# variables and parameters
|
|
|
|
+n_predict = 0
|
|
|
|
+
|
|
|
|
+def my_accuracy_scorer(*args):
|
|
|
|
+ global n_predict
|
|
|
|
+ score = accuracy_score(*args)
|
|
|
|
+ print('{0} - Score is {1}'.format(n_predict, score))
|
|
|
|
+ n_predict += 1
|
|
|
|
+ return score
|
|
|
|
|
|
def _get_best_model(X_train, y_train):
|
|
def _get_best_model(X_train, y_train):
|
|
|
|
|
|
- #Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
|
|
|
|
- Cs = [1, 2, 4, 8, 16, 32]
|
|
|
|
- # gammas = [0.001, 0.01, 0.1, 1, 5, 10, 100]
|
|
|
|
- gammas = [0.001, 0.1, 1, 10, 100]
|
|
|
|
|
|
+ Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
|
|
|
|
+ gammas = [0.001, 0.01, 0.1, 5, 10, 100]
|
|
param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
|
|
param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
|
|
|
|
|
|
- svc = svm.SVC(probability=True)
|
|
|
|
- clf = GridSearchCV(svc, param_grid, cv=5, scoring='accuracy', verbose=2)
|
|
|
|
|
|
+ svc = svm.SVC(probability=True, class_weight='balanced')
|
|
|
|
+ clf = GridSearchCV(svc, param_grid, cv=5, verbose=1, scoring=my_accuracy_scorer, n_jobs=-1)
|
|
|
|
|
|
clf.fit(X_train, y_train)
|
|
clf.fit(X_train, y_train)
|
|
|
|
|
|
@@ -30,6 +39,26 @@ def svm_model(X_train, y_train):
|
|
return _get_best_model(X_train, y_train)
|
|
return _get_best_model(X_train, y_train)
|
|
|
|
|
|
|
|
|
|
|
|
+def _get_best_gpu_model(X_train, y_train):
|
|
|
|
+
|
|
|
|
+ Cs = [0.001, 0.01, 0.1, 1, 2, 5, 10, 100, 1000]
|
|
|
|
+ gammas = [0.001, 0.01, 0.1, 1, 2, 5, 10, 100]
|
|
|
|
+ param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
|
|
|
|
+
|
|
|
|
+ svc = SVC(probability=True, class_weight='balanced')
|
|
|
|
+ clf = GridSearchCV(svc, param_grid, cv=10, verbose=1, scoring=my_accuracy_scorer, n_jobs=-1)
|
|
|
|
+
|
|
|
|
+ clf.fit(X_train, y_train)
|
|
|
|
+
|
|
|
|
+ model = clf.best_estimator_
|
|
|
|
+
|
|
|
|
+ return model
|
|
|
|
+
|
|
|
|
+def svm_gpu(X_train, y_train):
|
|
|
|
+
|
|
|
|
+ return _get_best_gpu_model(X_train, y_train)
|
|
|
|
+
|
|
|
|
+
|
|
def ensemble_model(X_train, y_train):
|
|
def ensemble_model(X_train, y_train):
|
|
|
|
|
|
svm_model = _get_best_model(X_train, y_train)
|
|
svm_model = _get_best_model(X_train, y_train)
|
|
@@ -72,6 +101,9 @@ def get_trained_model(choice, X_train, y_train):
|
|
if choice == 'svm_model':
|
|
if choice == 'svm_model':
|
|
return svm_model(X_train, y_train)
|
|
return svm_model(X_train, y_train)
|
|
|
|
|
|
|
|
+ if choice == 'svm_gpu':
|
|
|
|
+ return svm_gpu(X_train, y_train)
|
|
|
|
+
|
|
if choice == 'ensemble_model':
|
|
if choice == 'ensemble_model':
|
|
return ensemble_model(X_train, y_train)
|
|
return ensemble_model(X_train, y_train)
|
|
|
|
|