Data Science Exam  >  Data Science Notes  >  Python  >  CheatSheet:NumPy

CheatSheet:NumPy

1. NumPy Fundamentals

1.1 Core Concepts

Term Definition
NumPy Numerical Python library providing support for large multi-dimensional arrays and matrices with mathematical functions
ndarray N-dimensional array object, the core data structure in NumPy for homogeneous data
dtype Data type object describing the data type of array elements (int32, float64, bool, etc.)
Vectorization Performing operations on entire arrays without explicit loops, enabling faster computation
Broadcasting Mechanism allowing NumPy to perform operations on arrays of different shapes

1.2 Import Convention

  • Standard import: import numpy as np
  • All NumPy functions accessed via np. prefix

2. Array Creation

2.1 Basic Creation Methods

Function Description
np.array(object) Create array from list, tuple, or nested sequences
np.zeros(shape) Create array filled with zeros
np.ones(shape) Create array filled with ones
np.empty(shape) Create uninitialized array (contains arbitrary values)
np.full(shape, value) Create array filled with specified value
np.eye(n) or np.identity(n) Create n×n identity matrix

2.2 Range and Sequence Creation

Function Description
np.arange(start, stop, step) Create array with evenly spaced values within interval (excludes stop)
np.linspace(start, stop, num) Create array with num evenly spaced values (includes stop)
np.logspace(start, stop, num) Create array with logarithmically spaced values

2.3 Random Array Creation

Function Description
np.random.rand(d0, d1, ...) Random values from uniform distribution [0, 1)
np.random.randn(d0, d1, ...) Random values from standard normal distribution
np.random.randint(low, high, size) Random integers from low (inclusive) to high (exclusive)
np.random.random(size) Random floats in [0.0, 1.0)
np.random.choice(a, size) Random sample from given array
np.random.seed(value) Set random seed for reproducibility

3. Array Attributes

Attribute Description
arr.shape Tuple of array dimensions (rows, columns, ...)
arr.ndim Number of array dimensions
arr.size Total number of elements in array
arr.dtype Data type of array elements
arr.itemsize Size in bytes of each element
arr.nbytes Total bytes consumed by array (size × itemsize)
arr.T Transposed array

4. Data Types

4.1 Common dtypes

Type Description
int8, int16, int32, int64 Signed integers (8, 16, 32, 64 bits)
uint8, uint16, uint32, uint64 Unsigned integers
float16, float32, float64 Floating point numbers (half, single, double precision)
complex64, complex128 Complex numbers
bool Boolean (True/False)
object Python object type
string_, unicode_ Fixed-length string types

4.2 Type Conversion

  • arr.astype(dtype): Convert array to specified type
  • Example: arr.astype(np.float64), arr.astype('int32')
  • Type specified in array creation: np.array([1,2,3], dtype=np.float64)

5. Array Indexing and Slicing

5.1 Basic Indexing

  • 1D array: arr[index] - zero-based indexing
  • 2D array: arr[row, col] or arr[row][col]
  • Negative indexing: arr[-1] accesses last element

5.2 Slicing Syntax

  • arr[start:stop:step] - excludes stop index
  • arr[:] - entire array
  • arr[start:] - from start to end
  • arr[:stop] - from beginning to stop
  • arr[::step] - every step-th element
  • 2D slicing: arr[row_start:row_stop, col_start:col_stop]
  • Slices create views, not copies (modifications affect original)

