怎么能知道双色球开奖号码?

360影视 2025-02-02 20:17 2

摘要:# predict/ssq_predict.pyimport pandas as pdfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.model_selection import

首先得明确,双色球的开奖号码是完全随机产生的。本文主要作为数据研究和人工智能学习,帮助大家从感兴趣的话题入手,更方便学习python数据统计和分析。

声明:咱们买彩票啊,得抱着娱乐的心态,别把全部身家都押上去,更别指望靠这个发大财。毕竟,中奖是极小概率事件。

我准备了从2003-至今的所有历史数据,部分数据如下:

期号开奖时间红球 1红球 2红球 3红球 4红球 5红球 6蓝球一等奖二等奖三等奖20250112025/01/26061317222429116202170320250102025/01/230406071617210838129201920250092025/01/210204111223250614334195920250082025/01/1909141617253311215594320250072025/01/16070814182127113113149120250062025/01/140107081720220616328204520250052025/01/121016192728300913139247220250042025/01/09030717272932049118219420250032025/01/07101920262829157135144520250022025/01/05091213152226112116168820250012025/01/02020317182233161082041506

以上表格为2025年全年数据,其他数据量太大不单独列出。

首先想到的算法就是直接输入一组号码,让代码帮我总结规律,计算出下一组号码是什么,大家看代码:

# predict/ssq_predict.pyimport pandas as pdfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import MultiLabelBinarizerimport numpy as npclass SsqPredict: def __init__(self, history_data): self.history_data = history_data self.red_balls_model = self._train_model('red_balls') self.blue_ball_model = self._train_model('blue_ball') def _train_model(self, target): # 准备数据 if target == 'red_balls': X = self.history_data.drop(columns=['issuenumber', 'drawTime', 'redBall1', 'redBall2', 'redBall3', 'redBall4', 'redBall5', 'redBall6', 'blueBall', 'firstPrize', 'secondPrize', 'thirdPrize']) y = self.history_data[['redBall1', 'redBall2', 'redBall3', 'redBall4', 'redBall5', 'redBall6']].values elif target == 'blue_ball': X = self.history_data.drop(columns=['issueNumber', 'drawTime', 'redBall1', 'redBall2', 'redBall3', 'redBall4', 'redBall5', 'redBall6', 'blueBall', 'firstPrize', 'secondPrize', 'thirdPrize']) y = self.history_data['blueBall'].values # 分割数据集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 训练模型 if target == 'red_balls': mlb = MultiLabelBinarizer y_train = mlb.fit_transform(y_train) y_test = mlb.transform(y_test) model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train, y_train) # 评估模型 accuracy = model.score(X_test, y_test) print(f"Red balls model accuracy: {accuracy}") else: model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train, y_test) # 评估模型 accuracy = model.score(X_test, y_test) print(f"Blue ball model accuracy: {accuracy}") return model def predict_by_number(self, red_balls, blue_ball): # 准备输入数据 # 假设输入的 red_balls 和 blue_ball 是用于预测的特征 # 这里我们假设输入的 red_balls 和 blue_ball 是用于预测的特征 # 你需要根据实际情况构造输入数据 input_data = self.history_data.drop(columns=['issueNumber', 'drawTime', 'redBall1', 'redBall2', 'redBall3', 'redBall4', 'redBall5', 'redBall6', 'blueBall', 'firstPrize', 'secondPrize', 'thirdPrize']).iloc[0:1].copy # 假设输入的 red_balls 和 blue_ball 是用于预测的特征 # 你需要根据实际情况构造输入数据 # 这里我们假设输入的 red_balls 和 blue_ball 是用于预测的特征 # 你需要根据实际情况构造输入数据 input_data['redBall1'] = red_balls[0] input_data['redBall2'] = red_balls[1] input_data['redBall3'] = red_balls[2] input_data['redBall4'] = red_balls[3] input_data['redBall5'] = red_balls[4] input_data['redBall6'] = red_balls[5] input_data['blueBall'] = blue_ball # 预测红球 red_balls_pred = self.red_balls_model.predict(input_data.values.reshape(1, -1)) mlb = MultiLabelBinarizer red_balls_pred = mlb.inverse_transform(red_balls_pred)[0] # 预测蓝球 blue_ball_pred = self.blue_ball_model.predict(input_data.values.reshape(1, -1))[0] return { 'redBalls': red_balls_pred.tolist, 'blueBall': blue_ball_pred }

运行之后,出现了问题,报错了,根本没办法预测。

解决问题:

数据质量:确保 history_data 的数据质量良好,没有缺失值或其他异常数据。这个数据没有问题。

特征工程:没有。

问题出在特征列 X 上。具体来说,X 列被完全删除,导致 X_train 和 X_test 的形状为 (2596, 0) 和 (650, 0),这意味着没有特征用于训练模型。

问题分析

特征列缺失:在 _train_model 方法中,你尝试从 历史数据中删除特定列以获取特征列 X,但最终 X 列为空。

数据预处理问题:历史数据中没有其他可用的特征列,导致 X 列为空。

