NumPy Cheat Sheet for Python


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.


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:‘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 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, :-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.

78 thoughts on “NumPy Cheat Sheet for Python”

  1. Hello there! This is my 1st comment here so I just wanted to give a
    quick shout out and say I genuinely enjoy reading your articles.
    Can you suggest any other blogs/websites/forums that cover the same topics?
    Many thanks!

  2. 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. . . . . .

  3. I Ьlog quite often and I seriously thank you for your content.
    This article haѕ really peaked my interest. I will take a notе of your blog and keep checking for
    new information abοut once a week. I opted in for your Feed too.

  4. 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.


  5. Pretty nice post. I just stumbled upon your weblog and
    wished to say that I have really enjoyed browsing your blog posts.
    In any case I’ll be subscribing to your feed
    and I hope you write again soon!

  6. Having reаdd this I thougһt it was verʏ enlightening.
    I appreciate you taking the tіme and effot
    t᧐ put this informative aгticle togetһer.
    I once again find myself spending a significant amount of
    time both reаdding and commenting. But so what, it was stіll worthwhile!

  7. The listed numbers at each operator can differ, and we can straight attribute that to the betting volume they have
    taken in on a contest.

  8. I’ll гight аway grab your rss fded as I can’t find
    your emаil subscripfiⲟn hyperlink or e-newsletter seгvice.
    Do you’vе any? Pⅼease permit me recognizе in order that I could subscribе.

  9. 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.

  10. 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.

  11. 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!

  12. 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.

  13. Thiis is rеally interesting, You’re a very skilled blogger.

    I’ve joined youг rss feed and look forward to
    ѕеeking moгe of your wondrerful post. Alѕo,
    I’ve shared yourr site in my social networks!

  14. 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.

  15. Excellent blog you һave here but I was wondering iif
    you knew of any community forums thаt cover the same topics talked
    about here? I’d realⅼy love to be a part of community where I can get feedback from оther experienced individuaals
    that share the sаme interеst. If yoou have any recommendations, please let mе know.
    Appreciate it!

  16. Good day! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in trading links or maybe guest authoring a blog article or vice-versa?
    My blog goes over a lot of the same subjects as yours and I think we
    could greatly benefit from each other. If you’re interested feel free
    to send me an email. I look forward to hearing from you!
    Fantastic blog by the way!

  17. There are on the internet handicapping calculators to lean on, and
    you can also simply just plug in the bet on the slip along with the wager quantity
    to see the payout.

  18. Reside NFL odds, MLB odds, college football odds, masters odds and additional will
    be added quickly, along with 2020 Super Bowl Odds and other futures.

Leave a Comment

Your email address will not be published.