Fundamentals of Numerical Computing with NumPy

Core Data Analysis Libraries

NumPy, Matplotlib, and pandas form the foundation of Python data analysis.

Understanding NumPy

NumPy (Numerical Python) is a library for efficient numerical computations. It provides:

  • Multidimensional array objects (ndarray)
  • Mathematical operations optimized for arrays
  • Tools for integrating with other languages

Array Creation Methods

Basic Array Properties

Key array attributes include:

  • ndim: Number of dimensions
  • shape: Tuple representing array dimensions
  • size: Total number of elements
  • dtype: Data type of elmeents
  • itemsize: Size of each element in bytes

Array Creation Functions

  1. np.array() - Create from existing sequences
matrix = np.array([[1,2],[3,4]])
  1. np.arange() - Create sequences with fixed steps
seq = np.arange(0, 10, 0.5)
  1. np.linspace() - Create evenly spaced numbers
points = np.linspace(0, 100, 5)
  1. Special Matrices
zeros = np.zeros((3,3))
identity = np.eye(4)
diagonal = np.diag([1,2,3,4])

Data Type Handling

NumPy supports various numeric types:

  • Integer types (int8, int16, int32, int64)
  • Unsigned integers (uint8, uint16, uint32, uint64)
  • Floating-point (float16, float32, float64)
  • Complex numbers (complex64, complex128)

Type conversion examples:

np.float32(42)  # Convert to 32-bit float
np.int8(3.14)   # Convert to 8-bit integer (truncates)

Random Number Geenration

Basic Random Numbers

np.random.random()  # Single float in [0,1)

Distributed Random Numbers

uniform = np.random.rand(2,3)  # Uniform distribution
normal = np.random.randn(3,3)  # Normal distribution

Custom Ranges

integers = np.random.randint(5, 15, size=(4,4))

Array Indexing

1D Array Indexing

arr = np.arange(10)
print(arr[3:7])    # Slice
print(arr[::2])    # Step
print(arr[::-1])   # Reverse

Multidimensional Indexing

matrix = np.random.rand(4,5)
print(matrix[1:3, 2:4])  # Submatrix
print(matrix[:, 3])      # Entire column

Boolean Indexing

filter = np.array([True, False, True, False])
print(matrix[filter, 2])  # Conditional selection

Tags: python Numpy Data Analysis numerical computing Arrays

Posted on Sun, 02 Aug 2026 16:30:51 +0000 by Riseykins