Lavanya-M22 Numpy

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

NumPy Cheat Sheet

by Lavanya-M22 via cheatography.com/138231/cs/29097/

Why NumPy? Creating Arrays

NumPy is an open-s​ource numerical Python library used for working np.arr​ay(​object) Creates an array
with arrays. It aims to provide an array object that is upto 50x faster np.arr​ay(​[1,​2,3]) 1D array
than tradit​ional python list takes signif​icantly less amount of memory
np.array([(1,2,3),(4,5,6)]) 2D array
as compared to python lists.
np.zer​os(​shape) Return a new array of given shape
and type, filled with zeros
How to Install Numpy
np.one​s(s​hape) Return a new array of given shape
pip install numpy
and type, filled with ones
or
np.eye(no. of rows) Return a 2-D array with ones on the
conda install numpy
diagonal and zeros elsewhere

Importing Library np.ara​nge​(st​art​,st​op,​step) Return evenly spaced values within a


given interval.
import numpy as np
np.ran​dom.ra​nd(​shape) Return array of random floats
between 0–1 of fiven shape
Attributes of ndarray
np.random.randint(low,high) Return random integers from low
ndarray.shape Tuple of array shape
(inclu​sive) to high (exclu​sive)
ndarra​y.ndim Number of array dimensions as interger
np.linspace(start, stop, n) Returns n evenly spaced numbers
ndarra​y.size Number of elements in the array
over a specified interval
ndarra​y,dtype Data type of array’s elements
ndarra​y.base To check if object has its own memory commonly used methods

np.sor​t(arr) Returns a sorted copy of the array


Slicing
np.arg​sor​t(arr) Returns the indices that would sort an array
arr[0] Returns the element at index 0
np.res​ize(a, Return a new array with the specified shape
arr[1,2] Returns array element on index [1][2] new_shape)
arr[0:3] Returns the elements at indices on outer dimension np.dot​(arr1, arr2) Dot product of two arrays
arr[0:3,2] Returns the elements on rows 0,1,2 at column 2 arr.copy() Returns a copy of the array
arr<n Returns an array with boolean values arr.view() New view of array with the same data
~arr Returns an array with boolean values arr.fl​atten() Return a copy of the array collapsed into 1D
arr.re​sha​pe(​new​‐ Returns an array containing the same data with
Statistics
_shape) a new shape
np.mean(arr,axis=0/1) Compute the arithmetic mean along the
specified axis. Math operators
arr.sum() Sum of array elements over a given axis np.add(arr_1, arr_2) Add arguments elemen​t-wise
arr.min() Return the minimum along a given axis np.subtract(arr_1, arr_2) Subtract arguments, elemen​t-wise
arr.max() Return the maximum along a given axis np.multiply(arr_1, arr_2) Multiply arguments, elemen​t-wise
np.var​(arr) Compute the variance along the specified np.divide(arr_1, arr_2) Divide arguments, elemen​t-wise
axis
np.std​(arr) Compute the standard deviation along the
specified axis.
arr.co​rrc​oef() Return Pearson produc​t-m​oment correl​‐
ation coeffi​cients

By Lavanya-M22 Published 11th September, 2021. Sponsored by CrosswordCheats.com


cheatography.com/lavanya- Last updated 12th September, 2021. Learn to solve cryptic crosswords!
m22/ Page 1 of 2. http://crosswordcheats.com
NumPy Cheat Sheet
by Lavanya-M22 via cheatography.com/138231/cs/29097/

Math operators (cont)

np.power(arr_1, arr_2) First array elements raised to powers from


second array, elemen​t-wise
np.sqr​t(arr) Return the non-ne​gative square​-root of an
array, elemen​t-wise
np.log​(arr) Natural logarithm, elemen​t-wise
np.cei​l(arr) Rounds up to the nearest int , elemen​t-wise
np.flo​or(arr) Rounds down to the nearest int ,eleme​nt-
wise
np.abs​(arr) Absolute value of each element in the array
np.rou​nd(arr) Rounds to the nearest int

Useful links

NumPy Official docume​nataion


w3schools NumPy Tutorial
NumPy Illust​rated: The Visual Guide to NumPy
NumPy: creating and manipu​lating numerical data

By Lavanya-M22 Published 11th September, 2021. Sponsored by CrosswordCheats.com


cheatography.com/lavanya- Last updated 12th September, 2021. Learn to solve cryptic crosswords!
m22/ Page 2 of 2. http://crosswordcheats.com

You might also like