NumPy for ML: Tutorial 4

NumPy Tutorial 4

Concept: Math Without Loops

This is the "magic" of NumPy. In standard Python, you cannot add a number to a list directly (it causes an error). In NumPy, the number is "broadcast" to every item instantly.

Python List
data = [1, 2, 3]
data + 5
ERROR: Can only concatenate list to list.
NumPy Array
data = np.array([1, 2, 3])
print(data + 5)
Output: [6, 7, 8]
Activity: Match the Result

Drag the Python/NumPy operation on the left to the correct mathematical result on the right.

Operations:
np.array([2, 4]) * 2
np.array([10, 20]) + 5
np.array([10, 10]) / 2
Results:
Result: [15, 25]
Drop Addition Here
Result: [4, 8]
Drop Multiplication Here
Result: [5., 5.]
Drop Division Here
Knowledge Check

What happens if you run np.array([1, 2, 3]) * 0?

A. It deletes the array.
B. It returns [0, 0, 0].
C. It returns an Error.

Post a Comment

0 Comments