Introduction to NumPy

Jul 30, 2024

Introduction to NumPy

Overview

  • NumPy is a fundamental library for scientific computing in Python.
  • It's the base for many data science libraries in Python (e.g., Pandas).

Lecture Structure

  1. Background information on NumPy.
  2. Demonstration of useful methods and coding examples.

Background Information

What is NumPy?

  • A multi-dimensional array library.
  • Supports 1D, 2D, 3D, 4D, and more dimensions.

Why Use NumPy Over Lists?

  • Speed: NumPy arrays are faster than lists.
  • Fixed Types: NumPy uses fixed data types (e.g., int32, int16, int8).
    • Example: A 3x4 matrix with integer values.
    • Binary representation of values.
  • Memory Efficiency: Lists store more information per integer than NumPy arrays.
    • Lists store Object Value, Object Type, Reference Count, and Size.
    • NumPy uses fewer bytes.
  • Contiguous Memory: NumPy utilizes contiguous memory blocks.
    • Benefits: Faster computation using SIMD (Single Instruction Multiple Data).
    • More effective CPU cache utilization.
    • Memory blocks are next to each other.
  • No Type Checking: No need to check element types during iteration in NumPy.

Key Differences Between Lists and NumPy Arrays

  • Lists support insertion, deletion, appending, and concatenation.
  • NumPy allows item-wise computations directly.
  • Examples: Element-wise multiplication.

Applications of NumPy

  • Mathematics (similar to MATLAB).
  • Supports libraries like SciPy for advanced functions.
  • Backend for data manipulation libraries like Pandas.
  • Used in game programming (e.g., Connect 4).
  • Image storage (e.g., PNG images).
  • Relevant in machine learning due to the concept of tensors.

Getting Started with NumPy

Installation

  • Import with import numpy as np.
  • Install with pip install numpy or pip3 install numpy.

Initializing Arrays

  • 1D Array: np.array([1, 2, 3]).
  • 2D Array: np.array([[1.1, 2.2], [3.3, 4.4]]).
  • Checking dimensions, shape, data types, and size:
    • a.ndim, a.shape, a.dtype, a.itemsize.

Accessing and Modifying Arrays

Access Elements

  • Access by indexing: array[row, column].
  • Access rows and columns using slices: array[:, 0:2].
  • Boolean masking and advanced indexing.

Modifying Elements

  • Change specific elements, rows, columns using syntax like array[row, col] = value.
  • Ensure dimensions match when assigning new values.

Additional Array Initialization Methods

  • Zeros: np.zeros((shape)).
  • Ones: np.ones((shape)).
  • Full (specific value): np.full((shape), value).
  • Identity Matrix: np.identity(n).
  • Random Values (decimals or integers): np.random.rand(shape), np.random.randint(low, high, size).
  • Repeating Elements: np.repeat(array, repetitions, axis).

Copying and Performance Tips

Copying Arrays

  • Creating copies using array.copy() to avoid inadvertent modifications.
  • Example: b = a.copy().

Arithmetic Operations

  • Element-wise addition, subtraction, multiplication: array + 2, array * 2.
  • Use functions like np.sin(array) for mathematical operations.

Linear Algebra with NumPy

Matrix Multiplication

  • Using np.matmul(a, b) for matrix multiplication.
  • Operations like determinants, inverses available via np.linalg.

Statistical Operations

  • Finding minimum, maximum values: np.min(array), np.max(array), with axis parameter for row/column-wise operations.
  • Sum: np.sum(array).

Array Reshaping and Stacking

Reshape Arrays

  • Using array.reshape(new_shape) to change array dimensions.
  • Constraints: New shape must have the same total number of elements.

Stacking Arrays

  • Vertical Stack: np.vstack((array1, array2)).
  • Horizontal Stack: np.hstack((array1, array2)).

Miscellaneous Tips

Loading Data from Files

  • Use np.genfromtxt('file.txt', delimiter=',') to load data from a text file.
  • Convert data types using data.astype(np.int32).

Advanced Indexing

  • Boolean masking to filter data: array[array > 50].
  • Indexing with lists.
  • Combining conditions.

Conclusion

  • Importance of NumPy in scientific computing.
  • Practical applications and examples.