I have the following function
import math
import pandas as pd
import pandas_datareader as web
import numpy as np
import matplotlib.pyplot as plt
import os.path
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
from tensorflow.keras.models import load_model
def predict_stock(stock_name, predict_days=30):
start = dt.datetime(2021, 1, 1)
end = dt.datetime.now()
stock = web.DataReader(stock_name, data_source="yahoo", start=start, end=end)
stock = stock.filter(["Adj Close"])
stock_data = stock.values
# splits the stock into training data and test data
training_len = math.ceil(len(stock) - predict_days)
scale = MinMaxScaler()
scaled_data = scale.fit_transform(stock_data)
train_data = scaled_data[:training_len]
# sets train values
x_train = []
y_train = []
# test starts at day 60 and ends at 80 % of day end (test data)
for i in range(predict_days, len(train_data)):
x_train.append(train_data[i - predict_days:i])
y_train.append(train_data[i:i+predict_days])
x_train = np.array(x_train)
y_train = np.array(y_train)
#y_train.reshape(y_train, x_train.shape)
predict_stock('ALB', 30)
while x_train is of shape (164, 30, 1), y_train is for some reason of shape (164,), whereby the generation was the same.
How can I reshape y_train to (164,30,1)?
I tried the command:
y_train.reshape(y_train, x_train.shape)
but this gives me the error:
TypeError: only integer scalar arrays can be converted to a scalar index
How can I reshape the array correctly?
x_train..shapeargument was for. But why the wholex_train? Did you check thereshapedocs to see what kinds of arguments it expected? Check the docs before running off to the web seeking help!y_trainto matchx_train. You can reshape it to(164,1), but you can't increase the total number of elements to matchx_train. But ifx_trainhas 164 "samples" and 30 "features",y_trainshouldn't have that 30 dimension. It's just one value for each sample.