-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
33 lines (25 loc) · 877 Bytes
/
utils.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 numpy import mean, sum
from pickle import load
from os import path
def load_automaton_data(model_name, simulation_index):
""" Loads a particular automaton data of a given model """
model_path = path.join("models", model_name)
file_path = path.join(path.dirname(__file__), model_path, f"simulation_{simulation_index}.pkl")
with open(file_path, 'rb') as file:
return load(file)
def perform_linear_regression(x, y):
""" Performs a linear regression on the data """
x_mean = mean(x)
y_mean = mean(y)
nume = 0
deno = 0
for i in range(len(x)):
nume += (x[i] - x_mean) * (y[i] - y_mean)
deno += (x[i] - x_mean)**2
m = nume / deno
c = y_mean - m * x_mean
y_hat = m * x + c
SSE = sum((y - y_hat)**2)
SST = sum((y - y_mean)**2)
r_squared = 1 - (SSE / SST)
return m, c, r_squared