-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregressionintro.py
124 lines (76 loc) · 2.63 KB
/
regressionintro.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# coding: utf-8
# In[20]:
import pandas as pd
import math, datetime
import numpy as np
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from matplotlib import style
import pickle
style.use('ggplot')
# In[21]:
#Reading data
df = pd.read_csv('PETR4.SA.csv', header=0, index_col='Date', parse_dates=True)
print(df.head())
# In[22]:
# High - Low percentage
df['HL_PCT'] = (df['High'] - df['Low']) / df['Low'] * 100.0
# Percentage change
df['PCT_change'] = (df['Close'] - df['Open']) / df['Open'] * 100.0
df = df[['Close', 'HL_PCT', 'PCT_change', 'Volume']]
# In[23]:
# Select wich col will be used to forecast the prices
forecast_col = 'Close'
# Fill the NaN values with -99999, the algorithm will ignore these values
df.fillna(-99999, inplace=True)
# The number of predictions that will be done
forecast_out = int(math.ceil(0.01*len(df)))
print(forecast_out)
# Put all the values up the forecast_out number
df['label'] = df[forecast_col].shift(-forecast_out)
# In[24]:
X = np.array(df.drop(['label'], 1))
# Processing the entire data ignoring the 'label' col
X = preprocessing.scale(X)
# Array of predictions
X_lately = X[-forecast_out:]
X = X[:-forecast_out]
df.dropna(inplace=True)
y = np.array(df['label'])
# In[25]:
# Setting up the machine learning algorithm
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2)
# This is an example to use with big data, we don't need to train data every time we run the program
# with Pickle we can save the data trained before.
##clf = LinearRegression(n_jobs=-1)
##clf.fit(X_train, y_train)
##with open('linearregression.pickle', 'wb') as f:
## pickle.dump(clf, f)
# And now we read the file created by pickle
pickle_in = open('linearregression.pickle', 'rb')
clf = pickle.load(pickle_in)
# This is an interesting value, this determines how much our algorithm can be "trusted" in percentage
accuracy = clf.score(X_test, y_test)
print(accuracy)
# In[26]:
# Predicting some values
forecast_set = clf.predict(X_lately)
print(forecast_set, accuracy, forecast_out)
# In[27]:
# Plotting a graph with real prices and forecasts generated by our algorithm
df['Forecast'] = np.nan
last_date = df.iloc[-1].name
last_unix = last_date.timestamp()
one_day = 86400
next_unix = last_unix + one_day
for i in forecast_set:
next_date = datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
df.loc[next_date] = [np.nan for _ in range(len(df.columns)-1)] + [i]
df['Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()