不使用sklearn能实现上述要求吗
时间: 2024-03-20 09:38:42 浏览: 48
不使用sklearn推演决策树
可以的,我们可以手动实现数据归一化的过程,不需要使用sklearn中的MinMaxScaler。以下是修改后的代码实现:
```python
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM, Dense
# 读取数据
df = pd.read_csv('600000.txt', header=None, names=['date', 'open', 'high', 'low', 'close', 'vol', 'mount'])
# 提取收盘价
close_prices = df['close'].values
# 计算涨跌幅并转换为[0,20]之间的整数
diff = (close_prices[1:] - close_prices[:-1]) / close_prices[:-1]
diff = np.clip(diff, -0.1, 0.1)
scaled_diff = ((diff + 0.1) / 0.2 * 20).astype(int)
# 归一化数据
max_value = np.max(scaled_diff)
min_value = np.min(scaled_diff)
scaled_data = (scaled_diff - min_value) / (max_value - min_value)
# 分割数据集
train_size = int(len(scaled_data) * 0.8)
train_data = scaled_data[:train_size]
test_data = scaled_data[train_size:]
# 构建训练数据
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back):
a = dataset[i:(i+look_back)]
dataX.append(a)
dataY.append(dataset[i + look_back])
return np.array(dataX), np.array(dataY)
look_back = 5 # 每个样本包含5天的数据
trainX, trainY = create_dataset(train_data, look_back)
testX, testY = create_dataset(test_data, look_back)
# 转换为3D数据
trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# 构建LSTM模型
model = Sequential()
model.add(LSTM(50, input_shape=(look_back, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
# 训练模型
model.fit(trainX, trainY, epochs=100, batch_size=32, verbose=2)
# 在验证集上评估模型
train_predict = model.predict(trainX)
test_predict = model.predict(testX)
train_predict = train_predict * (max_value - min_value) + min_value
trainY = trainY * (max_value - min_value) + min_value
test_predict = test_predict * (max_value - min_value) + min_value
testY = testY * (max_value - min_value) + min_value
train_score = np.sqrt(np.mean((train_predict - trainY) ** 2))
test_score = np.sqrt(np.mean((test_predict - testY) ** 2))
print('Train Score: %.2f RMSE' % (train_score))
print('Test Score: %.2f RMSE' % (test_score))
```
这里我们手动计算了最大值和最小值,并使用它们进行归一化。在预测时,我们需要将预测结果反归一化得到真实值。
阅读全文