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.

Leave a Reply

Your email address will not be published.


© 2018 Code in Python