mix_predict_600.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # -*- encoding:utf-8 -*-
  2. import numpy as np
  3. from keras.models import load_model
  4. import joblib
  5. import random
  6. def read_data(path):
  7. lines = []
  8. with open(path) as f:
  9. for line in f.readlines()[:]:
  10. line = eval(line.strip())
  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, dtype=np.float32),np.array(train_y, dtype=np.float32),lines
  16. def _score(fact,):
  17. up_right = 0
  18. up_error = 0
  19. # if fact[0] == 1:
  20. # up_right = up_right + 1.06
  21. # elif fact[1] == 1:
  22. # up_error = up_error + 0.5
  23. # up_right = up_right + 0.98
  24. # else:
  25. # up_error = up_error + 1
  26. # up_right = up_right + 0.93
  27. if fact[0] == 1:
  28. up_right = up_right + 1.1
  29. elif fact[1] == 1:
  30. up_error = up_error + 0.4
  31. up_right = up_right + 1.03
  32. else:
  33. up_error = up_error + 1
  34. up_right = up_right + 0.9
  35. return up_right,up_error
  36. def random_predict(file_path=''):
  37. test_x,test_y,lines=read_data(file_path)
  38. a=0
  39. b=0
  40. c=0
  41. d=0
  42. R=0
  43. up_num = 0
  44. up_error = 0
  45. up_right = 0
  46. down_num = 0
  47. down_error = 0
  48. down_right = 0
  49. score = 0
  50. for x in test_y:
  51. xx = random.uniform(0, 3)
  52. if xx > 2:
  53. if x[0] == 1:
  54. R = R + 1
  55. if x[0] == 1:
  56. up_right = up_right + 1
  57. score = 3 + score
  58. elif x[1] == 1:
  59. score = 2 + score
  60. else:
  61. score = 1 + score
  62. up_num = up_num + 1
  63. elif xx > 1 and x[1] == 1:
  64. R = R + 1
  65. elif xx > 0 and x[2] == 1:
  66. R = R + 1
  67. if x[0] == 1:
  68. a = a + 1
  69. elif x[1] == 1:
  70. b = b + 1
  71. elif x[2] == 1:
  72. c = c + 1
  73. if up_num == 0:
  74. up_num = 1
  75. if down_num == 0:
  76. down_num = 1
  77. print(R/(a + b + c +d), score/up_num, up_right/up_num)
  78. def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
  79. test_x,test_y,lines=read_data(file_path)
  80. test_x_a = test_x[:,:row*col]
  81. test_x_a = test_x_a.reshape(test_x.shape[0], 1, row, col)
  82. # test_x_b = test_x[:, row*col:row*col+18*2]
  83. # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
  84. test_x_c = test_x[:,row*col:]
  85. model=load_model(model_path)
  86. score = model.evaluate([test_x_c, test_x_a, ], test_y)
  87. print('MIX', score)
  88. up_num = 0
  89. up_error = 0
  90. up_right = 0
  91. down_num = 0
  92. down_error = 0
  93. down_right = 0
  94. i = 0
  95. result = model.predict([test_x_c, test_x_a, ])
  96. win_dnn = []
  97. for r in result:
  98. fact = test_y[i]
  99. xx = random.uniform(0, 3)
  100. if idx in [-2]:
  101. if r[0] > 0.5 or r[1] > 0.5:
  102. pass
  103. else:
  104. if r[0] > 0.7:
  105. # if xx > 2:
  106. tmp_right,tmp_error = _score(fact,)
  107. up_right = tmp_right + up_right
  108. up_error = tmp_error + up_error
  109. up_num = up_num + 1
  110. elif r[2] > 0.5:
  111. # elif xx < 2:
  112. if fact[0] == 1:
  113. down_error = down_error + 1
  114. down_right = down_right + 1.04
  115. elif fact[1] == 1:
  116. down_error = down_error + 0.5
  117. down_right = down_right + 0.98
  118. else:
  119. down_right = down_right + 0.92
  120. down_num = down_num + 1
  121. i = i + 1
  122. if up_num == 0:
  123. up_num = 1
  124. if down_num == 0:
  125. down_num = 1
  126. print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
  127. return win_dnn,up_right/up_num,down_right/down_num
  128. if __name__ == '__main__':
  129. # predict(file_path='D:\\data\\quantization\\stock181_18d_test.log', model_path='181_18d_mix_6D_ma5_s_seq.h5')
  130. # predict(file_path='D:\\data\\quantization\\stock217_18d_train1.log', model_path='218_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
  131. # predict(file_path='D:\\data\\quantization\\stock400_18d_train1.log', model_path='400_18d_mix_5D_ma5_s_seq.h5', row=18, col=18)
  132. # predict(file_path='D:\\data\\quantization\\stock571_12d_train1.log', model_path='570_5d_mix_5D_ma5_s_seq.h5', row=5, col=31)
  133. # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
  134. # multi_predict(model='15_18d')
  135. random_predict(file_path='D:\\data\\quantization\\stock578N_12d_train1.log')