Created
July 31, 2016 15:58
-
-
Save adamj57/2d2231c7e45460cccd63f6cbed11cfe5 to your computer and use it in GitHub Desktop.
Mała biblioteka w pythonie służąca do obliczania regresji.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import * | |
class Regression: | |
def __init__(self, x, y): | |
if len(x) == len(y): | |
self.x = x | |
self.y = y | |
else: | |
raise ValueError("x and y lists must be the same size!") | |
def predict_y(self, x): | |
raise NotImplementedError | |
def equasion(self): | |
raise NotImplementedError | |
def compute(self): | |
raise NotImplementedError | |
def __str__(self): | |
raise NotImplementedError | |
class Linear(Regression): | |
def __init__(self, x, y): | |
super().__init__(x, y) | |
self.orig_x, self.orig_y = x[:], y[:] | |
self.m, self.b = self.compute() | |
def predict_y(self, x): | |
return self.m * x + self.b | |
def equasion(self): | |
return "y=" + str(self.m) + "*x+(" + str(self.b) + ")" | |
def compute(self): | |
S = self.S(self.x) | |
Sx = self.Sx(self.x) | |
Sxx = self.Sxx(self.x) | |
Sy = self.Sy(self.y) | |
Sxy = self.Sxy(self.x, self.y) | |
delta = self.delta(S, Sx, Sxx) | |
m = self.M(S, Sx, Sy, Sxy, delta) | |
b = self.B(Sx, Sy, Sxx, Sxy, delta) | |
return m, b | |
def __str__(self): | |
return self.equasion() | |
def S(self, x): | |
return len(x) | |
def Sx(self, x): | |
s = 0 | |
for i in x: | |
s += i | |
return s | |
def Sy(self, y): | |
s = 0 | |
for i in y: | |
s += i | |
return s | |
def Sxx(self, x): | |
s = 0 | |
for i in x: | |
s += i ** 2 | |
return s | |
def Sxy(self, x, y): | |
s = 0 | |
for i in range(len(x)): | |
s += x[i] * y[i] | |
return s | |
def delta(self, S, Sx, Sxx): | |
return S * Sxx - (Sx**2) | |
def M(self, S, Sx, Sy, Sxy, delta): | |
return ((S * Sxy) - (Sx * Sy))/delta | |
def B(self, Sx, Sy, Sxx, Sxy, delta): | |
return ((Sxx * Sy) - (Sx * Sxy))/delta | |
def coefficient_of_determination(self): | |
sum1 = 0 | |
sum2 = 0 | |
avg_y = 0 | |
for i in self.orig_y: | |
avg_y += i | |
avg_y /= len(self.orig_y) | |
for item in self.orig_x: | |
sum1 += (self.predict_y(item) - avg_y)**2 | |
for item in self.orig_y: | |
sum2 += (item - avg_y)**2 | |
return sum2/sum1 | |
class LinearExtension(Linear): | |
def __init__(self, x, y, orig_x, orig_y): | |
self.orig_x, self.orig_y = orig_x, orig_y | |
super().__init__(x, y) | |
def log_all(self, x, log = log): | |
return list(map(lambda i: log(i), x)) | |
def without_zeros(self, to_check_for_zeros, another_to_delete): | |
offset = 0 | |
for i, item in enumerate(to_check_for_zeros[:]): | |
if item <= 0: | |
del to_check_for_zeros[i - offset] | |
del another_to_delete[i - offset] | |
offset += 1 | |
return to_check_for_zeros, another_to_delete | |
class Exponential(LinearExtension): | |
def __init__(self, x, y): | |
orig_x, orig_y = x[:], y[:] | |
y, x = super().without_zeros(y, x) | |
super().__init__(x, y, orig_x, orig_y) | |
def predict_y(self, x): | |
return self.b * (self.m ** x) | |
def equasion(self): | |
return "y=" + str(self.b) + "*" + str(self.m) + "^x" | |
def compute(self): | |
self.y = super().log_all(self.y, log10) | |
m, b = super().compute() | |
m = 10**m | |
b = 10**b | |
return m, b | |
class Power(LinearExtension): | |
def __init__(self, x, y): | |
orig_x, orig_y = x[:], y[:] | |
x, y = super().without_zeros(x, y) | |
y, x = super().without_zeros(y, x) | |
super().__init__(x, y, orig_x, orig_y) | |
def predict_y(self, x): | |
return self.b * x**self.m | |
def equasion(self): | |
return "y=" + str(self.b) + "*x^" + str(self.m) | |
def compute(self): | |
self.x = super().log_all(self.x, log10) | |
self.y = super().log_all(self.y, log10) | |
m, b = super().compute() | |
b = 10**b | |
return m, b | |
class Logarithmical10(LinearExtension): | |
def __init__(self, x, y): | |
orig_x, orig_y = x[:], y[:] | |
x, y = super().without_zeros(x, y) | |
super().__init__(x, y, orig_x, orig_y) | |
def predict_y(self, x): | |
return self.m * log10(x) + self.b | |
def equasion(self): | |
return "y=" + str(self.m) + "*log10(x)+(" + str(self.b) + ")" | |
def compute(self): | |
self.x = super().log_all(self.x, log10) | |
m, b = super().compute() | |
return m, b | |
class LogarythmicalE(LinearExtension): | |
def __init__(self, x, y): | |
orig_x, orig_y = x[:], y[:] | |
x, y = super().without_zeros(x, y) | |
super().__init__(x, y, orig_x, orig_y) | |
def predict_y(self, x): | |
return self.m * log(x) + self.b | |
def equasion(self): | |
return "y=" + str(self.m) + "*ln(x)+(" + str(self.b) + ")" | |
def compute(self): | |
self.x = super().log_all(self.x) | |
m, b = super().compute() | |
return m, b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment