Gold Price Prediction Using Machine Learning in Python

In this tutorial, we will be predicting Gold Price by training on a Kaggle Dataset using machine learning in Python. This dataset from Kaggle contains all the depending factors that drive the price of gold. To achieve this, we will have to import various modules in Python. We will be using Google Colab To Code.

Modules can be directly installed through the “$ pip install” command in Colab in case they are not already present there.

We will be importing Pandas to import dataset, Matplotlib and Seaborn for visualizing the Data, sklearn for algorithms, train_test_split for splitting the dataset in testing and training set, classification report and accuracy_score for calculating accuracy of the model.

Various errors will be analyzed to check the overall accuracy. Plotting the graph will help us see how deviated the actual and predicted results are.

The Algorithm we will be using is Random Forest as it is a combination of several Decision Trees, so it has higher overall accuracy on all the models.

Let’s start by importing the necessary libraries

import numpy as np 

# data processing

import pandas as pd 
import numpy as np

# data visualization

import seaborn as sns
%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import style

Analyzing, cleaning and understanding the dataset of gold price

Reading the CSV file of the dataset and storing in “df”


It is really important to understand and know the dataset we are working with to yield better results.

Printing the information about the dataset
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2290 entries, 0 to 2289
Data columns (total 6 columns):
Date       2290 non-null object
SPX        2290 non-null float64
GLD        2290 non-null float64
USO        2290 non-null float64
SLV        2290 non-null float64
EUR/USD    2290 non-null float64
dtypes: float64(5), object(1)
memory usage: 107.5+ KB

Clearly we see there is no null value in the dataset, so no replacing with median values.
In case there are any NULL values in the dataset in a particular column then it should be replaced with values like the median or average of that particular column. Although the average is not preferred as then outliers are also taken into account.
Let us study the Statistical Inference of the dataset

Data Visualisation: Gold price prediction in Python

It is really important to visualize the data pictorially to get a flow of it, internal relationships,  and to see hidden patterns from graphical representation.

Plotting heatmap to analyze the dependency and relationship between features

import matplotlib.pyplot as plt
import seaborn as sns

corr = df.corr()
plt.figure(figsize = (6,5))
plt.title('Feature Corelation using Heatmap ', y = 1.12, size=13, loc="center")

Data Visualisation: Gold price prediction in Python

Printing the factors on which “GLD” factor depends on most in descending order

print (corr['GLD'].sort_values(ascending=False), '\n')
GLD        1.000000
SLV        0.866632
SPX        0.049345
EUR/USD   -0.024375
USO       -0.186360
Name: GLD, dtype: float64

Printing histograms to see layout of values for each feature

import matplotlib.pyplot as plt
df.hist(bins=50, figsize=(15, 10))

Printing histograms to see layout of values for each feature

Plotting sns pair plot to see pairwise relation between all the features

sns.pairplot(df.loc[:,df.dtypes == 'float64'])
Plotting sns pair plot to see pairwise relation between all the features
sns.distplot(df['GLD'], color = 'red')
print('Skewness: %f', df['GLD'].skew())
print("Kurtosis: %f" % df['GLD'].kurt())

Plotting sns pair plot to see pairwise relation between all the features

Joint Plot between two features
sns.jointplot(x =df['SLV'], y = df['GLD'])

Joint Plot between two features

Preparing a new feature with intensifying the most important feature driving the output

#Preparing a copy to woek on\
temp = df1[['SPX','USO','SLV','EUR/USD','new1']]
x = temp.iloc[:, :].values
y = df1.iloc[:, 2].values

Training and testing the new dataset and printing the accuracy and errors

Training and testing splitting

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)

from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators = 100, random_state = 0), y_train)
RandomForestRegressor(bootstrap=True, ccp_alpha=0.0, criterion='mse',
                      max_depth=None, max_features='auto', max_leaf_nodes=None,
                      max_samples=None, min_impurity_decrease=0.0,
                      min_impurity_split=None, min_samples_leaf=1,
                      min_samples_split=2, min_weight_fraction_leaf=0.0,
                      n_estimators=100, n_jobs=None, oob_score=False,
                      random_state=0, verbose=0, warm_start=False)

#storinng the "y_pred" label values
y_pred = regressor.predict(x_test)

Printing the RandomForest accuracy of the model

accuracy_train = regressor.score(x_train, y_train)
accuracy_test = regressor.score(x_test, y_test)
print("Training Accuracy: ", accuracy_train)
print("Testing Accuracy: ", accuracy_test)
Training Accuracy:  0.9984340783384931
Testing Accuracy:  0.9898570361228797

#Now Check the error for regression
from sklearn import metrics
print('MAE :'," ", metrics.mean_absolute_error(y_test,y_pred))
print('MSE :'," ", metrics.mean_squared_error(y_test,y_pred))
print('RMAE :'," ", np.sqrt(metrics.mean_squared_error(y_test,y_pred)))
MAE :   1.3028743574672486
MSE :   5.218041419378834
RMAE :   2.2843032678212483

#Visualising the Accuracy of Predicted result
plt.plot(y_test, color = 'red', label = 'Real Value')
plt.plot(y_pred, color = 'yellow', label = 'Predicted Value')

Gold Price Prediction Using Machine Learning in Python

Also read: Malaria Image prediction in Python using Machine Learning

Leave a Reply

Your email address will not be published. Required fields are marked *