Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Gold Price Prediction

Predict the GLD (Gold ETF) closing price in real time - powered by Machine Learning
11 global market indicators Β Β·Β  7 years of daily trading data Β Β·Β  RΒ² β‰ˆ 99.8%


Python Flask scikit-learn Pandas NumPy License: MIT


GitHub stars GitHub forks GitHub issues


πŸ“Œ Table of Contents


πŸ” Overview

This project builds a complete end-to-end machine learning pipeline to predict the price of the GLD ETF (Gold Exchange-Traded Fund) based on correlated global market indicators.

The workflow covers:

  1. Exploratory Data Analysis (EDA) - distributions, trends, outlier detection, and correlation analysis
  2. Model Training & Comparison - Linear Regression, Decision Tree, Random Forest, and KNN evaluated head-to-head
  3. Model Selection - KNN (k=2) selected via elbow-curve analysis for the lowest error
  4. Web Application - Interactive Flask app where users enter market values and get an instant GLD price prediction

🌐 Live Demo

Run locally following the Getting Started steps below.

Route Page Description
/ Home Project overview, dataset statistics & live model metrics
/predict Predict Enter 11 market values β†’ get instant GLD price estimate
/about About Dataset info, model details & feature breakdown

✨ Features

🎯 Real-Time Prediction

Enter 11 market indicators and receive an instant GLD price estimate - no page reload needed.

πŸ“ˆ High-Accuracy Model

KNN Regressor tuned to k=2 via elbow-curve analysis achieves RΒ² β‰ˆ 0.998.

πŸ“± Responsive Design

Modern UI built with Inter typography, Font Awesome icons, and a clean gold-accented palette.

πŸ““ Full EDA Notebook

Jupyter notebook with exploratory analysis, feature correlations, and model comparison.

πŸ› οΈ Tech Stack

Layer Technology
Backend Python 3.8+, Flask 2.3+
ML / Data scikit-learn, Pandas, NumPy
Frontend HTML5, CSS3, Vanilla JS
Fonts & Icons Google Fonts (Inter), Font Awesome 6
ML Algorithm K-Nearest Neighbours Regressor
Deployment Gunicorn (production WSGI)

🧠 How It Works

 Market Inputs  ──►  StandardScaler  ──►  KNN Regressor (k=2)  ──►  GLD Price
 (11 features)        (fitted on            (80/20 split,              (USD)
                       train set)            RΒ² β‰ˆ 0.998)
  1. Data - 1,718 daily rows of global market data (Jan 2011 – Dec 2018) loaded from FINAL_USO.csv.
  2. Preprocessing - Features scaled with StandardScaler fitted only on the training set to prevent data leakage.
  3. Model Selection - Elbow-curve plotted for k = 1 to 20; k = 2 minimises error without overfitting.
  4. Prediction - User inputs are scaled with the same scaler, then passed to the trained KNN model for an instant result.

πŸ“Š Model Performance

All models trained on 80% of the data and evaluated on the remaining 20% (random_state=0).

Model RΒ² MAE RMSE
πŸ₯‡ KNN (k=2) ← selected 0.9983 $0.59 $1.10
Random Forest 0.9961 $0.78 $1.41
Decision Tree 0.9934 $0.91 $1.83
Multiple Linear Regression 0.9612 $2.44 $4.45
Simple Linear Regression 0.8741 $4.37 $8.01

KNN with k=2 was selected as the best model based on elbow-curve analysis (k=1 to 20).
It explains 99.83% of variance in GLD closing prices on completely unseen test data.


πŸ“ Dataset Overview

Property Detail
Source Kaggle - Gold Price Prediction Dataset
Source file Dataset/FINAL_USO.csv
Records 1,718 daily rows
Date Range January 2011 – December 2018
Total Columns 81
Features Used 11
Target Column Close - GLD ETF daily closing price (USD)
Missing Values βœ… None
Duplicates βœ… None

πŸ”‘ 11 Input Features

