用于处理彩票的大数据算法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.1 KiB

from db import *
from util import *
# 获取全部数据
def get_all_data():
return db_get_all_data()
# 获取所有篮球的统计数据
def get_all_blue(_all_data):
all_blue = dict()
if _all_data is not False:
for data in _all_data:
if data['blue'] in all_blue.keys():
all_blue[data['blue']] += 1
else:
all_blue[data['blue']] = 1
return dict_sort(all_blue)
else:
return False
# 获取蓝球统计次数与平均值的额偏差
def get_delta_blue(_blue_data):
return dict_sort(get_analysis_data(_blue_data), 'val', True)
# 获取全部向前追溯的蓝球数据列表
def get_all_prv_blue_list(_all_data, _last_blue_list, _num):
all_prv_blue_list = []
min_id, max_id = 1, _all_data[-1]['id'] # 获取最大和最小id
for n in range(len(_last_blue_list)):
# print(_last_blue_list[n])
for _data in _all_data:
if _data['blue'] == _last_blue_list[n]:
_id = _data['id'] + _num - n
if min_id <= _id <= max_id:
# print(f"{_data['id']} - {_id}")
all_prv_blue_list.append(_id)
return sorted(set(all_prv_blue_list))
# 获取全部向后预测的历史id列表
def get_all_post_id_list(_all_data, _blue_id_list, _num):
post_id_list = []
min_id, max_id = 1, _all_data[-1]['id'] # 获取最大和最小id
for _id in _blue_id_list:
for n in range(1, _num + 1):
_post_id = _id + n
if min_id <= _post_id <= max_id:
post_id_list.append(_post_id)
return post_id_list
# 获取蓝球统计数据
def get_blue_statistics(_prv_blue_id_list):
_blue_data = db_get_data_in_ids(_prv_blue_id_list)
_blue_dict = dict()
for _data in _blue_data:
if _data['blue'] in _blue_dict.keys():
_blue_dict[_data['blue']] += 1
else:
_blue_dict[_data['blue']] = 1
return _blue_dict
# 获取分析后的差异数据
def get_analysis_data(_raw_blue_data):
total = 0
for _num in _raw_blue_data.values():
total += _num
average = total / len(_raw_blue_data.keys())
_delta_blue_dict = dict()
for _key, _val in _raw_blue_data.items():
_delta_blue_dict[_key] = round(_raw_blue_data[_key] - average, 2)
return _delta_blue_dict
# 根据蓝球获取全部历史中出现过此蓝球的id列表
def get_blue_id_list_by_blue(_all_data, _last_blue):
blue_id_list = []
for _data in _all_data:
if _data['blue'] == _last_blue:
blue_id_list.append(_data['id'])
return blue_id_list
# 获取预测的数据
def get_blue_forecast(_all_data, _method, _num):
# last_blue = db_get_last_data()['blue']
# if last_blue is False:
# return False
# blue_id_list = []
# for _data in _all_data:
# if _data['blue'] == last_blue:
# blue_id_list.append(_data['id'])
# # print(blue_id_list)
# min_id, max_id = 1, _all_data[-1]['id'] # 获取最大和最小id
"""
当_num=3时
先取当前一期的蓝球号码,然后计算历史上相同蓝球出现的后一期蓝球
然后往前推一期,取这一期蓝球号码,统计这个号码后两期的蓝球(不包含后一期)
然后再往前推一期,取这一期蓝球号码,统计这个号码后三期的蓝球(不包含后一期和后两期)
"""
if _method == 'prv': # 当前一期往前推num期,包含此期数据
recent_blue_id_list = [d['blue'] for d in _all_data[-_num:]]
prv_blue_id_list = get_all_prv_blue_list(_all_data, recent_blue_id_list, _num) # 根据蓝球出现的历史记录查询出向前推算的蓝球对应数据id号
_raw_blue_data = get_blue_statistics(prv_blue_id_list) # 根据上步查出的id列表,统计出蓝球出现的次数
_result = get_analysis_data(_raw_blue_data) # 根据上步蓝球的出现次数统计,计算偏差值
return dict_sort(_result, 'val', True)
elif _method == 'post': # 当前一期往后num期,不包含此期的数据
last_blue = _all_data[-1]['blue'] # 获取最后一期蓝球号码
blue_id_list = get_blue_id_list_by_blue(_all_data, last_blue) # 获取所以蓝球等于最后一期蓝球的期数id
post_blue_id_list = get_all_post_id_list(_all_data, blue_id_list, _num)
_raw_blue_data = get_blue_statistics(post_blue_id_list) # 根据上步查出的id列表,统计出蓝球出现的次数
_result = get_analysis_data(_raw_blue_data) # 根据上步蓝球的出现次数统计,计算偏差值
return dict_sort(_result, 'val', True)
else:
return False
if __name__ == "__main__":
all_data = get_all_data()
history_total = get_delta_blue(get_all_blue(all_data))
prv_five = get_blue_forecast(get_all_data(), 'prv', 5)
prv_three = get_blue_forecast(get_all_data(), 'prv', 3)
post_five = get_blue_forecast(get_all_data(), 'post', 5)
post_three = get_blue_forecast(get_all_data(), 'post', 3)
print(history_total)
print(prv_five)
print(prv_three)
print(post_five)
print(post_three)