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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:14:59+00:00 2026-06-17T10:14:59+00:00

My code for slicing a numpy array (via fancy indexing) is very slow. It

  • 0

My code for slicing a numpy array (via fancy indexing) is very slow. It is currently a bottleneck in program.

a.shape
(3218, 6)

ts = time.time(); a[rows][:, cols]; te = time.time(); print('%.8f' % (te-ts));
0.00200009

What is the correct numpy call to get an array consisting of the subset of rows ‘rows’ and columns ‘col’ of the matrix a? (in fact, I need the transpose of this result)

  • 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-17T10:15:00+00:00Added an answer on June 17, 2026 at 10:15 am

    You can get some speed up if you slice using fancy indexing and broadcasting:

    from __future__ import division
    import numpy as np
    
    def slice_1(a, rs, cs) :
        return a[rs][:, cs]
    
    def slice_2(a, rs, cs) :
        return a[rs[:, None], cs]
    
    >>> rows, cols = 3218, 6
    >>> rs = np.unique(np.random.randint(0, rows, size=(rows//2,)))
    >>> cs = np.unique(np.random.randint(0, cols, size=(cols//2,)))
    >>> a = np.random.rand(rows, cols)
    >>> import timeit
    >>> print timeit.timeit('slice_1(a, rs, cs)',
                            'from __main__ import slice_1, a, rs, cs',
                            number=1000)
    0.24083110865
    >>> print timeit.timeit('slice_2(a, rs, cs)',
                            'from __main__ import slice_2, a, rs, cs',
                            number=1000)
    0.206566124519
    

    If you think in term of percentages, doing something 15% faster is always good, but in my system, for the size of your array, this is taking 40 us less to do the slicing, and it is hard to believe that an operation taking 240 us will be your bottleneck.

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

Sidebar

Related Questions

I've just fixed a very subtle bug in our code, caused by slicing of
In my code I have for loop that indexes over a multidimensional numpy array
How can I set the Sliding time in below code? $.mobile.changePage(#page37, { transition: slideup});
I currently have some code that draws using pycairo and renders to an SDL
I'm having a devil of a time understanding references. Consider the following code: class
Possible Duplicate: What is the slicing problem in C++? I've got a simple code
I have an array with 32 objects inside. I am currently trying to figure
I use this code: http://blogswizards.com/plugin-development/sliding-boxes-and-captions-with-jquery On a simple gallery site I am building. Specifically
I'm using the following code to know a slider is now sliding or not.
Given the following jquery code.. jQuery.fn.sliding = function () { return this.each(function () {

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.