0% found this document useful (0 votes)
53 views8 pages

Numpy CheatSheet

This document is a cheat sheet for Numpy, covering essential functions for array creation, manipulation, indexing, filtering, statistics, linear algebra, data handling, and saving/loading arrays. It provides examples for each category, illustrating how to perform various operations with Numpy arrays. The cheat sheet serves as a quick reference guide for users to efficiently utilize Numpy in their coding tasks.

Uploaded by

r97920087
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)
53 views8 pages

Numpy CheatSheet

This document is a cheat sheet for Numpy, covering essential functions for array creation, manipulation, indexing, filtering, statistics, linear algebra, data handling, and saving/loading arrays. It provides examples for each category, illustrating how to perform various operations with Numpy arrays. The cheat sheet serves as a quick reference guide for users to efficiently utilize Numpy in their coding tasks.

Uploaded by

r97920087
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

coding_knowladge

Harry

Numpy
CheatSheet
coding_knowladge
Harry

Array Creation
[Link]([1, 2, 3]): Create a 1D array from a list.

[Link]((3, 3)): Create a 3x3 array filled with


zeros.

[Link]((2, 2)): Create a 2x2 array filled with


ones.

[Link](0, 10, 2): Generate values from 0 to


10 with a step of 2.

[Link](3, 3): Generate a 3x3 array of


random values between 0 & 1.

[Link](4): Create a 4x4 identity matrix.

[Link]((2, 2), 7): Create a 2x2 array filled with


the value 7.

[Link](0, 10, 5): Create 5 equally spaced


values between 0 and 10.
coding_knowladge
Harry

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

comment “num” and get it’s


complete pdf in your DM 📌
coding_knowladge
Harry

Indexing & Filtering

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.

[Link](arr > 5, 1, 0): Replace elements


based on a condition.

[Link](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.
coding_knowladge
Harry

Statistics
[Link](arr): Compute the mean of the array.

[Link](arr): Compute the median of the


array.

[Link](arr): Compute the standard deviation of


the array.

[Link](arr): Calculate the total sum of all


elements.

[Link](arr), [Link](arr): Find the minimum and


maximum values.

[Link](arr, 50): Compute the 50th


percentile.

[Link](arr): Compute the variance of the array.

[Link](arr1, arr2): Calculate the correlation


coefficient between two arrays.

comment “num” and get it’s


complete pdf in your DM 📌
coding_knowladge
Harry

Linear Algebra
[Link](arr1, arr2): Perform the dot product
of two arrays.

arr1 @ arr2: Perform matrix multiplication.

arr.T: Compute the transpose of the array.

[Link](arr): Compute the inverse of


a square matrix.

[Link](arr): Compute the


determinant of a matrix.

[Link](arr): Find the eigenvalues


and eigenvectors.

[Link](arr): Compute the Singular


Value Decomposition of a matrix.

[Link](arr): Compute the


Frobenius norm of a matrix.
coding_knowladge
Harry

Data Handling
[Link](arr): Return the unique elements
of the array.

[Link](arr): Sort the array elements in


ascending order.

[Link](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.

[Link](arr): Identify which elements are


NaN.

[Link](arr, 2): Delete the element at index


2.

[Link](arr, 1, 10): Insert the value 10 at


index 1.
coding_knowladge
Harry

Saving & Loading


[Link]('[Link]', arr): Save an array to a
binary file.

[Link]('[Link]'): Load an array from a


binary file.

[Link]('[Link]', arr): Save an array to


a text file.

[Link]('[Link]'): Load an array from a


text file.

[Link]('[Link]', arr1=arr1, arr2=arr2):


Save multiple arrays in one file.

[Link]('[Link]'): Load multiple arrays


from a .npz file.

np.savez_compressed('[Link]', arr=arr):
Save arrays in compressed .npz format.

[Link]('[Link]', delimiter=','): Load an


array from a CSV file.

comment “num” and get it’s


complete pdf in your DM 📌

You might also like