pythonlinear-regressionleast-squares

Is there any function for calculating k and b coefficients for linear regression model with only one independent variable?


I know I can just write needed method by myself but there must be a function for this because this problem is so common as heck. If somebody does't understand what I am talking about take a look at the following formula {Image must be here}

For example, I have a function y = kx+b where y is dependent variable, and x is independent. I need to calculate k (slope) and b (intercept), and I have formulas from the picture, and everything those formulas need. Is there any function in common data science libraries which can help calculate those ones? I mentioned "only one independent variable" because sometimes there are multiple independent vars which leads to multidimentional plots

Googling gives nothing. I already use my own implementation of those ones, but I prefer native functions from packages such as scipy and numpy, or sklearn


Solution

  • not sure to fully understand the question (especially, what do you mean by "one independent variable"?), so I try to reformulate. If you have two variables, x andy, both represented by samples (x_1,..., x_n), (y_1,..., y_n) and that you suspect a linear relationship between them, y = a*x +b, then you can use numpy.polyfit to find the coefficients a and b. Here is an example:

    import numpy as np
    
    n = 100
    x = np.linspace(0, 1, n)
    y = 2*x + 0.3
    
    a, b = np.polyfit(x, y, 1)
    print(f"a={a}, b={b}")
    

    Returns

    a=2.0, b=0.30000000000000016
    

    Hope that helps!