努比的__array_interface__不回字典
Numpy's __array_interface__ not returning dict
我正在使用一个外部程序来计算一个用C++编写的矩阵,该矩阵通过boost::python
与python接口。我想把这个C数组传递给numpy,根据作者的说法,这个能力已经用numpy的obj.__array_interface__
实现了。如果我在python脚本中调用它,并将C++对象分配给X
,我将获得以下内容:
print X
#<sprint.Matrix object at 0x107c5c320>
print X.__array_interface__
#<bound method Matrix.__array_interface__ of <sprint.Matrix object at 0x107c5c320>>
print X.__array_interface__()
#{'shape': (5, 5), 'data': (4416696960, True), 'typestr': '<f8'}
print np.array(X)
#Traceback (most recent call last):
# File "<string>", line 96, in <module>
#ValueError: Invalid __array_interface__ value, must be a dict
根据我有限的理解,我认为问题是X.__array_interface__
在没有()
的情况下实际上不会返回任何内容。有没有办法将这些参数显式传递给np.array
,或者解决这个问题的方法。
我对混合C++和python真的很陌生,如果这毫无意义,或者如果我需要阐述任何部分,请告诉我!
__array_interface__应该是一个属性(实例变量),而不是一个方法。因此,在C++中,或者在任何定义了"sprint.Matrix"对象的地方,更改它,使其不具有:
print X.__array_interface__
#<bound method Matrix.__array_interface__ of <sprint.Matrix object at 0x107c5c320>>
你有
print X.__array_interface__
#{'shape': (5, 5), 'data': (4416696960, True), 'typestr': '<f8'}
另一种选择是定义一个自定义包装类:
class SprintMatrixWrapper(object):
def __init__(self, sprint_matrix):
self.__array_interface__ = sprint_matrix.__array_interface__()
然后简单地做:
numpy.array(SprintMatrixWrapper(X))
相关文章:
- C++中的MSD基排序(字典序)
- Android NDK 编译 LAME HAVE_MPGLIB > 'interface.h' 文件未找到
- 重载 -> shared_ptr 个实例中的箭头运算符<interface>,接口中没有纯虚拟析构函数
- C++中是否有任何函数等同于python中字典的get函数?
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- C++中类似Python字典的结构?
- 我应该有 2 个单独的班级,一个用于"logic",一个用于"graphic interface"?
- 如何使用带有Qt和C++的"Interface Class"进行通信
- C++ 按数值对元组<字符串、浮点数>然后按字典顺序排序的向量
- 使用C++对一个巨大文件中的文本行进行字典式外部排序
- 与C++std::map::insert行为相比,C#排序字典的效率
- 为字典cpp创建[]=运算符
- 如何使用Java Native Interface在C++中导入python库-Android Studio
- c++如何使用Curl Multi-Interface连续循环
- C++ 随机生成的字典单词将仅显示以 B 开头的单词
- 如何将字典添加到向量?
- 蟒蛇字典到C++映射
- 使用给定的字典构建C++翻译器?
- PyBind - Python to C++ interface
- C++类:如何使用字典["hello"]=20;像蟒蛇