mix_kmeans_predict_1.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- encoding:utf-8 -*-
  2. import numpy as np
  3. from keras.models import load_model
  4. import joblib
  5. model_path = '160_18d_mix_6D_ma5_s_seq.h5' # 160roc分类 161dmi分类
  6. data_dir = 'D:\\data\\quantization\\macd\\'
  7. args = 'stock160_18d_train2_B'
  8. def read_data(path):
  9. lines = []
  10. with open(path) as f:
  11. for line in f.readlines()[:]:
  12. line = eval(line.strip())
  13. if line[-2][0].startswith('0') or line[-2][0].startswith('3'):
  14. lines.append(line)
  15. size = len(lines[0])
  16. train_x=[s[:size - 2 - 18*3] for s in lines]
  17. train_y=[s[size-1] for s in lines]
  18. return np.array(train_x),np.array(train_y),lines
  19. def _score(fact, line):
  20. with open('mix_predict_dmi_18d.txt', 'a') as f:
  21. f.write(str([line[-2], line[-1]]) + "\n")
  22. up_right = 0
  23. up_error = 0
  24. if fact[0] == 1:
  25. up_right = up_right + 1.12
  26. elif fact[1] == 1:
  27. up_right = up_right + 1.06
  28. elif fact[2] == 1:
  29. up_right = up_right + 1
  30. up_error = up_error + 0.5
  31. elif fact[3] == 1:
  32. up_right = up_right + 0.94
  33. up_error = up_error + 1
  34. else:
  35. up_error = up_error + 1
  36. up_right = up_right + 0.88
  37. return up_right,up_error
  38. def mul_predict(name="10_18d"):
  39. r = 0
  40. p = 0
  41. for x in range(0, 12):
  42. win_dnn, up_ratio,down_ratio = predict(data_dir + name + '_' + str(x) + ".log", x) # stock160_18d_trai_0
  43. r = r + up_ratio
  44. p = p + down_ratio
  45. print(r, p)
  46. # 2,9,10最佳
  47. def predict(file_path='', idx=-1):
  48. test_x,test_y,lines=read_data(file_path)
  49. print(idx, 'Load data success')
  50. test_x_a = test_x[:,:18*24]
  51. test_x_a = test_x_a.reshape(test_x.shape[0], 18, 24, 1)
  52. # test_x_b = test_x[:, 18*16:18*16+10*18]
  53. # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 10, 1)
  54. test_x_c = test_x[:,18*24:]
  55. model=load_model(model_path.split(".")[0] + '.h5')
  56. score = model.evaluate([test_x_c, test_x_a, ], test_y)
  57. print('MIX', score)
  58. up_num = 0
  59. up_error = 0
  60. up_right = 0
  61. down_num = 0
  62. down_error = 0
  63. down_right = 0
  64. i = 0
  65. result=model.predict([test_x_c, test_x_a, ])
  66. win_dnn = []
  67. for r in result:
  68. fact = test_y[i]
  69. if idx in [-2]:
  70. if r[0] > 0.5 or r[1] > 0.5:
  71. pass
  72. else:
  73. if r[0] > 0.6 or r[1] > 0.6:
  74. tmp_right,tmp_error = _score(fact, lines[i])
  75. up_right = tmp_right + up_right
  76. up_error = tmp_error + up_error
  77. up_num = up_num + 1
  78. elif r[3] > 0.7 or r[4] > 0.7:
  79. if fact[0] == 1:
  80. down_error = down_error + 1
  81. down_right = down_right + 1.12
  82. elif fact[1] == 1:
  83. down_error = down_error + 1
  84. down_right = down_right + 1.06
  85. elif fact[2] == 1:
  86. down_error = down_error + 0.5
  87. down_right = down_right + 1
  88. elif fact[3] == 1:
  89. down_right = down_right + 0.94
  90. else:
  91. down_right = down_right + 0.88
  92. down_num = down_num + 1
  93. i = i + 1
  94. if up_num == 0:
  95. up_num = 1
  96. if down_num == 0:
  97. down_num = 1
  98. print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
  99. return win_dnn,up_right/up_num,down_right/down_num
  100. if __name__ == '__main__':
  101. # predict(file_path='D:\\data\\quantization\\stock160_18d_10D_test.log', model_path='160_18d_lstm_5D_ma5_s_seq.h5')
  102. # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
  103. mul_predict(args)
  104. # predict_today(20200229, model='11_18d')