NumPy: Everything A Data Scientist Should Know
By: Jitendra Singh Balla
Tags: Artificial Intelligence, Machine Learning, Numpy, Python, Data Science

Photo by Markus Spiske on Unsplash

Numpy is one of the most used library of Python in Data Science field. In this article I’ll explain all the functions of Numpy that a Data Scientist should know. This article will outline the core features of the NumPy library.

Contents

What is Numpy?

Why we use Numpy in Python?

Advantages of Numpy library

Core features or functions of Numpy

Numpy Special Functions

What is Numpy?

NumPy is a python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely. NumPy stands for Numerical Python.

Why we use Numpy in Python?

NumPy is an open-source numerical Python library. NumPy contains a multi-dimensional array and matrix data structures. It can be utilised to perform a number of mathematical operations on arrays such as trigonometric, statistical, and algebraic routines. Pandas objects rely heavily on NumPy objects.

Advantages of Numpy library:

The core of Numpy is its arrays. One of the main advantages of using Numpy arrays is that they take less memory space and provide better runtime speed when compared with similar data structures in python(lists and tuples). Numpy support some specific scientific functions such as linear algebra. They help us in solving linear equations. Numpy support vectorized operations, like elementwise addition and multiplication, computing Kronecker product, etc. Python lists fail to support these features. It is a very good substitute for MATLAB, OCTAVE, etc as it provides similar functionalities and supports with faster development and less mental overhead(as python is easy to write and comprehend) NumPy is very good for data analysis.

Core features or functions of Numpy

First of all , import Numpy library :

import numpy as np

Create an array of one dimension or two dimension as you want:

a = np.array([(1,2,3),(4,5,6)])

print(a)

#output will be an array of two dimensional.

Get dimensions of an array:

a = np.array([(1,2,3),(4,5,6)])

print(a.ndim)

#output : 2

Find data type of array:

a = np.array([(1,2,3),(4,5,6)])

print(a.dtype)

#ouput: int32

Find size of an array:

a = np.array([(1,2,3),(4,5,6)])

print(a.size)

#ouput : 6

Find number of rows and columns:

a = np.array([(1,2,3),(4,5,6)])

print(a.shape)

#output: (2,3) which means array has 2 rows and 3 columns

Use arange function as range and find byte size of each element:

#create an array using arange function

M = np.arange(1000)

print(M.size*M.itemsize)

#output will be like:

4000

which shows that memory occupied by numpy array

Reshape an array:

a = np.array([(1,2,3),(4,5,6)])

a = a.reshape(3,2)

print(a)

#In output we'll have an array of 3 rows and 2 columns because reshape function will change shape of array

Slicing for getting a specific element from an array:

a = np.array([(1,2,3)],(4,5,6)])

print(a[0,2])

#output: 3

print(a[1,1])

#output: 5 print(a[0:,2]) #getting 2nd element from all the rows

#output: [3 5]

Use linespace function:

#print three values between 1 to 5

b = np.linespace(1,3,5)

print(b)

#output: [1. 1.5 2. 2.5 3.]

Some Math function:

a = np.array([(1,2,3),(4,5,6)])

print(a.min())

#output: 1

print(1.max())

#output: 6

print(a.sum())

#output: 21

Sum for row and column , for that we use axis where axis = 0 means column and axis = 1 means row:

a = np.array([(1,2,3),(4,5,6)])

print(a.sum(axis=0))

#output: [4 6 8]

print(a.sum(axis=1))

#output: [6 12]

Finding Square root and Standing deviation:

#for Square root

a = np.array([(1,2,3)])

print(np.sqrt(a))

#output: [1. 1.434 1.732]

#for Standard deviation

print(np.std(a))

#output: 1.2909944

Basic Math function:

#Addition of two arrays

a = np.array([(1,2,3),(4,5,6)])

b = np.array([(1,2,3),(4,5,6)])

print(a+b)

#output: [[2 4 6]

[6 8 10]]

#Substraction of two arrays

print(a-b)

#output: [[0 0 0]

[0 0 0]]

#Multiplication of two arrays

print(a*b)

##output: [[1 4 9]

[9 16 25]] #Division of two arrays

print(a/b)

##output: [[1 1 1]

[1 1 1]]

Stacking arrays:

#vertical stacking

a = np.array([(1,2,3),(4,5,6)])

b = np.array([(1,2,3),(4,5,6)])

print(np.vstack((a,b)))

#ouput: [[1 2 3]

[3 4 5]

[1 2 3]

[3 4 5]] #horizontal Stacking

print(np.hstack((a,b)))

#output:[[1 2 3 1 2 3]

[3 4 5 3 4 5]] #column Stacking

print(np.column_stack((a,b)))

Convert whole array to a column:

a = np.array([(1,2,3),(4,5,6)])

print(a.ravel())

#output: [1 2 3 4 5 6]

Initializing Numpy array with zeros:

import numpy as np

n1 = np.zeros((2,2)) print(n1)

#output: [[0. 0.]

[0. 0.]]

Initializing Numpy array with same number:

import numpy as np

n1 = np.full((2,2),10) print(n1)

#output: [[10 10]

[10 10]]

Initializing Numpy array with random numbers:

import numpy as np

n1 = np.random.randint((1,100,5) print(n1)

#output: [10 23 56 86 51]

Numpy Special Functions

First one is any Trigonometric Function:

#for example Sine plot

import numpy as np

import matplotlib.pyplot as plt

a = np.arange(0,3*np.pi,0.1)

b = np.sin(a)

plt.plot(a,b)

plt.show()

#output will be a graph of Sine as below picture shows

Second special function is Exponential Function:

a = np.array([(1,2,3)])

print(np.exp(a))

#output: [ 2.718281 7.3890561 20.08553692]

Third special function is Logarithmic Function: