0% found this document useful (0 votes)
3 views3 pages

NumPy Notes

The document provides a quick reference guide for NumPy, covering essential topics such as array creation, indexing, mathematical operations, linear algebra, statistics, and data manipulation. It includes code snippets for initializing arrays, performing arithmetic, and reshaping data, along with key functions for each category. Additionally, it highlights boolean masking and file operations for data handling.

Uploaded by

Sarthak Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

NumPy Notes

The document provides a quick reference guide for NumPy, covering essential topics such as array creation, indexing, mathematical operations, linear algebra, statistics, and data manipulation. It includes code snippets for initializing arrays, performing arithmetic, and reshaping data, along with key functions for each category. Additionally, it highlights boolean masking and file operations for data handling.

Uploaded by

Sarthak Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NumPy Quick Reference Notes

Condensed study notes covering arrays, indexing, initialization, math, linear algebra, statistics, reshaping, and file/boolean
operations.

1. Getting Started
Import NumPy with the standard alias. Install it first if needed.
pip install numpy
import numpy as np

2. The Basics: Creating Arrays & Inspecting Them


Arrays can be 1-D, 2-D, or N-D. Use dtype to control storage type (e.g. int32 uses less memory than the default int64).
a = [Link]([1, 2, 3], dtype='int32')
b = [Link]([[9.0, 8.0, 7.0], [6.0, 5.0, 4.0]])
Key attributes for inspecting an array:
• [Link] — number of dimensions
• [Link] — size of each dimension, e.g. (2, 3)
• [Link] — data type of elements, e.g. int32
• [Link] — bytes per element
• [Link] — total bytes (itemsize × size)
• [Link] — total number of elements

3. Accessing & Changing Elements, Rows, Columns


Index 2-D arrays as [row, column]. Use slicing [start:end:step] for ranges.
a = [Link]([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])
a[1, 5] # single element -> 13
a[0, :] # entire row 0
a[:, 2] # entire column 2
a[0, 1:-1:2] # start:end:step slice
a[1, 5] = 20 # set a single element
a[:, 2] = [1, 2] # set an entire column
For 3-D+ arrays, index from the outside in: b[0, 1, 1] drills into block 0, row 1, element 1.
Note: When replacing a slice, the replacement shape must exactly match the slice shape, or NumPy raises a ValueError.

4. Initializing Different Types of Arrays


[Link]((2,3)) # all 0s
[Link]((4,2,2), dtype='int32') # all 1s
[Link]((2,2), 99) # all one value
np.full_like(a, 4) # same shape as a, filled with 4
[Link](4,2) # random floats [0,1)
[Link](-4,8, size=(3,3)) # random ints in range
[Link](5) # identity matrix
[Link](arr, 3, axis=0) # repeat array along an axis
Note: Copying arrays: b = [Link]() creates an independent array. Writing b = a instead just creates a second reference —
changing b would also change a.

5. Mathematics
Arithmetic on arrays is element-wise and broadcasts scalars automatically.
a = [Link]([1,2,3,4])
a + 2 # [3 4 5 6]
a - 2 # [-1 0 1 2]
a * 2 # [2 4 6 8]
a / 2 # [0.5 1. 1.5 2.]
a ** 2 # [1 4 9 16]
[Link](a) # trig functions element-wise
Two arrays of matching shape can also be added/subtracted element-wise, e.g. a + b.

6. Linear Algebra
Use matmul for matrix multiplication (not * , which is element-wise).
a = [Link]((2,3))
b = [Link]((3,2), 2)
[Link](a, b) # matrix product
[Link]([Link](3)) # determinant -> 1.0
[Link] also provides trace, singular value decomposition, eigenvalues, matrix norm, and inverse.

7. Statistics
stats = [Link]([[1,2,3],[4,5,6]])
[Link](stats) # 1 (overall minimum)
[Link](stats, axis=1) # [3 6] (row-wise max)
[Link](stats, axis=0) # [5 7 9] (column-wise sum)
axis=0 operates down columns; axis=1 operates across rows.

8. Reorganizing Arrays
before = [Link]([[1,2,3,4],[5,6,7,8]])
after = [Link]((4,2)) # total elements must match

[Link]([v1, v2, v1, v2]) # stack rows vertically


[Link]((h1, h2)) # stack columns horizontally
Note: reshape() only works if the new shape holds the same total number of elements as the original.

9. Miscellaneous
Loading Data from File
filedata = [Link]('[Link]', delimiter=',')
filedata = [Link]('int32')

Boolean Masking & Advanced Indexing


filedata > 50 # elementwise boolean array
(filedata > 50) & (filedata < 100) # combine conditions with & / |
~((filedata > 50) & (filedata < 100)) # negate with ~
A boolean array can be used directly to index another array, returning only the elements where the mask is True — this
is the basis of filtering data with NumPy.

10. Quick Reference Table


Category Key Functions
Creation [Link], [Link], [Link], [Link], [Link],
[Link]/randint
Inspecting ndim, shape, dtype, itemsize, nbytes, size
Indexing a[r,c], a[:, c], a[start:end:step], boolean masks
Category Key Functions
Math + - * / ** , [Link]/sin/tan, broadcasting
Linear Algebra [Link], [Link]/inv/eig
Statistics [Link], [Link], [Link] (with axis=)
Reshaping reshape, vstack, hstack, repeat
I/O [Link], astype

You might also like