forked from Rajat-dhyani/Stock-Price-Predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearRegressionModel.py
35 lines (27 loc) · 1.03 KB
/
LinearRegressionModel.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
from sklearn import linear_model
import numpy as np
import stock_data as sd
def build_model(X, y):
"""
build a linear regression model using sklearn.linear_model
:param X: Feature dataset
:param y: label dataset
:return: a linear regression model
"""
linear_mod = linear_model.LinearRegression() # defining the linear regression model
X = np.reshape(X, (X.shape[0], 1))
y = np.reshape(y, (y.shape[0], 1))
linear_mod.fit(X, y) # fitting the data points in the model
return linear_mod
def predict_prices(model, x, label_range):
"""
Predict the label for given test sets
:param model: a linear regression model
:param x: testing features
:param label_range: normalised range of label data
:return: predicted labels for given features
"""
x = np.reshape(x, (x.shape[0], 1))
predicted_price = model.predict(x)
predictions_rescaled, re_range = sd.scale_range(predicted_price, input_range=[-1.0, 1.0], target_range=label_range)
return predictions_rescaled.flatten()