mix_predict_200.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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),np.array(train_y),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.04
  20. elif fact[1] == 1:
  21. up_right = up_right + 1
  22. elif fact[1] == 2:
  23. up_right = up_right + 0.96
  24. up_error = up_error + 0.3
  25. else:
  26. up_error = up_error + 1
  27. up_right = up_right + 0.90
  28. return up_right,up_error
  29. def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
  30. test_x,test_y,lines=read_data(file_path)
  31. test_x_a = test_x[:,:row*col]
  32. test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
  33. # test_x_b = test_x[:, 9*26:9*26+9*26]
  34. # test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
  35. test_x_c = test_x[:,row*col:]
  36. model=load_model(model_path)
  37. score = model.evaluate([test_x_c, test_x_a,], test_y)
  38. print('MIX', score)
  39. up_num = 0
  40. up_error = 0
  41. up_right = 0
  42. down_num = 0
  43. down_error = 0
  44. down_right = 0
  45. i = 0
  46. result = model.predict([test_x_c, test_x_a])
  47. win_dnn = []
  48. for r in result:
  49. fact = test_y[i]
  50. if idx in [-2]:
  51. if r[0] > 0.5 or r[1] > 0.5:
  52. pass
  53. else:
  54. if r[0] > 0.7:
  55. tmp_right,tmp_error = _score(fact, lines[i])
  56. up_right = tmp_right + up_right
  57. up_error = tmp_error + up_error
  58. up_num = up_num + 1
  59. elif r[2] > 0.6:
  60. if fact[0] == 1:
  61. down_error = down_error + 1
  62. down_right = down_right + 1.1
  63. elif fact[1] == 1:
  64. down_error = down_error + 0.2
  65. down_right = down_right + 0.99
  66. else:
  67. down_right = down_right + 0.88
  68. down_num = down_num + 1
  69. i = i + 1
  70. if up_num == 0:
  71. up_num = 1
  72. if down_num == 0:
  73. down_num = 1
  74. print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
  75. return win_dnn,up_right/up_num,down_right/down_num
  76. if __name__ == '__main__':
  77. # predict(file_path='D:\\data\\quantization\\stock181_18d_test.log', model_path='181_18d_mix_6D_ma5_s_seq.h5')
  78. predict(file_path='D:\\data\\quantization\\stock217_18d_train1.log', model_path='217_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
  79. # predict(file_path='D:\\data\\quantization\\stock321_28d_train1.log', model_path='321_28d_mix_6D_ma5_s_seq.h5', row=28, col=20)
  80. # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
  81. # multi_predict(model='15_18d')
  82. # predict_today(20200229, model='11_18d')