python矩阵相乘算法,Python中稀疏矩阵的矩阵乘法

I want to multiply a sparse matrix A, with a matrix B which has 0, -1, or 1 as elements. To reduce the complexity of the matrix multiplication, I can ignore items if they are 0, or go ahead and add the column without multiplication if the item is 1, or subs. if it's -1. The discussion about this is here:

Now I can go ahead and implement this trick but I wonder if I use Numpy's multiplication functions it'll be faster.

Does anyone knows if they optimised matrix multiplication for such matrices? Or can you suggest something to speed this process up since I have a matrix 300000x1000.

解决方案

Have you looked at scipy.sparse? There's no point in re-inventing the wheel, here. Sparse matricies are a fairly standard thing.

(In the example, I'm using a 300000x4 matrix for easier printing after the multiplication. A 300000x1000 matrix shouldn't be any problem, though. This will be much faster than multiplying two dense arrays, assuming you have a majority of 0 elements.)

import scipy.sparse

import numpy as np

# Make the result reproducible...

np.random.seed(1977)

def generate_random_sparse_array(nrows, ncols, numdense):

"""Generate a random sparse array with -1 or 1 in the non-zero portions"""

i = np.random.randint(0, nrows-1, numdense)

j = np.random.randint(0, ncols-1, numdense)

data = np.random.random(numdense)

data[data <= 0.5] = -1

data[data > 0.5] = 1

ij = np.vstack((i,j))

return scipy.sparse.coo_matrix((data, ij), shape=(nrows, ncols))

A = generate_random_sparse_array(4, 300000, 1000)

B = generate_random_sparse_array(300000, 5, 1000)

C = A * B

print C.todense()

This yields:

[[ 0. 1. 0. 0. 0.]

[ 0. 2. -1. 0. 0.]

[ 1. -1. 0. 0. 0.]

[ 0. 0. 0. 0. 0.]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值