def estimate(self, deal, current_date, history_length): """ 使用当天deal历史销量拟合直线估计当天销量 """ daily_volumes = filter(lambda x:x[1] >= 0, deal['all_daily_volumes'].items()) daily_volumes.sort(key = lambda x:x[0]) earlist_date = current_date - datetime.timedelta(days = history_length) history_records = [item for item in daily_volumes if str(earlist_date) <= item[0] < str(current_date)] #logging.debug("DAILY_VOLUMES: %s; EARLY: %s; RECORD: %s" % (daily_volumes, earlist_date, history_records)) if len(history_records) < 2: # 想画条线至少需要两个点 return None xs = [(datetime.datetime.strptime(item[0], '%Y-%m-%d').date() - earlist_date).days for item in history_records] ys = [item[1] for item in history_records] x = (current_date - earlist_date).days linear_fit_volume = max(Utils.get_linear_estimate(xs, ys, x), 1) #logging.debug("X: %s, Y: %s, x: %s, fit: %s" % (xs, ys, x, linear_fit_volume)) return linear_fit_volume