I have recently been brushing up on my statistics and calculus and wanted to implement Linear Regression, the code will be for a calculus/statistics library I am working on (I know there are libraries for this but I am trying improve my both coding and math skills).
The following code works as intended. However, I feel like it has some structural flaws and I can't put my finger on what it is
class LinearReg:
x = []
y = []
x_mean = 0
y_mean = 0
b_zero = 0
slope = 0
def __init__(self,x,y):
if len(x) != len(y):
raise Error("Both axis must have the same number of values")
self.x = x
self.y = y
self.x_mean = self.axis_mean(self.x)
self.y_mean = self.axis_mean(self.y)
def axis_mean(self,axis):
return sum(axis) / len(axis)
def sum_of_deviation_products(self):
result = 0
for i in range(len(self.y)):
x_dev = (self.x[i] - self.x_mean)
y_dev = (self.y[i] - self.y_mean)
result += x_dev * y_dev
return result
def sum_of_x_deviation_squared(self):
result = 0
for i in range(len(self.x)):
result += (self.x[i] - self.x_mean)**2
return result
def get_b_zero(self):
return self.b_zero
def get_slope(self):
self.slope = self.sum_of_deviation_products() / self.sum_of_x_deviation_squared()
return self.slope
def fit_best_line(self):
self.b_zero = self.y_mean - (self.get_slope() * self.x_mean)
print("The best line equation is: " +
"%.2f" % self.slope
+ "x + " +
"%.2f" % self.b_zero)
My primary focus is now: structure and cleanliness,
- How efficient would this play out in a large library?
- How clean and readable is the code?
- What kind of problems would I face if this class were to interact with other classes in the library?
Perhaps there is quicker way to do Linear Regression in Theory, but I'm not really interested in that right now.