Home »
Python »
Python Programs
How to apply a lookup table to a large array in NumPy?
Learn about the convenient way to apply a lookup table to a large array in NumPy.
By Pranit Sharma Last updated : December 24, 2023
NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.
Problem statement
Suppose we are given an image that we want to read into numpy. We need to apply a lookup table for this image in numpy.
Applying a lookup table to a large array in NumPy
For this purpose, we can just use the image to index the lookup table if the lookup table is 1d.
Let us understand with the help of an example,
Python code to apply a lookup table to a large array in NumPy
# Import numpy
import numpy as np
# Creating a lookup table
lookup = np.arange(10) * 10
# Display lookup table
print("lookup table:\n",lookup,"\n")
# Creating an image array
img = np.random.randint(0,9,size=(3,3))
# Display original image
print("Image:\n",img,"\n")
# using img to index lookup
res = lookup[img]
# Display result
print("Result:\n",res)
Output
Python NumPy Programs »