Introduction to NumPy

Video Tutorial
FREE
NumPy Introduction thumbnail
This video belongs to
Python and SQL for Data Science
8 modules
Certificate
Topics Covered

Overview

This article is a brief " Introduction to Numpy in Python ", which is an open-source library widely used for Data Science, Artificial intelligence, etc. This Numpy Python tutorial gives a basic understanding of Numpy in Python, why and how it is used, and what are the applications of Numpy in Python. There are libraries like Pandas, Tkinter, Matplotlib, scikit-learn, etc. in Python that uses Numpy because of its fast computation.

Introduction

In Python, a list, by default, stores only 1D arrays. To store multidimensional arrays in contiguous memory, Numpy is used. It supports N-dimensional arrays, and also advanced mathematical operations can be performed on Numpy arrays. Numpy is very fast and more efficient than Python lists.

What is NumPy?

Numpy in Python or Numerical Python is an open-source Python library created by "Travis Oliphant" in 2005 for numerical and scientific computing in Python. Numpy arrays are stored in a single continuous block of memory; this makes NumPy faster than the Python list and takes lesser memory. In the field of data science and Artificial Intelligence, Numpy is used widely.

  • It is a group of same-type elements.
  • The array in NumPy is called ndarray, which is also known as an alias array.
  • Axes are the dimensions in NumPy.The number of axes determines the rank.

A Simple Numpy Example

Let's see some Simple Numpy Examples for a better understanding of the numpy tutorial.

  • Firstly, there is a need to import the Numpy Python library
  • np is an alternative used in place of NumPy; not necessary to use np; something else can also be used.
    Eg : import numpy as any

Numpy 1-d array:

Code:

Output:


Numpy 2-D array:

Code:

Output:

Operations Using Numpy

Basic NumPy Array Operation

1. Indexing:

Indexing is used for accessing elements from an array. It starts from 0. Below is a code example where the element of the index (0,2)(0,2) are accessed, where 0 stands for the 1st row and 1 for the 3rd column from the 2-D array.

Code:

Output:


2. Slicing:

Slicing is the method for getting substrings from the original arrays by mentioning the start and end inside the slice operator []. Let's see the below example where the substring from the 2nd element to the 5th element is sliced.

Note: elements are accessed through indexing.

Code:

Output:


3. array.ndim:

array.ndim is used to find the number of dimensions of the array. Let us look into the code example below :

Code:

Output:


4. array.itemsize :

array.itemsize is used to find the size of each element of the Numpy array in bytes. Below is an array consisting of integers, and the size of an integer is 8 bytes. That's, why the itemsize is 8.

Code:

Output:


5. array.dtype:

array.dtype is used to find the data type of elements of an array. In the code example given below, elements are of integer data type.

Code:

Output:


6. array.size:

array.size is used to find the size of the array by counting the number of elements from the given array.

  • It returns the count of elements along a specific axis.

Code:

Output:


7. array.shape:

By using the array.shape function, we can find the shape, i.e., the number of rows and columns of an array.

  • It returns a tuple.
  • The lengths of the corresponding array dimensions are given by the tuple items.

Code:

Output:


8. array.reshape() :

array.reshape is used to reshape the array; it means changing the shape, i.e., rows and columns of the array.

Code:

Output:


9. array.sum() :

By using an array.sum, we can find the sum of the array. Let's see the code example below :

Code:

Output:


10. array.ravel() :

array.ravel converts a 2-D or Multidimensional array into a single row of the same type.

Code:

Output:

Apart from the above-mentioned basic Numpy operations, there are several arithmetic operations supported by numpy,

for example:
numpy.add(), numpy.divide() , numpy.multiply, numpy.subtract(), numpy.mod(), numpy.power() and numpy.reciprocal() supported by numpy. All these functions are self-explanatory and performed element-by-element wise manner.

  • Numpy uses less memory and is stored in a single continuous block of memory.
  • Because of its expressive and compact syntax, it is quick and simple to comprehend.
  • Makes accessing elements and specific portions of the array very easy using Indexing and Slicing.
  • It has lots of built-in functions for mathematical and statistical calculations like power function, addition, deviation, etc., which provides better ways for handling and processing data.

Comparison between Core Python and Numpy

Core PythonNumpy
Python is an open-source interpreted high-level, general-purpose dynamically typed programming language developed by Guido van Rossum.NumPy is an open-source library for the Python programming language developed by Travis Oliphant.
Object-oriented, imperative, procedural, and functional programming paradigms are supported by Python. It has built-in containers, which are lists, tuples, dictionaries, and sets.Numpy supports built-in mathematical functions to operate on large multidimensional arrays and matrices.
Simple python list program :
list=[1,2,"hi"]
Print(list)
Output :
[1,2,'hi']
Simple Numpy program :
import numpy as np
arr=np.array([1,2,3])
print(arr)
Output :
[1,2,3]

