lstm_predict.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if line[-2][0].startswith('0') or line[-2][0].startswith('3'):
  11. lines.append(line)
  12. size = len(lines[0])
  13. train_x=[s[:size - 2] for s in lines]
  14. train_y=[s[size-1] for s in lines]
  15. return np.array(train_x),np.array(train_y),lines
  16. def _score(fact, line):
  17. with open('mix_predict_dmi_18d.txt', 'a') as f:
  18. f.write(str([line[-2], line[-1]]) + "\n")
  19. up_right = 0
  20. up_error = 0
  21. if fact[0] == 1:
  22. up_right = up_right + 1.1
  23. elif fact[1] == 1:
  24. up_error = up_error + 0.4
  25. up_right = up_right + 1.03
  26. elif fact[2] == 1:
  27. up_right = up_right + 0.96
  28. up_error = up_error + 0.7
  29. elif fact[3] == 1:
  30. up_right = up_right + 0.9
  31. up_error = up_error + 1
  32. else:
  33. up_error = up_error + 1
  34. up_right = up_right + 0.88
  35. return up_right,up_error
  36. def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
  37. test_x,test_y,lines=read_data(file_path)
  38. print('Load data success')
  39. test_x_a = test_x[:,:6*9]
  40. test_x_a = test_x_a.reshape(test_x.shape[0], 6, 9)
  41. # test_x_b = test_x[:, 18*16:18*16+10*18]
  42. # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 10, 1)
  43. test_x_c = test_x[:,6*9:]
  44. model=load_model(model_path)
  45. score = model.evaluate([test_x_c, test_x_a, ], test_y)
  46. print('MIX', score)
  47. up_num = 0
  48. up_error = 0
  49. up_right = 0
  50. down_num = 0
  51. down_error = 0
  52. down_right = 0
  53. i = 0
  54. result=model.predict([test_x_c, test_x_a, ])
  55. win_dnn = []
  56. for r in result:
  57. fact = test_y[i]
  58. if idx in [-2]:
  59. if r[0] > 0.5 or r[1] > 0.5:
  60. pass
  61. else:
  62. if r[0] > 0.6 or r[1] > 0.6:
  63. tmp_right,tmp_error = _score(fact, lines[i])
  64. up_right = tmp_right + up_right
  65. up_error = tmp_error + up_error
  66. up_num = up_num + 1
  67. elif r[3] > 0.7 or r[2] > 0.7:
  68. if fact[0] == 1:
  69. down_error = down_error + 1
  70. down_right = down_right + 1.12
  71. elif fact[1] == 1:
  72. down_error = down_error + 1
  73. down_right = down_right + 1.06
  74. elif fact[2] == 1:
  75. down_error = down_error + 0.5
  76. down_right = down_right + 1
  77. # elif fact[3] == 1:
  78. # down_right = down_right + 0.94
  79. else:
  80. down_right = down_right + 0.88
  81. down_num = down_num + 1
  82. i = i + 1
  83. if up_num == 0:
  84. up_num = 1
  85. if down_num == 0:
  86. down_num = 1
  87. print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
  88. return win_dnn,up_right/up_num,down_right/down_num
  89. if __name__ == '__main__':
  90. predict(file_path='D:\\data\\quantization\\stock563_12d_train1.log', model_path='563E_lstm_5D_ma5_s_seq.h5')
  91. # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
  92. # multi_predict(model='15_18d')
  93. # predict_today(20200229, model='11_18d')