models.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, 2, 5, 10, 100, 1000]
  32. gammas = [0.001, 0.01, 0.1, 1, 2, 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=10, verbose=1, scoring=my_accuracy_scorer, n_jobs=-1)
  36. clf.fit(X_train, y_train)
  37. model = clf.best_estimator_
  38. return model
  39. def svm_gpu(X_train, y_train):
  40. return _get_best_gpu_model(X_train, y_train)
  41. def ensemble_model(X_train, y_train):
  42. svm_model = _get_best_model(X_train, y_train)
  43. lr_model = LogisticRegression(solver='liblinear', multi_class='ovr', random_state=1)
  44. rf_model = RandomForestClassifier(n_estimators=100, random_state=1)
  45. ensemble_model = VotingClassifier(estimators=[
  46. ('svm', svm_model), ('lr', lr_model), ('rf', rf_model)], voting='soft', weights=[1,1,1])
  47. ensemble_model.fit(X_train, y_train)
  48. return ensemble_model
  49. def ensemble_model_v2(X_train, y_train):
  50. svm_model = _get_best_model(X_train, y_train)
  51. knc_model = KNeighborsClassifier(n_neighbors=2)
  52. gbc_model = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
  53. lr_model = LogisticRegression(solver='liblinear', multi_class='ovr', random_state=1)
  54. rf_model = RandomForestClassifier(n_estimators=100, random_state=1)
  55. ensemble_model = VotingClassifier(estimators=[
  56. ('lr', lr_model),
  57. ('knc', knc_model),
  58. ('gbc', gbc_model),
  59. ('svm', svm_model),
  60. ('rf', rf_model)],
  61. voting='soft', weights=[1, 1, 1, 1, 1])
  62. ensemble_model.fit(X_train, y_train)
  63. return ensemble_model
  64. def get_trained_model(choice, X_train, y_train):
  65. if choice == 'svm_model':
  66. return svm_model(X_train, y_train)
  67. if choice == 'svm_gpu':
  68. return svm_gpu(X_train, y_train)
  69. if choice == 'ensemble_model':
  70. return ensemble_model(X_train, y_train)
  71. if choice == 'ensemble_model_v2':
  72. return ensemble_model_v2(X_train, y_train)