Python代码:大乐透根据近期数据按概率选号

360影视 2025-01-30 10:13 2

摘要:import numpy as npfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.mod

大乐透创建随机森林模型,根据近期数据按概率选择号码,本代码同样适用于双色球和快乐8等,只需修改一定的参数,输入相应的历史数据即可。

模拟数据大乐透

import numpy as npfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.model_selection import train_test_split# 输入已知大乐透历史数据越多越好data = [ [7, 25, 34, 27, 35], [7, 15, 9, 10, 24], [7, 14, 23, 29, 25], [2, 24, 26, 30, 34], [2, 16, 21, 23, 27], [13,21,25,28,32], [10,12,13,24,25], [3,17,22,26,28], [13,33,22,24,34], [32,19,22,35,29], [2,9,6,31,32], [8,14,21,25,16], [6,11,13,30,32], [21,22,24,27,31], [5,6,9,11,14], [15,25,28,30,35], [5,16,18,26,35], [2,3,6,26,27], [25,6,23,28,35], [16,4,3,19,24], [4,6,13,19,35],[5,6,17,30,32],[1,3,9,18,29],[6,7,13,25,33],[5,11,15,23,33],[1,7,9,25,29],[6,18,23,27,32],[7,25,26,29,32],[9,23,25,27,33],[13,20,27,29,30],[4,19,21,30,31],[15,16,25,31,34],[4,22,25,30,31],[5,14,18,25,35],[6,10,12,33,32],[11,12,20,21,27],[2,6,27,18,19],[1,3,32,34,27],[8,11,14,24,31],[19,20,26,29,30],[8,10,12,14,22],[3,5,12,17,26],[9,12,15,30,34],[5,15,26,33,35],[20,17,29,19,35],[4,27,14,8,3],[26,21,12,18,28],[4,6,9,10,22],[5,21,29,23,22],[1,7,19,20,35],[6,10,13,20,32],[1,12,19,31,33],[5,14,15,16,33],[5,12,21,32,33],[25,13,20,19,24],[21,23,7,9,12],[1,3,26,29,25],[5,7,10,14,15],[3,8,23,17,18],[4,10,24,31,35],[1,5,9,20,14],[19,29,21,32,33],[27,28,26,12,23],[27,30,19,9,3],[21,16,19,9,3],[7,15,18,24,33],[7,2,10,25,3],[16,2,10,28,3],[31,7,10,29,3],[31,11,21,25,33],[30,6,22,24,8],[5,21,12,24,23],[3,6,15,31,23],[8,24,34,33,21],[8,6,7,30,21],[5,21,32,20,22],[6,35,32,20,9]]#历史数据预处理scaler = MinMaxScaler(feature_range=(0, 1))data_scaled = scaler.fit_transform(data)# 创建序列数据def create_sequences(data, seq_length=5): X, Y = , for i in range(len(data) - seq_length): X.append(data[i:(i + seq_length)].flatten) # 展平序列 Y.append(data[i + seq_length]) return np.array(X), np.array(Y)X, Y = create_sequences(data_scaled)# 分割训练集和测试集X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)# 创建随机森林模型随机数42可修改model = RandomForestRegressor(n_estimators=100, random_state=42)model.fit(X_train, Y_train)# 大模型预选多组号码# 取最后几期作为预测的起始点num_predictions = 5 # 预选的组数可选改为你需要的十组二十组等last_sequences = X[-num_predictions:] # 取最后几期数据/近期数据权重高predicted_numbers = model.predict(last_sequences)# 逆归一化predicted_numbers = scaler.inverse_transform(predicted_numbers)# 后处理:将大乐透选择结果四舍五入为整数predicted_numbers = np.round(predicted_numbers).astype(int)# 将选择结果限制在1到35之间predicted_numbers = np.clip(predicted_numbers, 1, 35)# 确保每组号码有5个数字predicted_numbers = predicted_numbers.reshape(-1, 5)# 计算号码出现的概率然后根据概率预选号码def calculate_number_probabilities(data): all_numbers = np.array(data).flatten probabilities = np.zeros(35) for number in all_numbers: probabilities[int(number) - 1] += 1 probabilities /= len(all_numbers) return probabilities# 根据概率调整号码def adjust_numbers_by_probability(numbers, probabilities): unique_numbers = np.unique(numbers) while len(unique_numbers)

构建大模型代码

大模型预选多组号码,此为简单示例,仅供娱乐。

来源:江东旭侯

相关推荐