1. Concept of broadcasting
Broadcasting in NumPy is a mechanism that allows arrays of different shapes to be combined in arithmetic operations without explicitly replicating data. Instead of looping, NumPy automatically “stretches” smaller arrays across the larger array’s dimensions.
Long form
import numpy as np
arr = np.array([1, 2, 3])
result = arr + 5
One-liner
result = __import__('numpy').array([1,2,3]) + 5
Here, the scalar 5 is broadcast across each element, producing [6, 7, 8].
2. Scalar with array broadcasting
Scalars can be broadcast easily across arrays, which removes the need for explicit loops or list comprehensions.
Long form
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
result = matrix * 10
One-liner
result = __import__('numpy').array([[1,2],[3,4]]) * 10
The scalar 10 multiplies every element in the 2×2 matrix, giving [[10, 20], [30, 40]].
3. Broadcasting between 1D and 2D arrays
When shapes differ, NumPy stretches dimensions of smaller arrays to match.
Long form
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
vector = np.array([10, 20, 30])
result = matrix + vector
One-liner
result = __import__('numpy').array([[1,2,3],[4,5,6]]) + __import__('numpy').array([10,20,30])
Here, the row vector [10, 20, 30] broadcasts across each row of the 2D array.
4. Shape compatibility rules
Broadcasting only works if dimensions are compatible:
Dimensions match, or
One of them is 1.
Long form
import numpy as np
a = np.ones((3,1))
b = np.ones((1,3))
result = a + b
One-liner
result = __import__('numpy').ones((3,1)) + __import__('numpy').ones((1,3))
Here, shapes (3,1) and (1,3) expand to (3,3). The output is a 3×3 matrix of ones.
5. Real-world use cases of broadcasting
Broadcasting simplifies scaling, normalization, and batch operations.
Long form
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6]])
mean = np.mean(data, axis=0)
normalized = data - mean
One-liner
normalized = __import__('numpy').array([[1,2,3],[4,5,6]]) - __import__('numpy').mean(__import__('numpy').array([[1,2,3],[4,5,6]]),axis=0)
Here, broadcasting subtracts the column-wise mean from each row—used heavily in machine learning preprocessing.
5 Interview Questions (with Answers):
Q: What is broadcasting in NumPy?
A: Broadcasting allows arithmetic operations on arrays of different shapes by virtually expanding dimensions without creating data copies.
Q: When can broadcasting fail?
A: It fails if array dimensions are not compatible (neither equal nor 1).
Q: Does broadcasting create copies of data?
A: No, broadcasting uses virtual expansion, making it memory-efficient.
Q: How does broadcasting improve performance?
A: It eliminates explicit loops and leverages vectorized operations implemented in C for speed.
Q: Give one real-world example of broadcasting.
A: Normalizing datasets by subtracting mean values or scaling features without writing loops.
#numpy #Python #DataScience #MachineLearning #Broadcasting #CodingTips #InterviewPrep
Информация по комментариям в разработке