mix_predict_600.py 3.1 KB

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