NumPy – A Replacement for MATLAB

  • Numpy in Python is an open-source, free library that can process linear algebra, complex matrix transforms, etc., and act as a gateway for c and C++ integrations and is also used in the film visual effect development and gaming world.
  • Numpy is used with packages like Matplotlib for plotting libraries and SciPy for Scientific Python, which makes a perfect combination widely used in place of MATLAB.

Why should we Use NumPy?

  • It’s fast :
    The CPython interpreter interprets Python, which is a dynamic programming language, converts it to bytecode, and then executes it. NumPy is written in the C language, and C code is always compiled faster. Numpy arrays use less memory and are stored in a single continuous block of memory which makes NumPy multiple times faster than the Python list.
  • It works very well with SciPy and other Libraries:
    Numpy is used by many libraries, including MatPlotLib, Pandas, SciPy, and others. NumPy and SciPy have lots of similarities between them and can together be used as a replacement for MATLAB.
  • It lets you do matrix arithmetic:
    Many arithmetic operations, such as high-level Mathematical and logical operations, Fourier transforms and routines, and linear algebra operations such as dot product of two matrices, addition of matrices, and so on, can be performed. It can also solve matrix equations.
  • It has lots of built-in functions :
    Numpy in Python has lots of built-in functions for searching, sorting, indexing, math functions, random sampling, string operations, statistics, etc., which provide a better way of handling data for pre-processing.
  • It has universal functions:
    Universal functions or ufunc are Mathematical operations like trigonometric functions, etc., used on NumPy arrays. It implements vectorization on Numpy arrays which is faster than iterating over elements. Universal functions also provide Array Broadcasting by which arithmetic operations can be performed on arrays of different shapes and provide additional methods to accumulate, reduce, etc., that are helpful in computation.

Features of Numpy

NumPy contains many features, some of which are listed below:

  • NumPy is a combination of Python and the C language as it is partially written in Python, and most of its parts are written in C or C++.
  • The object-oriented approach is also fully supported by Numpy.
  • NumPy functions can be used to work with code that is written in other programming languages and provides tools for integrating with languages such as C, Fortran, etc.
  • NumPy is an open-source core Python package for scientific computing.
  • NumPy uses less space and stores data in contiguous memory.
  • It offers a multidimensional array object with excellent performance as well as methods for working with these arrays.
  • Arrays can be reshaped into different dimensions using the reshape function provided by NumPy.
  • We can work with different data types using NumPy and can determine the type of data using the dtype function.
  • NumPy comes with a plethora of built-in functions such as sum, sort, max, and so on, allowing users to write fewer lines of code while improving the quality of their work.
  • NumPy provides a broadcasting technique by which we can perform arithmetic operations on arrays of different shapes.
  • NumPy with SciPy is also used as an alternative to MATLAB.

The Relationship between NumPy, SciPy, Scikit-learn, and Pandas

  • Scipy is the Scientific library that has a slower computation speed. Scipy builds on Numpy and consists of all functionalities of Numpy. Both can use for faster mathematical and scientific calculations.
  • Pandas is a Numpy tutorial extension that adds functions for exploratory data analysis, statistics, and data visualization to Numpy. It's like Python's version of Microsoft Excel spreadsheets for manipulating and examining tabular data.
  • Scikit-learn is also built on Numpy and Scipy. It is an extension of advanced machine learning algorithms Numpy and Scipy.

Applications of Numpy

  • Numpy arrays are used as an alternative for Python lists.
  • Numpy in Python is used for performing mathematical operations on Multidimensional arrays.
  • Numpy is used with different libraries like Scipy, Pandas, Tkinter, etc.
  • Numpy is also used for reshaping the arrays called Broadcasting for performing operations on different sized arrays.
  • Scipy with Numpy in Python can be used in place of MATLAB.

Conclusion

  • Numpy in Python is an open-source free library of Python programming language.
  • It has a bundle of built-in functions used for searching, sorting, statistics, etc., which makes it easy to learn and helps developers to work efficiently.
  • It is multiple times faster than the Python lists and can be used for performing various mathematical and logical operations like arithmetic operations, etc. on multidimensional arrays.
  • It is very popular and widely used in the field of Data Science and Artificial Intelligence. Many libraries like Scipy, MatplotLIb, Tkinter, and Pandas use Numpy.
  • As it is also used in visual films and the gaming world so, using it with Scipy can replace MATLAB.