NumPy — A beginner’s approach…

Jyotiprasad Patil
4 min readJan 9, 2021
towardsdatascience.com

Before we begin with the real flavor of what NumPy is, let us take a look at the basics of it from a layman’s perspective…

What is NumPy? -

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

The heart of NumPy lies in the ndarray object which encapsulates n-dimensional arrays of homogeneous data types with many operations being performed in the compiled code for performance.

Some differences between NumPy arrays and the standard Python Sequences are summed up in here.

  1. NumPy arrays have a fixed size at creation unlike Python lists and changing the size of a ndarray will create a new array and delete the original one.
  2. Elements in a NumPy array are all required to be of the same type and thus will be the same in memory size.
  3. NumPy arrays help in doing advanced mathematical calculations on array than what is facilitated by regular python arrays and also in less amount of code.
  4. Many advanced technologies of today are using NumPy in their functioning and hence NumPy is becoming necessary in the modern world of coding.

To Sum up the speed and efficiency of NumPy we can consider the following example,

Let us assume that we need to calculate the product of elements in two arrays and then store them in the third array. For this we will be using looping in our basic languages.

In Python, the code will be executed as follows,

c = []
for i in range(len(a)):
c.append(a[i]*b[i])

but this will result in lower speed as we all know that Python is interpreted and hence will take more time.

On the other hand we can use C language to get the same result but then we will have to let go off the advantages of Python programming. The C code for the similar operation will be as follows,

for (i = 0; i < rows; i++): {
c[i] = a[i]*b[i];
}

Now, if we consider NumPy to do the same job we can get the taste of both the sweets at the same time, we will get the simplicity of Python with the near speed of C. The code in NumPy will look like this.

c = a * b

Now let us take a look at why is NumPy efficient and Fast?

We can say that NumPy uses Vectorized code and this is the main reason for it being fast. Vectorization describes the absence of any explicit looping, indexing, etc. These things take place in the code but just in the background which is an optimized pre compiled C code. Some advantages of vectorised code include the compactness of the code and the easier readability. Fewer lines is also an advantage which means fewer lines to decode as well. This code resembles standard mathematical notations also.

Basics of NumPy -

NumPy’s basic object is the homogeneous multi dimensional array as mentioned above which is a table of elements all of the same type, indexed by a tuple of non-negative integers. We call dimensions as axes in NumPy.

NumPy’s array class is called as ndarray also known by the alias array.

Some of the basic array operations are as follows.

  1. ndarray.ndim gives the number of axes of the array.

2. ndarray.shape gives the dimensions of the array. For a matrix with n rows and m columns the shape will be (n,m).

3. ndarray.size gives the total number of elements in the arrays which is equal to the product of the elements of the shape.

4. ndarray.dtype returns an object describing the type of elements in an array which can be as follows numpy.int.32 and numpy.int.16.

5. ndarray.itemsize returns the sizze in bytes of each element in the array.

Examples on the above methods.

>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
>>> a.itemsize
8

Array Creation -

Using NumPy we get several ways to create an array. We can create an array using regular Python List or tuple with the help of the array function.

>>> import numpy as np
>>> a = np.array([2,3,4])
>>> a
array([2, 3, 4])

Printing Arrays -

When we print an array NumPy displays it in a similar way as of nested lists in which one dimensionals are printed as rows bidimensionals as matrices and tridimensionals as list of marices.

>>> b = np.arange(12).reshape(4,3)           # 2d array
>>> print(b)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]

where arange is a function that is used to generate random values.

I hope that I have given a basic understanding of what NumPy is and how it can be beneficial.

Please go through the official documentation of NumPy for further details :)

References :

--

--