v Selecting Elements In An Array - Machine Learning

Selecting Elements In An Array

Preliminaries

# Load library
import numpy as np

Create Vector

# Create row vector
vector = np.array([1, 2, 3, 4, 5, 6])

Select Element

# Select second element
vector[1]
2

Create Matrix

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

Select Element

# Select second row, second column
matrix[1,1]
5

Create Tensor

# Create matrix
tensor = np.array([
                    [[[1, 1], [1, 1]], [[2, 2], [2, 2]]],
                    [[[3, 3], [3, 3]], [[4, 4], [4, 4]]]
                  ])

Select Element

# Select second element of each of the three dimensions
tensor[1,1,1]
array([4, 4])