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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:19:01+00:00 2026-05-23T21:19:01+00:00

So, I’m doing some Kmeans classification using numpy arrays that are quite sparse– lots

  • 0

So, I’m doing some Kmeans classification using numpy arrays that are quite sparse– lots and lots of zeroes. I figured that I’d use scipy’s ‘sparse’ package to reduce the storage overhead, but I’m a little confused about how to create arrays, not matrices.

I’ve gone through this tutorial on how to create sparse matrices:
http://www.scipy.org/SciPy_Tutorial#head-c60163f2fd2bab79edd94be43682414f18b90df7

To mimic an array, I just create a 1xN matrix, but as you may guess, Asp.dot(Bsp) doesn’t quite work because you can’t multiply two 1xN matrices. I’d have to transpose each array to Nx1, and that’s pretty lame, since I’d be doing it for every dot-product calculation.

Next up, I tried to create an NxN matrix where column 1 == row 1 (such that you can multiply two matrices and just take the top-left corner as the dot product), but that turned out to be really inefficient.

I’d love to use scipy’s sparse package as a magic replacement for numpy’s array(), but as yet, I’m not really sure what to do.

Any advice?

  • 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-05-23T21:19:02+00:00Added an answer on May 23, 2026 at 9:19 pm

    Use a scipy.sparse format that is row or column based: csc_matrix and csr_matrix.

    These use efficient, C implementations under the hood (including multiplication), and transposition is a no-op (esp. if you call transpose(copy=False)), just like with numpy arrays.

    EDIT: some timings via ipython:

    import numpy, scipy.sparse
    n = 100000
    x = (numpy.random.rand(n) * 2).astype(int).astype(float) # 50% sparse vector
    x_csr = scipy.sparse.csr_matrix(x)
    x_dok = scipy.sparse.dok_matrix(x.reshape(x_csr.shape))
    

    Now x_csr and x_dok are 50% sparse:

    print repr(x_csr)
    <1x100000 sparse matrix of type '<type 'numpy.float64'>'
            with 49757 stored elements in Compressed Sparse Row format>
    

    And the timings:

    timeit numpy.dot(x, x)
    10000 loops, best of 3: 123 us per loop
    
    timeit x_dok * x_dok.T
    1 loops, best of 3: 1.73 s per loop
    
    timeit x_csr.multiply(x_csr).sum()
    1000 loops, best of 3: 1.64 ms per loop
    
    timeit x_csr * x_csr.T
    100 loops, best of 3: 3.62 ms per loop
    

    So it looks like I told a lie. Transposition is very cheap, but there is no efficient C implementation of csr * csc (in the latest scipy 0.9.0). A new csr object is constructed in each call 🙁

    As a hack (though scipy is relatively stable these days), you can do the dot product directly on the sparse data:

    timeit numpy.dot(x_csr.data, x_csr.data)
    10000 loops, best of 3: 62.9 us per loop
    

    Note this last approach does a numpy dense multiplication again. The sparsity is 50%, so it’s actually faster than dot(x, x) by a factor of 2.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.