5.3 Boolean Indexing

  • arr[condition] - select elements meeting condition
  • Example: arr[arr > 5] returns elements greater than 5
  • Multiple conditions: arr[(arr > 5) & (arr <>
  • Boolean operators: & (and), | (or), ~ (not)

5.4 Fancy Indexing

  • arr[[1, 3, 5]] - select elements at indices 1, 3, 5
  • arr[[0, 1], [2, 3]] - select elements (0,2) and (1,3)
  • Fancy indexing creates copies, not views

6. Array Reshaping and Manipulation

6.1 Shape Modification

Function Description
arr.reshape(shape) Return array with new shape (must have same total elements)
arr.resize(shape) Modify array shape in-place
arr.flatten() Return flattened 1D copy of array
arr.ravel() Return flattened 1D view (when possible)
arr.T or arr.transpose() Transpose array (swap rows and columns)
np.expand_dims(arr, axis) Add new axis at specified position
np.squeeze(arr) Remove single-dimensional entries

6.2 Array Concatenation

Function Description
np.concatenate((a1, a2), axis) Join arrays along existing axis
np.vstack((a1, a2)) Stack arrays vertically (row-wise)
np.hstack((a1, a2)) Stack arrays horizontally (column-wise)
np.dstack((a1, a2)) Stack arrays depth-wise (along third axis)
np.column_stack((a1, a2)) Stack 1D arrays as columns into 2D array
np.row_stack((a1, a2)) Stack 1D arrays as rows (equivalent to vstack)

6.3 Array Splitting

Function Description
np.split(arr, indices, axis) Split array into multiple sub-arrays
np.vsplit(arr, indices) Split array vertically
np.hsplit(arr, indices) Split array horizontally
np.dsplit(arr, indices) Split array depth-wise

7. Broadcasting Rules

7.1 Broadcasting Principles

  • Arrays with different shapes can be used in arithmetic operations
  • Smaller array is "broadcast" across larger array to match shapes
  • Rule 1: If arrays differ in dimensions, prepend 1s to smaller array's shape
  • Rule 2: Arrays compatible if dimensions are equal or one is 1
  • Rule 3: Arrays broadcast together along dimensions where one has size 1

7.2 Broadcasting Examples

  • (3, 4) and (4,) → (4,) becomes (1, 4) → broadcasts to (3, 4)
  • (3, 1) and (1, 4) → broadcasts to (3, 4)
  • (3, 4) and (3, 1) → broadcasts to (3, 4)
  • Incompatible: (3, 4) and (3,) - cannot broadcast

8. Mathematical Operations

8.1 Element-wise Arithmetic

Operation Description
arr1 + arr2 or np.add() Element-wise addition
arr1 - arr2 or np.subtract() Element-wise subtraction
arr1 * arr2 or np.multiply() Element-wise multiplication
arr1 / arr2 or np.divide() Element-wise division
arr1 // arr2 or np.floor_divide() Floor division
arr1 % arr2 or np.mod() Modulus
arr1 ** arr2 or np.power() Exponentiation

8.2 Comparison Operations

  • ==, !=, <,>, <=,>= - return boolean arrays
  • np.equal(), np.not_equal(), np.less(), np.greater(), np.less_equal(), np.greater_equal()
  • np.array_equal(a1, a2) - check if arrays have same shape and elements

8.3 Mathematical Functions

Function Description
np.sqrt(arr) Square root
np.exp(arr) Exponential (e^x)
np.log(arr), np.log10(arr), np.log2(arr) Natural, base-10, base-2 logarithm
np.abs(arr) or np.absolute(arr) Absolute value
np.sign(arr) Sign of elements (-1, 0, 1)
np.ceil(arr), np.floor(arr) Ceiling and floor
np.round(arr, decimals) Round to specified decimals
np.clip(arr, min, max) Limit values to range [min, max]

8.4 Trigonometric Functions

  • np.sin(), np.cos(), np.tan() - trigonometric functions (radians)
  • np.arcsin(), np.arccos(), np.arctan() - inverse trigonometric
  • np.sinh(), np.cosh(), np.tanh() - hyperbolic functions
  • np.deg2rad(), np.rad2deg() - convert between degrees and radians

8.5 Linear Algebra

Function Description
np.dot(a, b) or a.dot(b) Dot product (1D) or matrix multiplication (2D)
a @ b Matrix multiplication operator
np.matmul(a, b) Matrix multiplication
np.linalg.inv(arr) Matrix inverse
np.linalg.det(arr) Matrix determinant
np.linalg.eig(arr) Eigenvalues and eigenvectors
np.linalg.solve(A, b) Solve linear system Ax = b
np.trace(arr) Sum of diagonal elements
np.linalg.norm(arr) Matrix or vector norm

9. Statistical Functions

9.1 Aggregate Functions

Function Description
np.sum(arr, axis) Sum of elements along axis
np.mean(arr, axis) Arithmetic mean
np.median(arr, axis) Median value
np.std(arr, axis) Standard deviation
np.var(arr, axis) Variance
np.min(arr, axis) or arr.min() Minimum value
np.max(arr, axis) or arr.max() Maximum value
np.argmin(arr, axis) Index of minimum value
np.argmax(arr, axis) Index of maximum value
np.cumsum(arr, axis) Cumulative sum
np.cumprod(arr, axis) Cumulative product
np.percentile(arr, q, axis) q-th percentile
np.quantile(arr, q, axis) q-th quantile

9.2 Axis Parameter

  • axis=None (default): compute over entire array
  • axis=0: compute along rows (column-wise)
  • axis=1: compute along columns (row-wise)
  • Example: arr.sum(axis=0) sums each column

9.3 Correlation and Covariance

  • np.corrcoef(x, y) - Pearson correlation coefficient matrix
  • np.cov(x, y) - covariance matrix

10. Array Sorting and Searching

10.1 Sorting Functions

Function Description
np.sort(arr, axis) Return sorted copy of array
arr.sort() Sort array in-place
np.argsort(arr, axis) Return indices that would sort array
np.lexsort(keys) Sort using multiple keys
np.partition(arr, kth) Partial sort (kth element in sorted position)

10.2 Searching and Counting

Function Description
np.where(condition, x, y) Return elements from x or y depending on condition
np.argwhere(condition) Return indices where condition is True
np.searchsorted(arr, values) Find indices to insert values to maintain order
np.unique(arr, return_counts) Find unique elements (optionally with counts)
np.count_nonzero(arr) Count non-zero elements
np.nonzero(arr) Return indices of non-zero elements

11. Logical Operations

Function Description
np.logical_and(a, b) Element-wise AND
np.logical_or(a, b) Element-wise OR
np.logical_not(arr) Element-wise NOT
np.logical_xor(a, b) Element-wise XOR
np.all(arr, axis) Test if all elements are True
np.any(arr, axis) Test if any element is True
np.isnan(arr) Test for NaN values
np.isinf(arr) Test for infinity
np.isfinite(arr) Test for finite values

12. Set Operations

Function Description
np.intersect1d(a, b) Find common elements in two arrays
np.union1d(a, b) Union of elements from two arrays
np.setdiff1d(a, b) Elements in a that are not in b
np.setxor1d(a, b) Elements in either array but not both
np.in1d(a, b) Test whether elements of a are in b

13. File I/O

13.1 Binary Format (.npy, .npz)

Function Description
np.save('file.npy', arr) Save single array to binary file
np.load('file.npy') Load array from binary file
np.savez('file.npz', a=arr1, b=arr2) Save multiple arrays in uncompressed archive
np.savez_compressed('file.npz', a=arr1) Save multiple arrays in compressed archive

13.2 Text Format

Function Description
np.savetxt('file.txt', arr, delimiter=',') Save array to text file
np.loadtxt('file.txt', delimiter=',') Load array from text file
np.genfromtxt('file.csv', delimiter=',') Load data with missing values handling

14. Advanced Concepts

14.1 Copies vs Views

  • View: References same data (slicing creates views)
  • Copy: New array with separate data (arr.copy())
  • Modifying view affects original array
  • arr.base returns base array if view, None if copy

14.2 Memory Layout

Concept Description
C-contiguous (row-major) Elements stored row-by-row (NumPy default)
F-contiguous (column-major) Elements stored column-by-column (Fortran order)
arr.flags Information about memory layout
np.ascontiguousarray(arr) Return C-contiguous array
np.asfortranarray(arr) Return F-contiguous array

14.3 Structured Arrays

  • Arrays with named fields and heterogeneous data types
  • dtype = [('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]
  • Access fields: arr['name'], arr['age']
  • Create: np.array([(data)], dtype=dtype)

14.4 Universal Functions (ufuncs)

  • Functions that operate element-wise on arrays
  • Faster than Python loops due to vectorization
  • Support broadcasting
  • Methods: reduce(), accumulate(), outer(), reduceat()
  • Example: np.add.reduce(arr) equivalent to arr.sum()

14.5 Masking

  • np.ma.masked_array(data, mask) - create masked array
  • Mask invalid or missing data
  • Operations ignore masked values
  • arr.mask - access mask

15. Performance Tips

  • Use vectorized operations instead of loops
  • Preallocate arrays when size is known
  • Use in-place operations (+=, -=, *=, /=) to save memory
  • Specify dtype explicitly to avoid unnecessary conversions
  • Use views instead of copies when possible
  • Avoid creating temporary arrays in complex expressions
  • Use np.dot() or @ for matrix multiplication, not nested loops
  • Profile code to identify bottlenecks

16. Common Operations Summary

16.1 Array Manipulation Quick Reference

Task Function
Create from list np.array([1, 2, 3])
Create zeros np.zeros((3, 4))
Create range np.arange(0, 10, 2)
Reshape arr.reshape(3, 4)
Flatten arr.flatten() or arr.ravel()
Transpose arr.T
Join arrays np.concatenate((a, b), axis=0)
Sum all elements arr.sum()
Mean value arr.mean()
Max value arr.max()
Sort np.sort(arr)
Unique elements np.unique(arr)
The document CheatSheet:NumPy is a part of the Data Science Course Python for Data Science.
All you need of Data Science at this link: Data Science
Explore Courses for Data Science exam
Get EduRev Notes directly in your Google search
Related Searches
shortcuts and tricks, Important questions, Semester Notes, Extra Questions, Viva Questions, Objective type Questions, Sample Paper, video lectures, past year papers, CheatSheet:NumPy, CheatSheet:NumPy, pdf , Previous Year Questions with Solutions, study material, ppt, Summary, practice quizzes, Free, MCQs, Exam, CheatSheet:NumPy, mock tests for examination;