查找大型矩阵的最小二乘解的更快方法 [英] Faster way of finding least-square solution for large matrix

查看:84
本文介绍了查找大型矩阵的最小二乘解的更快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到矩阵的最小二乘解,我正在使用numpy linalg.lstsq函数;

I want to find the least-square solution of a matrix and I am using the numpy linalg.lstsq function;

weights = np.linalg.lstsq(semivariance, prediction, rcond=None)

我的变量的维数是;

semivariance是大小为5030 x 5030的浮点数

semivariance is a float of size 5030 x 5030

prediction是长度为5030的一维数组

prediction is a 1D array of length 5030

我遇到的问题是返回weights的值大约需要80秒,并且我必须重复weights的计算大约10000次,因此只是增加了计算时间.

The problem I have is it takes approximately 80sec to return the value of weights and I have to repeat the calculation of weights about 10000 times so the computational time is just elevated.

是否有更快的方法/pythonic函数来执行此操作?

Is there a faster way/pythonic function to do this?

推荐答案

@ Brenlla 似乎是正确的,即使您可以通过使用Moore-Penrose伪逆进行求解来执行最小二乘,它比np.linalg.lstsq快得多:

@Brenlla appears to be right, even if you perform least squares by solving using the Moore-Penrose pseudo inverse, it is significantly faster than np.linalg.lstsq:

import numpy as np
import time

semivariance=np.random.uniform(0,100,[5030,5030]).astype(np.float64)
prediction=np.random.uniform(0,100,[5030,1]).astype(np.float64)

start=time.time()
weights_lstsq = np.linalg.lstsq(semivariance, prediction, rcond=None)
print("Took: {}".format(time.time()-start))

>>> Took: 34.65818190574646

start=time.time()
weights_pseudo = np.linalg.solve(semivariance.T.dot(semivariance),semivariance.T.dot(prediction))
print("Took: {}".format(time.time()-start))

>>> Took: 2.0434153079986572

np.allclose(weights_lstsq[0],weights_pseudo)

>>> True

以上内容并非针对您确切的矩阵,而是样本中的概念可能会转移. np.linalg.lstsq通过最小化|| b - a x ||^2来解决ax=b中的x来执行优化问题.通常在非常大的矩阵上这会更快,因此为什么通常在神经网络中使用梯度合适的方法来求解线性模型,但是在这种情况下,矩阵的大小不足以带来性能上的好处.

The above is not on your exact matrices but the concept on the samples likely transfers. np.linalg.lstsq performs an optimisation problem by minimizing || b - a x ||^2 to solve for x in ax=b. This is usually faster on extremely large matrices, hence why linear models are often solved using gradient decent in neural networks, but in this case the matrices just aren't large enough for the performance benefit.

这篇关于查找大型矩阵的最小二乘解的更快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