2248

slicing multi-dimensional numpy arrays with arrays [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/30917753/subsetting-a-2d-numpy-array" dir="ltr" rel="nofollow">Subsetting a 2D numpy array</a> <span class="question-originals-answer-count"> 4 answers </span> </li> </ul>

I have a numpy array (we'll call it test) of ~286 x 181 x 360, and need to extract a 3-D array from it. The ranges needed for the three dimensions are defined as other numpy arrays (a_dim, b_dim, and c_dim) (based ultimately on user input). Naively, I had hoped I'd be able to do something like big_array[a_dim,b_dim,c_dim]. That ran happily when the b_dim and c_dim contained only a single value (which happened to occur in my main test case), but doesn't work when they're bigger than 1.

Traceback (most recent call last): File "<pyshell#541>", line 1, in <module> test[a_dim,b_dim,c_dim] ValueError: shape mismatch: objects cannot be broadcast to a single shape

To simplify, given the following 4 arrays:

test=arange(125).reshape((5,5,5)) a_dim=[0,1] b_dim=[1,2] c_dim=[2,3]

What I'd like to get as output from that combination above is:

array([[[ 7, 8], [12, 13]], [[32, 33], [37, 38]]])

Or, a 3D array containing the all of the rows, columns, and bands (or whatever you'd like to call a third dimension) that are defined in a_dim, b_dim, and c_dim. I've tried using ix_ for this, but am clearly missing something from the examples I've seen:

>>> test[ix_((a_dim,b_dim,c_dim))] Traceback (most recent call last): File "<pyshell#517>", line 1, in <module> test[ix_((a_dim,b_dim,c_dim))] File "C:\Python27\lib\site-packages\numpy\lib\index_tricks.py", line 73, in ix_ raise ValueError, "Cross index must be 1 dimensional" ValueError: Cross index must be 1 dimensional

Any suggestions? Thanks!

Answer1:

So close -- just remove one pair of parens in the call to numpy.ix_():

>>> test[ix_(a_dim,b_dim,c_dim)] array([[[ 7, 8], [12, 13]], [[32, 33], [37, 38]]])

Recommend

  • Use SQLite as a key:value store
  • IOS Swift : UIAccessibilitySpeechAttributeIPANotation
  • ContentDialog max width
  • Duplicate Spring Batch Job Instance
  • How to split array dynamically based on single value in JavaScript Node.js
  • How should I reference services in my controller functions without using scope?
  • Switching between Google Maps Android API v2 DEBUG and RELEASE API Key
  • Referring to matched value in Rust
  • VS30063: You are not authorized to access
  • How to copy a array of unmanaged memory into the same unmanaged memory
  • Second layout is not showing with include tag in android studio
  • How does Reactive Banana's mapAccum function work?
  • How to bind from a ContentTemplate to the surrounding custom Control?
  • most efficient way to write data into a file
  • Using `rand()` with `having`
  • How to visualize k-means centroids for each iteration?
  • HTTPS access to Azure ubuntu Virtual Machine
  • SQL Server - Find records that have appeared 3 times in last 30 days
  • making an array of bigInteger of size biginteger in java
  • Flask Blueprints: How to use them?
  • Firebase Database OutofMemory Error
  • tensorflow-lite - using tflite Interpreter to get an image in the output
  • Moving an Android Studio project causes an error
  • How to add an Auth Token in every request using AFIncrementalStore?
  • Manipulating a group view in a ExpandableListView from the child view
  • Is there a way to remove a script from a doc (using the new doc embedded script)
  • SQLITE multiple table join with a condition
  • How do I set the logging properties in a spring java configuration?
  • Magento-Change Attribute of All Products
  • How to use Flask's render_template from an ajax POST form submit
  • Find angle of point on circle
  • LinkedIn API: Access Denied when getting Access Token
  • Google Spreadsheet Script to Blink a range of Cells
  • Using Service Component Runtime
  • WPF custom control and direct content support
  • convert json to excel in java
  • Create/delete users from text file using Bash script