-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_regression.py
74 lines (60 loc) · 2.35 KB
/
linear_regression.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
from __future__ import absolute_import, division, print_function, unicode_literals
import statistics
import pathlib
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
print(tf.__version__)
from src import data_refactoring
def get_stocks(stocks):
stock_list = []
price_list = []
mean_list = []
std_list = []
for stock in stocks:
growth = np.array(stock.growth)
returns = np.array(stock.returns)
multiple = np.array(stock.multiple)
value = np.array(stock.value)
matrix_of_stock = np.column_stack((growth, returns, multiple))
mean_list.append(np.mean(growth))
mean_list.append(np.mean(returns))
mean_list.append(np.mean(multiple))
mean_list.append(np.mean(value))
std_list.append(np.stdev(growth))
std_list.append(np.stdev(returns))
std_list.append(np.stdev(multiple))
std_list.append(np.stdev(value))
stock_list.append(matrix_of_stock)
price_list.append(value)
return stock_list, price_list, mean_list, std_list
def normalize(growth, returns, multiple, value, mean, std):
index = 0
for i in range(mean):
if index % 4 == 0:
growth[(index / 4) % 1] = (growth[(index / 4) % 1] - mean[i])/std[i]
if index % 4 == 1:
returns[(index / 4) % 1] = (returns[(index / 4) % 1] - mean[i])/std[i]
if index % 4 == 2:
multiple[(index / 4) % 1] = (multiple[(index / 4) % 1] - mean[i])/std[i]
if index % 4 == 3:
value[(index / 4) % 1] = (value[(index / 4) % 1] - mean[i]) / std[i]
index +=1
return growth, returns, multiple, value
def regression(X , Y):
X_total = pd.DataFrame({'growth': X[:, 0]}, {'returns': X[:, 1]}, {'multiple': X[:, 2]})
Y_total = pd.DataFrame({'price': Y[:, 0]})
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=[len(X_total.keys())]),
layers.Dense(64, activation='relu'),
layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mse',
optimizer=optimizer,
metrics=['mae', 'mse'])
model.summary()