How to overcome this error in the code: Error:…
- User
How to overcome this error in the code: Error:
ValueError Traceback (most recent call last) in <cell line: 4>() 11 12 new_row = np.concatenate((next_day_input[0, 1:, :], next_day_extended), axis=0) ---> 13 X_test = np.append(X_test, [new_row], axis=0) 14 15 predictions = np.array(predictions).reshape(-1, 1)
2 frames /usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in concatenate(*args, **kwargs)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 40 and the array at index 1 has size 39
Code: import numpy as np import pandas as pd import matplotlib.pyplot as plt import yfinance as yf from sklearn.preprocessing import MinMaxScaler from keras.models import Sequential from keras.layers import Dense, LSTM, Dropout
def calculate_atr(data, period): data['h-l'] = data['high'] - data['low'] data['h-pc'] = abs(data['high'] - data['close'].shift(1)) data['l-pc'] = abs(data['low'] - data['close'].shift(1)) data['tr'] = data[['h-l', 'h-pc', 'l-pc']].max(axis=1) data['atr'] = data['tr'].rolling(window=period).mean() return data
def calculate_super_trend(data, period, multiplier): data = calculate_atr(data, period) data['Upper Basic'] = (data['high'] + data['low']) / 2 + multiplier * data['atr'] data['Lower Basic'] = (data['high'] + data['low']) / 2 - multiplier * data['atr'] data['Upper Band'] = data.apply(lambda x: x['Upper Basic'] if x['close'] > x['Upper Basic'] else x['Lower Basic'], axis=1) data['Lower Band'] = data.apply(lambda x: x['Lower Basic'] if x['close'] < x['Lower Basic'] else x['Upper Basic'], axis=1) data['Super Trend'] = np.where(data['close'] > data['Upper Band'], data['Lower Band'], data['Upper Band']) return data.dropna()
def load_preprocess_data(ticker, start_date, end_date, window_size, period=14, multiplier=3): stock_data = yf.download(ticker, start=start_date, end=end_date) print("Original columns:", stock_data.columns) stock_data.columns = [col.lower() for col in stock_data.columns] stock_data_with_super_trend = calculate_super_trend(stock_data, period, multiplier)
columns_to_use = stock_data_with_super_trend[['open', 'high', 'low', 'close', 'Super Trend']].values scaler = MinMaxScaler(feature_range=(0, 1)) data_normalized = scaler.fit_transform(columns_to_use) X, y = [], [] for i in range(window_size, len(data_normalized)): X.append(data_normalized[i - window_size:i]) y.append(data_normalized[i, 3]) # Use Close prices directly as labels train_len = int(0.8 * len(X)) X_train, y_train = np.array(X[:train_len]), np.array(y[:train_len]) X_test, y_test = np.array(X[train_len:]), np.array(y[train_len:]) return X_train, y_train, X_test, y_testdef create_lstm_model(input_shape): model = Sequential() model.add(LSTM(units=50, return_sequences=True, input_shape=input_shape)) model.add(LSTM(units=50, return_sequences=True)) model.add(LSTM(units=50)) model.add(Dense(units=1))
- Assistant
The error occurs because the dimensions of the
new_rowandX_testdo not match along axis 1 for the concatenation operation. To resolve the issue, ensure that the concatenated arrays have matching dimensions along the specified axis.In the code,
next_day_extendedis missing a column, so it has only 4 columns instead of 5:- Initialize
next_day_extendedwith the same number of columns asnext_day_input:
next_day_extended = np.zeros((1, next_day_input.shape[2] + 1))- Assign the predicted value to the 4th column of , keeping the 5th column for 'Super Trend':
- Initialize
- User
- Assistant
To modify the code for predicting the next 5 days, you can create a
predict_next_daysfunction that takesmodel,X_test,scaler, andnext_daysas inputs. You can then loop for the specified number of days and use themodel.predictfunction at each step. Here's the modified code:def predict_next_days(model, X_test, scaler, next_days): predictions = [] for i in range(next_days): next_day_input = np.array([X_test[-1, 1:, :]]) next_day_pred = model.predict(next_day_input) predictions.append(next_day_pred[0, 0]) next_day_extended = np.zeros((1, next_day_input.shape[2])) next_day_extended[:, :4] = np.column_stack((next_day_input[0, -1, :3], next_day_pred)) next_day_extended[:, 4] = next_day_input[0, -1, 4] new_row = np.concatenate((next_day_input[0, 1:, :], next_day_extended), axis=0) X_test = np.append(X_test, [new_row], axis=0) predictions = np.array(predictions).reshape(-1, 1) predictions_inverse = scaler.inverse_transform(np.column_stack((np.zeros(predictions.shape), predictions)))[:, 1] return predictions_inverse # Define the number of days to predict and call the function next_days = 5 predictions = predict_next_days(model, X_test, scaler, next_days) # Plot the predictions plt.figure(figsize=(14, 6)) plt.plot(predictions, marker="o", label='Predicted') plt.xlabel('Days') plt.ylabel('Close Price') plt.title(f'{ticker} Stock Price Prediction for Upcoming {next_days} Days') plt.legend() plt.show() - User
ValueError Traceback (most recent call last) in <cell line: 3>() 1 # Define the number of days to predict and call the function 2 next_days = 5 ----> 3 predictions = predict_next_days(model, X_test, scaler, next_days) 4 5 # Plot the predictions
3 frames /usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in concatenate(*args, **kwargs)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 3 and the array at index 1 has size 1
- Assistant
Apologies for the mistake. I forgot to update the shape of
next_day_extended. Follow these steps to fix the issue:- Change the shape of
next_day_extendedto be the same asnext_day_inputby updating the dimensions:
next_day_extended = np.zeros((1, next_day_input.shape[2]))- Update the assignments for the
next_day_extendedaccordingly:
next_day_extended[:, :3] = next_day_input[0, -1, :3] next_day_extended[:, 3] = next_day_pred next_day_extended[:, 4] = next_day_input[0, -1, 4]Here's the corrected code for the
predict_next_daysfunction:def predict_next_days(model, X_test, scaler, next_days): predictions = [] for i in range(next_days): next_day_input = np.array([X_test[-1, 1:, :]]) next_day_pred = model.predict(next_day_input) predictions.append(next_day_pred[0, 0]) next_day_extended = np.zeros((1, next_day_input.shape[2])) next_day_extended[:, :3] = next_day_input[0, -1, :3] next_day_extended[:, 3] = next_day_pred next_day_extended[:, 4] = next_day_input[0, -1, 4] new_row = np.concatenate((next_day_input[0, 1:, :], next_day_extended), axis=0) X_test = np.append(X_test, [new_row], axis=0) predictions = np.array(predictions).reshape(-1, 1) predictions_inverse = scaler.inverse_transform(np.column_stack((np.zeros(predictions.shape), predictions)))[:, 1] return predictions_inverse - Change the shape of
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.