Numpy


NumPy is a Python package. It stands for 'Numerical Python'. It is a library consisting of multidimensional array objects and a collection of routines for processing of array.


Operations using NumPy

  • Using NumPy, a developer can perform the following operations −
  • Mathematical and logical operations on arrays.
  • Fourier transforms and routines for shape manipulation.
  • Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation.



NumPy – A Replacement for MatLab


NumPy is often used along with packages like SciPy (Scientific Python) and Mat−plotlib (plotting library). 

This combination is widely used as a replacement for MatLab, a popular platform for technical computing. 

Python is a more modern alternative to MatLab

It is open source, which is an added advantage of NumPy.


Before we can use NumPy we will have to import it. It has to be imported like any other module:


import numpy


But you will hardly ever see this. Numpy is usually renamed to np:

import numpy as np


Our first simple Numpy example deals with temperatures. Given is a list with values, e.g. temperatures in Celsius:

cvalues = [20.1, 20.8, 21.9, 22.5, 22.7, 22.3, 21.8, 21.2, 20.9, 20.1]


We will turn our list "cvalues" into a one-dimensional numpy array:

C = np.array(cvalues)

print(C)


Let's assume, we want to turn the values into degrees Fahrenheit. This is very easy to accomplish with a numpy array. The solution to our problem can be achieved by simple scalar multiplication:

print(C * 9 / 5 + 32)