models.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. from sklearn.metrics import roc_auc_score
  9. import sklearn.svm as svm
  10. def _roc_auc_scorer(estimator, X, y):
  11. y_pred = estimator.predict(X)
  12. return roc_auc_score(y, y_pred)
  13. def _get_best_model(X_train, y_train):
  14. Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
  15. gammas = [0.001, 0.01, 0.1, 1, 5, 10, 100]
  16. param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
  17. svc = svm.SVC(probability=True)
  18. clf = GridSearchCV(svc, param_grid, cv=10, scoring=_roc_auc_scorer, verbose=0)
  19. clf.fit(X_train, y_train)
  20. model = clf.best_estimator_
  21. return model
  22. def svm_model(X_train, y_train):
  23. return _get_best_model(X_train, y_train)
  24. def rfe_svm_model(X_train, y_train, n_components=1):
  25. Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
  26. gammas = [0.001, 0.01, 0.1, 1, 5, 10, 100]
  27. param_grid = [{'estimator__C': Cs, 'estimator__gamma' : gammas}]
  28. estimator = svm.SVC(kernel="linear")
  29. selector = RFECV(estimator, step=1, cv=4, verbose=1)
  30. clf = GridSearchCV(selector, param_grid, cv=5, verbose=1, scoring=_roc_auc_scorer)
  31. clf.fit(X_train, y_train)
  32. return clf.best_estimator_
  33. def get_trained_model(choice, X_train, y_train):
  34. if choice == 'svm_model':
  35. return svm_model(X_train, y_train)
  36. if choice == 'rfe_svm_model':
  37. return rfe_svm_model(X_train, y_train)