Code in Python
Machine Learning and Deep Learning Blogs

Learn more about NumPy, NdArrays and its attributes

Numpy is a python library to create homogeneous multidimensional arrays and provide tools for working with these arrays.

The elements of numpy array are of the same type. One can create an array in Python using lists or array() function. The advantage of using Numpy array is it scales better and takes less memory when compared to standard Python list or array.

Using Numpy, you can perform following operations:

  1. Mathematical and logical operations on arrays.
  2. Fourier transforms and routines for shape manipulation.
  3. Operations related to linear algebra. NumPy has inbuilt functions for linear algebra and random number generation.

Basic Definitions

  1. NumPy Arrays: A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers.
  2. Rank: The number of dimensions is the rank of the array.
  3. Shape: the shape of an array is a tuple of integers giving the size of the array along each dimension.

Question: Give Rank for the following NumPy arrays:

1)    [1, 2, 1]

2)    [[ 1., 2., 3.],
[ 3., 0., 2.]]

The first array has rank 1 because the array is one dimensional. Second numpy array has rank 2 because it is a two-dimensional array.

Installation – NumPy

There are many ways to install Numpy library. Here is the link to official documentation to do so.

The simplest way to install Numpy is to write this in your command line.
pip install numpy

Ndarray and Ndarray Attributes

NumPy’s array class is called ndarray. It is also known by the alias array. Note that numpy.array is not the same as the Standard Python Library class array.array, which only handles one-dimensional arrays and offers less functionality. Here is some most important attribute of the ndarray object:

  1. ndarray.ndim: the number of dimensions of the array. In Numpy, the number of dimensions is also the rank of the array.
  2. ndarray.shape: A tuple of integers representing the size of an array. For a matrix with n rows and m columns, the shape will be (n,m).
  3. ndarray.size: the total number of elements of the array. It is also equal to the product of the elements of shape.
  4. ndarray.dtype: It is an object that describes the type of the elements in the array.
  5. It is a buffer that contains the actual elements of the array.



import numpy as np
a = np.array([1, 2, 3])   # Create a rank 1 array
print(type(a))            # Prints "<class 'numpy.ndarray'>"
print(a.shape)            # Prints "(3,)"
print(a[0], a[1], a[2])   # Prints "1 2 3"
a[0] = 5                  # Change an element of the array
print(a)                  # Prints "[5, 2, 3]"
b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array
print(b.shape)                     # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0])   # Prints "1 2 4"\

Here are the links to tutorials to get started with basics of Python.

Python Lists and its in-built functions

Top 20 Programming Terms that Everyone should know

Conditional Statements in Python (if, elif, else)

Learn about Classes and Objects in Python

Akarsh Singh
Follow me

Akarsh Singh

Founder, Code in Python. Web and Android App Developer. He loves Python and wants to share his knowledge via this website. Follow him on Facebook, Twitter and Github.
Akarsh Singh
Follow me

Latest posts by Akarsh Singh (see all)


  1. This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.

  2. I simply want to tell you that I am just new to blogs and definitely enjoyed your blog. Most likely I’m going to bookmark your website . You really have beneficial articles and reviews. Thank you for sharing with us your web-site.

  3. This is my first tіme visit at herе and i am гeally һappy to reaɗ everthing аt
    al᧐ne placе.

  4. I simply want to tell you that I am newbie to blogging and definitely liked you’re web blog. Most likely I’m planning to bookmark your blog . You certainly have exceptional article content. Thanks a lot for sharing with us your web site.

  5. “Thanks for sharing superb informations. Your web-site is so cool. I’m impressed by the details that you’ve on this website. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the info I already searched all over the place and simply could not come across. What an ideal web-site.”

  6. Superb blog! Do you have any suggestions for aspiring
    writers? I’m hoping to start my own blog soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or go for a paid option? There are so many
    options out there that I’m totally confused .. Any recommendations?


  7. Hi, I believe your site could possibly be having browser compatibility issues.
    When I look at your blog in Safari, it looks fine
    however when opening in IE, it’s got some overlapping issues.
    I merely wanted to provide you with a quick heads
    up! Other than that, wonderful blog!

  8. I visit daily some web pages and information sites to read articles
    or reviews, however this web site gives quality based writing.

  9. I do agree with all of the ideas you’ve offered
    for your post. They’re very convincing and can definitely work.
    Nonetheless, the posts are too brief for newbies.
    May just you please extend them a bit from subsequent time?
    Thank you for the post.

  10. Wow, thiks paragraph іs fastidious, my sister іs analyzing these kinds of things, therefore Ӏ ɑm goіng
    to convey һer.

  11. I am really impressed with your writing skills as well as with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?
    Either way keep up the nice quality writing, it is rare to
    see a nice blog like this one nowadays.

  12. I like the valuable information you provide to your articles.
    I will bookmark your blog and take a look at once more here regularly.
    I am relatively certain I will learn plenty of new stuff right here!
    Best of luck for the next!

  13. Your style is unique compared to other people I’ve read stuff from.
    Many thanks for posting when you’ve got the opportunity, Guess I’ll
    just bookmark this page.

  14. Thank you a lot for giving everyone remarkably splendid opportunity to read critical reviews from this web site. It really is very excellent plus stuffed with fun for me personally and my office acquaintances to search the blog at the very least thrice in a week to learn the new items you have. And definitely, I am just certainly fascinated for the terrific things you give. Selected 2 ideas in this posting are undeniably the most efficient we have all ever had.

  15. Youre so cool! I dont suppose Ive learn something like this before. So good to find any person with some authentic ideas on this subject. realy thank you for starting this up. this web site is one thing that is needed on the net, somebody with slightly originality. helpful job for bringing something new to the internet!

  16. I simply want to tell you that I am just new to blogs and definitely enjoyed your blog. Most likely I’m going to bookmark your website . You really have beneficial articles and reviews. Thank you for sharing with us your web-site.

  17. I take pleasure in, result in I found exactly what I used to be looking
    for. You’ve ended my four day long hunt! God Bless
    you man. Have a nice day. Bye

  18. Thanks pertaining to furnishing these sort of amazing knowledge.

  19. It’s truly a nice and helpful piece of information. I’m glad that you simply shared this helpful info with us. Please stay us up to date like this. Thank you for sharing.

  20. Woah! I’m really digging the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance” between usability and visual appeal.
    I must say you have done a great job with this. In addition, the blog loads extremely fast
    for me on Firefox. Superb Blog!

  21. Thank you for the auspicious writeup. It in fact was
    a amusement account it. Look advanced to more added agreeable from you!

    However, how could we communicate?

  22. I got this web page from my pal who shared with
    me about this website and at the moment this time I am browsing this site and reading very informative posts at this place.

Leave a Reply

Your email address will not be published.


© 2019 Code in Python