cvxopt.matrix 和 numpy.array 之间的 python3 转换 [英] python3 conversion between cvxopt.matrix and numpy.array

查看:146
本文介绍了cvxopt.matrix 和 numpy.array 之间的 python3 转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

蟒蛇:python3.2cvxopt:1.1.5麻木:1.6.1

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')

推荐答案

cvxopt == 1.1.9numpy == 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'>

结果矩阵具有整数类型('i') 因为起始 numpy 数组包含整数.以 double 开头的结果是 'd' 类型.

The resulting matrix has integer type (the 'i') because the starting numpy array contained integers. Starting with double results in a 'd' type.

这篇关于cvxopt.matrix 和 numpy.array 之间的 python3 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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