如何在没有toDense()的情况下将scipy.csr稀疏矩阵作为普通的密集矩阵? [英] How do I get a scipy.csr sparse-matrix as a normal dense matrix without toDense()?

查看:658
本文介绍了如何在没有toDense()的情况下将scipy.csr稀疏矩阵作为普通的密集矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对scipy中的稀疏矩阵有疑问.我想将它们用作普通矩阵,但不与todense()函数一起使用.我是该领域的新手,我不知道当我想乘以稀疏矩阵时如何获得相同的结果,但是却没有蜂拥而至的稀疏矩阵...我认为稀疏矩阵仅用于更快的计算,所以应该有可能要做到这一点而无需稀疏矩阵:

I have a problem with sparse matrixes in scipy. I want to use them as a normal matrix but not with todense() function. I m new in this field, I dont know how I can get the same result when I want to multiply the sparse matrix, but without beeing a sparse matrix... I think sparse matrix only used for faster computation, so it should be possible to do this without a sparse matrix:

sparse_matrix * 5 == sparase_matrix.todense()* 5 == no_sparse_matrix * 5

data = np.ones(5178)
indices   = [34,12,545,23...,25,18,29] Shape:5178L
indptr = np.arange(5178 + 1)

sparse_matrix = sp.csr_matrix((data, indices, indptr), shape = (5178, 3800))

这是正确的吗? sparse_matrix * 5 == sparase_matrix.todense() * 5 == data * 5 ?

Is this correct? sparse_matrix * 5 == sparase_matrix.todense() * 5 == data * 5 ?

我的目标是获得与将稀疏矩阵相乘而不使用稀疏矩阵时的结果相同的结果吗?这可能吗?我该怎么办?

My goal is to get the same result as when the sparse matrix is multiplied without using a sparse matrix? Is this possible? How can I do this?

关于我的意图: 我的问题是我想将python代码传输到Java中,而线性代数的Java库不提供稀疏矩阵运算.

edit: about my intension: My problem is that I want to transfer a python code into java and my java libary for linear algeba does not provide sparse matrix operstions.

因此,我必须在没有稀疏矩阵的Java中执行相同的操作.我不确定,是否可以仅使用数据数组而不是稀疏矩阵.

So I have to do the same in java without sparse matrixes. I was not sure, if I can just use the data array instead of a sparse matrix.

在原始代码中,稀疏矩阵与另一个矩阵相乘. 要将其传输到java,我将稀疏矩阵的数据数组与另一个矩阵相乘.这是正确的吗?

In the original code a sparse matrix is multiplied with an other matrix. To transfer that to java I will just multiply the data array of the sparse matrix with the other matrix. Is this correct?

推荐答案

目前尚不清楚您要的是什么,但这是我的猜测.

It's not entirely clear what you are asking for, but here's my guess.

让我们尝试一个简单的数组:

Let's just experiment with a simple array:

从3个数组开始(我从另一个稀疏矩阵中获取了数组,但这并不重要):

Start with 3 arrays (I took these from another sparse matrix, but that isn't important):

In [165]: data
Out[165]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)

In [166]: indices
Out[166]: array([1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int32)

In [167]: indptr
Out[167]: array([ 0,  3,  7, 11], dtype=int32)

In [168]: M=sparse.csr_matrix((data,indices,indptr),shape=(3,4))

这些数组已分配给新矩阵的3个属性

These arrays have been assigned to 3 attributes of the new matrix

In [169]: M.data
Out[169]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)

In [170]: M.indices
Out[170]: array([1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int32)

In [171]: M.indptr
Out[171]: array([ 0,  3,  7, 11], dtype=int32)

现在尝试乘以.data属性:

In [172]: M.data *= 3

瞧瞧,我们已经将整个"数组相乘了

Low and behold we have multiplied the 'whole' array

In [173]: M.A
Out[173]: 
array([[ 0,  3,  6,  9],
       [12, 15, 18, 21],
       [24, 27, 30, 33]], dtype=int32)

当然,我们也可以直接将矩阵相乘.也就是说,为csr稀疏矩阵定义了乘以常数的方法:

Of course we can also multiply the matrix directly. That is, multiplication by a constant is defined for csr sparse matrices:

In [174]: M *= 2

In [175]: M.A
Out[175]: 
array([[ 0,  6, 12, 18],
       [24, 30, 36, 42],
       [48, 54, 60, 66]], dtype=int32)

In [176]: M.data
Out[176]: array([ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66], dtype=int32)

出于好奇,让我们看一下源数组.它也改变了.因此,M.data指向同一数组.换一个,换另一个.

Out of curiousity lets look at the source array. It too has changed. So M.data points to the same array. Change one, change the other.

In [177]: data
Out[177]: array([ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66], dtype=int32)

因此,当以此方式创建矩阵时,可以通过几种不同的方式将其乘以标量.

So when the matrix is created this way, it is possible to multiply it by a scalar in several different ways.

哪个最好?直接乘以.data属性可能比乘以矩阵更快.但是您应该意识到直接操作.data与对整个矩阵使用定义的数学运算之间的区别.例如,M*N执行矩阵乘法.您真的应该了解矩阵数据结构,然后再尝试直接更改其内部结构.

Which is best? Directly multiplying the .data attribute might be faster than multiplying the matrix. But you should be aware of the differences between manipulating .data directly, and using the defined math operations for the whole matrix. For example M*N performs matrix multiplication. You really should understand the matrix data structure before you try changing its internals directly.

修改源数组data的能力取决于以这种方式创建矩阵并维护该指针链接.如果通过coo矩阵(或coo样式输入)定义它,则将不维护data链接.而且M1 = M*2不会将此链接传递给M1.

The ability to modify data, the source array, depends on creating the matrix just this way, and maintaining that pointer link. If you defined it via a coo matrix (or coo style inputs), the data link would not be maintained. And M1 = M*2 is not going to pass this link on to M1.

让您的代码与sparse定义的常规数学运算一起使用.以后,如果您仍然想提高速度,则可以深入研究内部原理,并简化所选的操作.

Get your code working with the normal math operations sparse has defined. Later, if you still to squeeze out more speed, you can dig into the internals, and streamline selected operations.

这篇关于如何在没有toDense()的情况下将scipy.csr稀疏矩阵作为普通的密集矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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