1 of 13

Introduction to NumPy

Efficient Numerical Computations in Python

Presented by: Atul Nag

2 of 13

What is NumPy?

  • A powerful library for numerical computations in Python.
  • Core component: ndarray (N-dimensional array).
  • Applications: Data Science, Machine Learning, Bioinformatics, Image Processing.

  • Example:�import numpy as np�arr = np.array([1, 2, 3])�print(arr)

3 of 13

Why Use NumPy?

  • Performance: Faster than Python lists.
  • Memory Efficiency: Uses less memory.
  • Convenience: Provides functions for mathematical, logical, and statistical operations.
  • Example:

import numpy as np

import time

list_data = list(range(1000000))

start = time.time()

list_result = [x * 2 for x in list_data]

end = time.time()

print("Python list time:", end - start)

arr = np.arange(1000000)

start = time.time()

numpy_result = arr * 2

end = time.time()

print("NumPy time:", end - start)

4 of 13

Creating Arrays

  • Basic Arrays: np.array()
  • - Predefined Arrays:
    • np.zeros(),
    • np.ones(),
    • np.random.random()
  • Example:

# Basic array

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

# Predefined arrays

zeros = np.zeros((2, 3))

ones = np.ones((3, 2))

random_arr = np.random.random((2, 2))

5 of 13

Inspecting Arrays

  • shape, size, dtype, ndim
  • Example:

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

print("Shape:", arr.shape)

print("Size:", arr.size)

print("Data type:", arr.dtype)

print("Number of dimensions:", arr.ndim)

6 of 13

Indexing and Slicing

  • Access specific elements or subsets of an array.
  • Boolean indexing for conditions.
  • Example:

arr = np.array([[1, 2, 3], [4, 5, 6]])# Access specific element

print(arr[0, 1])

# Slice

print(arr[:, 1])

# Boolean indexing

print(arr[arr > 3])

7 of 13

Array Operations

  • Element-wise operations, Broadcasting, Aggregations
  • Example:

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

print(arr + 2)

matrix = np.ones((3, 3))

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

print(matrix + vector)

print(np.sum(arr))

print(np.mean(arr))

8 of 13

Linear Algebra

  • Dot product, Matrix multiplication, Transpose
  • Example:

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

matrix2 = np.array([[5, 6], [7, 8]])

print(np.dot(matrix1, matrix2))

print(matrix1 @ matrix2)

print(matrix1.T)

9 of 13

Reshaping and Resizing

  • reshape, ravel, flatten, resize
  • Example:

arr = np.arange(6)

reshaped = arr.reshape((2, 3))

print(reshaped)

flattened = reshaped.flatten()

print(flattened)

10 of 13

Stacking and Splitting

  • Vertical and horizontal stacking
  • Splitting arrays
  • Example:

arr1 = np.array([1, 2])

arr2 = np.array([3, 4])

print(np.vstack((arr1, arr2)))

print(np.hstack((arr1, arr2)))

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

print(np.split(arr, 2))

11 of 13

Masking and Filtering

  • Use conditions to filter data.
  • Example:

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

mask = arr > 3

print(arr[mask])

12 of 13

Practical Examples

  • Image Processing: Convert an image to grayscale.
  • Bioinformatics: Perform operations on genetic data matrices.

  • Example:

data = np.array([[1, 0, 1], [0, 1, 1], [1, 1, 0]])

print(np.sum(data, axis=0))

13 of 13

Resources

  • - NumPy Documentation: https://numpy.org/doc/
  • - Tutorials: W3Schools, GeeksforGeeks
  • - Practice: Kaggle Notebooks, NumPy exercises