In a numpy array array how do you select an element? Selecting an element is very easy if you know the index of the element. In this we will be selecting element from vector matrix and tensor.
So this is the recipe on how we can Select Elements from Numpy Array.
import numpy as np
We have only imported numpy which is needed.
We have created a vector and we will select a element from it by passing the index in the object.
vector = np.array([1, 2, 3, 4, 5, 6])
print(vector[1])
We have created a matrix and we will select a element from it by passing the index in the object. For matrix we have to pass two values in the form (row , column) to select the element.
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(matrix[1,1])
We have created a tensor and we will select a element from it by passing the index in the object. For tensor we have to pass three value to select the element.
tensor = np.array([
[[[1, 1], [1, 1]], [[2, 2], [2, 2]]],
[[[3, 3], [3, 3]], [[4, 4], [4, 4]]]
])
print(tensor[1,1,1])
So the output comes as
2 5 [4 4]