[Python numpy] Dynamically specify the index of the array

Vocabulary Book. Reprinting a lot because it is troublesome to search every time. There is an original URL.

Method 1: take (indices, axis =) method

ʻIndices can be an array. However, it is not possible to specify multiple dimensions by putting tuples in ʻaxis.

nx, ny, nz = 2, 3, 4
arr = np.arange(nx * ny * nz).reshape(nx, ny, nz)

arr.take(0, axis=0) # arr[0]Same as
arr.take(1, axis=1) # arr[:,1]Same as

[Numpy: numpy.take] (https://docs.scipy.org/doc/numpy/reference/generated/numpy.take.html)

Method 2: Combine sline (None)

slice (None) or slice (None, None, None) is the same as : in the index.

#The following is arr[:,0,2]Same as
I = [slice(None)] * arr.ndim # [:,:,:]Same as
I[1] = 0
I[2] = 2
arr[tuple(I)]

[stackoverflow: Dynamic axis indexing of Numpy ndarray] (https://stackoverflow.com/questions/31094641/dynamic-axis-indexing-of-numpy-ndarray)

Recommended Posts

[Python numpy] Dynamically specify the index of the array
the zen of Python
python numpy array calculation
The shape of the one-dimensional array of numpy was complicated
Output in the form of a python array
[Python] Summary of functions that return the index that takes the closest value in the array
[Golang] Specify an array in the value of map
[Python] Summary of how to specify the color of the figure
Towards the retirement of Python2
About the ease of Python
Create a python numpy array
About the features of Python
The Power of Pandas: Python
Find the index of the maximum value (minimum value) of a multidimensional array
[Python3] Call by dynamically specifying the keyword argument of the function
I want to judge the authenticity of the elements of numpy array
Get the index of each element of the confusion matrix in Python
The story of Python and the story of NaN
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
[Python] Understanding the potential_field_planning of Python Robotics
Python application: Numpy Part 3: Double array
Review of the basics of Python (FizzBuzz)
About the basics list of Python basics
Learn the basics of Python ① Beginners
Specify the encoding of the unicode string in the Python 2.x print statement
Change the length of Python csv strings
Check the behavior of destructor in Python
[Python3] Understand the basics of Beautiful Soup
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Python / numpy> list (numpy array) file save / load
[Python] Summary of array generation (initialization) time! !! !!
[Python] Understand the content of error messages
[Python3] Rewrite the code object of the function
I didn't know the basics of Python
[Python] Try pydash of the Python version of lodash
[python] Checking the memory consumption of variables
Check the path of the Python imported module
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
The Python project template I think of.
In search of the fastest FizzBuzz in Python
Python Basic Course (at the end of 15)
Set the process name of the Python program
[Python] Get the character code of the file
The story of blackjack A processing (python)
Intuitively learn the reshape of Python np
Python Note: The secret role of commas
Learning notes from the beginning of Python 2
How to sort by specifying a column in the Python Numpy array.
[Python] Combine all the elements in the array
I made a function to see the movement of a two-dimensional array (Python)
Japanese translation: PEP 20 --The Zen of Python
[Python3] Understand the basics of file operations
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
plot the coordinates of the processing (python) list and specify the number of times in draw ()
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)