python:python3.2 cvxopt:1.1.5 numpy:1.6.1
我看了http://abel.ee.ucla.edu/cvxopt/examples/tutorial/numpy.html
import cvxopt
import numpy as np
cvxopt.matrix(np.array([[7, 8, 9], [10, 11, 12]]))
我得到了
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: non-numeric element in list
按np.array(cvxopt.matrix([[7, 8, 9], [10, 11, 12]]))
,我得到了
array([[b'\x07', b'\n'],
[b'\x08', b'\x0b'],
[b'\t', b'\x0c']],
dtype='|S8')
答案 0 :(得分:2)
检查我在cvxopt讨论论坛(https://groups.google.com/forum/?fromgroups=#!topic/cvxopt/9jWnkbJvk54)上提到的修补密集。重新编译,您将能够将np数组转换为密集矩阵。我假设稀疏矩阵需要进行相同类型的编辑,但由于我不需要它们,我会将其留给开发人员。
答案 1 :(得分:2)
虽然没有修复,但
的简单解决方法cvxopt.matrix(nparray)
是
cvxopt.matrix(nparray.T.tolist())
相反的方向更难。如果你期望int数组,
np.vectorize(lambda x: int.from_bytes(x, 'big'))(np.array(cvxoptmat).T)
对于双数组:
import struct
np.vectorize(lambda x: struct.unpack('d', x))(np.array(cvxoptmat).T)
答案 2 :(得分:2)
截至cvxopt == 1.1.9
和numpy == 1.13.1
:
import cvxopt
import numpy as np
a = cvxopt.matrix(np.array([[7, 8, 9], [10, 11, 12]]))
print(a)
产生输出
[ 7 8 9]
[ 10 11 12]
和a
是
<2x3 matrix, tc='i'>
结果矩阵具有整数类型(the 'i'
),因为起始numpy
数组包含整数。从double
开始会产生'd'
类型。