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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:20:37+00:00 2026-06-09T16:20:37+00:00

I am working with multi-dimensional Numpy arrays. I have noticed some inconsistent behavior when

  • 0

I am working with multi-dimensional Numpy arrays. I have noticed some inconsistent behavior when accessing these arrays with other index arrays. For example:

import numpy as np
start = np.zeros((7,5,3))
a     = start[:,:,np.arange(2)]
b     = start[0,:,np.arange(2)]
c     = start[0,:,:2]
print 'a:', a.shape
print 'b:', b.shape
print 'c:', c.shape

In this example, I get the result:

a: (7, 5, 2)
b: (2, 5)
c: (5, 2)

This confuses me. Why do “b” and “c” not have the same dimensions? Why does “b” swap the axis order, but not “a”?

I have been able to design my code around these inconsistencies thanks to lots of unit tests, but understanding what is going on would be appreciated.

For reference, I am using Python 2.7.3, and Numpy 1.6.2 via MacPorts.

  • 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-09T16:20:38+00:00Added an answer on June 9, 2026 at 4:20 pm

    Syntactically, this looks like an inconsistency, but semantically, you’re doing two very different things here. In your definition of a and b, you’re doing advanced indexing, sometimes called fancy indexing, which returns a copy of the data. In your definition of c, you’re doing basic slicing, which returns a view of the data.

    To tell the difference, it helps to understand how indices are passed to python objects. Here are some examples:

    >>> class ShowIndex(object):
    ...     def __getitem__(self, index):
    ...         print index
    ... 
    >>> ShowIndex()[:,:]
    (slice(None, None, None), slice(None, None, None))
    >>> ShowIndex()[...,:]
    (Ellipsis, slice(None, None, None))
    >>> ShowIndex()[0:5:2,::-1]
    (slice(0, 5, 2), slice(None, None, -1))
    >>> ShowIndex()[0:5:2,np.arange(3)]
    (slice(0, 5, 2), array([0, 1, 2]))
    >>> ShowIndex()[0:5:2]
    slice(0, 5, 2)
    >>> ShowIndex()[5, 5]
    (5, 5)
    >>> ShowIndex()[5]
    5
    >>> ShowIndex()[np.arange(3)]
    [0 1 2]
    

    As you can see, there are many different possible configurations. First, individual items may be passed, or tuples of items may be passed. Second, the tuples may contain slice objects, Ellipsis objects, plain integers, or numpy arrays.

    Basic slicing is activated when you pass only objects like int, slice, or Ellipsis objects, or None (which is the same as numpy.newaxis). These can be passed singly or in a tuple. Here’s what the docs have to say about how basic slicing is activated:

    Basic slicing occurs when obj is a slice object (constructed by start:stop:step notation inside of brackets), an integer, or a tuple of slice objects and integers. Ellipsis and newaxis objects can be interspersed with these as well. In order to remain backward compatible with a common usage in Numeric, basic slicing is also initiated if the selection object is any sequence (such as a list) containing slice objects, the Ellipsis object, or the newaxis object, but no integer arrays or other embedded sequences.

    Advanced indexing is activated when you pass a numpy array, a non-tuple sequence containing only integers or containing subsequences of any kind, or a tuple containing an array or subsequence.

    For details on how advanced indexing and basic slicing differ, see the docs (linked to above). But in this particular case, it’s clear to me what’s happening. It has to do with the following behavior when using partial indexing:

    The rule for partial indexing is that the shape of the result (or the interpreted shape of the object to be used in setting) is the shape of x with the indexed subspace replaced with the broadcasted indexing subspace. If the index subspaces are right next to each other, then the broadcasted indexing space directly replaces all of the indexed subspaces in x. If the indexing subspaces are separated (by slice objects), then the broadcasted indexing space is first, followed by the sliced subspace of x.

    In your definition of a, which uses advanced indexing, you effectively pass the sequence [0, 1] in as the third item of the tuple, and since no broadcasting happens (because there is no other sequence), everything happens as expected.

    In your definition of b, also using advanced indexing, you effectively pass two sequences, [0], the first item (which is converted into an intp array), and [0, 1], the third item. These two items are broadcast together, and the result has the same shape as the third item. However, since broadcasting has happened, we’re faced with a problem: where in the new shape tuple do we insert the broadcasted shape? As the docs say,

    there is no unambiguous place to drop in the indexing subspace, thus it is tacked-on to the beginning.

    So the 2 that results from broadcasting is moved to the beginning of the shape tuple, producing an apparent transposition.

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

Sidebar

Related Questions

I have been working on this a while. I see multi-dimensional arrays in php
I'm working with an open-source CMS that uses some very big multi dimensional arrays/objects.
I'm working on a project of which I should store constant multi dimensional arrays.I
I am new to Multi-Dimensional arrays and working on building my php skills as
I have a multi-dimensional array similar to the example below that I want to
I am working in Java and am wondering, are multi-dimensional arrays like grids where
I’m working on a multi dimensions array but i have a problem Imagine I
I am working with multi-dimentioned arrays of bool , int , and various struct
I am working on a multi-thread program, and have a question about where to
I'm working on a multi-language website. I have a problem with the color of

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.