Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

NumPy Arithmetic Operations

Perform fast element-wise arithmetic, comparison operations, aggregation, and understand matrix multiplication in NumPy.

Element-wise arithmetic

NumPy operations between equal-size arrays apply the operation element-wise โ€” this is what the book calls "vectorization," and it's what lets you skip writing for loops.

Python
import numpy as np
 
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
 
print(a + b)
print(a - b)
print(a * b)
print(a / b)

Scalar operations

Arithmetic with a scalar propagates that value to every element in the array (this is broadcasting in its simplest form).

Python
import numpy as np
 
arr = np.array([1, 2, 3])
print(arr * 10)
print(arr + 0.5)

Power and modulo

Python
import numpy as np
 
arr = np.array([2, 3, 4])
print(arr ** 2)  # [4 9 16]
print(arr % 2)   # [0 1 0]

Comparisons

Comparisons produce boolean arrays, applied element-wise just like arithmetic.

Python
import numpy as np
 
arr = np.array([10, 20, 30])
print(arr > 15)       # [False True True]
print(arr == 20)      # [False True False]
diagram Same array, four kinds of results mermaid
Arithmetic, comparison, and aggregation all start from the same element-wise machinery but end in different shapes.

Aggregations (sum, mean, min, max)

Python
import numpy as np
 
mat = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
 
print(mat.sum())          # sum of all
print(mat.sum(axis=0))    # column sums
print(mat.sum(axis=1))    # row sums
 
print(mat.mean(axis=0))
print(mat.min(axis=1))
print(mat.max(axis=1))

Matrix multiplication vs element-wise multiplication

Element-wise multiplication

* multiplies matching positions โ€” it is not the same as matrix multiplication.

Python
import numpy as np
 
a = np.array([[1, 2], [3, 4]])
b = np.array([[10, 20], [30, 40]])
print(a * b)

Matrix multiplication

Use @ operator or np.matmul.

Python
import numpy as np
 
a = np.array([[1, 2], [3, 4]])
b = np.array([[10, 20], [30, 40]])
print(a @ b)

Rounding

Python
import numpy as np
 
arr = np.array([1.234, 5.678])
print(np.round(arr, 2))
print(np.floor(arr))
print(np.ceil(arr))

Check your understanding

quizCheck yourself
  1. For mat = np.array([[1, 2, 3], [4, 5, 6]]), what does mat.sum(axis=0) return?

    Show answer

    B โ€” [5 7 9] โ€” axis=0 collapses the rows and gives one sum per column: 1+4, 2+5 and 3+6. axis=1 would give the row sums [6 15].

  2. For a = np.array([[1, 2], [3, 4]]) and b = np.array([[10, 20], [30, 40]]), what is a * b?

    Show answer

    B โ€” [[10 40] [90 160]] โ€” The * operator multiplies matching positions: 1*10, 2*20, 3*30 and 4*40. The matrix product a @ b would be [[70 100] [150 220]].

  3. What does arr % 2 return for arr = np.array([2, 3, 4])?

    Show answer

    A โ€” [0 1 0] โ€” % is the element-wise remainder after dividing by 2. The squares [4 9 16] come from arr ** 2.

  4. For arr = np.array([1.234, 5.678]), which call returns [1. 6.]-style whole numbers rounded up?

    Show answer

    C โ€” np.ceil(arr) โ€” np.ceil rounds every value up, giving 2 and 6. np.floor rounds down to 1 and 5, and np.round(arr, 2) keeps two decimals.

Next

Continue to: NumPy Universal Functions (ufuncs) to learn fast built-in vectorized functions.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Element-wise vs Matrix Multiplication

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Sum Along an Axis

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Round, Floor, Ceil

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did