【发布时间】:2013-04-19 18:31:40
【问题描述】:
我正在计算 scipy.sparse 矩阵 (CSC) 和 numpy ndarray 向量之间的点积:
>>> print type(np_vector), np_vector.shape
<type 'numpy.ndarray'> (200,)
>>> print type(sp_matrix), sparse.isspmatrix(sp_matrix), sp_matrix.shape
<class 'scipy.sparse.csc.csc_matrix'> True (200, 200)
>>> dot_vector = dot(np_vector, sp_matrix)
结果似乎是我所期望的一个新的 ndarray 向量:
>>> print type(dot_vector), dot_vector.shape
<type 'numpy.ndarray'> (200,)
但是当我尝试向该向量添加一个标量时,我收到了异常:
>>> scalar = 3.0
>>> print dot_vector + scalar
C:\Python27\lib\site-packages\scipy\sparse\compressed.pyc in __add__(self, other)
173 return self.copy()
174 else: # Now we would add this scalar to every element.
--> 175 raise NotImplementedError('adding a nonzero scalar to a '
176 'sparse matrix is not supported')
177 elif isspmatrix(other):
NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported
好像结果dot_vector又是一个稀疏矩阵。
具体来说,好像我有一个 ndarray,但稀疏矩阵 __add__ 是为 + 运算符调用的。
这是我希望调用的方法:
>>> print dot_vector.__add__
<method-wrapper '__add__' of numpy.ndarray object at 0x05250690>
我在这里遗漏了什么还是真的看起来很奇怪?
什么决定了 + 运算符调用哪个方法?
我在 IPython Notebook (ipython notebook --pylab inline) 中运行此代码。会不会是 IPython --pylab 或 notebook 内核搞砸了?
感谢您的帮助!
【问题讨论】:
-
请创建一个 minimal 显示此行为的脚本并将其发布在您的问题中。我怀疑有一个包含稀疏矩阵的全局
dot_vector变量,并被使用而不是包含向量的本地变量。但是没有看到完整的剧本,这纯属猜测。 -
你不能在 scipy.sparse 中使用 numpy 的点,很简单。
标签: python numpy scipy ipython sparse-matrix