train_model.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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('--components', type=int, help='number of components expected by model', default=1)
  30. args = parser.parse_args()
  31. p_data_file = args.data
  32. p_output = args.output
  33. p_choice = args.choice
  34. p_components = args.components
  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. #######################
  65. # 2. Construction of the model : Ensemble model structure
  66. #######################
  67. model = mdl.get_trained_model(p_choice, x_dataset_train, y_dataset_train, p_components)
  68. indices = model.support_
  69. selected_indices = [(i+1) for i in np.arange(len(indices)) if indices[i] == True]
  70. print(selected_indices)
  71. #######################
  72. # 3. Fit model : use of cross validation to fit model
  73. #######################
  74. val_scores = cross_val_score(model, x_dataset_train, y_dataset_train, cv=5)
  75. print("Accuracy: %0.2f (+/- %0.2f)" % (val_scores.mean(), val_scores.std() * 2))
  76. ######################
  77. # 4. Test : Validation and test dataset from .test dataset
  78. ######################
  79. # we need to specify validation size to 20% of whole dataset
  80. val_set_size = int(final_df_train_size/3)
  81. test_set_size = val_set_size
  82. total_validation_size = val_set_size + test_set_size
  83. if final_df_test_size > total_validation_size:
  84. x_dataset_test = x_dataset_test[0:total_validation_size]
  85. y_dataset_test = y_dataset_test[0:total_validation_size]
  86. X_test, X_val, y_test, y_val = train_test_split(x_dataset_test, y_dataset_test, test_size=0.5, random_state=1)
  87. # update data using indices values
  88. X_test = X_test.loc[:, selected_indices]
  89. X_val = X_val.loc[:, selected_indices]
  90. y_test_model = model.estimator_.predict(X_test)
  91. y_val_model = model.estimator_.predict(X_val)
  92. val_accuracy = accuracy_score(y_val, y_val_model)
  93. test_accuracy = accuracy_score(y_test, y_test_model)
  94. val_f1 = f1_score(y_val, y_val_model)
  95. test_f1 = f1_score(y_test, y_test_model)
  96. ###################
  97. # 5. Output : Print and write all information in csv
  98. ###################
  99. print("Validation dataset size ", val_set_size)
  100. print("Validation: ", val_accuracy)
  101. print("Validation F1: ", val_f1)
  102. print("Test dataset size ", test_set_size)
  103. print("Test: ", val_accuracy)
  104. print("Test F1: ", test_f1)
  105. ##################
  106. # 6. Save model : create path if not exists
  107. ##################
  108. if not os.path.exists(saved_models_folder):
  109. os.makedirs(saved_models_folder)
  110. joblib.dump(model, output_model_folder + '/' + p_output + '.joblib')
  111. if __name__== "__main__":
  112. main()