NumPy Operations

Data ScienceNumPyFree Lesson

Advertisement

Introduction

NumPy provides efficient vectorized operations for mathematical computations.

Mathematical Functions

arr = np.array([1, 2, 3, 4, 5])

# Trigonometric
np.sin(arr)
np.cos(arr)
np.tan(arr)

# Exponential and log
np.exp(arr)       # e^1, e^2, ...
np.log(arr)       # Natural log
np.log10(arr)     # Base 10 log
np.log2(arr)      # Base 2 log

# Power and roots
np.sqrt(arr)      # Square root
np.power(arr, 3)  # Cubed

Array Manipulation

arr = np.array([[1, 2], [3, 4]])

# Transpose
arr.T

# Reshape
arr.reshape(4, 1)

# Flatten
arr.flatten()

# Concatenate
np.concatenate([arr, arr], axis=0)  # Vertical
np.concatenate([arr, arr], axis=1)  # Horizontal

Statistical Functions

arr = np.array([1, 2, 3, 4, 5])

np.min(arr)
np.max(arr)
np.mean(arr)
np.median(arr)
np.std(arr)      # Standard deviation
np.var(arr)      # Variance
np.percentile(arr, 75)  # 75th percentile

Practice Problems

  1. Normalize array to 0-1 range
  2. Calculate moving average
  3. Find correlation between arrays
  4. Implement matrix multiplication
  5. Compute covariance matrix

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement