1

I'm trying to make the following code piece at the end run.

However, i'm getting the following error when i try to fit my model: "ValueError: setting an array element with a sequence."

I'm trying to use a RNN to predict the next 5 days of prices. So, in the function create_ts I'm trying to create two time series, one with the first X items and another with X+1, X+2, X+3, X+4, and X+5 - these five items being the next five days of prices i'd like to predict.

I suspect the problem is here somewhere:

def create_ts(ds, series, day_gap):
      x, y = [], []
      for i in range(len(ds) - series - 1):
        item = ds[i:(i+series),0]
        x.append(item)
        next_item = ds[i+series:(i+series+day_gap), 0]
        y.append(next_item)
      #print(type(np.array(x)), type(np.array(y)))
      return np.array(x), np.array(y).reshape(-1,1)

series = 5
predict_days = 5

train_x, train_y = create_ts(stock_train, series, predict_days)
test_x, test_y = create_ts(stock_test, series, predict_days)

#reshape into LSTM format - samples, steps, features
train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
test_x = np.reshape(test_x, (test_x.shape[0], test_x.shape[1], 1))

#build model
model = Sequential()
model.add(LSTM(4,input_shape = (series, 1)))
model.add(Dense(1))
model.compile(loss='mse', optimizer = 'adam')
#fit model
model.fit(train_x, train_y, epochs = 100, batch_size = 32)

Thanks in advance for any help!

Below is the full code piece:

from keras import backend as k
import os
from importlib import reload

def set_keras_backend(backend):
  if k.backend() != backend:
    os.environ['KERAS_BACKEND'] = backend
    reload(k)
    assert k.backend() == backend


set_keras_backend("cntk")

import numpy as np
import pandas as pd
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import math

np.random.seed(7)

#load dataset
fileloc = "C:\\Stock Data\\CL1.csv"

stock_data = pd.read_csv(fileloc)
stock_data.head()

stock_data.dtypes

stock_data['Date'] = pd.to_datetime(stock_data['Date'])
stock_data['Price'] = pd.to_numeric(stock_data['Price'], downcast = 'float')

stock_data.set_index('Date', inplace=True)

stock_close = stock_data['Price']

stock_close = stock_close.values.reshape(len(stock_close), 1)
plt.plot(stock_close)

#normalize data
scaler = MinMaxScaler(feature_range = (0,1))
stock_close = scaler.fit_transform(stock_close)

#split data into a train, test set
train_size = int(len(stock_close)*0.7)
test_size = len(stock_close) - train_size

stock_train, stock_test = stock_close[0:train_size, :], stock_close[train_size:len(stock_close), :]

#convert the data into a time series looking back over a period fo days

def create_ts(ds, series, day_gap):
  x, y = [], []
  for i in range(len(ds) - series - 1):
    item = ds[i:(i+series),0]
    x.append(item)
    next_item = ds[i+series:(i+series+day_gap), 0]
    y.append(next_item)
  #print(type(np.array(x)), type(np.array(y)))
  return np.array(x), np.array(y).reshape(-1,1)

series = 5
predict_days = 5

train_x, train_y = create_ts(stock_train, series, predict_days)
test_x, test_y = create_ts(stock_test, series, predict_days)

#reshape into LSTM format - samples, steps, features
train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
test_x = np.reshape(test_x, (test_x.shape[0], test_x.shape[1], 1))

#build model
model = Sequential()
model.add(LSTM(4,input_shape = (series, 1)))
model.add(Dense(1))
model.compile(loss='mse', optimizer = 'adam')
#fit model
model.fit(train_x, train_y, epochs = 100, batch_size = 32)
rvinas
  • 11,824
  • 36
  • 58
Rohit Kar
  • 35
  • 4
  • Yes, some where in your code you are trying to assign an array to a single element (numeric) slot. Without a traceback we can't say where that is happening, or even whether it is directly in your code, or buried somewhere deep inside a module function (but still the result of one of your inputs). – hpaulj Jul 28 '18 at 01:23

0 Answers0