In this article, we are going to learn basics about, what is Python NumPy Library and how to create arrays in NumPy.

Introduction to Python NumPy Library

NumPy stands for Numerical Python. It has many inbuilt Mathematical functions for fast calculations without writing loops. NumPy functions are usually 10 to 100 times efficient than native Python counterparts. For example, consider the below code.

import numpy as np
a=np.arange(100000)
b=list(range(100000))
%time for _ in range(1000):    c=a+5
%time for _ in range(1000):    d=[x+5 for x in b]

Output:

Wall time: 59.8 ms
Wall time: 5.91 s

We can see that NumPy arrays took 59.8 ms, whereas, native Python list took 5910 ms.
This happens because the Numpy array is densely packed in memory due to its homogeneous type, it also frees the memory faster. Whereas the native Python Lists are heterogeneous in nature, which means, you can store any data type elements in Python List.

1. Creation of NumPy arrays

NumPy arrays are of type ndarray we can create NumPy arrays in various ways.

1.1 np.arange

np.arange function creates NumPy arrays given (start,stop,step) as a parameter. All of the parameters are optional. It just works like range function in Python. Consider the below example for a quick understanding.

import numpy as np
a=np.arange(4)
print("NumPy array is",a)
print("Type of NumPy array is ",type(a))

Output:

NumPy array is [0 1 2 3]
Type of NumPy array is 'numpy.ndarray'

1.2 np.array

Given a native Python list as a parameter to np.array function, it creates ndarray out of it. It converts native Python list to ndarray.

import numpy as np
l=[1,2,3,4]
print('l is ',l)
print('type(l) is ',type(l))
a=np.array(l)
print('a is ',a)
print('type(a) is ',type(a))

Output:

l is  [1, 2, 3, 4]
type(l) is 'list'
a is  [1 2 3 4]
type(a) is 'numpy.ndarray'

1.3 np.random

We can generate NumPy arrays of random numbers using 2 functions i.e np.random.randn which creates NumPy arrays which follow the standard normal distribution. Another function is np.random.rand which creates random numbers between 0 and 1. Both of the functions take shape of ndarray as a parameter. The shape of ndarray means dimensions of ndarray.

import numpy as np
a=np.random.rand(2,3)
print('a is ',a)
b=np.random.randn(2,3)
print('b is ',b)

Output:

a is  [[0.84007998 0.68697637 0.18478619]
[0.62999256 0.36127338 0.77176328]]
b is  [[-2.0411092  -0.76434655  0.83364755]
[-0.81265267  0.40939792 -1.98997584]]

2. Basic functions in NumPy

2.1. ndim

ndim function returns the number of dimensions of ndarray.

import numpy as np
a=np.random.rand(2,3)
print('a is ',a)
print('Number of dimensions of ndarray a are',a.ndim)

Output:

a is  [[0.89955932 0.44864809 0.82538467]
[0.24063851 0.74021459 0.30311691]]
Number of dimensions of ndarray a are 2

2.2. shape

shape function returns the dimensions of ndarray.

import numpy as np
a=np.random.rand(2,3)
print('a is ',a)
print('Dimensions of ndarray a are',a.shape)

Output:

a is  [[0.87329951 0.96582738 0.07259562]
[0.94085258 0.39460208 0.97039398]]
Dimensions of ndarray a are (2, 3)

2.3. dtype

dtype function returns the data type of ndarray.

import numpy as np
a=np.random.rand(2,3)
print('a is ',a)
print('Data type of ndarray a is',a.dtype)

Output:

a is  [[0.53457247 0.65259818 0.38042279]
[0.11895136 0.10993727 0.68071357]]
Data type of ndarray a is float64

2.4. size

ndarray.size returns the total number of elements of the array. This is equal to the product of the elements of shape.

import numpy as np
a=np.random.rand(2,3)
print('a is ',a)
print('Number of elements in ndarray a is',a.size)

Output:

a is  [[0.63950041 0.82499852 0.85583265]
[0.33566962 0.87589146 0.80804273]]
Number of elements in ndarray a is 6

3. Reference:

Happy Learning 🙂