从数学上已知,通过 Cholesky 分解对正定矩阵求逆比仅使用 np.linalg.inv(X)
更快。然而,当我对两者进行试验时,发现 Cholesky 分解的性能更差!
# Inversion through Cholesky
p = X.shape[0]
Ip = np.eye(p)
%timeit scipy.linalg.cho_solve(scipy.linalg.cho_factor(X,lower=True), Ip)
The slowest run took 17.96 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 107 µs per loop
# Simple inversion
%timeit np.linalg.inv(X)
The slowest run took 58.81 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 25.9 µs per loop
后者耗时更短。为什么是这样?在 R
中,chol2inv(chol(X))
通常比 solve(X)
快。
最佳答案
我对 1000x1000 矩阵进行了比较,通过 Cholesky 的反演速度大约是原来的两倍。
关于python - 为什么通过 Cholesky 分解反转正定矩阵比使用 numpy 进行常规反转慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39574924/