2

What equivalent function can I can in Python that is equivalent to solve() in R?

In R, if I call solve(a, b), it will return me the x as in a*x = b where a is my covariance matrix.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Jasper
  • 53
  • 2

1 Answers1

2

Try np.linalg.solve:

>>> a = np.array([[1, 2], [3, 5]])
>>> b = np.array([1, 2])
>>> x = np.linalg.solve(a, b)
>>> x
array([-1.,  1.])
> solve(matrix(c(1,2,3,5), 2, 2, byrow = TRUE), c(1, 2))
[1] -1  1
Dmitry Zotikov
  • 2,133
  • 15
  • 12
  • Just in case anyone is interested in the one-argument case where `a` is specified but `b` is not: R's `solve` supports `solve(a)` (i.e., with no `b` argument) but `np.linalg.solve` does not. In this case the Python equivalent is `np.linalg.inv(a)`. – ToddP Jan 13 '23 at 03:38