train_model_filters.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # main imports
  2. import numpy as np
  3. import pandas as pd
  4. import sys, os, argparse
  5. # models imports
  6. from sklearn.model_selection import train_test_split
  7. from sklearn.model_selection import GridSearchCV
  8. from sklearn.linear_model import LogisticRegression
  9. from sklearn.ensemble import RandomForestClassifier, VotingClassifier
  10. import sklearn.svm as svm
  11. from sklearn.utils import shuffle
  12. from sklearn.externals import joblib
  13. from sklearn.metrics import accuracy_score, f1_score
  14. from sklearn.model_selection import cross_val_score
  15. # modules and config imports
  16. sys.path.insert(0, '') # trick to enable import of main folder module
  17. import custom_config as cfg
  18. import models as mdl
  19. # variables and parameters
  20. saved_models_folder = cfg.saved_models_folder
  21. models_list = cfg.models_names_list
  22. current_dirpath = os.getcwd()
  23. output_model_folder = os.path.join(current_dirpath, saved_models_folder)
  24. def main():
  25. parser = argparse.ArgumentParser(description="Train SKLearn model and save it into .joblib file")
  26. parser.add_argument('--data', type=str, help='dataset filename prefix (without .train and .test)')
  27. parser.add_argument('--output', type=str, help='output file name desired for model (without .joblib extension)')
  28. parser.add_argument('--choice', type=str, help='model choice from list of choices', choices=models_list)
  29. parser.add_argument('--solution', type=str, help='Data of solution to specify filters to use')
  30. args = parser.parse_args()
  31. p_data_file = args.data
  32. p_output = args.output
  33. p_choice = args.choice
  34. p_solution = list(map(int, args.solution.split(' ')))
  35. if not os.path.exists(output_model_folder):
  36. os.makedirs(output_model_folder)
  37. ########################
  38. # 1. Get and prepare data
  39. ########################
  40. dataset_train = pd.read_csv(p_data_file + '.train', header=None, sep=";")
  41. dataset_test = pd.read_csv(p_data_file + '.test', header=None, sep=";")
  42. # default first shuffle of data
  43. dataset_train = shuffle(dataset_train)
  44. dataset_test = shuffle(dataset_test)
  45. # get dataset with equal number of classes occurences
  46. noisy_df_train = dataset_train[dataset_train.ix[:, 0] == 1]
  47. not_noisy_df_train = dataset_train[dataset_train.ix[:, 0] == 0]
  48. nb_noisy_train = len(noisy_df_train.index)
  49. noisy_df_test = dataset_test[dataset_test.ix[:, 0] == 1]
  50. not_noisy_df_test = dataset_test[dataset_test.ix[:, 0] == 0]
  51. nb_noisy_test = len(noisy_df_test.index)
  52. final_df_train = pd.concat([not_noisy_df_train[0:nb_noisy_train], noisy_df_train])
  53. final_df_test = pd.concat([not_noisy_df_test[0:nb_noisy_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. final_df_train_size = len(final_df_train.index)
  58. final_df_test_size = len(final_df_test.index)
  59. # use of the whole data set for training
  60. x_dataset_train = final_df_train.ix[:,1:]
  61. x_dataset_test = final_df_test.ix[:,1:]
  62. y_dataset_train = final_df_train.ix[:,0]
  63. y_dataset_test = final_df_test.ix[:,0]
  64. # get indices of filters data to use (filters selection from solution)
  65. indices = []
  66. print(p_solution)
  67. for index, value in enumerate(p_solution):
  68. if value == 1:
  69. indices.append(index*2)
  70. indices.append(index*2+1)
  71. print(indices)
  72. x_dataset_train = x_dataset_train.iloc[:, indices]
  73. x_dataset_test = x_dataset_test.iloc[:, indices]
  74. #######################
  75. # 2. Construction of the model : Ensemble model structure
  76. #######################
  77. print("-------------------------------------------")
  78. print("Train dataset size: ", final_df_train_size)
  79. model = mdl.get_trained_model(p_choice, x_dataset_train, y_dataset_train)
  80. #######################
  81. # 3. Fit model : use of cross validation to fit model
  82. #######################
  83. val_scores = cross_val_score(model, x_dataset_train, y_dataset_train, cv=5)
  84. print("Accuracy: %0.2f (+/- %0.2f)" % (val_scores.mean(), val_scores.std() * 2))
  85. ######################
  86. # 4. Test : Validation and test dataset from .test dataset
  87. ######################
  88. # we need to specify validation size to 20% of whole dataset
  89. val_set_size = int(final_df_train_size/3)
  90. test_set_size = val_set_size
  91. total_validation_size = val_set_size + test_set_size
  92. if final_df_test_size > total_validation_size:
  93. x_dataset_test = x_dataset_test[0:total_validation_size]
  94. y_dataset_test = y_dataset_test[0:total_validation_size]
  95. X_test, X_val, y_test, y_val = train_test_split(x_dataset_test, y_dataset_test, test_size=0.5, random_state=1)
  96. y_test_model = model.predict(X_test)
  97. y_val_model = model.predict(X_val)
  98. val_accuracy = accuracy_score(y_val, y_val_model)
  99. test_accuracy = accuracy_score(y_test, y_test_model)
  100. val_f1 = f1_score(y_val, y_val_model)
  101. test_f1 = f1_score(y_test, y_test_model)
  102. ###################
  103. # 5. Output : Print and write all information in csv
  104. ###################
  105. print("Validation dataset size ", val_set_size)
  106. print("Validation: ", val_accuracy)
  107. print("Validation F1: ", val_f1)
  108. print("Test dataset size ", test_set_size)
  109. print("Test: ", val_accuracy)
  110. print("Test F1: ", test_f1)
  111. ##################
  112. # 6. Save model : create path if not exists
  113. ##################
  114. if not os.path.exists(saved_models_folder):
  115. os.makedirs(saved_models_folder)
  116. joblib.dump(model, output_model_folder + '/' + p_output + '.joblib')
  117. if __name__== "__main__":
  118. main()