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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:09:27+00:00 2026-06-14T11:09:27+00:00

I have a sparse matrix that I arrived at through a complicated bunch of

  • 0

I have a sparse matrix that I arrived at through a complicated bunch of calculations which I cannot reproduce here. I will try to find a simpler example of this.

For now, does anyone know how it might be (even remotely) possible that I could have a sparse matrix X with the property that:

In [143]: X.sum(0).sum()
Out[143]: 131138

In [144]: X.sum()
Out[144]: 327746

In [145]: X.sum(1).sum()
Out[145]: 327746

In [146]: type(X)
Out[146]: scipy.sparse.csr.csr_matrix

My only guess is that if I want to sum columns correctly, I need to first cast the matrix as csc — which makes sense. Although one would think that the sparse package would handle column sums gracefully (or throw an error) instead of just giving a WRONG answer.

After more thought, I tried the following:

In [164]: X.tocsr().sum(0).sum()
Out[164]: 131138

In [165]: X.tocsc().sum(0).sum()
Out[165]: 131138

In [166]: X.tocoo().sum(0).sum()
Out[166]: 131138

In [167]: X.tolil().sum(0).sum()
Out[167]: 131138

In [168]: X.todok().sum(0).sum()
Out[168]: 131138

In [169]: X.shape
Out[169]: (196980, 43)

In [170]: X
Out[170]: 
<196980x43 sparse matrix of type '<type 'numpy.uint16'>'
        with 70875 stored elements in Compressed Sparse Row format>

In [172]: X.todense().sum(0)
Out[172]: 
matrix([[170726,   1041, 117398,   3526,  13202,   3585,   2355,   1895,   1392,   2189,   2070,   2603,   1676,    496,   1194,    933,    129,
            529,    544,    256,      7,      0,      0,      0,      0,      0,      0,      0,      0,      0,      0,      0,      0,      0,
              0,      0,      0,      0,      0,      0,      0,      0,      0]], dtype=uint64)

In [173]: X.sum(0)
Out[173]: 
matrix([[39654,  1041, 51862,  3526, 13202,  3585,  2355,  1895,  1392,  2189,  2070,  2603,  1676,   496,  1194,   933,   129,   529,   544,   256,
             7,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
             0,     0,     0]], dtype=uint16)

I should add some more context: the matrix has only non-negative entries (they are counts). In particular there were two sparse count matrices A and B which I multiplied together to get X.

  • 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-14T11:09:29+00:00Added an answer on June 14, 2026 at 11:09 am

    Ok, so seberg answered the question. Thanks a lot. Go seberg!

    He observed that the data type uint16 might be a problem. Sure enough — uint16 maxes out around 65,000 and my sums are much bigger than that, even though my individual datapoints are much much smaller than that.

    Proof in the pudding:

    In [184]: Y = sparse.csc_matrix(X,dtype=np.uint32)
    
    In [185]: Y.sum(0).sum()
    Out[185]: 327746
    
    In [187]: Y.sum(0)
    Out[187]: 
    matrix([[170726,   1041, 117398,   3526,  13202,   3585,   2355,   1895,   1392,   2189,   2070,   2603,   1676,    496,   1194,    933,    129,
                529,    544,    256,      7,      0,      0,      0,      0,      0,      0,      0,      0,      0,      0,      0,      0,      0,
                  0,      0,      0,      0,      0,      0,      0,      0,      0]], dtype=uint32)
    

    This explains the inconsistent sums and, changing the data type, rectifies the issue. Although, still there is the persistent problem that — if I have a matrix with all small entries, I want to be able to use a smaller datatype for it (to save memory).

    This is kind of a separate but related question:

    Is there a way to gracefully handle numerical overflow problems when summing columns of sparse matrices?

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

Sidebar

Related Questions

I have to implement a sparse matrix (a matrix that has predominantly zeroes, so
I have a sparse matrix that represents a 3D rectangular space. Along some of
I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat
I have the following sparse matrix that contains O(N) elements boost::numeric::ublas::compressed_matrix<int> adjacency (N, N);
I have a regular matrix (non-sparse) that I would like to convert to a
I have an N x N sparse matrix in Matlab, that has cell values
We have an application that stores a sparse matrix. This matrix has entries that
I have a large matrix that I would like to convert to sparse CSR
I have a 30000x14000 sparse matrix in MATLAB (version 7), which I need to
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.