如何将 CVXPY 结果转换为 numpy
How to convert a CVXPY result into numpy
我在 cvxpy 中定义了一个优化问题,但之后想在我的代码中使用 numpy 中的结果 - 如何将它从 cvxpy 转换为 numpy?
类型为
<class 'numpy.matrixlib.defmatrix.matrix'>
如果我想绘制它来查看结果,matplotlib 只显示一个蓝色区域。
https://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.view.html 似乎有你需要的一切
numpy 的 view
可以以任何方式使用(afaik),您可以直接使用 numpy 的 array
。
如果您想获得 copy 数据,"convert" 到 numpy 数组是有意义的:
y
matrix([[1, 0, 0, 0, 2, 0, 0, 0],
[3, 0, 0, 0, 4, 0, 0, 0]], dtype=int16)
type(y)
numpy.matrixlib.defmatrix.matrix
n = numpy.array(y)
n[1,2] = 999
n
array([[ 1, 0, 0, 0, 2, 0, 0, 0],
[ 3, 0, 999, 0, 4, 0, 0, 0]], dtype=int16)
y
matrix([[1, 0, 0, 0, 2, 0, 0, 0],
[3, 0, 0, 0, 4, 0, 0, 0]], dtype=int16)
我在 cvxpy 中定义了一个优化问题,但之后想在我的代码中使用 numpy 中的结果 - 如何将它从 cvxpy 转换为 numpy?
类型为
<class 'numpy.matrixlib.defmatrix.matrix'>
如果我想绘制它来查看结果,matplotlib 只显示一个蓝色区域。
https://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.view.html 似乎有你需要的一切
numpy 的 view
可以以任何方式使用(afaik),您可以直接使用 numpy 的 array
。
如果您想获得 copy 数据,"convert" 到 numpy 数组是有意义的:
y
matrix([[1, 0, 0, 0, 2, 0, 0, 0],
[3, 0, 0, 0, 4, 0, 0, 0]], dtype=int16)
type(y)
numpy.matrixlib.defmatrix.matrix
n = numpy.array(y)
n[1,2] = 999
n
array([[ 1, 0, 0, 0, 2, 0, 0, 0],
[ 3, 0, 999, 0, 4, 0, 0, 0]], dtype=int16)
y
matrix([[1, 0, 0, 0, 2, 0, 0, 0],
[3, 0, 0, 0, 4, 0, 0, 0]], dtype=int16)