用于处理彩票的大数据算法
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.

189 lines
7.8 KiB

import json
from db import *
from util import *
# 获取全部数据
def get_all_data():
return db_history_get_all_data()
def get_all_data_desc():
return db_history_get_all_data('desc')
# 获取所有篮球的统计数据
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_history_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
# 根据蓝球获取全部历史中出现过此蓝球的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):
"""
prv
当_num=3时
先取当前一期的蓝球号码,然后计算历史上相同蓝球出现的后一期蓝球
然后往前推一期,取这一期蓝球号码,统计这个号码后两期的蓝球(不包含后一期)
然后再往前推一期,取这一期蓝球号码,统计这个号码后三期的蓝球(不包含后一期和后两期)
post
当_num=3时
先取当前一期的蓝球,然后从获取历史中蓝球的id
在此id的基础上累加1,2,3,获取到3个id
查询这些id对应蓝球,统计蓝球数据
"""
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
def insert_blue_data(_all_data, _history, _prv, _post, _num):
insert_data = dict()
insert_data['dateId'] = _all_data[-1]['dateId']
insert_data['history'] = _history
insert_data['prv'] = _prv
insert_data['post'] = _post
result = db_blue_insert(insert_data, _num)
if result > 0:
print(f"输入数据成功,插入{result}条数据")
else:
print("数据插入失败")
def get_interval_blue():
blue_dict = {}
_all_data_desc = get_all_data_desc()
_last_blue = _all_data_desc[0].get('blue')
_blue_list = [_data['blue'] for _data in _all_data_desc]
for n in range(len(_blue_list)):
if _blue_list[n] not in blue_dict.keys():
blue_dict[_blue_list[n]] = n
if len(blue_dict.keys()) == 16:
break
# print(dict_sort(blue_dict, 'val', True))
return dict_sort(blue_dict, 'val', True)
def print_blue_data(_history, _prv_five, _post_five, _prv_three, _post_three, _prv_one, _post_one, _interval):
_history_list = list(_history.items())
_prv_five_list = list(_prv_five.items())
_post_five_list = list(_post_five.items())
_prv_three_list = list(_prv_three.items())
_post_three_list = list(_post_three.items())
_prv_one_list = list(_prv_one.items())
_post_one_list = list(_post_one.items())
_interval_list = list(_interval.items())
print('篮球数据表:')
for i in range(16):
print(f"{_history_list[i][0]} [{_history_list[i][1]}]\t"
f"{_prv_one_list[i][0]} [{_prv_one_list[i][1]}]\t"
f"{_prv_three_list[i][0]} [{_prv_three_list[i][1]}]\t"
f" {_prv_five_list[i][0]} [{_prv_five_list[i][1]}]\t"
f"{_post_one_list[i][0]} [{_post_one_list[i][1]}]\t"
f"{_post_three_list[i][0]} [{_post_three_list[i][1]}]\t"
f"{_post_five_list[i][0]} [{_post_five_list[i][1]}]\t"
f"{_interval_list[i][0]} [{_interval_list[i][1]}]")
def forecast_blue():
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)
post_five = get_blue_forecast(get_all_data(), 'post', 5)
prv_three = get_blue_forecast(get_all_data(), 'prv', 3)
post_three = get_blue_forecast(get_all_data(), 'post', 3)
prv_one = get_blue_forecast(get_all_data(), 'prv', 1)
post_one = get_blue_forecast(get_all_data(), 'post', 1)
interval_blue = get_interval_blue()
insert_blue_data(all_data, json.dumps(history_total), json.dumps(prv_five), json.dumps(post_five), 5)
insert_blue_data(all_data, json.dumps(history_total), json.dumps(prv_three), json.dumps(post_three), 3)
insert_blue_data(all_data, json.dumps(history_total), json.dumps(prv_one), json.dumps(post_one), 1)
print_blue_data(history_total, prv_five, post_five, prv_three, post_three, prv_one, post_one, interval_blue)
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)
# post_five = get_blue_forecast(get_all_data(), 'post', 5)
# prv_three = get_blue_forecast(get_all_data(), 'prv', 3)
# post_three = get_blue_forecast(get_all_data(), 'post', 3)
# insert_blue_data(all_data, json.dumps(history_total), json.dumps(prv_five), json.dumps(post_five), 5)
# insert_blue_data(all_data, json.dumps(history_total), json.dumps(prv_three), json.dumps(post_three), 3)
# print_blue_data(history_total, prv_five, post_five, prv_three, post_three)
# get_interval_blue()
pass