models.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 import accuracy_score
  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_score(*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. gammas = [0.001, 0.01, 0.1, 5, 10, 100]
  22. param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
  23. svc = svm.SVC(probability=True, class_weight='balanced')
  24. clf = GridSearchCV(svc, param_grid, cv=5, verbose=1, scoring=my_accuracy_scorer, n_jobs=-1)
  25. clf.fit(X_train, y_train)
  26. model = clf.best_estimator_
  27. return model
  28. def svm_model(X_train, y_train):
  29. return _get_best_model(X_train, y_train)
  30. def _get_best_gpu_model(X_train, y_train):
  31. # Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
  32. # gammas = [0.001, 0.01, 0.1, 5, 10, 100]
  33. # param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
  34. # svc = SVC(probability=True, class_weight='balanced')
  35. # clf = GridSearchCV(svc, param_grid, cv=5, verbose=1, scoring=my_accuracy_scorer, n_jobs=-1)
  36. # clf.fit(X_train, y_train)
  37. Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
  38. gammas = [0.001, 0.01, 0.1, 5, 10, 100]
  39. bestModel = None
  40. bestScore = 0.
  41. n_eval = 1
  42. for c in Cs:
  43. for g in gammas:
  44. svc = SVC(probability=True, class_weight='balanced', kernel='rbf', gamma=g, C=c)
  45. svc.fit(X_train, y_train)
  46. score = svc.score(X_train, y_train)
  47. # keep track of best model
  48. if score > bestScore:
  49. bestScore = score
  50. bestModel = svc
  51. print('Eval n° {} [C: {}, gamma: {}] => [score: {}, bestScore:{}]'.format(n_eval, c, g, score, bestScore))
  52. n_eval += 1
  53. return bestModel
  54. def svm_gpu(X_train, y_train):
  55. return _get_best_gpu_model(X_train, y_train)
  56. def ensemble_model(X_train, y_train):
  57. svm_model = _get_best_model(X_train, y_train)
  58. lr_model = LogisticRegression(solver='liblinear', multi_class='ovr', random_state=1)
  59. rf_model = RandomForestClassifier(n_estimators=100, random_state=1)
  60. ensemble_model = VotingClassifier(estimators=[
  61. ('svm', svm_model), ('lr', lr_model), ('rf', rf_model)], voting='soft', weights=[1,1,1])
  62. ensemble_model.fit(X_train, y_train)
  63. return ensemble_model
  64. def ensemble_model_v2(X_train, y_train):
  65. svm_model = _get_best_model(X_train, y_train)
  66. knc_model = KNeighborsClassifier(n_neighbors=2)
  67. gbc_model = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
  68. lr_model = LogisticRegression(solver='liblinear', multi_class='ovr', random_state=1)
  69. rf_model = RandomForestClassifier(n_estimators=100, random_state=1)
  70. ensemble_model = VotingClassifier(estimators=[
  71. ('lr', lr_model),
  72. ('knc', knc_model),
  73. ('gbc', gbc_model),
  74. ('svm', svm_model),
  75. ('rf', rf_model)],
  76. voting='soft', weights=[1, 1, 1, 1, 1])
  77. ensemble_model.fit(X_train, y_train)
  78. return ensemble_model
  79. def get_trained_model(choice, X_train, y_train):
  80. if choice == 'svm_model':
  81. return svm_model(X_train, y_train)
  82. if choice == 'svm_gpu':
  83. return svm_gpu(X_train, y_train)
  84. if choice == 'ensemble_model':
  85. return ensemble_model(X_train, y_train)
  86. if choice == 'ensemble_model_v2':
  87. return ensemble_model_v2(X_train, y_train)