train_model_attributes.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 joblib
  11. import sklearn.svm as svm
  12. from sklearn.utils import shuffle
  13. from sklearn.metrics import accuracy_score, roc_auc_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.output_models
  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 loadDataset(filename, n_step):
  25. ########################
  26. # 1. Get and prepare data
  27. ########################
  28. # scene_name; zone_id; image_index_end; label; data
  29. dataset_train = pd.read_csv(filename + '.train', header=None, sep=";")
  30. dataset_test = pd.read_csv(filename + '.test', header=None, sep=";")
  31. # default first shuffle of data
  32. dataset_train = shuffle(dataset_train)
  33. dataset_test = shuffle(dataset_test)
  34. dataset_train = dataset_train[dataset_train.iloc[:, 2] % n_step == 0]
  35. dataset_test = dataset_test[dataset_test.iloc[:, 2] % n_step == 0]
  36. # get dataset with equal number of classes occurences
  37. noisy_df_train = dataset_train[dataset_train.iloc[:, 3] == 1]
  38. not_noisy_df_train = dataset_train[dataset_train.iloc[:, 3] == 0]
  39. #nb_noisy_train = len(noisy_df_train.index)
  40. noisy_df_test = dataset_test[dataset_test.iloc[:, 3] == 1]
  41. not_noisy_df_test = dataset_test[dataset_test.iloc[:, 3] == 0]
  42. #nb_noisy_test = len(noisy_df_test.index)
  43. # use of all data
  44. final_df_train = pd.concat([not_noisy_df_train, noisy_df_train])
  45. final_df_test = pd.concat([not_noisy_df_test, noisy_df_test])
  46. # shuffle data another time
  47. final_df_train = shuffle(final_df_train)
  48. final_df_test = shuffle(final_df_test)
  49. # use of the whole data set for training
  50. x_dataset_train = final_df_train.iloc[:, 4:]
  51. x_dataset_test = final_df_test.iloc[:, 4:]
  52. y_dataset_train = final_df_train.iloc[:, 3]
  53. y_dataset_test = final_df_test.iloc[:, 3]
  54. return x_dataset_train, y_dataset_train, x_dataset_test, y_dataset_test
  55. def main():
  56. parser = argparse.ArgumentParser(description="Train SKLearn model and save it into .joblib file")
  57. parser.add_argument('--data', type=str, help='dataset filename prefiloc (without .train and .test)', required=True)
  58. parser.add_argument('--output', type=str, help='output file name desired for model (without .joblib extension)', required=True)
  59. parser.add_argument('--choice', type=str, help='model choice from list of choices', choices=models_list, required=True)
  60. parser.add_argument('--step', type=int, help='step number of samples expected', default=20)
  61. parser.add_argument('--solution', type=str, help='Data of solution to specify filters to use')
  62. args = parser.parse_args()
  63. p_data_file = args.data
  64. p_output = args.output
  65. p_step = args.step
  66. p_choice = args.choice
  67. p_solution = list(map(int, args.solution.split(' ')))
  68. if not os.path.exists(output_model_folder):
  69. os.makedirs(output_model_folder)
  70. ########################
  71. # 1. Get and prepare data
  72. ########################
  73. x_dataset_train, y_dataset_train, x_dataset_test, y_dataset_test = loadDataset(p_data_file, p_step)
  74. # get indices of filters data to use (filters selection from solution)
  75. indices = []
  76. print(p_solution)
  77. for index, value in enumerate(p_solution):
  78. if value == 1:
  79. indices.append(index)
  80. print(f'Selected indices are: {indices}')
  81. print(f"Train dataset size {len(x_dataset_train)}")
  82. print(f"Test dataset size {len(x_dataset_test)}")
  83. x_dataset_train = x_dataset_train.iloc[:, indices]
  84. x_dataset_test = x_dataset_test.iloc[:, indices]
  85. print()
  86. return
  87. #######################
  88. # 2. Construction of the model : Ensemble model structure
  89. #######################
  90. print("-------------------------------------------")
  91. model = mdl.get_trained_model(p_choice, x_dataset_train, y_dataset_train)
  92. #######################
  93. # 3. Fit model : use of cross validation to fit model
  94. #######################
  95. val_scores = cross_val_score(model, x_dataset_train, y_dataset_train, cv=5)
  96. print("Accuracy: %0.2f (+/- %0.2f)" % (val_scores.mean(), val_scores.std() * 2))
  97. ######################
  98. # 4. Metrics
  99. ######################
  100. y_train_model = model.predict(x_dataset_train)
  101. y_test_model = model.predict(x_dataset_test)
  102. train_accuracy = accuracy_score(y_dataset_train, y_train_model)
  103. test_accuracy = accuracy_score(y_dataset_test, y_test_model)
  104. train_auc = roc_auc_score(y_dataset_train, y_train_model)
  105. test_auc = roc_auc_score(y_dataset_test, y_test_model)
  106. ###################
  107. # 5. Output : Print and write all information in csv
  108. ###################
  109. print("Train dataset size ", len(x_dataset_train))
  110. print("Train acc: ", train_accuracy)
  111. print("Train AUC: ", train_auc)
  112. print("Test dataset size ", len(x_dataset_test))
  113. print("Test acc: ", test_accuracy)
  114. print("Test AUC: ", test_auc)
  115. ##################
  116. # 6. Save model : create path if not exists
  117. ##################
  118. if not os.path.exists(saved_models_folder):
  119. os.makedirs(saved_models_folder)
  120. joblib.dump(model, output_model_folder + '/' + p_output + '.joblib')
  121. if __name__== "__main__":
  122. main()