⬡ Hub
Skip to content

NumPy: Numerical Computing with Python

NumPy (Numerical Python) is the fundamental package for numerical computation in Python. It provides an efficient multi-dimensional array object, ndarray, and a collection of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation, and much more.

Key Features:

  • ndarray: A fast and memory-efficient N-dimensional array object.
  • Broadcasting: Sophisticated (broadcasting) functions.
  • Integration: Tools for integrating C/C++ and Fortran code.
  • Linear Algebra: Useful linear algebra, Fourier transform, and random number capabilities.
  • Mathematical Functions: A wide range of universal functions for operating on arrays.

Getting Started: Installation

You can install NumPy using pip or conda.

Using pip:

pip install numpy

Using conda:

conda install numpy

Basic Concepts: ndarray

The core of NumPy is its ndarray object. Arrays are grid-like data structures that can store elements of the same type.

Creating Arrays

import numpy as np

# Creating an array from a Python list
a = np.array([1, 2, 3, 4])
print(a)
print(a.shape)
print(a.dtype)

# Creating a 2D array (matrix)
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
print(b.shape)

# Creating arrays with initial placeholders
# Zeros array
zeros = np.zeros((3, 4))
print(zeros)

# Ones array
ones = np.ones((2, 2), dtype=np.int16)
print(ones)

# Empty array (content is random and depends on memory state)
empty = np.empty((2, 3))
print(empty)

# Array with a range of numbers
arange = np.arange(10)
print(arange)

# Array with linearly spaced numbers
linspace = np.linspace(0, 10, 5) # 5 numbers from 0 to 10 (inclusive)
print(linspace)

Array Operations

NumPy arrays support vectorized operations, meaning operations are applied element-wise without explicit loops, leading to significant performance improvements.

import numpy as np

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

# Element-wise addition
c = a + b
print(c)

# Element-wise multiplication
d = a * b
print(d)

# Dot product
dot_product = a.dot(b)
print(dot_product)

# Scalar operations
e = a * 2
print(e)

# Universal Functions (ufuncs)
f = np.sqrt(a)
print(f)

Indexing and Slicing

NumPy arrays can be indexed and sliced in ways similar to Python lists, but with extensions for multiple dimensions.

import numpy as np

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

# Accessing a single element
print(arr[0, 0]) # Output: 1

# Slicing rows
print(arr[0:2, :]) # First two rows, all columns

# Slicing columns
print(arr[:, 1]) # All rows, second column

# Boolean indexing
print(arr[arr > 5]) # Elements greater than 5

Further Topics:

  • Array Reshaping and Manipulation
  • Broadcasting Rules
  • Linear Algebra Operations
  • Random Number Generation
  • Statistical Operations
  • File I/O with NumPy

This document provides a basic introduction to NumPy. More detailed topics, advanced techniques, and practical examples will be covered in subsequent files.