解决方法

为了解决这个问题,我们需要构造一些特征列。由于双色球的历史数据通常没有其他特征列,我们可以构造一些简单的特征,例如前一期的开奖结果。

(关于训练特征,我在下一篇文章中,将使用期数作为特征来预测。后续还有星期几等方式统计)

说人话!意思就是,没有规律让我怎么寻找,你要给我一个参考值,我才能进行训练。

例如:本期出现的所有号码的和是120,那么代码可以根据这个120来总结总共有多少个120出现过,然后再根据期数的单双号预测,这样至少有特征可以找了吧。

接下来,优化模型。

我们需要构造一个特征列,为什么需要构造特征列?(就是模拟寻找号码的规律)

在机器学习中,模型需要特征(features)来学习模式和进行预测。对于双色球这种彩票数据,原始数据中并没有直接可以用来预测下一期开奖结果的特征。因此,我们需要构造一些特征来帮助模型学习历史数据中的模式。

说人话:

原始数据只有每期的开奖结果(红球和蓝球),没有其他特征可以用来预测下一期的开奖结果。

机器学习模型需要特征来学习模式,如果没有特征,模型无法进行有效的学习。通过前一期的开奖结果可以作为特征,帮助模型学习到一些可能的模式或趋势。

接下来,我们使用三组号码作为特征进行预测。

更新后的代码如下:

# predict/ssq_predict.pyimport pandas as pdfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import MultiLabelBinarizerimport numpy as npfrom imblearn.over_sampling import SMOTEfrom collections import Counterfrom imblearn.over_sampling import SMOTEclass SsqPredict: def __init__(self, history_data): self.history_data = self._construct_features(history_data) self.red_balls_model, self.red_balls_mlb = self._train_model('red_balls') self.blue_ball_model = self._train_model('blue_ball') def _construct_features(self, history_data): # 构造特征列 # 前一期的开奖结果 history_data['prev_redBall1'] = history_data['redBall1'].shift(1) history_data['prev_redBall2'] = history_data['redBall2'].shift(1) history_data['prev_redBall3'] = history_data['redBall3'].shift(1) history_data['prev_redBall4'] = history_data['redBall4'].shift(1) history_data['prev_redBall5'] = history_data['redBall5'].shift(1) history_data['prev_redBall6'] = history_data['redBall6'].shift(1) history_data['prev_blueBall'] = history_data['blueBall'].shift(1) # 前两期的开奖结果 history_data['prev2_redBall1'] = history_data['redBall1'].shift(2) history_data['prev2_redBall2'] = history_data['redBall2'].shift(2) history_data['prev2_redBall3'] = history_data['redBall3'].shift(2) history_data['prev2_redBall4'] = history_data['redBall4'].shift(2) history_data['prev2_redBall5'] = history_data['redBall5'].shift(2) history_data['prev2_redBall6'] = history_data['redBall6'].shift(2) history_data['prev2_blueBall'] = history_data['blueBall'].shift(2) # 前三期的开奖结果 history_data['prev3_redBall1'] = history_data['redBall1'].shift(3) history_data['prev3_redBall2'] = history_data['redBall2'].shift(3) history_data['prev3_redBall3'] = history_data['redBall3'].shift(3) history_data['prev3_redBall4'] = history_data['redBall4'].shift(3) history_data['prev3_redBall5'] = history_data['redBall5'].shift(3) history_data['prev3_redBall6'] = history_data['redBall6'].shift(3) history_data['prev3_blueBall'] = history_data['blueBall'].shift(3) # 删除包含 NaN 的行 history_data.dropna(inplace=True) return history_data def _train_model(self, target): # 准备数据 if target == 'red_balls': X = self.history_data.drop( columns=['issueNumber', 'drawTime', 'blueBall', 'firstPrize', 'secondPrize', 'thirdPrize']) y = self.history_data[['redBall1', 'redBall2', 'redBall3', 'redBall4', 'redBall5', 'redBall6']].values models = for i in range(6): y_single = y[:, i] X_train, X_test, y_train, y_test = train_test_split(X, y_single, test_size=0.2, random_state=42) # 打印训练集中每个类别的样本数 print(f"类别[{i + 1}]样本数:{Counter(y_train)}") # 动态调整 k_neighbors 参数 smote_k_neighbors = min(3, len(X_train) - 1) # 确保 k_neighbors 不超过样本数 print(f"SMOTE 使用的 k_neighbors: {smote_k_neighbors}") if len(np.unique(y_train)) > 1 and smote_k_neighbors > 0: smote = SMOTE(random_state=42, k_neighbors=smote_k_neighbors) X_train_resampled, y_train_resampled = smote.fit_resample(X_train, y_train) else: print(f"跳过 SMOTE,因为样本数不足: {Counter(y_train)}") X_train_resampled, y_train_resampled = X_train, y_train # 使用原始数据 # 训练模型 model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train_resampled, y_train_resampled) models.append(model) print("红球模型训练完成") return models # 返回所有模型 elif target == 'blue_ball': X = self.history_data.drop( columns=['issueNumber', 'drawTime', 'redBall1', 'redBall2', 'redBall3', 'redBall4', 'redBall5', 'redBall6', 'firstPrize', 'secondPrize', 'thirdPrize']) y = self.history_data['blueBall'].values # 分割数据集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 打印训练集中每个类别的样本数 print(f"蓝球类别样本数:{Counter(y_train)}") # 动态调整 k_neighbors 参数 smote_k_neighbors = min(3, len(X_train) - 1) print(f"SMOTE 使用的 k_neighbors: {smote_k_neighbors}") if len(np.unique(y_train)) > 1 and smote_k_neighbors > 0: smote = SMOTE(random_state=42, k_neighbors=smote_k_neighbors) X_train_resampled, y_train_resampled = smote.fit_resample(X_train, y_train) else: print(f"跳过 SMOTE,因为样本数不足: {Counter(y_train)}") X_train_resampled, y_train_resampled = X_train, y_train # 使用原始数据 # 训练模型 model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train_resampled, y_train_resampled) # 评估模型 accuracy = model.score(X_test, y_test) print(f"蓝球模型准确率: {accuracy}") return model def predict_by_number(self, red_balls, blue_ball): # 准备输入数据 input_data = self.history_data.drop( columns=['issueNumber', 'drawTime', 'blueBall', 'firstPrize', 'secondPrize', 'thirdPrize']).iloc[0:1].copy # 填充输入数据 input_data['prev_redBall1'] = red_balls[0] input_data['prev_redBall2'] = red_balls[1] input_data['prev_redBall3'] = red_balls[2] input_data['prev_redBall4'] = red_balls[3] input_data['prev_redBall5'] = red_balls[4] input_data['prev_redBall6'] = red_balls[5] input_data['prev_blueBall'] = blue_ball # 填充其他特征列 input_data = input_data.reindex(columns=self.history_data.drop( columns=['issueNumber', 'drawTime', 'redBall1', 'redBall2', 'redBall3', 'redBall4', 'redBall5', 'redBall6', 'blueBall', 'firstPrize', 'secondPrize', 'thirdPrize']).columns, fill_value=0) # 预测红球 red_balls_pred = for model in self.red_balls_model: pred = model.predict(input_data.values.reshape(1, -1))[0] red_balls_pred.append(pred) # 预测蓝球 blue_ball_pred = self.blue_ball_model.predict(input_data.values.reshape(1, -1))[0] return { 'redBalls': red_balls_pred, 'blueBall': blue_ball_pred }

