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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:59:44+00:00 2026-06-10T17:59:44+00:00

If you have a sparse matrix X: >> print type(X) <class ‘scipy.sparse.csr.csr_matrix’> …How can

  • 0

If you have a sparse matrix X:

>> print type(X)
<class 'scipy.sparse.csr.csr_matrix'>

…How can you sum the squares of each element in each row, and save them into a list? For example:

>>print X.todense()
[[0 2 0 2]
 [0 2 0 1]]

How can you turn that into a list of sum of squares of each row:

[[0²+2²+0²+2²]
 [0²+2²+0²+1²]]

or:
[8, 5]

  • 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-10T17:59:45+00:00Added an answer on June 10, 2026 at 5:59 pm

    First of all, the csr matrix has a .sum method (relying on the dot product) which works well, so what you need is the squaring. The simplest solution is to create a copy of the sparse matrix, square its data and then sum it:

    squared_X = X.copy()
    # now square the data in squared_X
    squared_X.data **= 2
    
    # and sum each row:
    squared_sum = squared_X.sum(1)
    # and delete the squared_X:
    del squared_X
    

    If you really must save the space, I guess you could just replace .data and then replace it back, something along:

    X.sum_duplicate() # make sure, not sure if this happens with normal usage.
    old_data = X.data.copy()
    X.data **= 2
    squared_sum = X.sum(1)
    X.data = old_data
    

    EDIT: There is actually another nice way, as the csr matrix has a .multiply method for elementwise multiplication:

    squared_sum = X.multiply(X).sum(1)
    

    Addition:
    Elementwise operations are thus easily done by accessing csr.data which stores the values for all nonzero elements. NOTE: I guess .sum_duplicates() may be necessary, I am not sure what kind of operations would make it necessary.

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

Sidebar

Related Questions

If you have a sparse matrix X: >> X = csr_matrix([[0,2,0,2],[0,2,0,1]]) >> print type(X)
I am writing a sparse matrix class. I need to have a node class,
I have a rather large, dynamic sparse matrix object class to write, and I
I have a large matrix that I would like to convert to sparse CSR
I have a sparse matrix Formal class 'dgCMatrix' [package Matrix] with 6 slots ..@
I have a sparse matrix A of very high dimension around 30000x30000 . And
I have to implement a sparse matrix (a matrix that has predominantly zeroes, so
I have a need for a sparse matrix in up to 4 dimensions in
I was wondering if there's any way to have a scipy.sparse.csc_matrix format for mlpy
Related to my other question , I have now modified the sparse matrix solver

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.