私は 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 --pylab またはノートブック カーネルが何らかの形で問題を引き起こしている可能性はありますか?+
ipython notebook --pylab inline
助けてくれてありがとう!