models.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # models imports
  2. from sklearn.model_selection import GridSearchCV
  3. from sklearn.linear_model import LogisticRegression
  4. from sklearn.ensemble import RandomForestClassifier, VotingClassifier
  5. from sklearn.neighbors import KNeighborsClassifier
  6. from sklearn.ensemble import GradientBoostingClassifier
  7. from sklearn.feature_selection import RFECV
  8. import sklearn.svm as svm
  9. from sklearn.metrics.scorer import accuracy_scorer
  10. from thundersvm import SVC
  11. # variables and parameters
  12. n_predict = 0
  13. def my_accuracy_scorer(*args):
  14. global n_predict
  15. score = accuracy_scorer(*args)
  16. print('{0} - Score is {1}'.format(n_predict, score))
  17. n_predict += 1
  18. return score
  19. def _get_best_model(X_train, y_train):
  20. #Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
  21. Cs = [1, 2, 4, 8, 16, 32]
  22. # gammas = [0.001, 0.01, 0.1, 1, 5, 10, 100]
  23. gammas = [0.001, 0.1, 1, 10, 100]
  24. param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
  25. svc = svm.SVC(probability=True)
  26. clf = GridSearchCV(svc, param_grid, cv=10, verbose=1, scoring=my_accuracy_scorer)
  27. clf.fit(X_train, y_train)
  28. model = clf.best_estimator_
  29. return model
  30. def svm_model(X_train, y_train):
  31. return _get_best_model(X_train, y_train)
  32. def _get_best_gpu_model(X_train, y_train):
  33. Cs = [0.001, 0.01, 0.1, 1, 2, 5, 10, 100, 1000]
  34. gammas = [0.001, 0.01, 0.1, 1, 2, 5, 10, 100]
  35. param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
  36. svc = SVC(probability=True)
  37. clf = GridSearchCV(svc, param_grid, cv=10, verbose=1, scoring=my_accuracy_scorer)
  38. clf.fit(X_train, y_train)
  39. model = clf.best_estimator_
  40. return model
  41. def svm_gpu(X_train, y_train):
  42. return _get_best_gpu_model(X_train, y_train)
  43. def ensemble_model(X_train, y_train):
  44. svm_model = _get_best_model(X_train, y_train)
  45. lr_model = LogisticRegression(solver='liblinear', multi_class='ovr', random_state=1)
  46. rf_model = RandomForestClassifier(n_estimators=100, random_state=1)
  47. ensemble_model = VotingClassifier(estimators=[
  48. ('svm', svm_model), ('lr', lr_model), ('rf', rf_model)], voting='soft', weights=[1,1,1])
  49. ensemble_model.fit(X_train, y_train)
  50. return ensemble_model
  51. def ensemble_model_v2(X_train, y_train):
  52. svm_model = _get_best_model(X_train, y_train)
  53. knc_model = KNeighborsClassifier(n_neighbors=2)
  54. gbc_model = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
  55. lr_model = LogisticRegression(solver='liblinear', multi_class='ovr', random_state=1)
  56. rf_model = RandomForestClassifier(n_estimators=100, random_state=1)
  57. ensemble_model = VotingClassifier(estimators=[
  58. ('lr', lr_model),
  59. ('knc', knc_model),
  60. ('gbc', gbc_model),
  61. ('svm', svm_model),
  62. ('rf', rf_model)],
  63. voting='soft', weights=[1, 1, 1, 1, 1])
  64. ensemble_model.fit(X_train, y_train)
  65. return ensemble_model
  66. def get_trained_model(choice, X_train, y_train):
  67. if choice == 'svm_model':
  68. return svm_model(X_train, y_train)
  69. if choice == 'svm_gpu':
  70. return svm_gpu(X_train, y_train)
  71. if choice == 'ensemble_model':
  72. return ensemble_model(X_train, y_train)
  73. if choice == 'ensemble_model_v2':
  74. return ensemble_model_v2(X_train, y_train)