简体   繁体   English

如何计算python中简单线性回归拟合的截距和斜率?

[英]How to calculate intercept and slope of the simple linear regression fit in python?

I have the following code class Data: def init (self, x, y): """ (Data, list, list) -> NoneType我有以下代码类数据: def init (self, x, y): """ (Data, list, list) -> NoneType

    Create a new data object with two attributes: x and y.
    """
    self.x = x
    self.y = y

def num_obs(self):
    """ (Data) -> int

    Return the number of observations in the data set.

    >>> data = Data([1, 2], [3, 4])
    >>> data.num_obs()
    2
    """

    return len(self.x)

def __str__(self):
    """ (Data) -> str
    Return a string representation of this Data in this format:
    x               y
    18.000          120.000
    20.000          110.000
    22.000          120.000
    25.000          135.000
    26.000          140.000
    29.000          115.000
    30.000          150.000
    33.000          165.000
    33.000          160.000
    35.000          180.000
    """

    return 'x               y\n' + '\n'.join('{0:.3f}         {1:.3f}'.format(a, b) for a, b in zip(self.x, self.y))

def compute_sample_means(self):
    """ (Data) -> number, number

    Return sample mean of x and sample mean of y.
    """
    a = sum(self.x)/len(self.x)
    b = sum(self.y)/len(self.y)
    return a,b

def compute_least_squares_fit(self):
    """ (Data) -> number, number

 Return the intercept and slope of the simple linear regression fit
    of the data.
    """
    pass


def compute_SST(self):
    """ (Data) -> number

    Return the sum of squares total (SST).
    """

    avg_y = np.mean(self.y)
    squared_errors = (self.y - avg_y) ** 2
    return np.sum(squared_errors)

I am stuck on returning the compute_least_squares_fit part.我坚持返回 compute_least_squares_fit 部分。 How to calculate the intercept and slope of the simple linear regression fit of the data.如何计算数据的简单线性回归拟合的截距和斜率。 Is there an in-built function that I can use?是否有我可以使用的内置功能?

The SciPy module has scipy.optimize.least_squares which I have been using for a simple linear regression model. SciPy模块有scipy.optimize.least_squares ,我一直将其用于简单的线性回归模型。

As per the documentation, it solves a nonlinear least-squares problem with bounds on the variables.根据文档,它解决了具有变量边界的非线性最小二乘问题。 It returns the solution found which I imagine would result in what you are looking for.它返回找到的解决方案,我认为这会导致您正在寻找的结果。

Let me know if this helps!让我知道这是否有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 scikit-learn 在 Python 中打印简单线性回归的截距和斜率? - How to print intercept and slope of a simple linear regression in Python with scikit-learn? 如何在python的线性回归模型中计算斜率的99%置信区间? - How to calculate the 99% confidence interval for the slope in a linear regression model in python? 我想使用pykalman模块计算线性拟合的斜率和截距 - I want to calculate slope and intercept of a linear fit using pykalman module pymc3:如何在多级线性回归中建模相关截距和斜率 - pymc3: how to model correlated intercept and slope in multilevel linear regression 计算线性回归的斜率和截距误差 - Calculating slope and intercept error of linear regression 计算线性回归斜率矩阵(类似于相关矩阵)-Python/Pandas - calculate linear regression slope matrix (analogous to correlation matrix) - Python/Pandas 计算线性回归斜率矩阵(与相关矩阵相同) - Python/Pandas - Calculate linear regression slope matrix (Same to correlation matrix) - Python/Pandas Python/Numpy - 新数组中简单线性回归的返回斜率 - Python/Numpy - return slope for simple linear regression in a new array 多元线性回归中没有找到斜率和截距,nan值来了 - did not find slope and intercept in multiple linear regression, nan value coming 使用线性回归在 Numpy/Scypy 中计算斜率和截距时遇到问题 - Trouble calculating slope and intercept in Numpy/Scypy using linear regression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM