industry_predict_100.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- encoding:utf-8 -*-
  2. import numpy as np
  3. from keras.models import load_model
  4. import joblib
  5. def read_data(path):
  6. lines = []
  7. with open(path) as f:
  8. for line in f.readlines()[:]:
  9. line = eval(line.strip())
  10. lines.append(line)
  11. size = len(lines[0])
  12. train_x=[s[:size - 2] for s in lines]
  13. train_y=[s[size-1] for s in lines]
  14. return np.array(train_x, dtype=np.float32),np.array(train_y, dtype=np.float32),lines
  15. def _score(fact, line):
  16. up_right = 0
  17. up_error = 0
  18. if fact[0] == 1:
  19. up_right = up_right + 1.05
  20. elif fact[1] == 1:
  21. up_error = up_error + 0.3
  22. up_right = up_right + 1.02
  23. elif fact[2] == 1:
  24. up_error = up_error + 0.6
  25. up_right = up_right + 0.98
  26. else:
  27. up_error = up_error + 1
  28. up_right = up_right + 0.95
  29. return up_right,up_error
  30. def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
  31. test_x,test_y,lines=read_data(file_path)
  32. test_x_a = test_x[:,:row*col]
  33. test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
  34. # test_x_b = test_x[:, row*col:row*col+18*2]
  35. # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
  36. test_x_c = test_x[:,row*col:]
  37. model=load_model(model_path)
  38. score = model.evaluate([test_x_c, test_x_a, ], test_y)
  39. print('MIX', score)
  40. up_num = 0
  41. up_error = 0
  42. up_right = 0
  43. down_num = 0
  44. down_error = 0
  45. down_right = 0
  46. i = 0
  47. result = model.predict([test_x_c, test_x_a, ])
  48. win_dnn = []
  49. for r in result:
  50. fact = test_y[i]
  51. if idx in [-2]:
  52. if r[0] > 0.5 or r[1] > 0.5:
  53. pass
  54. else:
  55. if r[0] > 0.5 or r[1] > 0.5:
  56. tmp_right,tmp_error = _score(fact, lines[i])
  57. up_right = tmp_right + up_right
  58. up_error = tmp_error + up_error
  59. up_num = up_num + 1
  60. elif r[2] > 0.5 or r[3] > 0.5:
  61. if fact[0] == 1:
  62. down_error = down_error + 1
  63. down_right = down_right + 1.05
  64. elif fact[1] == 1:
  65. down_error = down_error + 0.5
  66. down_right = down_right + 1.02
  67. elif fact[2] == 1:
  68. down_right = down_right + 0.98
  69. else:
  70. down_right = down_right + 0.95
  71. down_num = down_num + 1
  72. i = i + 1
  73. if up_num == 0:
  74. up_num = 1
  75. if down_num == 0:
  76. down_num = 1
  77. print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
  78. return win_dnn,up_right/up_num,down_right/down_num
  79. if __name__ == '__main__':
  80. predict(file_path='D:\\data\\quantization\\industry\\stock13_10d_3D_train3.log', model_path='107_10d_mix_3D_s_seq.h5', row=10, col=8)