NumPy Cheat Sheet for Python

Fortnite

NumPy is a library for the Python programming language which is used for working with multi-dimensional arrays and matrices. This is very useful in large scientific computing. Because NumPy ndarrays is way faster compared to a regular python list. Arrays are very frequently used in data science too, where speed and resources are very important. That’s why NumPy is a very handy tool in data-science.

NumPy

But remembering all the NumPy commands might be overwhelming for both beginners and professionals. So Datalators makes the complex simple.
It’s also a good idea to check the official NumPy documentation from time to time. Even if you can find what you need in the cheat sheet. Reading documentation is a skill every data professional needs. Also, the documentation goes into a lot more detail than we can fit in a single sheet anyway!

Creating Arrays:

np.array([1.3, 2, 3], dtype = float)Creating a 1d array
np.array([1, 2, 3], [2, 3, 4], [3, 4, 5])Creating a 3d array
np.arange(2, 8, 2)Array of evenly spaced values
np.zeros((2, 3))2×3 array consists of zeros
np.ones((3, 4))3×4 array consists of ones
np.random.random((3, 3))Array consists of random values
np.empty((2, 3))Empty array
np.full((3,2), 5)Constant array

Import Export:

np.save(‘saved_array’, a)Save ‘a’ array as on disk
np.savez(‘array.npz’, a, b)Save 2 arrays
np.savetxt(‘array.txt’, a, delimiter= ” “)Saving as text file
np.genformatxt(‘array.csv’, a, delimiter= “,”)Saving as CSV file
np.load(‘array.npz’)Load from disk
np.loadtxt(‘array.txt’)Load from text file

Inspecting Array:

a.shapeDimensions of ‘a’ array
len(a)Length of array
a.ndimArray dimensions
a.sizeNumber of elements
a.dtypeData type of elements
a == bElement-wise comparison
a > 1Element-wise comparison
np.array_equal(a, b)Array-wise comparison

Data Types:

np.int64Signed integer types
np.float32Floating point
np.complexComplex number
np.boolBoolean type
np.objectPython object type
np.string_String type
np.unicodeUnicode type
a.astype(int)Convert to int type

Array Mathematics:

np.subtract(a, b)Subtraction
np.add(a, b)Addition
np.devide(a, b)Division
np.multiply(a, b)Multiplication
a+b, a-b, a*b, a/bOperation – arithmetic sign
np.sin(a), np.cos(a), np.log(a)Mathematical operation
a.dot(b)Dot product
np.exp(a), np.sqrt(a)Exponentiation and Square root

Statistics on NumPy:

a.sum()Array-wise sum
a.min(), a.max(axis = 0)Minimum and Maximum value
a.mean(), a.median()Mean and Median
a.corrcoef()Correlation coefficient
np.std(a)Standard deviation
b.cumsum(axis = 1)Cumulative sum of elements
a.sort(), a.sort(axis = 0)Sort an array

Indexing and Slicing

b = a.view() / a.copy()Create a copy of array
a[1, 2]Subsetting
a[0:2]Slicing
a[0:2, :-1]Slicing
a[a<2]Boolean Indexing

Array Manipulation:

b = np.transpose(a)Permute array dimensions
a.reshape(3,-2)Reshape but don’t change data
h.resize((2,4)) Return a new array with shape (2,4)
np.append(a,b)Append items to an array
np.insert(a, 1, 5)Insert items in an array
np.delete(a,[1])Delete items from an array
np.hsplit(a,3)Split the array horizontally at the 3rd index

I hope this cheat sheet will be useful to you. No matter you are new to python who is learning python for data science or a data professional. Happy Programming.

You can also download the printable PDF file from here.

Down Arrow on Twitter Twemoji 13.0.1

The source code for NumPy is located at this GitHub repository.
You might also be interested in Pandas Cheat Sheet For Data Science In Python.

15 thoughts on “NumPy Cheat Sheet for Python”

  1. Hello νery nice blog!! Guuy .. Excellent .. Woneerfuⅼ ..
    I ԝilⅼ bookmark your sitе annd take the freds additionallу?
    I am satisfied to search out so many helpful info right
    here inn the puut up, we’d like develop more strategies iin tthis гegard,
    thanks for sharing. . . . . .

  2. I don’t even know the way I stopped up right here, but I thought this submit was good.

    I do not recognise who you are however definitely you are going to a famous blogger should you are not already.

    Cheers!

  3. If somе one wishes expert viww on the topic of blogging and
    site-buildіng after tһat i recommend him/her tο visit
    this webpage, Keep up the fastidiⲟus work.

  4. I’m impressed, I havе to admit. Seⅼɗom ⅾo I encounter a bloց that’s
    both eduϲative and engaging, and let me tell you,
    you hаᴠe һit thhe naiⅼ on the head. The isѕue is something too few people are speaking intelligently about.

    I amm very happy that I stmbled across tyis iin my hunt for ѕomthing regɑrding this.

  5. Somebody essentіally aasѕist to make seveeely posts I’d state.That is the very first time I frequеnted your ԝeb
    page and so fɑr? I amazed with the reseaгch you made to mwke
    this actuaⅼ pᥙbblish amazing. Magnificent activіty!

  6. We are ɑ group of volunteerѕ andd opening a neew scheme in our
    community. Your website provided us with vaⅼuable infoгmation to work on. Youu have
    done a formidable job and our entirе community will be thankful tօ you.

  7. Hello there! I just wish to offеr yoս a huge thumbs up ffor the еxceⅼllent information youu have right here on thiѕ post.

    Ι am returning to your web site for more soon.

Leave a Comment

Your email address will not be published.