Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:46:12+00:00 2026-06-16T04:46:12+00:00

I have a large scipy sparse symmetric matrix which I need to condense by

  • 0

I have a large scipy sparse symmetric matrix which I need to condense by taking the sum of blocks to make a new smaller matrix.

For example, for a 4×4 sparse matrix A I will like to make a 2×2 matrix B in which B[i,j] = sum(A[i:i+2,j:j+2]).

Currently, I just go block by block to recreate the condensed matrix but this is slow. Any ideas on how to optimize this?

Update: Here is an example code that works fine, but is slow for a sparse matrix of 50.000×50.000 that I want to condense in a 10.000×10.000:

>>> A = (rand(4,4)<0.3)*rand(4,4)
>>> A = scipy.sparse.lil_matrix(A + A.T) # make the matrix symmetric

>>> B = scipy.sparse.lil_matrix((2,2))
>>> for i in range(B.shape[0]):
...     for j in range(B.shape[0]):
...         B[i,j] = A[i:i+2,j:j+2].sum()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:46:13+00:00Added an answer on June 16, 2026 at 4:46 am

    First of all, lil matrix for the one your are summing up is probably really bad, I would try COO or maybe CSR/CSS (I don’t know which will be better, but lil is probably inherently slower for many of these operations, even the slicing might be much slower, though I did not test). (Unless you know that for example dia fits perfectly)

    Based on COO I could imagine doing some tricking around. Since COO has row and col arrays to give the exact positions:

    matrix = A.tocoo()
    
    new_row = matrix.row // 5
    new_col = matrix.col // 5
    bin = (matrix.shape[0] // 5) * new_col + new_row
    # Now do a little dance because this is sparse,
    # and most of the possible bin should not be in new_row/new_col
    # also need to group the bins:
    unique, bin = np.unique(bin, return_inverse=True)
    sum = np.bincount(bin, weights=matrix.data)
    new_col = unique // (matrix.shape[0] // 5)
    new_row = unique - new_col * (matrix.shape[0] // 5)
    
    result = scipy.sparse.coo_matrix((sum, (new_row, new_col)))
    

    (I won’t guarantee that I didn’t confuse row and column somewhere and this only works for square matrices…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python, coming from matlab. I have a large sparse matrix saved
I am using Scipy to construct a large, sparse (250k X 250k) co-occurrence matrix
I have a large scipy.sparse.csc_matrix and would like to normalize it. That is subtract
I have two large square sparse matrices, A & B, and need to compute
I have large text files upon which all kinds of operations need to be
I have a large matrix that I would like to convert to sparse CSR
I have a moderately large piece (a few thousand lines) of Python/Numpy/Scipy code that
I have a python module which generates large data files which I want to
I am developing an application which doesn't have large requirements for data storage. I
I have large number of test cases which runs with Spring Junit Support with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.