Numpy ix_ Function: Things You Need to Know

The numpy library in python is used for working with multi-dimensional arrays and matrices while performing logical and mathematical operations on them. In addition, Numpy is the fundamental library for performing all types of scientific computations in python. This article will be learning about a function in numpy, which is the numpy ix function.

What is numpy ix function?

The numpy ix function in python is used for constructing open mesh from multiple sequences. The function takes N number of one-dimensional sequences, and as the output, it returns N number of N dimension sequences. The main purpose of the numpy ix function is for slicing arrays. We can use the ix function as an index to a given array.

Syntax of numpy ix function

The syntax for the ix function in numpy is:


The function accepts only one argument which is the N dimensional array.


args: It is the N number of one-dimensional sequence(s). The sequence can be of either boolean or integer type.

Return Value of numpy ix function

out : The output of the numpy ix function is a tuple of n dimensional arrays. The N number of arrays with N dimensions together form an open mesh.

By using numpy ix, we can index arrays by passing the output of the function as an index to the array. ix will construct index arrays that will index the cross product. Let us understand the function by implementing it in python.

Numpy ix_ in Python

Obtaining tuple from numpy.ix_()

For using the ix function, we will first have to import the numpy library.

import numpy as np

Now, we will call the ix function using np.ix_(), and we shall pass 2 one dimensional arrays as an argument to the function. Finally, we shall assign the output of the function to the variable ix_tuple.

ix_tuple = np.ix_([1,0],[2,3])

Let us print the type of the variable ix_tuple.


The output is:

<class 'tuple'>

So it is clear that the numpy ix_() function returns tuple as the output. Now, we shall print the tuple ‘ix_tuple’.


The output is:

       [0]]), array([[2, 3]]))

It returns two 1 dimensional arrays wrapped in a single tuple. Now we shall try to pass this tuple as an index to a given array so as to use it as an index. But first, let us create an array.

Using the obtained tuple as an array index

To create an array, we will make use of two functions: the arange() and the reshape() function.

arange() is a function present in the numpy library. It creates an object of N dimensional array containing values in the given interval.

reshape() function is used for shaping a given array of values into the mentioned space size. To use it as a method, we use the syntax ndarray.reshape().

Here, we will create an array containing values from 0 – 7. So, we will pass 8 as an argument to the arange() function where the value 8 is exclusive. For the array size, we want it to be an array containing 2 rows and 4 columns. So, we pass 2 and 4 as an argument for the reshape() method.

array = np.arange(8).reshape(2, 4)

The array created is:

[[0 1 2 3]
 [4 5 6 7]]

Now, we shall use the ix_tuple as the index to the array ‘array’ and access the respective elements.


The output is:

array([[6, 7],
       [2, 3]])

Our ix_tuple was : (array([[1], [0]]), array([[2, 3]])) and our array was :

[[0 1 2 3]
[4 5 6 7]]

The cross product of the ix_tuple accesses the array. The subarray was selected by taking 1 and 0 as the rows and 2 and 3 as the columns. Therefore, the indexes returned are (1,2), (1,3), (0,2) and (0,3). In the array,

( 1 , 2 ) denotes element 6, ( 1 , 3 ) denotes element 7, ( 0 , 2 ) denotes element 2. ( 0 , 3 ) denotes element 3.

So the elements are printed accordingly.

Numpy Ix_

Passing boolean values to numpy ix function

We can also pass boolean values as arguments to the ix function. Here, for the row value, we will be passing [ True, True ] instead of [ 1, 0 ] in the above example. Since we passed [ True, True ], we will get the array [ 0 , 1 ]. We shall only count the values which have been marked as true. For false, none will be passed.

ix_tuple = np.ix_([True,True],[2,3])

The ix_tuple would be:

       [1]]), array([[2, 3]]))

So, on passing the ix_tuple as the index to the same array ‘array’, we will get the same values, but their order would be changed.

array([[2, 3],
       [6, 7]])

The array generated would be :

( 0 , 2 ) denotes element 2, ( 0 , 3 ) denotes element 3, ( 1 , 2 ) denotes element 6, ( 1 , 3 ) denotes element 7.

FAQ’s on numpy ix

What is ‘IndexError: index is out of bounds’ error ?

The ‘IndexError: index is out of bounds’ error can occur while using the numpy.ix_() function. It occurs when the indexes mentioned in the tuple does not match with the size of the array you are indexing. Because of that, the index goes out of bounds of the array so the above error is thrown.

What is ‘valueerror : cross index must be 1 dimensional’ error ?

‘valueerror : cross index must be 1 dimensional’ is raised when you try to pass a multi dimensional array as an argument to the numpy.ix() function. This is because the function only accepts one dimensional array and will raise an error otherwise.

That sums up Numpy ix function. If you have any questions in mind, let us know in the comments below.

Until then, Keep Learning!

Notify of
Inline Feedbacks
View all comments