pythonlstm

How can I fix this error : ValueError: X has 1 features, but MinMaxScaler is expecting 4 features as input


I am a beginner in programming and currently working on an assignment about predicting gold prices. I am stuck on the final part of the code, specifically the part that predicts the gold price for the next day, which is showing an error as mentioned in the title. The data I am using includes historical gold prices and influencing factors such as oil prices, USD exchange rates, and stock market index prices from 2000 to 2023. Please help me fix this issue. I have uploaded both my code and the dataset I am using to google drive. Here is the link:

https://drive.google.com/drive/u/3/folders/1qFjTYQGmyIgBMVep8jao5Ts23xM4vr2J

I have uploaded both my code and the dataset I am using to google drive. Here is the link:

https://drive.google.com/drive/u/3/folders/1qFjTYQGmyIgBMVep8jao5Ts23xM4vr2J


Solution

  • The shape of your train data is (4950, 50, 4), having 4 features. MinMaxScaler was fit initially, on all columns of the dataset.

    You can try using a different scaler for 'Gold_Price'

    sc_gold_price = MinMaxScaler(feature_range=(0, 1))
    sc_gold_price.fit(train_data[['Gold_Price']])
    next_closing_price = df['Gold_Price'].iloc[-1]
    next_closing_price_normalized = sc_gold_price.transform(np.array(next_closing_price).reshape(-1, 1))
    

    Hope that helps