简体   繁体   English

Scipy稀疏矩阵到备用功率矩阵

[英]Scipy sparse matrix to the power spare matrix

I have a scipy.sparse.csc.csc_matrix. 我有一个scipy.sparse.csc.csc_matrix。 I want to use a power function on each element of this matrix such that each element is raise to the power itself. 我想在此矩阵的每个元素上使用幂函数,以便将每个元素提升为幂本身。 How should I do that? 我应该怎么做?

I tried this: 我尝试了这个:

B.data ** B.data

But this removes 0 from the data 但这会从数据中删除0

I have no problems with that approach: 我没有这种方法的问题:

In [155]: B=sparse.random(10,10,.1,'csc')
In [156]: B
Out[156]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 10 stored elements in Compressed Sparse Column format>
In [157]: B.data
Out[157]: 
array([ 0.79437782,  0.74414493,  0.3922551 ,  0.61980213,  0.45231045,
        0.94498933,  0.53086532,  0.54611246,  0.52941419,  0.81069106])
In [158]: B.data=B.data**B.data
In [159]: B.data
Out[159]: 
array([ 0.83288253,  0.80259158,  0.69274789,  0.74342645,  0.69847422,
        0.94793528,  0.71450246,  0.71866496,  0.71412372,  0.84354814])
In [160]: B
Out[160]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 10 stored elements in Compressed Sparse Column format>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM