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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:40:08+00:00 2026-06-01T23:40:08+00:00

Say I have the array: >>> import numpy as np >>> myarr = np.array([[1],[2],[3],[2]])

  • 0

Say I have the array:

 >>> import numpy as np
 >>> myarr = np.array([[1],[2],[3],[2]])

and I want to use logical indexing to return the sub-array where myarr is not equal to 2.

 >>> subarr = myarr[myarr != 2]
 >>> print subarr
 [1 3]

Thus, myarr.shape = (4,1) but subarr.shape = (2,).

Why is the size of dimension 2 being changed when the logical indexing doesn’t affect dimension 2? I could easily just reshape the output, but if I need to use logical indexing often in my code, I don’t want to have to always account for extra dimensions (i.e. dimensions that are obviously not relevant to the logical slice I am doing) that may have changed.

This (N,1) –> (K,) example is especially bothersome, since any logical slice of a something-by-one array must also be a something-by-one array.

I feel like there must be a cleaner way to have the NumPy slice operations leave trivial array dimensions alone. Any suggestions?

  • 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-01T23:40:09+00:00Added an answer on June 1, 2026 at 11:40 pm

    In numpy indexing with a boolean array is the same as indexing with array.nonzero(). Because array.nonzero() returns two sets of indices for a 2darray, you are in fact indexing on both the first and the second dimension:

    >>> myarr = np.array([[1],[2],[3],[2]])
    >>> (myarr != 2).nonzero()
    (array([0, 2]), array([0, 0]))
    >>> myarr[(array([0, 2]), array([0, 0]))]
    array([1, 3])
    

    What you really want is:

    >>> myarr[array([0, 2])]
    array([[1],
           [3]])
    

    Which you can get by:

    >>> myarr[myarr[:,0] != 2]
    array([[1],
           [3]])
    

    or:

    >>> myarr[(myarr != 2).nonzero()[0]]
    array([[1],
           [3]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following array: import numpy as np a = ['hello','snake','plate'] I
Let's say I have a 10 element array: from ctypes import * arr =
Say I have an array of table DDL queries and I want to get
Say I have an array of arbitrary size holding single characters. I want to
What I want to do is following. Lets say we have array like that
Say I have an array of values nameArr = ['josh','is','a','person'] and I want a
Say I have a 100 element numpy array. I perform some calculation on a
Say I have an array, and I use the keep_if or select methods to
Say I have an array in NumPy containing evaluations of a continuous differentiable function,
Let's say I have array of bytes: byte[] arr = new byte[] { 0,

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.