prediction_scene.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. import pandas as pd
  4. from sklearn.metrics import accuracy_score
  5. import sys, os, getopt
  6. output_model_folder = './saved_models/'
  7. def main():
  8. if len(sys.argv) <= 1:
  9. print('Run with default parameters...')
  10. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  11. sys.exit(2)
  12. try:
  13. opts, args = getopt.getopt(sys.argv[1:], "hd:o:s", ["help=", "data=", "model=", "output=", "scene="])
  14. except getopt.GetoptError:
  15. # print help information and exit:
  16. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  17. sys.exit(2)
  18. for o, a in opts:
  19. if o == "-h":
  20. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  21. sys.exit()
  22. elif o in ("-d", "--data"):
  23. p_data_file = a
  24. elif o in ("-m", "--model"):
  25. p_model_file = a
  26. elif o in ("-o", "--output"):
  27. p_output = a
  28. elif o in ("-s", "--scene"):
  29. p_scene = a
  30. else:
  31. assert False, "unhandled option"
  32. if not os.path.exists(output_model_folder):
  33. os.makedirs(output_model_folder)
  34. dataset = pd.read_csv(p_data_file, header=None, sep=";")
  35. y_dataset = dataset.ix[:,0]
  36. x_dataset = dataset.ix[:,1:]
  37. noisy_dataset = dataset[dataset.ix[:, 0] == 1]
  38. not_noisy_dataset = dataset[dataset.ix[:, 0] == 0]
  39. y_noisy_dataset = noisy_dataset.ix[:, 0]
  40. x_noisy_dataset = noisy_dataset.ix[:, 1:]
  41. y_not_noisy_dataset = not_noisy_dataset.ix[:, 0]
  42. x_not_noisy_dataset = not_noisy_dataset.ix[:, 1:]
  43. model = joblib.load(p_model_file)
  44. y_pred = model.predict(x_dataset)
  45. y_noisy_pred = model.predict(x_noisy_dataset)
  46. y_not_noisy_pred = model.predict(x_not_noisy_dataset)
  47. accuracy_global = accuracy_score(y_dataset, y_pred)
  48. accuracy_noisy = accuracy_score(y_noisy_dataset, y_noisy_pred)
  49. accuracy_not_noisy = accuracy_score(y_not_noisy_dataset, y_not_noisy_pred)
  50. if(p_scene):
  51. print(p_scene + " | " + str(accuracy_global) + " | " + str(accuracy_noisy) + " | " + str(accuracy_not_noisy))
  52. else:
  53. print(str(accuracy_global) + " \t | " + str(accuracy_noisy) + " \t | " + str(accuracy_not_noisy))
  54. with open(p_output, 'w') as f:
  55. f.write("Global accuracy found %s " % str(accuracy_global))
  56. f.write("Noisy accuracy found %s " % str(accuracy_noisy))
  57. f.write("Not noisy accuracy found %s " % str(accuracy_not_noisy))
  58. for prediction in y_pred:
  59. f.write(str(prediction) + '\n')
  60. if __name__== "__main__":
  61. main()