0% found this document useful (0 votes)
35 views9 pages

Numpy Cheat Sheet

The document provides a comprehensive list of NumPy functions for array creation, manipulation, statistical analysis, linear algebra, and file I/O operations. It includes examples for creating arrays, reshaping, accessing elements, performing calculations, and saving/loading data. The content is structured as a reference guide for users working with NumPy in Python.

Uploaded by

prabhugaurav54
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)
35 views9 pages

Numpy Cheat Sheet

The document provides a comprehensive list of NumPy functions for array creation, manipulation, statistical analysis, linear algebra, and file I/O operations. It includes examples for creating arrays, reshaping, accessing elements, performing calculations, and saving/loading data. The content is structured as a reference guide for users working with NumPy in Python.

Uploaded by

prabhugaurav54
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
You are on page 1/ 9

Tajamul Khan

@Tajamulkhann
np.array([1, 2, 3]): Create a 1D
array from a list.
np.zeros((3, 3)): Create a 3x3
array filled with zeros.
np.ones((2, 2)): Create a 2x2
array filled with ones.
np.arange(0, 10, 2): Generate
values from 0 to 10 with a step of
2.
np.random.rand(3, 3): Generate a
3x3 array of random values between
0 & 1.
np.eye(4): Create a 4x4 identity
matrix.
np.full((2, 2), 7): Create a 2x2
array filled with the value 7.
np.linspace(0, 10, 5): Create 5
equally spaced values between 0
and 10.

@Tajamulkhann
arr.reshape((rows, cols)): Reshape
an array into the specified
dimensions.
arr.flatten(): Convert a multi-
dimensional array into a 1D array.
np.concatenate([arr1, arr2],
axis=0): Concatenate arrays along
a specific axis.
arr.T: Transpose the array (swap
rows and columns).
np.vstack([arr1, arr2]): Stack
arrays vertically.
np.hstack([arr1, arr2]): Stack
arrays horizontally.
np.expand_dims(arr, axis=0): Add a
new dimension to the array.
arr.swapaxes(0, 1): Swap the first
and second axes of an array.

@Tajamulkhann
arr[1, 2]: Access an element at
row 1 and column 2.
arr[1, :]: Select all columns from
row 1.
arr[arr > 5]: Filter elements
greater than 5.
np.where(arr > 5, 1, 0): Replace
elements based on a condition.
np.nonzero(arr): Get the indices
of non-zero elements.
arr[0:2, :] = 10: Set the first
two rows to 10.
arr[:, 1:3]: Slice columns from
index 1 to 3.
arr[arr % 2 == 0]: Select even
elements in the array.

@Tajamulkhann
np.mean(arr): Compute the mean of
the array.
np.median(arr): Compute the median
of the array.
np.std(arr): Compute the standard
deviation of the array.
np.sum(arr): Calculate the total
sum of all elements.
np.min(arr), np.max(arr): Find the
minimum and maximum values.
np.percentile(arr, 50): Compute
the 50th percentile.
np.var(arr): Compute the variance
of the array.
np.corrcoef(arr1, arr2): Calculate
the correlation coefficient
between two arrays.

@Tajamulkhann
np.dot(arr1, arr2): Perform the
dot product of two arrays.
arr1 @ arr2: Perform matrix
multiplication.
arr.T: Compute the transpose of
the array.
np.linalg.inv(arr): Compute the
inverse of a square matrix.
np.linalg.det(arr): Compute the
determinant of a matrix.
np.linalg.eig(arr): Find the
eigenvalues and eigenvectors.
np.linalg.svd(arr): Compute the
Singular Value Decomposition of a
matrix.
np.linalg.norm(arr): Compute the
Frobenius norm of a matrix.

@Tajamulkhann
np.unique(arr): Return the unique
elements of the array.
np.sort(arr): Sort the array
elements in ascending order.
np.argsort(arr): Return the
indices that would sort the array.
np.count_nonzero(arr): Count the
number of non-zero elements.
np.nan_to_num(arr): Replace NaN
values with zero or specified
value.
np.isnan(arr): Identify which
elements are NaN.
np.delete(arr, 2): Delete the
element at index 2.
np.insert(arr, 1, 10): Insert the
value 10 at index 1.

@Tajamulkhann
np.save('file.npy', arr): Save an
array to a binary file.
np.load('file.npy'): Load an array
from a binary file.
np.savetxt('file.txt', arr): Save
an array to a text file.
np.loadtxt('file.txt'): Load an
array from a text file.
np.savez('file.npz', arr1=arr1,
arr2=arr2): Save multiple arrays
in one file.
np.load('file.npz'): Load multiple
arrays from a .npz file.
np.savez_compressed('file.npz',
arr=arr): Save arrays in
compressed .npz format.
np.loadtxt('file.txt',
delimiter=','): Load an array from
a CSV file.

@Tajamulkhann
Follow for more!

You might also like