Next

Mathematical Operations

  • Learn to perform arithmetic operations on NumPy arrays for efficient data calculations.
  • Arithmetic Operations

    What are Arithmetic Operations?

    Arithmetic operations perform basic mathematical calculations on NumPy arrays.
    They are applied element-wise, meaning each element is operated on individually.

    Supported Arithmetic Operations

    • Addition (+)

    • Subtraction (-)

    • Multiplication (*)

    • Division (/)

    • Floor Division (//)

    • Modulus (%)

    • Power (**)

    Example – Arithmetic Operations

import numpy as np

a = np.array([10, 20, 30])
b = np.array([2, 4, 6])

print(a + b)   # [12 24 36]
print(a - b)   # [ 8 16 24]
print(a * b)   # [20 80 180]
print(a / b)   # [5. 5. 5.]
print(a ** 2)  # [100 400 900]
  • Operations with Scalars

print(a + 5)
print(a * 2)
  • Output:

[15 25 35]
[20 40 60]
  • Why NumPy Arithmetic is Powerful
    • Faster than Python loops

    • Cleaner and readable code

    • Supports large datasets efficiently

    • Used in vectorized calculations


    Statistical Functions

    What are Statistical Functions?

    Statistical functions help summarize and analyze numerical data by computing values like mean, median, sum, and standard deviation.


    Common Statistical Functions

    Function

    Description

    sum()

    Total of elements

    mean()

    Average value

    median()

    Middle value

    min()

    Smallest value

    max()

    Largest value

    std()

    Standard deviation

    var()

    Variance

    percentile()

    Percentile value

    Example – Statistical Functions

data = np.array([10, 20, 30, 40, 50])

print(np.sum(data))      # 150
print(np.mean(data))     # 30.0
print(np.median(data))   # 30.0
print(np.min(data))      # 10
print(np.max(data))      # 50
  • Standard Deviation & Variance

print(np.std(data))
print(np.var(data))
  • Percentile Example

print(np.percentile(data, 75))
  • Statistical Functions on 2D Arrays

arr2d = np.array([[10, 20, 30],
                  [40, 50, 60]])

print(np.mean(arr2d, axis=0))  # Column-wise mean
print(np.mean(arr2d, axis=1))  # Row-wise mean
  • Axis Explanation

    • axis=0 → Column-wise operation

    • axis=1 → Row-wise operation

    Real-World Use Cases

    • Data summarization

    • Performance analysis

    • Financial calculations

    • Machine learning preprocessing

    • Business analytics reports

Next