How to slice a block multiple array from a multiple array in Python

Overview

Use the function numpy.ix_ to slice a block multi-array from a multi-array numpy.ndarray or torch.Tensor in Python.

Details

In julia

# Julia
#Make multiple arrays
(i, j, k) = (2, 3, 4)
x = reshape(1:i * j * k, i, j, k)
#Slice multiple arrays with multiple subscripts
(is, js, ks) = ([2, 1], [1, 3], 2:3)
x[is, js, ks] |> println

I found it difficult to find information on how to slice multiple arrays with multiple subscripts in Python, so make a note. numpy.ix_ is what you want. Note that NumPy and PyTorch default to Julia's Array in the opposite way of arranging the dimensions of a multi-array.

# Python + NumPy
import numpy
#Make multiple arrays
i, j, k = 2, 3, 4
x = (numpy.arange(k * j * i) + 1).reshape(k, j, i)
#Slice multiple arrays with multiple subscripts
# (In Python, is is like a reserved word, so is_To)
is_, js, ks = numpy.array([2, 1]) - 1, numpy.array([1, 3]) - 1, numpy.arange(2 - 1, 3)
print(x[numpy.ix_(ks, js, is_)])
# Python + PyTorch
import torch
import numpy
#Make multiple arrays
i, j, k = 2, 3, 4
x = (torch.arange(k * j * i) + 1).reshape(k, j, i)
#Slice multiple arrays with multiple subscripts
# (In Python, is is like a reserved word, so is_To)
is_, js, ks = torch.tensor([2, 1]) - 1, torch.tensor([1, 3]) - 1, torch.arange(2 - 1, 3)
print(x[numpy.ix_(ks, js, is_)])

Recommended Posts

How to slice a block multiple array from a multiple array in Python
How to retrieve multiple arrays using slice in python.
How to define multiple variables in a python for statement
How to get a stacktrace in python
How to make a string into an array or an array into a string in Python
How to get a string from a command line argument in python
How to delete multiple specified positions (indexes) in a Python list
How to open a web browser from python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
How to generate a Python object from JSON
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to get a value from a parameter store in lambda (using python)
How to write a string when there are multiple lines in python
How to sort by specifying a column in the Python Numpy array.
How to develop in Python
How to convert / restore a string with [] in python
How to write string concatenation in multiple lines in Python
[Python] How to expand variables in a character string
How to download files from Selenium in Python in Chrome
How to execute a command using subprocess in Python
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
How to write a Python class
[Python] How to do PCA in Python
How to convert an array to a dictionary with Python [Application]
How to collect images in Python
How to swap elements in an array in Python, and how to reverse an array.
How to run a Python program from within a shell script
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to launch AWS Batch from a python client app
How to use SQLite in Python
How to import a file anywhere you like in Python
How to transpose a 2D array using only python [Note]
A simple way to avoid multiple for loops in Python
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to sample from any probability density function in Python
How to get the last (last) value in a list in Python
How to use Mysql in python
[Python] How to swap array values
How to get a list of built-in exceptions in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to access wikipedia from python
How to handle Japanese in Python
How to get a quadratic array of squares in a spiral!
To receive multiple return values ​​from a function executed using parallel processing (Pool) in Python
How to create an instance of a particular class from dict using __new__ () in python
How to quickly count the frequency of appearance of characters from a character string in Python?
How to determine the existence of a selenium element in Python
How to pass arguments to a Python script in SPSS Modeler Batch
[Introduction to Python] How to output a character string in a Print statement
How to check the memory size of a variable in Python
Post a message from IBM Cloud Functions to Slack in Python
[Python] How to get & change rows / columns / values from a table.
How to create a heatmap with an arbitrary domain in Python