面向元素的稀疏矩阵的sqrt [英] sqrt for element-wise sparse matrix

查看:116
本文介绍了面向元素的稀疏矩阵的sqrt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个稀疏矩阵:

from scipy import sparse
a = sparse.diags([1,4,9],[-1,0,1],shape =(10,10),format ="csr")

我想取稀疏矩阵中每个元素的平方根 我在互联网上查询,它说我可以使用numpy.sqrt()来实现这一点. 但是会发生错误:

I want to take the square root of each of the elements in the sparse matrix I look up on the internet and it says I can use numpy.sqrt() to implement this. But error occurs:

  b = numpy.sqrt(a)
  AttributeError: sqrt

我该怎么办?

推荐答案

注意,这将创建一个生成的numpy ndarray而不是一个稀疏的csr数组.

Caveat, this will create a resulting numpy ndarray instead of a sparse csr array.

from scipy import sparse
a = sparse.diags([1,4,9],[-1,0,1],shape =(10,10),format ="csr")

numpy.sqrt(a.data)

据我所知,大多数其他ufunc操作(sin,cos,...)的确具有稀疏的ufunc,但sqrt除外,不知道原因.看到此问题: https://github.com/scipy/scipy/pull/208

As far as I can tell most of the other ufunc operations (sin, cos, ... ) do have sparse ufuncs except for sqrt, don't know the reason why. See this issue: https://github.com/scipy/scipy/pull/208

这篇关于面向元素的稀疏矩阵的sqrt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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