numpy: Broadcasting, Vectorizing and Masking
Less common but useful numpy functions
Numpy is a powerful library in Python that is widely used for scientific computing and data analysis. It is built on top of the Python programming language and provides a large set of array and matrix operations that are optimized for performance. In this article, we will explore some advanced usage of numpy with code examples.
Broadcasting
Broadcasting is a powerful feature of numpy that allows you to perform operations on arrays of different sizes. It is a way of vectorizing operations so that they can be applied to multiple elements of an array at once. Broadcasting allows operations between arrays of different shapes, by "broadcasting" the smaller array across the bigger one.
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
b = np.array([10, 20, 30])
c = a + b
print(c)
# Output: [[11 22 33]
# [14 25 36]]
In the above example, we have two arrays a and b of different sizes, but we are able to perform an operation (addition) on them using broadcasting. In this example, the b array is being broadcasted to the shape of a so that the operation of addition can be performed element-wise.
Masking
Masking is a technique in numpy that allows you to select specific elements of an array based on a condition.
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
mask = a > 3
b = a[mask]
print(b) # Output: [4 5 6]
In this example, we have created a mask that selects elements of the array a that are greater than 3. We then use this mask to select the corresponding elements of the array a and store them in a new array b.
Vectorizing Functions
Numpy provides a function called vectorize that allows you to create a vectorized version of a function. This means that the function can be applied to multiple elements of an array at once, without the need for explicit loops.
import numpy as np
def my_func(x):
return x ** 2
vec_func = np.vectorize(my_func)
a = np.array([1, 2, 3, 4, 5, 6])
b = vec_func(a)
print(b) # Output: [ 1 4 9 16 25 36]In this example, we have created a vectorized version of the function my_func using the vectorize function. We can then use this function to apply the operation (squaring) on multiple elements of the array a at once.
These are just a few examples of the advanced functionality that numpy provides. With numpy, you can perform complex operations on arrays and matrices with ease and efficiency. It is a must-have tool for any data scientist or engineer working with large datasets in Python.

