NumPy

What is NumPy?

NumPy is Python’s fundamental scientific computing library. With NumPy we can

  1. Create multidimensional arrays
  2. Fast mathematical operations
  3. Integrate with lower level languages like C or C++
  4. Linear Algebra and Fourier transform
  5. Python’s solution for vectorization

Why use NumPy

Take an example where we need to calculate speeds of different cars. To calculate speeds we will need to use this formula

Speeds = Distances / Times

Let’s say we have Distanced and Times in lists and now we need to calculate speeds

The above code will output following result

[10.0, 22.22222222222222, 37.5, 66.66666666666667]

Another way of writing above code will be to use the List comprehension as given below

This will output

[10.0, 22.22222222222222, 37.5, 66.66666666666667]

If we use NumPy to achieve the above it can be as simple as  speeds = distances / times

How to convert Python List to NumPy Array

  1. First of all, we will import NumPy library to our programme by using import
  2. Now we will convert Python list to numpy array using the array(name of list) method