-
Notifications
You must be signed in to change notification settings - Fork 0
/
gold_price_prediction_using_ml.py
88 lines (53 loc) · 1.84 KB
/
gold_price_prediction_using_ml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
"""Gold Price Prediction Using ML.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1E8nMsbWy72jW9107hmKE3ert2xd9DEC2
**Importing the Libraries**
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn import metrics
"""**Data Collection and Processing **
"""
gold_data = pd.read_csv('/content/gld_price_data.csv')
gold_data.head()
gold_data.tail()
gold_data.shape
gold_data.info()
gold_data.isnull().sum()
gold_data.describe()
"""**Checking Correlation**"""
correlation = gold_data.corr()
plt.figure(figsize = (8,8))
sns.heatmap(correlation, cbar=True, square=True, fmt='.1f',annot=True, annot_kws={'size':8}, cmap='Blues')
print(correlation['GLD'])
sns.distplot(gold_data['GLD'],color='green')
"""**Splitting the Features and Target**"""
x = gold_data.drop(['Date','GLD'],axis=1)
y = gold_data['GLD']
x
y
"""**Splitting into Training data and Test Data**"""
X_train,X_test,Y_train,Y_test = train_test_split(x,y,test_size=0.2,random_state=2)
"""**Model Training: Random Forest Regressor**"""
regressor = RandomForestRegressor(n_estimators=100)
regressor.fit(X_train,Y_train)
"""**Model Evaluation**"""
test_data_prediction = regressor.predict(X_test)
print(test_data_prediction)
error_score = metrics.r2_score(Y_test, test_data_prediction)
print('R squared error :', error_score)
"""**Compare the Actual Values and Predicted Values**"""
Y_test = list(Y_test)
plt.plot(Y_test, color ='blue', label='Actual Value')
plt.plot(test_data_prediction, color='green', label='predicted value')
plt.title('actual vs predicted')
plt.xlabel('no. of values')
plt.ylabel('gld prices')
plt.legend()
plt.show()