find_best_attributes.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. # modules and config imports
  20. sys.path.insert(0, '') # trick to enable import of main folder module
  21. import custom_config as cfg
  22. import models as mdl
  23. from optimization.algorithms.IteratedLocalSearch import IteratedLocalSearch as ILS
  24. from optimization.solutions.BinarySolution import BinarySolution
  25. from optimization.operators.mutators.SimpleMutation import SimpleMutation
  26. from optimization.operators.mutators.SimpleBinaryMutation import SimpleBinaryMutation
  27. from optimization.operators.crossovers.SimpleCrossover import SimpleCrossover
  28. from optimization.operators.policies.RandomPolicy import RandomPolicy
  29. from optimization.checkpoints.BasicCheckpoint import BasicCheckpoint
  30. # variables and parameters
  31. models_list = cfg.models_names_list
  32. number_of_values = 26
  33. ils_iteration = 10
  34. ls_iteration = 5
  35. # default validator
  36. def validator(solution):
  37. if list(solution.data).count(1) < 5:
  38. return False
  39. return True
  40. # init solution (26 attributes)
  41. def init():
  42. return BinarySolution([], 26).random(validator)
  43. def loadDataset(filename):
  44. ########################
  45. # 1. Get and prepare data
  46. ########################
  47. dataset_train = pd.read_csv(filename + '.train', header=None, sep=";")
  48. dataset_test = pd.read_csv(filename + '.test', header=None, sep=";")
  49. # default first shuffle of data
  50. dataset_train = shuffle(dataset_train)
  51. dataset_test = shuffle(dataset_test)
  52. # get dataset with equal number of classes occurences
  53. noisy_df_train = dataset_train[dataset_train.iloc[:, 0] == 1]
  54. not_noisy_df_train = dataset_train[dataset_train.iloc[:, 0] == 0]
  55. #nb_noisy_train = len(noisy_df_train.index)
  56. noisy_df_test = dataset_test[dataset_test.iloc[:, 0] == 1]
  57. not_noisy_df_test = dataset_test[dataset_test.iloc[:, 0] == 0]
  58. #nb_noisy_test = len(noisy_df_test.index)
  59. # use of all data
  60. final_df_train = pd.concat([not_noisy_df_train, noisy_df_train])
  61. final_df_test = pd.concat([not_noisy_df_test, noisy_df_test])
  62. # shuffle data another time
  63. final_df_train = shuffle(final_df_train)
  64. final_df_test = shuffle(final_df_test)
  65. # use of the whole data set for training
  66. x_dataset_train = final_df_train.iloc[:,1:]
  67. x_dataset_test = final_df_test.iloc[:,1:]
  68. y_dataset_train = final_df_train.iloc[:,0]
  69. y_dataset_test = final_df_test.iloc[:,0]
  70. return x_dataset_train, y_dataset_train, x_dataset_test, y_dataset_test
  71. def main():
  72. parser = argparse.ArgumentParser(description="Train and find best filters to use for model")
  73. parser.add_argument('--data', type=str, help='dataset filename prefix (without .train and .test)', required=True)
  74. parser.add_argument('--choice', type=str, help='model choice from list of choices', choices=models_list, required=True)
  75. parser.add_argument('--length', type=str, help='max data length (need to be specify for evaluator)', required=True)
  76. args = parser.parse_args()
  77. p_data_file = args.data
  78. p_choice = args.choice
  79. p_length = args.length
  80. global number_of_values
  81. number_of_values = p_length
  82. print(p_data_file)
  83. # load data from file
  84. x_train, y_train, x_test, y_test = loadDataset(p_data_file)
  85. # create `logs` folder if necessary
  86. if not os.path.exists(cfg.output_logs_folder):
  87. os.makedirs(cfg.output_logs_folder)
  88. logging.basicConfig(format='%(asctime)s %(message)s', filename='data/logs/%s.log' % p_data_file.split('/')[-1], level=logging.DEBUG)
  89. # define evaluate function here (need of data information)
  90. def evaluate(solution):
  91. start = datetime.datetime.now()
  92. # get indices of filters data to use (filters selection from solution)
  93. indices = []
  94. for index, value in enumerate(solution.data):
  95. if value == 1:
  96. indices.append(index)
  97. # keep only selected filters from solution
  98. x_train_filters = x_train.iloc[:, indices]
  99. y_train_filters = y_train
  100. x_test_filters = x_test.iloc[:, indices]
  101. # TODO : use of GPU implementation of SVM
  102. model = mdl.get_trained_model(p_choice, x_train_filters, y_train_filters)
  103. y_test_model = model.predict(x_test_filters)
  104. test_roc_auc = roc_auc_score(y_test, y_test_model)
  105. end = datetime.datetime.now()
  106. diff = end - start
  107. print("Evaluation took :", divmod(diff.days * 86400 + diff.seconds, 60))
  108. return test_roc_auc
  109. if not os.path.exists(cfg.output_backup_folder):
  110. os.makedirs(cfg.output_backup_folder)
  111. backup_file_path = os.path.join(cfg.output_backup_folder, p_data_file.split('/')[-1] + '.csv')
  112. # prepare optimization algorithm
  113. updators = [SimpleBinaryMutation(), SimpleMutation(), SimpleCrossover()]
  114. policy = RandomPolicy(updators)
  115. algo = ILS(init, evaluate, updators, policy, validator, True)
  116. algo.addCheckpoint(_class=BasicCheckpoint, _every=1, _filepath=backup_file_path)
  117. bestSol = algo.run(ils_iteration, ls_iteration)
  118. # print best solution found
  119. print("Found ", bestSol)
  120. # save model information into .csv file
  121. if not os.path.exists(cfg.results_information_folder):
  122. os.makedirs(cfg.results_information_folder)
  123. filename_path = os.path.join(cfg.results_information_folder, cfg.optimization_attributes_result_filename)
  124. filters_counter = 0
  125. # count number of filters
  126. for index, item in enumerate(bestSol.data):
  127. if index != 0 and index % 2 == 1:
  128. # if two attributes are used
  129. if item == 1 or bestSol.data[index - 1] == 1:
  130. filters_counter += 1
  131. line_info = p_data_file + ';' + str(ils_iteration) + ';' + str(ls_iteration) + ';' + str(bestSol.data) + ';' + str(list(bestSol.data).count(1)) + ';' + str(filters_counter) + ';' + str(bestSol.fitness())
  132. with open(filename_path, 'a') as f:
  133. f.write(line_info + '\n')
  134. print('Result saved into %s' % filename_path)
  135. if __name__ == "__main__":
  136. main()