/Lstm_Time_SeriesV2

The code downloads Microsoft stock data, preprocesses it, builds and trains a deep learning model, and visualizes predictions.

Primary LanguageJupyter Notebook

This code is a Python script for analyzing Microsoft stock data using time series analysis techniques. Let's break down the steps:

  1. Setting up Kaggle API: The script begins by setting up the Kaggle API to download the dataset. It copies the Kaggle API key file (kaggle.json) to the appropriate directory and sets permissions.

  2. Downloading and Unzipping Data: It then downloads a dataset named "microsoft-stock-time-series-analysis" from Kaggle and unzips it.

  3. Importing Libraries: After the setup, it imports necessary libraries including pandas, numpy, and matplotlib.

  4. Reading and Preprocessing Data: The script reads the Microsoft stock data from a CSV file named "Microsoft_Stock.csv" into a pandas DataFrame. It checks for missing values, converts the "Date" column to datetime format, sets the date column as the index, and drops the "Date" column.

  5. Visualizing Data: It plots the first 1000 data points of the "Close" price as the training data (in blue) and the remaining data points as the test data (in orange).

  6. Data Preparation: The script prepares the data for training by creating input-output pairs. It defines a function data_creation() to create sequences of data points with a specified window size for input features (X) and corresponding target values (y).

  7. Train and Test Split: It splits the data into training and test sets.

  8. Model Building: It builds a deep learning model using Keras. The model consists of two LSTM layers followed by dropout layers and dense layers. The model is compiled with mean squared error (MSE) loss function and Adam optimizer.

  9. Model Training: The model is trained on the training data for 200 epochs.

  10. Model Evaluation: It predicts the stock prices on the test data and plots the real stock prices against the predicted ones.

  11. Making Predictions: It makes a single prediction for the stock price at a given index.

  12. Visualizing Predictions: It plots the predicted stock prices along with the training data and indicates the prediction line.

Overall, this script demonstrates a workflow for time series analysis and forecasting of Microsoft stock prices using deep learning techniques.