Python Multiple Simple Linear Regression

前端 未结 1 845
误落风尘
误落风尘 2021-02-09 06:33

Note this is not a question about multiple regression, it is a question about doing simple (single-variable) regression multiple times in Python/NumPy (2.7).

I

相关标签:
1条回答
  • 2021-02-09 07:25

    Single variable linear regression is simple enough to vectorize it manually:

    def multiple_linregress(x, y):
        x_mean = np.mean(x, axis=1, keepdims=True)
        x_norm = x - x_mean
        y_mean = np.mean(y, axis=1, keepdims=True)
        y_norm = y - y_mean
    
        slope = (np.einsum('ij,ij->i', x_norm, y_norm) /
                 np.einsum('ij,ij->i', x_norm, x_norm))
        intercept = y_mean[:, 0] - slope * x_mean[:, 0]
    
        return np.column_stack((slope, intercept))
    

    With some made up data:

    m = 1000
    n = 1000
    x = np.random.rand(m, n)
    y = np.random.rand(m, n)
    

    it outperforms your looping options by a fair margin:

    %timeit multiple_linregress(x, y)
    100 loops, best of 3: 14.1 ms per loop
    
    0 讨论(0)
提交回复
热议问题