Click to expand feature descriptions
# Feature Description Correlation w/ GLD
1 SP_close S&P 500 Index - daily closing value +0.63
2 DJ_close Dow Jones Industrial Average - daily close +0.61
3 EU_Price EUR / USD exchange rate -0.51
4 OF_Price Crude Oil Futures price (USD / barrel) +0.29
5 SF_Price Silver Futures price +0.87
6 PLT_Price Platinum Futures price +0.55
7 PLD_Price Palladium Futures price +0.38
8 USDI_Price US Dollar Index -0.74
9 GDX_Close VanEck Gold Miners ETF - daily close +0.92
10 USO_Close United States Oil Fund ETF - daily close -0.19
11 RHO_PRICE Rhodium spot price +0.41

πŸ—‚οΈ Project Structure

Gold_Price_Prediction/
β”‚
β”œβ”€β”€ πŸ“„ app.py                  ← Flask app, model training & API routes
β”œβ”€β”€ πŸ“‹ requirements.txt        ← Python dependencies
β”œβ”€β”€ πŸ““ Gold_predict.ipynb      ← EDA, model comparison, elbow-curve analysis
β”‚
β”œβ”€β”€ πŸ“‚ Dataset/
β”‚   └── FINAL_USO.csv          ← Historical market data (2011–2018)
β”‚
β”œβ”€β”€ πŸ“‚ templates/
β”‚   β”œβ”€β”€ index.html             ← Home - stats dashboard
β”‚   β”œβ”€β”€ predict.html           ← Prediction form with live results
β”‚   └── about.html             ← Methodology & feature breakdown
β”‚
└── πŸ“‚ static/
    └── style.css              ← Global stylesheet (gold-accented theme)

βš™οΈ Getting Started

Prerequisites

  • Python 3.8+
  • pip

1 - Clone the Repository

git clone https://github.com/anubhab1601/Gold_Price_Prediction.git
cd Gold_Price_Prediction

2 - Create & Activate a Virtual Environment

# Create
python -m venv venv

# Activate - Windows
venv\Scripts\activate

# Activate - macOS / Linux
source venv/bin/activate

3 - Install Dependencies

pip install -r requirements.txt

4 - Run the App

python app.py

Open http://127.0.0.1:5001 in your browser. πŸŽ‰


οΏ½ Usage

Predict via the Web UI

Navigate to http://127.0.0.1:5001/predict, fill in the 11 market indicator fields, and click Predict.

Run the Jupyter Notebook (EDA)

jupyter notebook Gold_predict.ipynb

πŸ““ Notebook

Gold_predict.ipynb walks through the full ML pipeline:

Section Content
EDA Distributions, box plots, correlation heatmap, pairplot, outlier analysis
Feature Engineering Selection of the 11 most predictive market indicators
Model Comparison Simple LR, Multiple LR, Decision Tree, Random Forest, KNN
Elbow Curve RMSE vs k (1–20) to identify optimal k = 2
Final Evaluation MAE, MSE, RMSE, RΒ² on held-out test set

πŸ“ˆ EDA Highlights

  • No missing values and no duplicate rows found in the dataset
  • GDX_Close (Gold Miners ETF) has the strongest positive correlation with GLD (+0.92)
  • SF_Price (Silver) is the second most correlated feature (+0.87)
  • USDI_Price (US Dollar Index) shows the strongest inverse relationship (-0.74)
  • USO_Close (Oil ETF) has the highest negative correlation among commodity features (-0.19)
  • Outlier analysis performed via IQR method on skewed distributions
  • Heatmap confirms multicollinearity between equity indices (SP_close, DJ_close)

🀝 Contributing

Contributions, issues and feature requests are welcome!

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/amazing-feature
  3. Commit your changes
    git commit -m "feat: add amazing feature"
  4. Push to the branch
    git push origin feature/amazing-feature
  5. Open a Pull Request πŸš€

Please follow Conventional Commits for commit messages.


πŸ“„ License

Distributed under the MIT License. See LICENSE for more information.


Built with ❀️ by Anubhab


If this project helped you, please consider giving it a ⭐ - it means a lot!


GitHub followers

About

AI-powered GLD ETF price prediction using KNN Regression | Flask web app with 11 market indicators | R2 ~ 99.8%

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages