-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code3.py
102 lines (86 loc) · 3.55 KB
/
Code3.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
import pandas as pd
import numpy as np
import pandas_ta as ta
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score
class Data:
def __init__(file,data):
file.date = data['Date']
file.open = data['Open']
file.high = data['High']
file.low = data['Low']
file.close = data['Close']
file.adj_close = data['Adj Close']
file.volume = data['Volume']
def google():
df_google = pd.read_csv("GOOG.csv")
googledata = Data(df_google)
df_google = df_google[['Close']]
predictime = 30
df_google['Prediction'] = df_google[['Close']].shift(-predictime)
X = np.array(df_google.drop(['Prediction'],1))
X = X[:-predictime]
y = np.array(df_google['Prediction'])
y = y[:-predictime]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
Gmodel = LinearRegression().fit(x_train, y_train)
Gpred = Gmodel.predict(x_test)
Gtrain = Gmodel.predict(x_train)
print("Google Model Coefficients:", Gmodel.coef_)
print("Mean Absolute Error for Testing data:", mean_absolute_error(y_test, Gpred))
print("Mean Absolute Error for Training data:", mean_absolute_error(y_train, Gtrain))
print("Coefficient of Determination:", r2_score(y_test, Gpred))
model_test = Gmodel.predict(x_test)
xtm, ytm = zip(*sorted(zip(x_test, model_test)))
x_test, y_test = zip(*sorted(zip(x_test, y_test)))
plt.scatter(x_test,y_test, 5,label = 'Actual', color ='red')
plt.plot(xtm, ytm, '-', label = 'Model')
plt.legend()
plt.title('Google')
plt.show()
#plotting model for data that was NOT given
x_predict = np.array(df_google.drop(['Prediction'],1))[-predictime:]
model_predict = Gmodel.predict(x_predict)
xm, ym = zip(*sorted(zip(x_predict, model_predict)))
plt.plot(xm, ym, '-', label = 'Model')
plt.legend()
plt.title('Model Prediction for 30 days into the future')
plt.show()
def apple():
df_apple = pd.read_csv("AAPL.csv")
appledata = Data(df_apple)
df_apple = df_apple[['Close']]
predictime = 30
df_apple['Prediction'] = df_apple[['Close']].shift(-predictime)
X = np.array(df_apple.drop(['Prediction'],1))
X = X[:-predictime]
y = np.array(df_apple['Prediction'])
y = y[:-predictime]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
Amodel = LinearRegression().fit(x_train, y_train)
Apred = Amodel.predict(x_test)
Atrain = Amodel.predict(x_train)
print("Google Model Coefficients:", Amodel.coef_)
print("Mean Absolute Error for Testing data:", mean_absolute_error(y_test, Apred))
print("Mean Absolute Error for Training data:", mean_absolute_error(y_train, Atrain))
print("Coefficient of Determination:", r2_score(y_test, Apred))
model_test = Amodel.predict(x_test)
xtm, ytm = zip(*sorted(zip(x_test, model_test)))
x_test, y_test = zip(*sorted(zip(x_test, y_test)))
plt.scatter(x_test,y_test, 5,label = 'Actual', color ='red')
plt.plot(xtm, ytm, '-', label = 'Model')
plt.title('Apple')
plt.legend()
plt.show()
#plotting model for data that was NOT given
x_predict = np.array(df_apple.drop(['Prediction'],1))[-predictime:]
model_predict = Amodel.predict(x_predict)
xm, ym = zip(*sorted(zip(x_predict, model_predict)))
plt.plot(xm, ym, '-', label = 'Model')
plt.legend()
plt.title('Model Prediction for 30 days into the future')
plt.show()
google()
apple()