@@ -580,25 +580,79 @@ Now it is time to prepare our testing data and send it through our deep-learning
580580First, we need to import the test data using the same approach we used for the training data using the time steps:
581581
582582``` python
583- # Testing Data Transformation
584- x_test = []
585- y_test = []
586- for i in range (time_steps, test_scaled.shape[0 ]):
587- x_test.append(test_scaled[i - time_steps:i])
588- y_test.append(test_scaled[i, 0 ])
589-
590- x_test, y_test = np.array(x_test), np.array(y_test)
591- x_test = np.reshape(x_test, (x_test.shape[0 ], x_test.shape[1 ], 1 ))
583+ def infer_data ():
584+ print (tf.version.VERSION )
585+ inference_folder = os.path.join(os.getcwd(), RUN_FOLDER )
586+ stock = StockPrediction(STOCK_TICKER , STOCK_START_DATE , STOCK_VALIDATION_DATE , inference_folder, GITHUB_URL , EPOCHS , TIME_STEPS , TOKEN , BATCH_SIZE )
587+
588+ data = StockData(stock)
589+
590+ (x_train, y_train), (x_test, y_test), (training_data, test_data) = data.download_transform_to_numpy(TIME_STEPS , inference_folder)
591+ min_max = data.get_min_max()
592+
593+ # load future data
594+ print (' Latest Stock Price' )
595+ latest_close_price = test_data.Close.iloc[- 1 ]
596+ latest_date = test_data[- 1 :][' Close' ].idxmin()
597+ print (latest_close_price)
598+ print (' Latest Date' )
599+ print (latest_date)
600+
601+ tomorrow_date = latest_date + timedelta(1 )
602+ # Specify the next 300 days
603+ next_year = latest_date + timedelta(TIME_STEPS * 100 )
604+
605+ print (' Future Date' )
606+ print (tomorrow_date)
607+
608+ print (' Future Timespan Date' )
609+ print (next_year)
610+
611+ x_test, y_test, test_data = data.generate_future_data(TIME_STEPS , min_max, tomorrow_date, next_year, latest_close_price)
612+
613+ # Check if the future data is not empty
614+ if x_test.shape[0 ] > 0 :
615+ # load the weights from our best model
616+ model = tf.keras.models.load_model(os.path.join(inference_folder, ' model_weights.h5' ))
617+ model.summary()
618+
619+ # perform a prediction
620+ test_predictions_baseline = model.predict(x_test)
621+ test_predictions_baseline = min_max.inverse_transform(test_predictions_baseline)
622+ test_predictions_baseline = pd.DataFrame(test_predictions_baseline, columns = [' Predicted_Price' ])
623+
624+ # Combine the predicted values with dates from the test data
625+ predicted_dates = pd.date_range(start = test_data.index[0 ], periods = len (test_predictions_baseline))
626+ test_predictions_baseline[' Date' ] = predicted_dates
627+
628+ # Reset the index for proper concatenation
629+ test_data.reset_index(inplace = True )
630+
631+ # Concatenate the test_data and predicted data
632+ combined_data = pd.concat([test_data, test_predictions_baseline], ignore_index = True )
633+
634+ # Plotting predictions
635+ plt.figure(figsize = (14 , 5 ))
636+ plt.plot(combined_data[' Date' ], combined_data.Close, color = ' green' , label = ' Simulated [' + STOCK_TICKER + ' ] price' )
637+ plt.plot(combined_data[' Date' ], combined_data[' Predicted_Price' ], color = ' red' , label = ' Predicted [' + STOCK_TICKER + ' ] price' )
638+ plt.xlabel(' Time' )
639+ plt.ylabel(' Price [USD]' )
640+ plt.legend()
641+ plt.title(' Simulated vs Predicted Prices' )
642+ plt.savefig(os.path.join(inference_folder, STOCK_TICKER + ' _future_comparison.png' ))
643+ plt.show()
644+ else :
645+ print (" Error: Future data is empty." )
646+
647+ start_date = pd.to_datetime(' 2017-01-01' )
648+ end_date = datetime.today()
649+ duration = end_date - start_date
650+ STOCK_VALIDATION_DATE = start_date + 0.8 * duration
592651```
593652
594- Now we can call the predict method which will allow us to generate the stock prediction based on the training done over the training data. As a result, we will generate a csv file that contains the result of the prediction and also a chart that shows what's the real vs the estimation.
653+ Now we can call the ` infer_data ` method which will allow us to generate the stock prediction based on the training done over the training data. As a result, we will generate a csv file that contains the result of the prediction and also a chart that shows what's the real vs the estimation.
595654
596- ![ ] ( https://github.com/JordiCorbilla/stock-prediction-deep-neural-learning/raw/master/Alphabet%20Inc_prediction.png )
597-
598- With the validation loss and validation MSE metrics:
599-
600- ![ ] ( https://github.com/JordiCorbilla/stock-prediction-deep-neural-learning/raw/master/loss.png )
601- ![ ] ( https://github.com/JordiCorbilla/stock-prediction-deep-neural-learning/raw/master/MSE.png )
655+ ![ image] ( https://github.com/JordiCorbilla/stock-prediction-deep-neural-learning/assets/7347994/36ce113b-acf4-4245-bf1e-771e77aeb0a1 )
602656
603657# 4) Usage
604658
0 commit comments