Gold Price Prediction Using RandomForestRegressor and ML
Gold Price Prediction Using RandomForestRegressor and ML
July 5, 2024
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2290 entries, 0 to 2289
1
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 2290 non-null object
1 SPX 2290 non-null float64
2 GLD 2290 non-null float64
3 USO 2290 non-null float64
4 SLV 2290 non-null float64
5 EUR/USD 2290 non-null float64
dtypes: float64(5), object(1)
memory usage: 107.5+ KB
[5]: (2290, 6)
[7]: Date 0
SPX 0
GLD 0
USO 0
SLV 0
EUR/USD 0
dtype: int64
Correlation
[12]: # Assuming 'gold_data' is your DataFrame
# Convert the date column to datetime objects
gold_data['Date'] = pd.to_datetime(gold_data['Date']) # Replace 'Date' with␣
↪the actual column name
2
# Extract numerical features from the datetime object if needed
gold_data['Year'] = gold_data['Date'].dt.year
gold_data['Month'] = gold_data['Date'].dt.month
gold_data['Day'] = gold_data['Date'].dt.day
[13]: #HeatMap
plt.figure(figsize=(8,8))
sns.heatmap(correlation,cbar=True,square=True,fmt='.
↪1f',annot=True,annot_kws={'size':8},cmap='Blues')
3
[14]: #correlation Of GLD
print(correlation['GLD'])
Date 0.209118
SPX 0.049345
GLD 1.000000
USO -0.186360
SLV 0.866632
EUR/USD -0.024375
Year 0.206654
Month 0.020494
Day -0.000198
Name: GLD, dtype: float64
<ipython-input-16-7518d28b8e5b>:2: UserWarning:
Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).
For a guide to updating your code to use the new functions, please see
https://gist.github.com/mwaskom/de44147ed2974457ad6372750bbe5751
sns.distplot(gold_data['GLD'], color='red')
4
Spliting Features and Target
[17]: X=gold_data.drop(['Date','GLD'],axis=1)
Y=gold_data['GLD']
[19]: print(X)
5
[20]: print(Y)
0 84.860001
1 85.570000
2 85.129997
3 84.769997
4 86.779999
…
2285 124.589996
2286 124.330002
2287 125.180000
2288 124.489998
2289 122.543800
Name: GLD, Length: 2290, dtype: float64
Spliting Into Training And Test data
[22]: X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2,random_state=2)
[23]: print(X.shape,X_train.shape,X_test.shape)
[26]: regressor.fit(X_train,Y_train)
[26]: RandomForestRegressor()
Model Evaluation
[27]: #prediction on test Data
test_data_prediction=regressor.predict(X_test)
[ ]: print(test_data_prediction)
6
[33]: [<matplotlib.lines.Line2D at 0x7f5c1fe04fd0>]
This project used Random Forest Regression to predict gold prices, achieving high accuracy by
leveraging historical data and economic indicators. The model’s effectiveness highlights the poten-
tial of machine learning in financial forecasting, offering valuable insights for investors and analysts.
1 Thank You!