简体   繁体   中英

SciPy/numpy: Only keep maximum value of a sparse matrix block

I am trying to operate on a large sparse matrix (currently 12000 x 12000). What I want to do is to set blocks of it to zero but keep the largest value within this block. I already have a running solution for dense matrices:

import numpy as np
from scipy.sparse import random

np.set_printoptions(precision=2)
#x = random(10,10,density=0.5)
x = np.random.random((10,10))
x = x.T * x
print(x)

def keep_only_max(a,b,c,d):
  sub = x[a:b,c:d]
  z = np.max(sub)
  sub[sub < z] = 0


sizes = np.asarray([0,1,5,4])
sizes_sum = np.cumsum(sizes)

for i in range(1,len(sizes)):
  current_i_min = sizes_sum[i-1]
  current_i_max = sizes_sum[i]
  for j in range(1,len(sizes)):
    if i >= j:
      continue
    current_j_min = sizes_sum[j-1]
    current_j_max = sizes_sum[j]

    keep_only_max(current_i_min, current_i_max, current_j_min, current_j_max)
    keep_only_max(current_j_min, current_j_max, current_i_min, current_i_max)

print(x)

This, however, doesn't work for sparse matrices (try uncommenting the line on top). Any ideas how I could efficiently implement this without calling todense()?

def keep_only_max(a,b,c,d):
  sub = x[a:b,c:d]
  z = np.max(sub)
  sub[sub < z] = 0

For a sparse x , the sub slicing works for csr format. It won't be as fast as the equivalent dense slice, but it will create a copy of that part of x .

I'd have to check the sparse max functions. But I can imagine convertering sub to coo format, using np.argmax on the .data attribute, and with the corresponding row and col values, constructing a new matrix of the same shape but just one nonzero value.

If your blocks covered x in a regular, nonoverlapping manner, I'd suggest constructing a new matrix with sparse.bmat . That basically collects the coo attributes of all the components, joins them into one set of arrays with the appropriate offsets, and makes a new coo matrix.

If the blocks are scattered or overlap you might have to generate, and insert them back into x one by one. csr format should work for that, but it will issue a sparse efficiency warning. lil is supposed to be faster for changing values. I think it will accept blocks.

I can imagine doing this with sparse matrices, but it will take time to setup a test case and debug the process.

Thanks to hpaulj I managed to implement a solution using scipy.sparse.bmat :

from scipy.sparse import coo_matrix
from scipy.sparse import csr_matrix
from scipy.sparse import rand
from scipy.sparse import bmat
import numpy as np


np.set_printoptions(precision=2)

# my matrices are symmetric, so generate random symmetric matrix
x = rand(10,10,density=0.4)
x = x.T * x
x = x


def keep_only_max(a,b,c,d):
    sub = x[a:b,c:d]
    z = np.unravel_index(sub.argmax(),sub.shape)
    i1 = z[0]
    j1 = z[1]
    new = csr_matrix(([sub[i1,j1]],([i1],[j1])),shape=(b-a,d-c))
    return new

def keep_all(a,b,c,d):
    return x[a:b,c:d].copy()

# we want to create a chessboard pattern where the first central block is 1x1, the second 5x5 and the last 4x4
sizes = np.asarray([0,1,5,4])
sizes_sum = np.cumsum(sizes)

# acquire 2D array to store our chessboard blocks
r = range(len(sizes)-1)
blocks = [[0 for x in r] for y in r] 


for i in range(1,len(sizes)):
    current_i_min = sizes_sum[i-1]
    current_i_max = sizes_sum[i]

    for j in range(i,len(sizes)):

        current_j_min = sizes_sum[j-1]
        current_j_max = sizes_sum[j]

        if i == j:
            # keep the blocks at the diagonal completely
            sub = keep_all(current_i_min, current_i_max, current_j_min, current_j_max)
            blocks[i-1][j-1] = sub
        else:
            # the blocks not on the digonal only keep their maximum value
            current_j_min = sizes_sum[j-1]
            current_j_max = sizes_sum[j]

            # we can leverage the matrix symmetry and only calculate one new matrix.
            m1 = keep_only_max(current_i_min, current_i_max, current_j_min, current_j_max)
            m2 = m1.T

            blocks[i-1][j-1] = m1
            blocks[j-1][i-1] = m2


z = bmat(blocks)
print(z.todense())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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