-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockLinearRegressionOpenClose.py
More file actions
88 lines (67 loc) · 2 KB
/
StockLinearRegressionOpenClose.py
File metadata and controls
88 lines (67 loc) · 2 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
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 17 01:19:48 2018
@author: atulr
"""
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 17 00:05:38 2018
@author: atulr
"""
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error, r2_score
import pandas as pd
# Load the stock dataset
stock = pd.read_csv('./Stockdata/ISOAX.csv')
#FUEL 6.500
#ETHI 7.790
#ISO 5.160
#QRE 6.050
#MVS 20.360
lastClose=5.00
stock=stock.sort_values(by='Date',ascending=False)
stock=stock.dropna()
# Open hig low
#stock_X = stock[['Open', 'High', 'Low']]
#Based on One
stock_X = stock['Open']
stock_y = stock[ 'Close']
# Split the data into training/testing sets
stock_X_train = stock_X[:-20]
stock_X_test = stock_X[-20:]
#stock_X_train = stock_X_train
#stock_X_test = stock_X_test.reshape(1,-1)
# Split the targets into training/testing sets
stock_y_train =stock_y[:-20]
stock_y_test = stock_y[-20:]
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(stock_X_train.values.reshape(-1,1), stock_y_train)
# Make predictions using the testing set
stock_y_pred = regr.predict(stock_X_test.values.reshape(-1,1))
# The coefficients
print('Coefficients: \n', regr.coef_)
# The mean squared error
print("Mean squared error: %.2f"
% mean_squared_error(stock_y_test, stock_y_pred))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(stock_y_test, stock_y_pred))
#7.620000,7.630000,7.550000,7.570000,7.447722,228953
#6.260000,6.260000,6.240000,6.250000
predictionData = np.array([[lastClose]])
#predictionData1 = np.array([[6.520]])
print((regr.predict(predictionData)))
#print((regr.predict(predictionData1)))
# Plot outputs
#plt.scatter(stock_X_test, stock_y_test, color='black')
#plt.plot(stock_X_test, stock_y_pred, color='blue', linewidth=3)
#
#plt.xticks(())
#plt.yticks(())
#
#plt.show()
#```