fit.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:scikit-cycling 作者: scikit-cycling 项目源码 文件源码
def log_linear_fitting(x, y, method='lsq'):
    """Function to perform log linear regression.

    Parameters
    ----------
    x : ndarray, shape (n_samples)
        Corresponds to the x. In our case, it should be the time.

    y : ndarray, shape (n_samples)
        Corresponds to the y. In our case, it should be the rpp.

    method : string, optional (default='lsq')
        The method to use to perform the regression. The choices are:

        - If 'lsq', an ordinary least-square approach is used.
        - If 'lm', the Levenberg-Marquardt is used.

    Returns
    -------
    slope : float
        slope of the regression line.

    intercept : float
        intercept of the regression line.

    std_err : float
        Standard error of the estimate.

    coeff_det : float
        Coefficient of determination.

    """

    # Check that the array x and y have the same size
    if x.shape != y.shape:
        raise ValueError('The size of x and y should be the same.')

    if method == 'lsq':
        # Perform the fitting using least-square
        slope, intercept, _, _, _ = linregress(np.log(x), y)

        std_err = res_std_dev(y, log_linear_model(x, slope, intercept))

        coeff_det = r_squared(y, log_linear_model(x, slope, intercept))
    elif method == 'lm':
        # Perform the fitting using non-linear least-square
        # Levenberg-Marquardt
        popt, _ = curve_fit(linear_model, np.log(x), y)

        slope = popt[0]
        intercept = popt[1]

        std_err = res_std_dev(y, log_linear_model(x, slope, intercept))

        coeff_det = r_squared(y, log_linear_model(x, slope, intercept))
    else:
        raise NotImplementedError

    return slope, intercept, std_err, coeff_det
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号