NumPy arrays are stored in contiguous blocks of memory, which allows NumPy to take advantage of vectorization and other optimization techniques. Python lists are stored as individual objects in memory, which makes them less efficient and performant than NumPy arrays for numerical data.
import numpy as np
# create a NumPy array of integers from 1 to 10
arr = np.arange(1, 11)
# compute the square of each element using vectorized operations
squares = arr**2
# print the squares
print(squares)
Use the np.arange() function to create a NumPy array of integers from 1 to 10. We then use vectorized operations provided by NumPy to compute the square of each element in the array. This is more efficient and performant than using a Python list and a for loop, or a Python list comprehension to compute the squares.
About
Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.