-
Notifications
You must be signed in to change notification settings - Fork 391
/
trend-predictions.py
62 lines (47 loc) · 1.7 KB
/
trend-predictions.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
# importing libraries
import pandas as pd
import numpy as np
import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt
# Import data
s = 'NBEV'
start_date = datetime.datetime(1984,1,1)
end_date = datetime.date.today()
# Get the stock data
df = web.DataReader(s, 'yahoo', start_date, end_date)
# looking at the first five rows of the data
print(df.head())
print('\n Shape of the data:')
print(df.shape)
#creating dataframe with date and the target variable
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Close'])
for i in range(0,len(data)):
new_data['Close'][i] = data['Close'][i]
# NOTE: While splitting the data into train and validation set, we cannot use random splitting since that will destroy the time component. So here we have set the last year’s data into validation and the 4 years’ data before that into train set.
# splitting into train and validation
train = new_data[:987]
valid = new_data[987:]
# shapes of training set
print('\n Shape of training set:')
print(train.shape)
# shapes of validation set
print('\n Shape of validation set:')
print(valid.shape)
# In the next step, we will create predictions for the validation set and check the RMSE using the actual values.
# making predictions
preds = []
for i in range(0,valid.shape[0]):
a = train['Close'][len(train)-248+i:].sum() + sum(preds)
b = a/248
preds.append(b)
# checking the results (RMSE value)
rms=np.sqrt(np.mean(np.power((np.array(valid['Close'])-preds),2)))
print('\n RMSE value on validation set:')
print(rms)
#plot
valid['Predictions'] = 0
valid['Predictions'] = preds
plt.plot(train['Close'])
plt.plot(valid[['Close', 'Predictions']])