forked from anubhavshrimal/Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_regression_sklearn.py
More file actions
95 lines (66 loc) · 2.74 KB
/
linear_regression_sklearn.py
File metadata and controls
95 lines (66 loc) · 2.74 KB
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
import quandl
import numpy as np
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle
import os
"""
# Needed if more than 50 request/day
quandl.ApiConfig.api_key = "Quandl_API_KEY"
"""
# Get the data set from quandl
df = quandl.get('WIKI/GOOGL')
df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
# Refine the data set to our needs
df['HL_Percent'] = ((df['Adj. High'] - df['Adj. Close']) / df['Adj. Close']) * 100
df['Percent_Change'] = ((df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open']) * 100
# Extract only the relevant features
df = df[['Adj. Close', 'HL_Percent', 'Percent_Change', 'Adj. Volume']]
# prediction column
forecast_col = 'Adj. Close'
# Replace null values in the dataset with a very small value so it has the least impact
df.fillna(-99999, inplace=True)
# Number of days in future that we want to predict the price for
future_days = 10
# define the label as Adj. Close future_days ahead in time
# shift Adj. Close column future_days rows up i.e. future prediction
df['label'] = df[forecast_col].shift(-future_days)
# Get the features array in X
X = np.array(df.drop(['label'], 1))
# Regularize the data set across all the features for better training
X = preprocessing.scale(X)
# Extract the last future_days rows for prediction as they don't have the values due to the shift
predict_X = X[-future_days:]
# Get the data for training and testing
X = X[:-future_days]
# Drop the last future_days rows as there is no label for them because we shifted the column up
df.dropna(inplace=True)
# Get the labels in y
y = np.array(df['label'])
# Shuffle the data and get Training and Testing data
# Testing data = 20% of total data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Load Classifier of linear regression
pickle_file_name = 'linear_regression_sklearn.pickle'
# If pre-trained pickle exists load it
if os.path.isfile('./' + pickle_file_name):
# Load the classifier from the pre-trained pickle
pickle_file = open(pickle_file_name, 'rb')
clf = pickle.load(pickle_file)
# Otherwise train the classifier and save it in a pickle
else:
# n_jobs = -1 means training the model parallely, as many jobs as possible
clf = LinearRegression(n_jobs=-1)
# train the model on training data
clf.fit(X_train, y_train)
# save the pickle
with open(pickle_file_name, 'wb') as f:
pickle.dump(clf, f)
# Test the accuracy of the data on the testing data set
# How well is the model predicting the future prices
accuracy = clf.score(X_test, y_test)
print('Accuracy on test data:', accuracy)
predictions = clf.predict(predict_X)
print('Predictions for next 10 days: ')
print(predictions)