week_predict_100.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- encoding:utf-8 -*-
  2. import numpy as np
  3. from keras.models import load_model
  4. import random
  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, ):
  16. up_right = 0
  17. up_error = 0
  18. if fact[0] == 1:
  19. up_right = up_right + 1.14
  20. elif fact[1] == 1:
  21. up_error = up_error + 0.2
  22. up_right = up_right + 1.08
  23. elif fact[2] == 1:
  24. up_error = up_error + 0.7
  25. up_right = up_right + 0.98
  26. else:
  27. up_error = up_error + 1
  28. up_right = up_right + 0.85
  29. return up_right,up_error
  30. def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1, row=18, col=20):
  31. test_x,test_y,lines=read_data(file_path)
  32. test_x_a = test_x[:,:row*col]
  33. test_x_a = test_x_a.reshape(test_x.shape[0], row, col, 1)
  34. test_x_b = test_x[:, row*col:row*col + 11*13]
  35. test_x_b = test_x_b.reshape(test_x.shape[0],11, 13, 1)
  36. test_x_c = test_x[:,row*col + 11*13:]
  37. model=load_model(model_path)
  38. score = model.evaluate([test_x_c, test_x_a, test_x_b], test_y)
  39. print('MIX', score)
  40. up_num = 0
  41. up_error = 0
  42. up_right = 0
  43. down_num = 0
  44. down_error = 0
  45. down_right = 0
  46. i = 0
  47. result = model.predict([test_x_c, test_x_a, test_x_b])
  48. win_dnn = []
  49. for r in result:
  50. fact = test_y[i]
  51. xx = random.uniform(0, 4)
  52. if idx in [-2]:
  53. if r[0] > 0.5 or r[1] > 0.5:
  54. pass
  55. else:
  56. if r[0] > 0.5:
  57. tmp_right,tmp_error = _score(fact, )
  58. up_right = tmp_right + up_right
  59. up_error = tmp_error + up_error
  60. up_num = up_num + 1
  61. elif r[2] > 0.5 or r[3] > 0.5:
  62. if fact[0] == 1:
  63. down_error = down_error + 1
  64. down_right = down_right + 1.05
  65. elif fact[1] == 1:
  66. down_error = down_error + 0.5
  67. down_right = down_right + 1.02
  68. elif fact[2] == 1:
  69. down_error = down_error + 0.2
  70. down_right = down_right + 0.98
  71. else:
  72. down_right = down_right + 0.95
  73. down_num = down_num + 1
  74. i = i + 1
  75. if up_num == 0:
  76. up_num = 1
  77. if down_num == 0:
  78. down_num = 1
  79. print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
  80. return win_dnn,up_right/up_num,down_right/down_num
  81. def random_predict(file_path=''):
  82. test_x,test_y,lines=read_data(file_path)
  83. a=0
  84. b=0
  85. c=0
  86. d=0
  87. R=0
  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. for x in test_y:
  95. xx = random.uniform(0, 4)
  96. if xx > 3:
  97. if x[0] == 1:
  98. R = R + 1
  99. tmp_right,tmp_error = _score(x,)
  100. up_num = up_num + 1
  101. up_right = up_right + tmp_right
  102. up_error = tmp_error + up_error
  103. elif xx > 2:
  104. if x[1] == 1:
  105. R = R + 1
  106. elif xx > 1:
  107. if x[2] == 1:
  108. R = R + 1
  109. elif xx > 0:
  110. if x[3] == 1:
  111. R = R + 1
  112. if x[0] == 1:
  113. a = a + 1
  114. elif x[1] == 1:
  115. b = b + 1
  116. elif x[2] == 1:
  117. c = c + 1
  118. else:
  119. d = d + 1
  120. if up_num == 0:
  121. up_num = 1
  122. if down_num == 0:
  123. down_num = 1
  124. print(R/(a + b + c +d), up_right/up_num, up_error/up_num)
  125. if __name__ == '__main__':
  126. predict(file_path='D:\\data\\quantization\\week120_18d_test.log', model_path='120_18d_mix_3W_s_seqA.h5', row=18, col=9)
  127. # random_predict(file_path='D:\\data\\quantization\\week118_18d_test.log')