# 使用scipy.sparse.csc_matrix替换numpy广播Substitute for numpy broadcasting using scipy.sparse.csc_matrix

``````a = (b / x[:, np.newaxis]).sum(axis=1)
``````

## 2 个回复2

### ===============>>#1 票数：7 已采纳

``````b.data /= np.take(x, b.indices)
``````

``````b = sps.rand(1000, 1000, density=0.01, format='csc')
x = np.random.rand(1000)

def row_divide_col_reduce(b, x):
data = b.data.copy() / np.take(x, b.indices)
ret = sps.csc_matrix((data, b.indices.copy(), b.indptr.copy()),
shape=b.shape)
return ret.sum(axis=1)

def row_divide_col_reduce_bis(b, x):
d = sps.spdiags(1.0/x, 0, len(x), len(x))
return (d * b).sum(axis=1)

In [2]: %timeit row_divide_col_reduce(b, x)
1000 loops, best of 3: 210 us per loop

In [3]: %timeit row_divide_col_reduce_bis(b, x)
1000 loops, best of 3: 697 us per loop

In [4]: np.allclose(row_divide_col_reduce(b, x),
...:             row_divide_col_reduce_bis(b, x))
Out[4]: True
``````

``````def row_divide_col_reduce(b, x):
b.data /= np.take(x, b.indices)
return b.sum(axis=1)

In [2]: %timeit row_divide_col_reduce(b, x)
10000 loops, best of 3: 131 us per loop
``````

### ===============>>#2 票数：6

``````In [190]: b
Out[190]:
<3x3 sparse matrix of type '<type 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>

In [191]: b.A
Out[191]:
array([[ 1.,  0.,  2.],
[ 0.,  3.,  0.],
[ 4.,  0.,  5.]])

In [192]: x
Out[192]: array([ 2.,  3.,  4.])

In [193]: b.sum(axis=1).A1 / x
Out[193]: array([ 1.5 ,  1.  ,  2.25])
``````

``````In [71]: from scipy.sparse import csc_matrix, spdiags

In [72]: b = csc_matrix([[1,0,2],[0,3,0],[4,0,5]], dtype=np.float64)

In [73]: b.A
Out[73]:
array([[ 1.,  0.,  2.],
[ 0.,  3.,  0.],
[ 4.,  0.,  5.]])

In [74]: x = array([2., 3., 4.])

In [75]: d = spdiags(1.0/x, 0, len(x), len(x))

In [76]: d.A
Out[76]:
array([[ 0.5       ,  0.        ,  0.        ],
[ 0.        ,  0.33333333,  0.        ],
[ 0.        ,  0.        ,  0.25      ]])

In [77]: p = d * b

In [78]: p.A
Out[78]:
array([[ 0.5 ,  0.  ,  1.  ],
[ 0.  ,  1.  ,  0.  ],
[ 1.  ,  0.  ,  1.25]])

In [79]: a = p.sum(axis=1)

In [80]: a
Out[80]:
matrix([[ 1.5 ],
[ 1.  ],
[ 2.25]])
``````

3回复

1回复

1回复

1回复

1回复

2回复

3回复

1回复

2回复

3回复