What is NumPy?

NumPy is a package to perform computing in python. It has support for scientific computing and processing. It is an acronym for Numerical Python.

It has random number generation capabilities, functions for basic linear algebra, Fourier transforms as well as tools for integrating Fortran and C/C++.

NumPy is an open-source project. It is the child of two Python libraries namely Numeric and Numarray.

It can be used as a multi-dimensional container of generic data. It allows NumPy to integrate a wide variety of databases. It also has support for collection of routines for processing single and multidimensional arrays.

NumPy is not a standard Python Library. So, we have to install it ourselves for usage.

pip install numpy

Once installed we can use it simply by importing using the import statement.

import numpy

OR

import numpy as np

NumPy Arrays

List in Python is powerful sequential data structure with various features. It can hold elements of various data types that can be added, changed or removed based on requirements. It allows indexing for traversal.

But the list lacks an important feature that is needed for data analysis operations. We often need to perform tasks on the entire collection of elements and we want to process data fast. We can not perform such operations with lists efficiently.

NumPy Arrays are similar to lists with one useful feature: perform operations on the entire array. It is easy as well as fast.

# importing NumPy

import numpy as np

# creating arrays

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

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

Here we have created two arrays. Now, we can perform operations within single line of code

# multiplying each elements of array_name with corresponding element of array_2

array_op = array_name * array_2

$o/p : array([2,6,12,20,30])

N-dimensional arrays

We can create n-dimensional arrays with numpy. type() returns numpy.ndarray signifying that NumPy arrays are n-dimensional arrays.

#creating two dimensional array

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

$o/p: 

array([[1,2,3],

[4,5,6]])

Array creation using Built-in functions

NumPy provides various built-in functions to create arrays and input to them will be provided by NumPy. 

  1. zeros(shape,dtype=float) returns an array of a given shape and type, filled with zeros(0). If dtype is not provided, it is float by default.

a = np.zeros(3)

$o/p: array([0.0,0.0,0.0])

b = np.zeros(3,dtype=int)

$o/p: array([0,0,0])

c = np.zeros((2,2),dtype=int)

$o/p: array([[0,0],[0,0]])

  1. ones(shape,dtype=float) returns an array of a given shape and type, filled with ones(1). If dtype is not provided, it is float by default.

a = np.ones(3)

$o/p: array([1.0,1.0,1.0])

b = np.ones(3,dtype=int)

$o/p: array([1,1,1])

c = np.ones((2,2),dtype=int)

$o/p: array([[1,1],[1,1]])

  1. full(shape,fill_value,dtype=None) returns an array of a given shape and type ,fill with fill_value given in input parameters.

a = full(3,4)

$o/p: array([4,4,4])

  1. linspace(start,stop,num=50,endpoint=True) returns evenly spaced numbers over a specified interval. Number of samples specified by num. endpoint can be optionally excluded.

        np.linspace(2.0,2.5,num=5)

        $o/p: array(2.0,2.1,2.2,2.3,2.4,2.5)

  1. arange([start,]stop,[step]) returns an array with evenly spaced values within a given interval.

a=np.arange(5)

$o/p: array([0,1,2,3,4])

b = np.arange(1,5)

$o/p: array([1,2,3,4])

c = np.arange(1,10,2)

$o/p: array([1,3,5,7,9])

More on NumPy on Upcoming posts

Happy Learning!

%d bloggers like this: