models.py 3.9 KB

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