models.py 3.5 KB

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