[python]基于LSTM模型大乐透预测简化版
大乐透程序预测
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM, Dense
from sklearn.preprocessing import MinMaxScaler
# 已知的最近二十期数据
data = [
[4,5,16,21,31],
[25,27,35,3,7],
[4,12,19,25,27],
[1,18,21,26,33],
[23,29,27,26,33],
[13,29,5,17,33],
[15,29,25,24,22],
[6,8,14,16,28],
[3,13,15,17,22],
[9,10,14,31,32],
[3,18,14,4,26],
[16,18,17,35,27],
[20,18,17,31,25],
[7,12,16,33,34],
[4,20,21,32,34],
[8,17,26,32,28],
[3,27,24,5,28],
[34,27,14,15,25],
[30,29,34,6,5],
[2,4,25,26,31]
]
# 数据预处理
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)])
Y.append(data[i + seq_length])
return np.array(X), np.array(Y)
X, Y = create_sequences(data_scaled)
Python基于LSTM的预测模型
# 分割训练集和测试集
split = int(0.8 * len(X))
X_train, X_test = X[:split], X[split:]
Y_train, Y_test = Y[:split], Y[split:]
# 重塑输入数据为[LSTM时间步长, 特征数量]
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
# 创建LSTM模型
model = Sequential()
model.add(LSTM(50, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(5))
model.compile(loss='mean_squared_error', optimizer='adam')
人工智能训练
# 训练模型
model.fit(X_train, Y_train, epochs=100, batch_size=1, verbose=1)
# 预测下一期号码
last_sequence = X[-1].reshape(1, 5, 1) # 取最后一期作为预测的起始点
predicted_numbers = model.predict(last_sequence, verbose=0)
# 逆归一化
predicted_numbers = scaler.inverse_transform(predicted_numbers)
print("预测的下一期号码是:", predicted_numbers[0])