mix_predict_518.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.1
  21. elif fact[1] == 1:
  22. up_error = up_error + 0.4
  23. up_right = up_right + 1.03
  24. elif fact[2] == 1:
  25. up_error = up_error + 0.7
  26. up_right = up_right + 0.96
  27. else:
  28. up_error = up_error + 1
  29. up_right = up_right + 0.9
  30. return up_right,up_error
  31. def random_predict(file_path=''):
  32. test_x,test_y,lines=read_data(file_path)
  33. a=0
  34. b=0
  35. c=0
  36. d=0
  37. R=0
  38. up_num = 0
  39. up_error = 0
  40. up_right = 0
  41. down_num = 0
  42. down_error = 0
  43. down_right = 0
  44. for x in test_y:
  45. xx = random.uniform(0, 4)
  46. if xx > 3:
  47. if x[0] == 1:
  48. R = R + 1
  49. tmp_right,tmp_error = _score(x,)
  50. up_num = up_num + 1
  51. up_right = up_right + tmp_right
  52. up_error = tmp_error + up_error
  53. elif xx > 2:
  54. if x[1] == 1:
  55. R = R + 1
  56. elif xx > 1:
  57. if x[2] == 1:
  58. R = R + 1
  59. elif xx > 0:
  60. if x[3] == 1:
  61. R = R + 1
  62. if x[0] == 1:
  63. a = a + 1
  64. elif x[1] == 1:
  65. b = b + 1
  66. elif x[2] == 1:
  67. c = c + 1
  68. else:
  69. d = d + 1
  70. if up_num == 0:
  71. up_num = 1
  72. if down_num == 0:
  73. down_num = 1
  74. print(R/(a + b + c +d), up_right/up_num, up_error/up_num)
  75. def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
  76. test_x,test_y,lines=read_data(file_path)
  77. test_x_a = test_x[:,:row*col]
  78. test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
  79. # test_x_b = test_x[:, 18*col:row*col]
  80. # test_x_b = test_x_b.reshape(test_x.shape[0], 10, col, 1)
  81. test_x_c = test_x[:,row*col:]
  82. model=load_model(model_path)
  83. score = model.evaluate([test_x_c, test_x_a, ], test_y)
  84. print('MIX', score)
  85. up_num = 0
  86. up_error = 0
  87. up_right = 0
  88. down_num = 0
  89. down_error = 0
  90. down_right = 0
  91. i = 0
  92. result = model.predict([test_x_c, test_x_a, ])
  93. win_dnn = []
  94. for r in result:
  95. fact = test_y[i]
  96. xx = random.uniform(0, 4)
  97. if idx in [-2]:
  98. if r[0] > 0.5 or r[1] > 0.5:
  99. pass
  100. else:
  101. if r[1] > 0.6 :
  102. tmp_right,tmp_error = _score(fact,)
  103. up_right = tmp_right + up_right
  104. up_error = tmp_error + up_error
  105. up_num = up_num + 1
  106. elif r[2] > 0.5 or r[3] > 0.5:
  107. if fact[0] == 1:
  108. down_error = down_error + 1
  109. down_right = down_right + 1.1
  110. elif fact[1] == 1:
  111. down_error = down_error + 0.7
  112. down_right = down_right + 1.04
  113. elif fact[2] == 1:
  114. down_error = down_error + 0.3
  115. down_right = down_right + 0.94
  116. else:
  117. down_right = down_right + 0.9
  118. down_num = down_num + 1
  119. i = i + 1
  120. if up_num == 0:
  121. up_num = 1
  122. if down_num == 0:
  123. down_num = 1
  124. print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
  125. return win_dnn,up_right/up_num,down_right/down_num
  126. if __name__ == '__main__':
  127. xx = random.uniform(0,4)
  128. # predict(file_path='D:\\data\\quantization\\stock513_28d_train1.log', model_path='513_28d_mix_3D_ma5_s_seq.h5', row=28, col=16)
  129. predict(file_path='D:\\data\\quantization\\stock538_28d_train1.log', model_path='539_28d_mix_5D_ma5_s_seq.h5', row=28, col=17)
  130. # random_predict(file_path='D:\\data\\quantization\\week101_18d_test.log')
  131. # random_predict(file_path='D:\\data\\quantization\\stock537_28d_train1.log')