Title: Apply Operations To Elements
Slug: apply_operations_to_elements
Summary: How to apply operations to elements of an array in Python.
Date: 2017-09-03 12:00
Category: Machine Learning
Tags: Vectors Matrices Arrays
Authors: Chris Albon
In [1]:
# Load library
import numpy as np
In [2]:
# Create matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [3]:
# 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)
In [4]:
# Apply function to all elements in matrix
vectorized_add_100(matrix)
Out[4]: