v Apply Operations To Elements - Machine Learning

Apply Operations To Elements

Preliminaries

# Load library
import numpy as np

Create Matrix

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

Create Vectorized Function

# Create a function that adds 100 to something
add_100 = lambda i: i + 100

# Create a vectorized function
vectorized_add_100 = np.vectorize(add_100)

Apply Function To Elements

# Apply function to all elements in matrix
vectorized_add_100(matrix)
array([[101, 102, 103],
       [104, 105, 106],
       [107, 108, 109]])