Numpy

NumPy allows us to do complex computations involving multi-dimensional arrays and matrices, generate randomness, and also perform more simple functions we encounter every day.
NumPy will serve as a sort of Swiss army knife for math.

  • The fundamental unit in NumPy is the numpy array, which is a matrix and is based on traditional Python lists.
    For example:
    if I made a list in Python containing 1 and 5. That's quite handy. It stores the numbers 1 and 5, but there's very little I can do with that. It's very difficult for me to perform math on a Python list. If I wanted to take x and add 1 to every element within, it wouldn't quite be so simple as x plus 1.
In [1]:
x = [2,8]
x+1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-5f383590cd77> in <module>()
      1 x = [2,8]
----> 2 x+1

TypeError: can only concatenate list (not "int") to list

With NumPy, however, life is greatly simplified.

What I'm going to do then is redefine x as a numpy array.

In [2]:
# The first thing you need to do is import it
import numpy as np

#And the way I do this is with np, or numpy, dot array.
x = np.array([2,8])

Now x is a numpy array with elements 1 and 5. and we can do all sort of things to a numpy array among them operations like addition, subtraction, multiplication and addition

In [3]:
x+1
Out[3]:
array([3, 9])
In [4]:
x-3
Out[4]:
array([-1,  5])
In [5]:
x*5
Out[5]:
array([10, 40])

NumPy fundamentally works like math,which means it's fairly simple to bring to it your mathematical intuition.

  • I can also define another NumPy array.So I'm going to make a matrix y.
In [6]:
y = np.array([2,5])
In [7]:
# I can do x + y since it's element-wise it would be: 2+2, 5+8
x+y
Out[7]:
array([ 4, 13])
In [8]:
# x-y
x - y
Out[8]:
array([0, 3])
In [9]:
#x * y
x*y
Out[9]:
array([ 4, 40])
In [10]:
x/y
Out[10]:
array([1. , 1.6])

Above operations gives me an element-wise operation.

NumPy also contains methods for making particular kinds of matrices such as:

  • Matrices of zeros, ones,
  • Random matrices.
In [11]:
# zero matrix with 3 rows and 4 columns
np.zeros([3,4])
Out[11]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
In [12]:
# ones matrix with 4 rows and 3 columns
np.ones([4,3])
Out[12]:
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

NumPy also contains methods for making random matrices. Now the way I do this is via np.random, which contains several functions, including random again.

In [13]:
#random matrix with 3 rows and 4 columns, Return random floats in the half-open interval [0.0, 1.0).
np.random.random([3,4])
Out[13]:
array([[0.19562252, 0.91895081, 0.35372375, 0.22609342],
       [0.43723989, 0.54199647, 0.21732799, 0.30470941],
       [0.50318549, 0.66145526, 0.18323291, 0.35377885]])

Example:

  • Write a function called randomization that takes as input a positive integer n, and returns A, a random n x 1 Numpy array.
In [14]:
def randomization(n):
    A = np.random.random([n,1])
    return A
In [15]:
randomization(3)
Out[15]:
array([[0.07700537],
       [0.58158776],
       [0.27861762]])
In [ ]: