首页 > 解决方案 > 我正在尝试使用 CNN 进行股票价格预测,但我的代码似乎不起作用,我需要更改或添加什么?

问题描述

import math
import numpy as np
import pandas as pd
import pandas_datareader as pdd
from sklearn.preprocessing import MinMaxScaler
from keras.layers import Dense, Dropout, Activation, LSTM, Convolution1D, MaxPooling1D, Flatten
from keras.models import Sequential
import matplotlib.pyplot as plt

df = pdd.DataReader('AAPL', data_source='yahoo', start='2012-01-01', end='2020-12-31')
data = df.filter(['Close'])
dataset = data.values
len(dataset)

# 2265

training_data_size = math.ceil(len(dataset)*0.7)
training_data_size

# 1586

scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset)
scaled_data

# array([[0.04288701],
#       [0.03870297],
#       [0.03786614],
#       ...,
#       [0.96610873],
#       [0.98608785],
#       [1.        ]])

train_data = scaled_data[0:training_data_size,:]
x_train = []
y_train = []
for i in range(60, len(train_data)):
    x_train.append(train_data[i-60:i, 0])
    y_train.append(train_data[i,0])
    if i<=60:
        print(x_train)
        print(y_train)  

'''
[array([0.04288701, 0.03870297, 0.03786614, 0.0319038 , 0.0329498 ,
       0.03577404, 0.03504182, 0.03608791, 0.03640171, 0.03493728,
       0.03661088, 0.03566949, 0.03650625, 0.03368202, 0.03368202,
       0.03598329, 0.04100416, 0.03953973, 0.04110879, 0.04320089,
       0.04089962, 0.03985353, 0.04037657, 0.03566949, 0.03640171,
       0.03619246, 0.03253139, 0.0294979 , 0.03033474, 0.02960253,
       0.03002095, 0.03284518, 0.03357739, 0.03410044, 0.03368202,
       0.03472803, 0.02803347, 0.02792885, 0.03556487, 0.03451886,
       0.0319038 , 0.03127613, 0.03274063, 0.02688284, 0.02635988,
       0.03211297, 0.03096233, 0.03472803, 0.03713392, 0.03451886,
       0.03441423, 0.03493728, 0.03587866, 0.0332636 , 0.03117158,
       0.02803347, 0.02897494, 0.03546024, 0.03786614, 0.0401674 ])]
[0.03933056376752886]
'''

x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_train.shape

# (1526, 60, 1)

model = Sequential()
model.add(Convolution1D(64, 3, input_shape= (100,4), padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Convolution1D(32, 3, padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(1))
model.add(Activation('linear'))
model.summary()

model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=50, epochs=50, validation_data = (X_test, y_test), verbose=2)

test_data = scaled_data[training_data_size-60: , :]
x_test = []
y_test = dataset[training_data_size: , :]
for i in range(60, len(test_data)):
    x_test.append(test_data[i-60:i, 0])

x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
predictions = model.predict(x_test)
predictions = scaler.inverse_transform(predictions)
rsme = np.sqrt(np.mean((predictions - y_test)**2))
rsme

train = data[:training_data_size]
valid = data[training_data_size:]
valid['predictions'] = predictions
plt.figure(figsize=(16,8))
plt.title('PFE')
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close Price in $', fontsize=18)
plt.plot(train['Close'])
plt.plot(valid[['Close', 'predictions']])
plt.legend(['Train', 'Val', 'predictions'], loc='lower right')
plt.show

import numpy as np

y_test, predictions = np.array(y_test), np.array(predictions)
mape = (np.mean(np.abs((predictions - y_test) / y_test))) * 100
accuracy = 100 - mape
print(accuracy)

以上是我的代码。我试图编辑它,但似乎没有工作。我怀疑我没有很好地格式化我的数据集,但我是这个领域的新手,所以我不知道我应该如何处理我的代码以使其适合。我希望你们能启发我,谢谢!

我遇到了如下错误:''IndexError: index 2264 is out of bounds for axis 0 with size 2264'' and '' ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 800但收到形状为 [None, 480]'' 的输入

标签: pythontensorflowkerasconv-neural-network

解决方案


您的模型与您的数据无关。

更改此行:

model.add(Convolution1D(64, 3, input_shape= (60,1), padding='same'))

推荐阅读