Data Science Crashers | Python | NumPy Basics

Sankalp Chourasia
3 min readJan 27, 2021

--

NumPy stands for Numerical Python. It is a library consisting of multidimensional array objects. It is also equipped with a plethora of ready to use inbuilt functions which are quite useful for streamlined and memory efficient processing of the arrays.

The generic python distribution doesn’t include the NumPy module by default. It is suggested that you use Anaconda Python Distribution to start using numpy. Otherwise you can always install numpy using the ever reliable pip.

!pip install numpy

NumPy uses a N-dimensional array object called ndarray. The ndarray is zero based just like the python array object.You can easily cast a python list/tuple into a ndarray using np.array function.

arr1=[1,2,3,4,5]
ndarr1=np.array(arr)
arr2=[[2.1,2],[19.99]]
ndarr2=np.array(arr2)
print(ndarr1)
print(ndarr2)
>>[1 2 5 4 5]
>>[[2.1,2],[19.99]]

Numpy’s inbuilt functions allows us to easily create some widely used array prototypes on the go.

ndarr3=np.zeros((2,2))#Elements initialized to 0 in a 2x2 ndarray
print(ndarr3)
ndarr4=np.ones((2,2))#Elements initialized to 1 in a 2x2 ndarray
print(ndarr4)
ndarr5=np.full((2,2),21)#Elements initialized to 21 in a 2x2 ndarray
print(ndarr5)
>>[[0. 0.]
[0. 0.]]
>>[[1. 1.]
[1. 1.]]
>>[[21 21]
[21 21]]

If we want to create a prototype array with the same size as a pre existing one we can append ‘_like’ to our function call and get the same.

ndarr6=np.arange(12).reshape((3,4))
print(ndarr6)
ndarr7=np.zeros_like(ndarr6)
print(ndarr7)
ndarr8=np.ones_like(ndarr6)
print(ndarr8)
ndarr10=np.full_like(ndarr6,21)
print(ndarr10)
>>[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
>>[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
>>[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
>>[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
>>[[21 21 21 21]
[21 21 21 21]
[21 21 21 21]]

Conversion from one data type to another can achieved easily using the astype() function.

ndarr=np.array([1,2,3,4,5])
ndarr=ndarr.astype(np.float32)
print(ndarr)

>>[1. 2. 3. 4. 5.]

Vectorized Operations

Numpy has inbuilt support for Vectorization. It is a process that extends an operation usually done on a single value at a time to one that operates on a set of values at once.For example: Using vectorization, all elements of two ndarray(s) can be added in a single calculation step instead of using a loop.

Some use cases of vectorization in numpy

Numpy ufuncs

Numpy Universal Functions (fondly called ufuncs) are mathematical operators that support numpy’s inherent fast element based array operations. Unary unfuncs take a single argument while binary ones take two.

Lets initialize two ndarrays and see a few example of unary and binary ufuncs.

arr1=np.arange(1,17).reshape(4,4)
arr2=-arr1
print(arr1)
print()
print(arr2)
>>[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]

>>[[ -1 -2 -3 -4]
[ -5 -6 -7 -8]
[ -9 -10 -11 -12]
[-13 -14 -15 -16]]
Numpy Unary Ufuncs
Numpy Unary Ufuncs
Numpy Binary Ufuncs

For more content on Data Analytics visit this link.

--

--

Sankalp Chourasia
0 Followers

Data Scientist @Innovaccer | Sardonic OddBall | Clandestine Beatnik | Ardent Cinephile