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 as np
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])
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])
Array creation using Built-in functions
NumPy provides various built-in functions to create arrays and input to them will be provided by NumPy.
- 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)
b = np.zeros(3,dtype=int)
c = np.zeros((2,2),dtype=int)
- 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)
b = np.ones(3,dtype=int)
c = np.ones((2,2),dtype=int)
- 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)
- 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.
- arange([start,]stop,[step]) returns an array with evenly spaced values within a given interval.
b = np.arange(1,5)
c = np.arange(1,10,2)
More on NumPy on Upcoming posts