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 8830491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:06:11+00:00 2026-06-14T08:06:11+00:00

What is the best way to efficiently remove columns from a sparse matrix that

  • 0

What is the best way to efficiently remove columns from a sparse matrix that only contain zeros. I have a matrix which I have created and filled with data:

matrix = sp.sparse.lil_matrix((100, 100))

I now wish to remove ~ the last 20 columns which only contain zero data. How can I do this?

  • 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-14T08:06:13+00:00Added an answer on June 14, 2026 at 8:06 am

    If this were just a numpy array, X, then you could say X!=0 which would give you a boolean array of the same shape as X, and then you could index X with the boolean array, i.e. non_zero_entries = X[X!=0]

    But this is a sparse matrix which does not support boolean indexing and also will not give you what you want if you try X!=0 — it just returns a single boolean value that seems to only return true if they are the exact same matrix (in memory).

    What you want is the nonzero method from numpy.

    import numpy as np
    from scipy import sparse
    
    X = sparse.lil_matrix((100,100)) # some sparse matrix
    X[1,17] = 1
    X[17,17] = 1
    indices = np.nonzero(X) # a tuple of two arrays: 0th is row indices, 1st is cols
    X.tocsc()[indices] # this just gives you the array of all non-zero entries
    

    If you want only the full columns where there are non-zero entries, then just take the 1st from indices. Except you need to account for the repeated indices (if there are more than one entries in a column):

    columns_non_unique = indices[1]
    unique_columns = sorted(set(columns_non_unique))
    X.tocsc()[:,unique_columns]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a way to remove unused images from my filesystem, i.e. images that
What's the best way to remove duplicate indices from an NSIndexSet? For example, say
Possible Duplicate: Best way to strip punctuation from a string in Python I have
which is the best way to write a bidimensional hashmap efficiently in Java? Just
What is the best way to remove the first occurrence of an object from
I'm trying to find the best (efficient) way of doing this. I have a
The best way to explain my problem is by a example. I have a
The best way of describing this is I have a table of people with
What would be the best way to add/remove a css class to a div
I have a big collection of millions entries. I want to remove efficiently all

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.