简体   繁体   English

最小二乘拟合的最小系数的多个系数集

[英]Multiple coefficient sets for least squares fitting in numpy/scipy

Is there a way to perform multiple simultaneous (but unrelated) least-squares fits with different coefficient matrices in either numpy.linalg.lstsq or scipy.linalg.lstsq ? 有没有一种方法可以在numpy.linalg.lstsqscipy.linalg.lstsq执行具有不同系数矩阵的多个同时(但不相关)最小二乘拟合? For example, here is a trivial linear fit that I would like to be able to do with different x-values but the same y-values. 例如,这是一个简单的线性拟合,我希望能够对不同的x值但相同的y值进行拟合。 Currently, I have to write a loop: 当前,我必须编写一个循环:

x = np.arange(12.0).reshape(4, 3)
y = np.arange(12.0, step=3.0)
m = np.stack((x, np.broadcast_to(1, x.shape)), axis=0)

fit = np.stack(tuple(np.linalg.lstsq(w, y, rcond=-1)[0] for w in m), axis=-1)

This results in a set of fits with the same slope and different intercepts, such that fit[n] corresponds to coefficients m[n] . 这导致具有相同斜率和不同截距的一组拟合,使得fit[n]对应于系数m[n]

Linear least squares is not a great example since it is invertible, and both functions have an option for multiple y-values. 线性最小二乘不是一个很好的例子,因为它是可逆的,并且两个函数都可以选择多个y值。 However, it serves to illustrate my point. 但是,它可以说明我的观点。

Ideally, I would like to extend this to any "broadcastable" combination of a and b , where a.shape[-2] == b.shape[0] exactly, and the last dimensions have to either match or be one (or missing). 理想情况下,我想将此扩展到ab任何“可广播”组合,其中a.shape[-2] == b.shape[0]恰好一样,并且最后一个尺寸必须匹配或为一个(或失踪)。 I am not really hung up on which dimension of a is the one representing the different matrices: it was just convenient to make it the first one to shorten the loop. 我并不是很想知道a哪个维度代表不同的矩阵:将它作为第一个缩短循环的维度很方便。

Is there a built in method in numpy or scipy to avoid the Python loop? 是否有numpy或scipy中的内置方法来避免Python循环? I am very much interested in using lstsq rather than manually transposing, multiplying and inverting the matrices. 我对使用lstsq感兴趣,而不是手动转置,乘法和求逆矩阵。

You could use scipy.sparse.linalg.lsqr together with scipy.sparse.block_diag . 您可以将scipy.sparse.linalg.lsqrscipy.sparse.block_diag一起使用。 I'm just not sure it will be any faster. 我只是不确定是否会更快。

Example: 例:

>>> import numpy as np
>>> from scipy.sparse import block_diag
>>> from scipy.sparse import linalg as sprsla
>>> 
>>> x = np.random.random((3,5,4))
>>> y = np.random.random((3,5))
>>> 
>>> for A, b in zip(x, y):
...     print(np.linalg.lstsq(A, b))
... 
(array([-0.11536962,  0.22575441,  0.03597646,  0.52014899]), array([0.22232195]), 4, array([2.27188101, 0.69355384, 0.63567141, 0.21700743]))
(array([-2.36307163,  2.27693405, -1.85653264,  3.63307554]), array([0.04810252]), 4, array([2.61853881, 0.74251282, 0.38701194, 0.06751288]))
(array([-0.6817038 , -0.02537582,  0.75882223,  0.03190649]), array([0.09892803]), 4, array([2.5094637 , 0.55673403, 0.39252624, 0.18598489]))
>>> 
>>> sprsla.lsqr(block_diag(x), y.ravel())
(array([-0.11536962,  0.22575441,  0.03597646,  0.52014899, -2.36307163,
        2.27693405, -1.85653264,  3.63307554, -0.6817038 , -0.02537582,
        0.75882223,  0.03190649]), 2, 15, 0.6077437777160813, 0.6077437777160813, 6.226368324510392, 106.63227777368986, 1.3277892240815807e-14, 5.36589277249043, array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM