find_best_attributes_from.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # main imports
  2. import os
  3. import sys
  4. import argparse
  5. import pandas as pd
  6. import numpy as np
  7. import logging
  8. import datetime
  9. # model imports
  10. from sklearn.model_selection import train_test_split
  11. from sklearn.model_selection import GridSearchCV
  12. from sklearn.linear_model import LogisticRegression
  13. from sklearn.ensemble import RandomForestClassifier, VotingClassifier
  14. import joblib
  15. import sklearn.svm as svm
  16. from sklearn.utils import shuffle
  17. from sklearn.metrics import roc_auc_score
  18. from sklearn.model_selection import cross_val_score
  19. from sklearn.feature_selection import SelectFromModel
  20. from sklearn.ensemble import ExtraTreesClassifier
  21. # modules and config imports
  22. sys.path.insert(0, '') # trick to enable import of main folder module
  23. import custom_config as cfg
  24. import models as mdl
  25. # variables and parameters
  26. models_list = cfg.models_names_list
  27. number_of_values = 30
  28. ils_iteration = 4000
  29. ls_iteration = 10
  30. # default validator
  31. def validator(solution):
  32. if list(solution.data).count(1) < 5:
  33. return False
  34. return True
  35. def loadDataset(filename):
  36. ########################
  37. # 1. Get and prepare data
  38. ########################
  39. dataset_train = pd.read_csv(filename + '.train', header=None, sep=";")
  40. dataset_test = pd.read_csv(filename + '.test', header=None, sep=";")
  41. # default first shuffle of data
  42. dataset_train = shuffle(dataset_train)
  43. dataset_test = shuffle(dataset_test)
  44. # get dataset with equal number of classes occurences
  45. noisy_df_train = dataset_train[dataset_train.iloc[:, 0] == 1]
  46. not_noisy_df_train = dataset_train[dataset_train.iloc[:, 0] == 0]
  47. #nb_noisy_train = len(noisy_df_train.index)
  48. noisy_df_test = dataset_test[dataset_test.iloc[:, 0] == 1]
  49. not_noisy_df_test = dataset_test[dataset_test.iloc[:, 0] == 0]
  50. #nb_noisy_test = len(noisy_df_test.index)
  51. # use of all data
  52. final_df_train = pd.concat([not_noisy_df_train, noisy_df_train])
  53. final_df_test = pd.concat([not_noisy_df_test, noisy_df_test])
  54. # shuffle data another time
  55. final_df_train = shuffle(final_df_train)
  56. final_df_test = shuffle(final_df_test)
  57. # use of the whole data set for training
  58. x_dataset_train = final_df_train.iloc[:,1:]
  59. x_dataset_test = final_df_test.iloc[:,1:]
  60. y_dataset_train = final_df_train.iloc[:,0]
  61. y_dataset_test = final_df_test.iloc[:,0]
  62. return x_dataset_train, y_dataset_train, x_dataset_test, y_dataset_test
  63. def main():
  64. parser = argparse.ArgumentParser(description="Train and find best filters to use for model")
  65. parser.add_argument('--data', type=str, help='dataset filename prefix (without .train and .test)', required=True)
  66. parser.add_argument('--choice', type=str, help='model choice from list of choices', choices=models_list, required=True)
  67. parser.add_argument('--selector', type=str, help='kind of model to use for selecting', choices=['svm', 'tree'], default='tree')
  68. parser.add_argument('--length', type=str, help='max data length (need to be specify for evaluator)', required=True)
  69. parser.add_argument('--output', type=str, help='output name expected for model results', required=True)
  70. args = parser.parse_args()
  71. p_data_file = args.data
  72. p_choice = args.choice
  73. p_selector = args.selector
  74. p_length = args.length
  75. p_output = args.output
  76. print(p_data_file)
  77. # load data from file
  78. x_train, y_train, x_test, y_test = loadDataset(p_data_file)
  79. for i in (np.arange(11) + 5):
  80. model_to_fit = None
  81. # use of svm here to fit well model
  82. if p_selector == 'tree':
  83. model_to_fit = ExtraTreesClassifier(n_estimators=100)
  84. elif p_selector == 'svm':
  85. Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
  86. gammas = [0.001, 0.01, 0.1, 5, 10, 100]
  87. param_grid = {'kernel':['rbf'], 'C': Cs, 'gamma' : gammas}
  88. svc = svm.SVC(probability=True, class_weight='balanced')
  89. #clf = GridSearchCV(svc, param_grid, cv=5, verbose=1, scoring=my_accuracy_scorer, n_jobs=-1)
  90. model_to_fit = GridSearchCV(svc, param_grid, cv=5, verbose=1, scoring='roc_auc', n_jobs=-1)
  91. model = SelectFromModel(model_to_fit, max_features=i)
  92. selector = model.fit(x_train, y_train)
  93. binary_selection = [ 0 if x < selector.threshold_ else 1 for x in selector.estimator_.feature_importances_ ]
  94. X_train_new = selector.transform(x_train)
  95. X_test_new = selector.transform(x_test)
  96. print('Shape for {}, is now {}'.format(i, X_train_new.shape))
  97. svm_model = mdl.get_trained_model(p_choice, X_train_new, y_train)
  98. y_test_model = svm_model.predict(X_test_new)
  99. test_roc_auc = roc_auc_score(y_test, y_test_model)
  100. if not os.path.exists(cfg.output_results_folder):
  101. os.makedirs(cfg.output_results_folder)
  102. # save model results into file
  103. with open(os.path.join(cfg.output_results_folder, p_output), 'a') as f:
  104. line = str(i) + ';'
  105. line += str(test_roc_auc) + ';'
  106. for index, b in enumerate(binary_selection):
  107. line += str(b)
  108. if index < len(binary_selection) - 1:
  109. line += ','
  110. f.write(line + '\n')
  111. # create `logs` folder if necessary
  112. if not os.path.exists(cfg.output_logs_folder):
  113. os.makedirs(cfg.output_logs_folder)
  114. logging.basicConfig(format='%(asctime)s %(message)s', filename='data/logs/%s.log' % p_data_file.split('/')[-1], level=logging.DEBUG)
  115. # init solution (`n` attributes)
  116. if __name__ == "__main__":
  117. main()