[SciPy-user] math operations on sparse matrices

josef.pktd@gmai... josef.pktd@gmai...
Sat Feb 21 19:33:48 CST 2009


On Sat, Feb 21, 2009 at 6:43 PM, Stéfan van der Walt <stefan@sun.ac.za> wrote:
> 2009/2/21  <josef.pktd@gmail.com>:
>> I have a (dok) sparse distance matrix and I would like to take the
>> exponential of the distances
>
> I guess you could just as well switch to dense matrices then, since
> exp(0) is no longer zero.
>
> If you just want to change the non-zero values, you can use
>
> x.data = np.exp(x.data)
>
> Regards
> Stéfan

That was also my first guess, however

>>> M
<50x50 sparse matrix of type '<type 'numpy.float64'>'
	with 208 stored elements in Dictionary Of Keys format>
>>> M.data
Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    M.data
  File "\Programs\Python25\Lib\site-packages\scipy\sparse\base.py",
line 429, in __getattr__
AttributeError: data not found


For now this seems to work pretty fast

Mexp = M.copy()
Mexp.update(((k,exp(-v)) for k,v in M.iteritems()))

But I'm not sure I know what I'm doing.

What I'm trying to do is something like OLS with a sparse X'X matrix
(kernel rigdge regression).
The next step are:
alpha = sparse.linalg.minres(M,y)
yhat = M1.matmat(alpha[0])

>From the graphical results it seems to work, but since this is my
first try with scipy.sparse.linalg, I'm not sure what the methods to
in detail.

Josef


More information about the SciPy-user mailing list