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

  • Home
  • SEARCH
  • 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 8478773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:50:22+00:00 2026-06-10T18:50:22+00:00

If you have a sparse matrix X: >> X = csr_matrix([[0,2,0,2],[0,2,0,1]]) >> print type(X)

  • 0

If you have a sparse matrix X:

>> X = csr_matrix([[0,2,0,2],[0,2,0,1]])
>> print type(X)    
>> print X.todense()    
<class 'scipy.sparse.csr.csr_matrix'>
[[0 2 0 2]
 [0 2 0 1]]

And a matrix Y:

>> print type(Y)
>> print text_scores
<class 'numpy.matrixlib.defmatrix.matrix'>
[[8]
 [5]]

…How can you multiply each element of X by the rows of Y. For example:

[[0*8 2*8 0*8 2*8]
 [0*5 2*5 0*5 1*5]]

or:

[[0 16 0 16]
 [0 10 0 5]]

I’ve tired this but obviously it doesn’t work as the dimensions dont match:
Z = X.data * Y

  • 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-10T18:50:24+00:00Added an answer on June 10, 2026 at 6:50 pm

    Unfortunatly the .multiply method of the CSR matrix seems to densify the matrix if the other one is dense. So this would be one way avoiding that:

    # Assuming that Y is 1D, might need to do Y = Y.A.ravel() or such...
    
    # just to make the point that this works only with CSR:
    if not isinstance(X, scipy.sparse.csr_matrix):
        raise ValueError('Matrix must be CSR.')
    
    Z = X.copy()
    # simply repeat each value in Y by the number of nnz elements in each row: 
    Z.data *= Y.repeat(np.diff(Z.indptr))
    

    This does create some temporaries, but at least its fully vectorized, and it does not densify the sparse matrix.


    For a COO matrix the equivalent is:

    Z.data *= Y[Z.row] # you can use np.take which is faster then indexing.
    

    For a CSC matrix the equivalent would be:

    Z.data *= Y[Z.indices]
    
    • 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: >> print type(X) <class 'scipy.sparse.csr.csr_matrix'> ...How can
You have an original sparse matrix X: >>print type(X) >>print X.todense() <class 'scipy.sparse.csr.csr_matrix'> [[1,4,3]
Suppose I have a NxN matrix M (lil_matrix or csr_matrix) from scipy.sparse, and I
I have an array y and a matrix X that is CSR sparse. I
I got some sparse matrix like this >>>import numpy as np >>>from scipy.sparse import
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

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.