正好手上有2003年-2024年所有双色球中奖的记录,用这20几年的数据分析,然后来预测,应该有一定的参考性。
声明:本文主要目的为编程学习,数据仅用来作为统计之用,且数据来源公开合法。本文无任何导向。个人看法仅供参考。
1,准备数据。
首先,我们需要采集公开的双色球中奖记录,我这里采集了2003-2025年所有的双色球记录,记录如下:
接下来,需要根据这些数据预处理。
因为数据比较完善,所以基本上都是正确的。
2,建立预测模型
将所有的数据分成训练数据和测试数据。
代码如下:
import pandas as pd
import matplotlib.pyplot as plt
from IPython.core.display import HTML
# 设置 Matplotlib 使用的中文字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
plt.rcParams['axes.unicode_minus'] = False # 支持负号显示
# 读取数据
df = pd.read_csv('lottery_results_2003_2024.csv', encoding='gbk')
def collect_next_numbers(red_balls, blue_drum):
next_red_numbers = []
next_blue_numbers = []
# 遍历数据,查找符合条件的下一期号码
for i in range(len(df) - 1):
current_reds = set(df.iloc[i][2:8]) # 当前红球
current_blue = df.iloc[i, 8] # 当前蓝球
# 如果当前红球与输入红球有交集且当前蓝球符合输入值
if current_reds.intersection(red_balls) and current_blue == blue_drum:
next_reds = df.iloc[i + 1][2:8] # 下一期红球
next_blue = df.iloc[i + 1, 8] # 下一期蓝球
next_red_numbers.extend(next_reds)
next_blue_numbers.append(next_blue) # 注意,这里是单独一个蓝球
return next_red_numbers, next_blue_numbers
# 只判断一个红球时
def collect_next_numbers_one(red_ball):
next_red_numbers = []
next_blue_numbers = []
# 遍历数据,查找符合条件的下一期号码
for i in range(len(df) - 1):
current_reds = set(df.iloc[i][2:8]) # 当前红球
# 如果当前红球与输入红球有交集且当前蓝球符合输入值
if red_ball in current_reds:
next_reds = df.iloc[i + 1][2:8] # 下一期红球
next_red_numbers.extend(next_reds)
return next_red_numbers
def analyze_frequencies(numbers):
frequency = pd.Series(numbers).value_counts()
return frequency
# 输入红球和蓝球
input_red_balls = [2, 3, 17, 18, 22, 33] # 示例输入
input_blue_ball = 16 # 示例输入
for i in input_red_balls:
next_red_numbers = collect_next_numbers_one(i)
# 统计红球频率
red_frequency = analyze_frequencies(next_red_numbers)
# 获取红球频率最高和最低的前5个号码
top_red_5 = red_frequency.nlargest(5)
#display(HTML(f'当红球是:<span style="color: red;font-size:30px">{i}</span>时,下一次出现频率最高的数字是:'))
# 将 Series 转换为 DataFrame
top_red_5_df = top_red_5.reset_index()
top_red_5_df.columns = ['红球号码', '频率'] # 重命名列
# 使用 HTML 格式化输出
html_table = top_red_5_df.to_html(index=False, border=0, justify='center', classes='table table-bordered', escape=False)
# 添加自定义样式
styled_html = f'''
<div style="margin: 0 auto; width: 50%; text-align: center;">
<h3>当红球是<span style="color: red;font-size:30px">{i}</span>时,下一次红球前5频率</h3>
{html_table.replace('table', 'table style="width:100%; text-align:center;"')}
</div>
'''
# 显示美观的 HTML 表格
display(HTML(styled_html))
display(HTML('<h1>当本次出现的红球其中有一个出现,并且篮球也出现时的概率统计如下:</h1>'))
# 收集下一期出现的号码
next_red_numbers, next_blue_numbers = collect_next_numbers(input_red_balls, input_blue_ball)
# 统计红球频率
red_frequency = analyze_frequencies(next_red_numbers)
# 统计蓝球频率
blue_frequency = analyze_frequencies(next_blue_numbers)
# 获取红球频率最高和最低的前5个号码
top_red_5 = red_frequency.nlargest(5)
#bottom_red_5 = red_frequency.nsmallest(5)
# 获取蓝球频率最高和最低的前5个号码
top_blue_5 = blue_frequency.nlargest(5)
# 输出红球结果
print("红球出现频率最高的前5个号码:")
print(top_red_5)
# 输出蓝球结果
print("\n蓝球出现频率最高的号码:")
print(top_blue_5)
# 绘制红球频率柱状图
plt.figure(figsize=(12, 6))
plt.bar(top_red_5.index.astype(str), top_red_5.values, color='blue', label='红球最高频率')
plt.xlabel('号码')
plt.ylabel('频率')
plt.title('红球出现频率')
plt.legend()
plt.show()
# 绘制蓝球频率柱状图
plt.figure(figsize=(12, 6))
plt.bar(top_blue_5.index.astype(str), top_blue_5.values, color='green', label='蓝球最高频率')
plt.xlabel('号码')
plt.ylabel('频率')
plt.title('蓝球出现频率')
plt.legend()
plt.show()
代码讲解:
本次使用的预测需求是:
使用上一次出现的号码,来计算下次出现号码频率最高的前5个。
例如,第2025011期 2025-01-26(周日)21:15:00开奖结果
6 历史上,6出现了之后,大概率会出现什么呢?
13
17
22
24
29
11 历史上,篮球11出现之后,大概率会出现什么呢?
接下来,我们运行代码,看看计算结果:
图解:当红球是6时,下一期出现9的次数是131次,出现6的次数是127次,出现10的次数是127次,出现20的次数是126,出现8的次数是125.
通过本次统计,得到的号码有5组。
相信大家有自己的判断了。
3,计算红球和蓝球同时出现的情况
接下来为了使结果更加精确,我们把需求整得准确一点。
要求红球和蓝球同时出现的情况下,下一期哪些号码出现的频率最高。
例如,我们只计算当蓝球11号出现时,红球6后面出现的数字。
根据预测,得到结果如下:
这是出现频率比较高的号码。
数据是根据代码统计的结果,没有预测的效果,请慎重参考。
还需要更多数据参考,请关注我,我会继续为大家分析数据。
#有哪些好玩的Python代码#
#畅聊人工智能#