algorithm.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from .DataStream import EMchPosmap
  2. import numpy as np
  3. import logging
  4. logger = logging.getLogger('calcer')
  5. def calc_chratios(data, pos_map, start, end):
  6. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  7. all = view[:, start:end]
  8. view = np.cumsum(all, axis=1)
  9. succ = view[0, :]
  10. fail = view[1, :]
  11. commit = succ + fail + 0.0000001
  12. y = succ / commit
  13. y = y.ravel()
  14. return int(view[0, -1]), int(view[0, -1] + view[1, -1]), y
  15. def calc_cov_chratios(data, pos_map, start, end, window, left_len,right_len):
  16. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  17. sum_view = view[:, start + left_len:end - right_len]
  18. sums = np.sum(sum_view, axis=1)
  19. succs = sums[0]
  20. fails = sums[1]
  21. view = view[:, start:end]
  22. succ = view[0, :]
  23. fail = view[1, :]
  24. succ = np.convolve(succ, window, 'same')
  25. fail = np.convolve(fail, window, 'same')
  26. commit = succ + fail + 0.0000001
  27. y = succ / commit
  28. y = y[left_len:end - start - right_len]
  29. return int(succs), int(succs + fails), y
  30. def calc_cov_chsuccs(data, pos_map, start, end, window, left_len,right_len):
  31. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  32. view = view[:, start:end]
  33. succ = view[0, :]
  34. commit = view[1, :]
  35. succ = np.convolve(succ, window, 'same')
  36. commit = np.convolve(commit, window, 'same')
  37. succ = succ[left_len:end - start - right_len]
  38. commit = commit[left_len:end - start - right_len]
  39. return succ, commit
  40. def calc_chspeed(data, pos_map, start, end):
  41. view = data[[pos_map.commit_count], :]
  42. view = view[:, start:end]
  43. speed = np.sum(view, axis=1)
  44. return int(speed[0])
  45. def calc_chratio(data, pos_map, start, end):
  46. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.succ_periods, pos_map.fail_periods, pos_map.commit_count], :]
  47. view = view[:, start:end]
  48. sums = np.sum(view, axis=1)
  49. succs = sums[0]
  50. fails = sums[1]
  51. succ_periods = sums[2]
  52. fail_periods = sums[3]
  53. commit_count = int(sums[4])
  54. all = int(succs + fails)
  55. ratio = succs / (all + 0.00001)
  56. back_time = (succ_periods + fail_periods) / (succs + fails + 0.00001)
  57. return round(ratio, 5), commit_count, round(back_time, 5)
  58. def calc_commit(data, pos_map, start, end):
  59. view = data[[pos_map.commit_count], :]
  60. view = view[:, start:end]
  61. sums = np.sum(view, axis=1)
  62. commit_count = int(sums[0])
  63. return commit_count
  64. def calc_mchratios(data, pos_map, start, end):
  65. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  66. view = view[:, start:end]
  67. all = np.cumsum(view, axis=1)
  68. succ = all[0, :]
  69. commit = all[0, :] + all[1, :]
  70. commit += 0.0000001
  71. y = succ / commit
  72. y = y.ravel()
  73. return int(all[0, -1]), int(all[0, -1] + all[1, -1]), y
  74. def calc_mchratios_val(data, pos_map, start, end):
  75. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  76. view = view[:, start:end]
  77. sums = np.sum(view, axis=1)
  78. succs = sums[0]
  79. fails = sums[1]
  80. ratio = succs / (succs + fails + 0.0000001)
  81. return int(succs), int(succs + fails), round(ratio,5)
  82. def calc_cov_mchratios(data, pos_map, start, end, window, left_len, right_len):
  83. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  84. sum_view = view[:, start + left_len:end - right_len]
  85. sums = np.sum(sum_view, axis=1)
  86. succs = sums[0]
  87. fails = sums[1]
  88. view = view[:, start:end]
  89. succ = view[0, :]
  90. fail = view[1, :]
  91. succ = np.convolve(succ, window, 'same')
  92. fail = np.convolve(fail, window, 'same')
  93. commit = succ + fail + 0.0000001
  94. y = succ / commit
  95. y = y[left_len:end - start - right_len]
  96. return int(succs), int(succs + fails), y
  97. def calc_morder_send(data, pos_map: type(EMchPosmap), start: int, end: int):
  98. view = data[:, start:end]
  99. sums = np.sum(view, axis=1)
  100. all_return = sums[pos_map.succ_mch_amounts] + sums[pos_map.fail_mch_amounts] + 0.0000001
  101. ratio = sums[pos_map.succ_mch_amounts] / all_return
  102. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  103. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  104. lack_amounts = send_amounts * ratio
  105. return send_count, sums[pos_map.submit_count], sums[pos_map.succ_count], sums[pos_map.fail_count], sums[pos_map.submit_amounts], \
  106. sums[pos_map.succ_mch_amounts], sums[pos_map.fail_mch_amounts], send_amounts, lack_amounts
  107. def calc_morder_lack(data, pos_map: type(EMchPosmap), start: int, end: int):
  108. view = data[:, start:end]
  109. sums = np.sum(view, axis=1)
  110. all_return = sums[pos_map.succ_count] + sums[pos_map.fail_count] + 0.0000001
  111. ratio = sums[pos_map.succ_count] / all_return
  112. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  113. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  114. lack_amounts = send_amounts * ratio
  115. logger.info("send_count=%d send_amounts=%.4f ratio=%.4f lack_amounts=%.4f", send_count, send_amounts, ratio, lack_amounts)
  116. return send_amounts, lack_amounts
  117. # 用于计算成功率及利润率
  118. # succ_count, fail_count, succ_ratio, profit,profit_ratio
  119. def calc_mch_profit(data, pos_map: type(EMchPosmap), start: int, end: int):
  120. view = data[:, start:end]
  121. sums = np.sum(view, axis=1)
  122. submit_count = sums[pos_map.submit_count]
  123. succ_count = sums[pos_map.succ_count]
  124. fail_count = sums[pos_map.fail_count]
  125. succ_ratio = succ_count / (succ_count + fail_count + 0.0000001)
  126. ch_amounts = sums[pos_map.succ_ch_amounts]
  127. mch_amounts = sums[pos_map.succ_mch_amounts]
  128. profit = mch_amounts - ch_amounts
  129. return int(submit_count), int(succ_count), int(fail_count), round(succ_ratio, 5), round(profit, 3)