运行代码后,结果显示,样本数不够,无法预测,也就是他还是找不到规律。

数据集中的某些类别样本太少,以至于无法满足 SMOTE 的 k_neighbors 要求(默认值为 5,您已经调整到 3,但实际上数据样本仍然不足)。具体来说,该问题源于 SMOTE 尝试基于少数类别邻居生成新样本时,发现样本不足以构造有效的邻域。

最终,我将特征数据调整到前5期,他还是找不到规律。

# 前一期的开奖结果 history_data['prev_redBall1'] = history_data['redBall1'].shift(1) history_data['prev_redBall2'] = history_data['redBall2'].shift(1) history_data['prev_redBall3'] = history_data['redBall3'].shift(1) history_data['prev_redBall4'] = history_data['redBall4'].shift(1) history_data['prev_redBall5'] = history_data['redBall5'].shift(1) history_data['prev_redBall6'] = history_data['redBall6'].shift(1) history_data['prev_blueBall'] = history_data['blueBall'].shift(1) # 前两期的开奖结果 history_data['prev2_redBall1'] = history_data['redBall1'].shift(2) history_data['prev2_redBall2'] = history_data['redBall2'].shift(2) history_data['prev2_redBall3'] = history_data['redBall3'].shift(2) history_data['prev2_redBall4'] = history_data['redBall4'].shift(2) history_data['prev2_redBall5'] = history_data['redBall5'].shift(2) history_data['prev2_redBall6'] = history_data['redBall6'].shift(2) history_data['prev2_blueBall'] = history_data['blueBall'].shift(2) # 前三期的开奖结果 history_data['prev3_redBall1'] = history_data['redBall1'].shift(3) history_data['prev3_redBall2'] = history_data['redBall2'].shift(3) history_data['prev3_redBall3'] = history_data['redBall3'].shift(3) history_data['prev3_redBall4'] = history_data['redBall4'].shift(3) history_data['prev3_redBall5'] = history_data['redBall5'].shift(3) history_data['prev3_redBall6'] = history_data['redBall6'].shift(3) history_data['prev3_blueBall'] = history_data['blueBall'].shift(3)

无奈,此种算法只能作罢,我想应该没人能够根据算法找到规律的。

为了能够有实际值出现,我使用红球作为特征,预测了一个蓝球:

下一期,我将使用期数和求和给大家预测。

个人观点,请慎重购彩。数据来源官方,本文只提供学习编程和统计,培养兴趣。

来源:李白也学编程

相关推荐