'Repeated values in prediction with sequential model
The problem I got is with the result, I get the same value in the 'future' field in all the rows as follows.
open high low close adjclose volume upperband middleband lowerband future
12 185.979996 191.669998 182.899994 184.149994 184.149994 49946000 206.918629 193.606000 180.293370 208.573837
13 189.669998 200.369995 184.899994 197.820007 197.820007 57032700 204.412283 192.804001 181.195718 208.573837
14 194.020004 201.279999 185.169998 185.470001 185.470001 50001100 203.453190 190.868002 178.282814 208.573837
15 185.410004 195.740005 183.910004 195.330002 195.330002 57204900 201.037422 190.130002 179.222582 208.573837
The following is my model creation method:
def deep_learning_model_creation(X_Train, layers = 2, optimizer="rmsprop", units=256, bidirectional = False, dropout=0.2, loss="mean_absolute_error"):
model = Sequential()
for i in range(layers):
if i == 0:
model.add(LSTM(units=units,return_sequences=True,input_shape=(X_Train.shape[1], 1)))
elif i == layers - 1:
model.add(LSTM(units=units,return_sequences=False))
else:
model.add(LSTM(units=units,return_sequences=True))
model.add(Dropout(dropout))
model.add(Dense(1, activation="linear"))
model.compile(optimizer=optimizer,loss=loss)
return model
The following is how I set the field to predict, then create and fit the model.
df = df.assign(future=df['adjclose'].shift(-1))
df.dropna(subset=['future'], how='all', inplace=True)
df = df.drop(columns=['date'])
X_Train, X_Test, Y_Train, Y_Test = train_test_split(df.drop(['future'], axis=1), df['future'], test_size=0.2, random_state=1, shuffle = False)
model = deep_learning_model_creation(X_Train, optimizer = "adam", loss = "huber_loss", dropout=0.4)
model.fit(
X_Train,
Y_Train,
epochs=100,
batch_size=5,
validation_data=(X_Test, Y_Test)
)
predicted_stock_price = model.predict(X_Test)
X_Test['future'] = predicted_stock_price
print(X_Test)
This is the dataframe:
open high low close adjclose volume upperband middleband lowerband future
0 222.139999 223.199997 216.600006 219.169998 219.169998 57520700 266.023308 239.164001 212.304695 215.039993
1 225.369995 227.250000 212.820007 215.039993 215.039993 66225800 253.735079 230.310001 206.884922 222.029999
2 217.279999 225.000000 214.550003 222.029999 222.029999 51694300 245.251131 225.901999 206.552867 212.580002
3 225.050003 227.770004 211.630005 212.580002 212.580002 56822500 232.953724 220.001999 207.050274 217.830002
4 212.000000 220.880005 210.789993 217.830002 217.830002 52570100 223.874138 217.329999 210.785860 221.979996
5 217.199997 223.729996 213.139999 221.979996 221.979996 51278100 225.385454 217.891998 210.398542 214.820007
6 225.169998 226.699997 212.000000 214.820007 214.820007 46897400 225.410190 217.848001 210.285812 201.830002
7 217.089996 223.919998 200.000000 201.830002 201.830002 65620900 227.342036 213.808002 200.273968 195.149994
8 203.029999 204.860001 195.000000 195.149994 195.149994 62471300 230.616134 210.322000 190.027866 199.020004
9 192.020004 199.449997 190.960007 199.020004 199.020004 64156600 226.865993 206.560001 186.254008 187.880005
10 197.179993 197.880005 186.699997 187.880005 187.880005 65314300 217.494343 199.740002 181.985662 184.149994
11 185.979996 191.669998 182.899994 184.149994 184.149994 49946000 206.918629 193.606000 180.293370 197.820007
12 189.669998 200.369995 184.899994 197.820007 197.820007 57032700 204.412283 192.804001 181.195718 185.470001
13 194.020004 201.279999 185.169998 185.470001 185.470001 50001100 203.453190 190.868002 178.282814 195.330002
14 185.410004 195.740005 183.910004 195.330002 195.330002 57204900 201.037422 190.130002 179.222582 196.020004
15 194.000000 198.250000 191.330002 196.020004 196.020004 47575100 203.250347 191.758002 180.265657 203.339996
EDIT
As @elbe suggested me scaling values before training and prediction makes the values not the same anymore:
open high low close adjclose volume upperband middleband lowerband future
12 0.215466 0.266205 0.071722 0.034847 0.034847 0.160577 0.045842 0.018367 0.000000 42.391476
13 0.000000 0.112743 0.031912 0.295143 0.295143 0.533283 0.000000 0.000000 0.028913 42.391270
14 0.214965 0.182271 0.266351 0.313358 0.313358 0.035062 0.041993 0.040518 0.061005 42.391472
15 0.345846 0.341551 0.145656 0.506600 0.506600 0.930656 0.116540 0.136038 0.176303 42.391541
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|