numpy provides various methods that help in extracting subsets of values from existing ndarray objects. Methods include compress(), diagonal(), squeeze() and take()
Based on the requirements, care should be taken to ensure that returned objects are copies.
Creating such subsets from an existing numpy.ndarray object is based on several criteria. For example, a subset of an ndarray can be created from the elements of a diagonal, or one dimension can be omitted of an array if there is only one element in that dimension.
Elements can be extracted from a numpy.ndarray object based on Boolean conditions for each of the indices or based on an array of indices.
The numpy.ndarray provides several methods that help creating ndarray objects with a subset of elements from an existing ndarray object.
These methods take various criteria such as selected index of an array or a specific index of a diagonal and so on.
Few such methods of numpy.ndarray include compress(), diagonal(), squeeze() and take() which are given focus here.
Various modes like clipping of values, wrapping around of indices are supported in take() method of numpy.ndarray.
Select each ndarray element based on a Boolean condition
Using the compress() method, for a given numpy.ndarray object each of its element can be specified a Boolean expression based on which the presence or the absence of that element in the new array is decided.
The Boolean expressions are specified as a flat 1-dimensional array.
Elements can be selected based on axis or without specifying any axis.
Example:numpy.ndarray.compress
python
import numpy as np
# define a 3-dimensional ndarray object
array_3d = np.array([[[10,20,30],
[40,50,60],
[70,80,90]],
[[100,110,120],
[130,140,150],
[160,170,180]],
[[190,200,210],
[220,230,240],
[250,260,270]]])
# Consider the 3d array as a flattened array
# Select only first 3 elements
compressedArray = array_3d.compress([1, 1, 1])
print("Selecting first 3 elements from a 3-dimensaional array")
print(compressedArray)
# Do not select the first two elements but select the next two elements
print("Selecting 5 elements from the beginning but excluding the first 2 elements")
compressedArray = array_3d.compress([0, 0, 1, 1, 1])
print(compressedArray)
# Consider the 3d array as a 3d array and select element elements on a given axis
# Select elements on axis 0
compressedArray = array_3d.compress([1,1,1], axis=0)
print("Elements selected along axis 0:")
print(compressedArray)
# Select elements on axis 1
compressedArray = array_3d.compress([1,1,1], axis=0)
print("Elements selected along axis 1:")
print(compressedArray)
# Select elements on axis 2
compressedArray = array_3d.compress([1,1,1], axis=0)
print("Elements selected along axis 2:")
print(compressedArray)
Output
Output
Selecting first 3 elements from a 3-dimensaional array
[10 20 30]
Selecting 5 elements from the beginning but excluding the first 2 elements
[30 40 50]
Elements selected along axis 0:
[[[ 10 20 30]
[ 40 50 60]
[ 70 80 90]]
[[100 110 120]
[130 140 150]
[160 170 180]]
[[190 200 210]
[220 230 240]
[250 260 270]]]
Elements selected along axis 1:
[[[ 10 20 30]
[ 40 50 60]
[ 70 80 90]]
[[100 110 120]
[130 140 150]
[160 170 180]]
[[190 200 210]
[220 230 240]
[250 260 270]]]
Elements selected along axis 2:
[[[ 10 20 30]
[ 40 50 60]
[ 70 80 90]]
[[100 110 120]
[130 140 150]
[160 170 180]]
[[190 200 210]
[220 230 240]
[250 260 270]]]
Remove a redundant dimension from ndarray object
If an array needs to be reduced of a dimension, when it has only one element in that dimension - the method squeeze() of ndarray can be used.
Example: numpy.ndarray.squeeze()
python
import numpy as np
# A 2-dimensional array with only one 1d array inside
array_2d = np.array([[1,2,4]])
squeezed = array_2d.squeeze()
print("Input 2-dimensional Array:")
print(array_2d)
# Print the array that has been reduced of a dimension
print("Output after a 2-dimensional array is reduced of a redundant dimension:")
print(squeezed)
# A 3-dimensional array with only one 2d array inside
array_3d = np.array([[[1,2,4],
[2,2,4]]])
print("Input 3-dimensional Array:")
print(array_3d)
# Print the array that has been reduced of a dimension
squeezed = array_3d.squeeze()
print("Output after a 3-dimensional array is reduced of a redundant dimension:")
print(squeezed)
Output
Output
Input 2-dimensional Array:
[[1 2 4]]
Output after a 2-dimensional array is reduced of a redundant dimension:
[1 2 4]
Input 3-dimensional Array:
[[[1 2 4]
[2 2 4]]]
Output after a 3-dimensional array is reduced of a redundant dimension:
[[1 2 4]
[2 2 4]]
Retrieve diagonals from an n-dimensional array using numpy.ndarray
The diagonal() method can be used to retrieve diagonals of multidimensional arrays and matrixes.
Developers need to take care of examining whether the returned array is a copy or a view and create a copy if necessary.
Extract elements by specifying an array of indices
The take() method of numpy.ndarray is similar to the compress() method of numpy.ndarray. However, unlike compress() method that accepts boolean expressions for extracting values from a specific index of the ndarray the take method accepts an array of indices whose values will be returned.
The wrap mode enables rotating from the beginning of the array if the indices specified are greater than or equal to the number of elements present in the input array.
In clip mode if an index is are greater than or equal to the number of elements present in the input array the value for that index will be replaced by the value of last index on that axis.
Example
python
import numpy as np
# Extract values from an 1-dimensional ndarray object
print("Input 1-dimensional array:")
array_1d = np.array([10,20,30,40,50])
print(array_1d)
indices = np.array([1,2,3])
print("Array of indices:")
print(indices)
subset = array_1d.take(indices)
print("Values from selected indices of an one dimensional ndarray object:")
print(subset)
indices = np.array([3,4,5])
subset = array_1d.take(indices, mode='wrap')
print("Array of indices:")
print(indices)
print("Array Extracted from ndarray using take() method with wrap Mode enbaled:")
print(subset)
# Extract values from an 2-dimensional ndarray object
array2d = np.array([[1,3,5],
[7,9,11],
[13,15,17]])
print("Input 2-dimensional ndarray object:")
print(array2d)
indices = np.array([[0,2],[3,5],[8,9]])
subset = array2d.take(indices, mode='wrap')
print("2-d array extracted from the input 2-d array using take() method with wrap Mode enbaled:")
print(subset)
Output
Output
Input 1-dimensional array:
[10 20 30 40 50]
Array of indices:
[1 2 3]
Values from selected indices of an one dimensional ndarray object:
[20 30 40]
Array of indices:
[3 4 5]
Array Extracted from ndarray using take() method with wrap Mode enbaled:
[40 50 10]
Input 2-dimensional ndarray object:
[[ 1 3 5]
[ 7 9 11]
[13 15 17]]
2-d array extracted from the input 2-d array using take() method with wrap Mode enbaled:
[[ 1 5]
[ 7 11]
[17 1]]
Your privacy choices
We'd like to use Google Analytics cookies to count visits and see which tutorials are useful.
They are only set if you accept. We don't use advertising cookies and we don't sell your data.
You can change your choice any time from “Your privacy choices” at the bottom of every page.
Privacy Policy
Your privacy choices
Your browser sent a Global Privacy Control signal, so analytics is off unless you turn it on here.
Strictly necessary
Remembers this choice in your browser. No cookies, nothing sent to anyone.Always on