NTDS’17 demo 3: Numpy

Copyright © Code Fetcher 2022

Hermina Petric Maretic, EPFL LTS4

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

In [1]:
import numpy as np
In [2]:
#create a numpy array
a = np.array([1,2,3,4])
a
Out[2]:
array([1, 2, 3, 4])
In [3]:
#or a 2 dimensional array
m = np.array([[1,2],[3,4]])
m
Out[3]:
array([[1, 2],
       [3, 4]])
In [4]:
m[0,0]
Out[4]:
1
In [5]:
m[:,1]
Out[5]:
array([2, 4])
In [6]:
a[:2]
Out[6]:
array([1, 2])
In [7]:
a[-2] #second last element of the array
Out[7]:
3
In [8]:
a[-2:] #last two elements of the array
Out[8]:
array([3, 4])

Careful if you’re used to Python list

In [9]:
b = [1,2,3,4]
In [10]:
b + b
b
Out[10]:
[1, 2, 3, 4]
In [11]:
a + a
a
Out[11]:
array([1, 2, 3, 4])
In [12]:
#if you want to add elements to a
np.append(a,a)
Out[12]:
array([1, 2, 3, 4, 1, 2, 3, 4])
In [13]:
np.append(a,[1,2,3])
Out[13]:
array([1, 2, 3, 4, 1, 2, 3])
In [14]:
np.insert(a, 1, 5) #insert 5 on position number 1
Out[14]:
array([1, 5, 2, 3, 4])

Basic arithmetics with numpy arrays

In [15]:
a + 3
Out[15]:
array([4, 5, 6, 7])
In [16]:
a * 3
Out[16]:
array([ 3,  6,  9, 12])
In [17]:
a ** 3
Out[17]:
array([ 1,  8, 27, 64])
In [18]:
a * a
Out[18]:
array([ 1,  4,  9, 16])
In [19]:
a.sum()
Out[19]:
10
In [20]:
m * m #still elementwise multiplication
Out[20]:
array([[ 1,  4],
       [ 9, 16]])
In [21]:
np.dot(m,m) #standard matrix multiplication
Out[21]:
array([[ 7, 10],
       [15, 22]])
In [22]:
m = np.matrix(m) #there is a type matrix
m * m #for matrices, multiplication works as we're used to
Out[22]:
matrix([[ 7, 10],
        [15, 22]])

Some functions to create arrays

In [23]:
x = np.arange(0,10,2) #beginning, end, step
x
Out[23]:
array([0, 2, 4, 6, 8])
In [24]:
np.linspace(0,10,5) #beginning, end, number of variables
Out[24]:
array([  0. ,   2.5,   5. ,   7.5,  10. ])
In [25]:
np.logspace(0,10,10,base=2) #beginning, end, number of variables
Out[25]:
array([  1.00000000e+00,   2.16011948e+00,   4.66611616e+00,
         1.00793684e+01,   2.17726400e+01,   4.70315038e+01,
         1.01593667e+02,   2.19454460e+02,   4.74047853e+02,
         1.02400000e+03])
In [26]:
np.diag([1,2,3])
Out[26]:
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])
In [27]:
np.zeros(5)
Out[27]:
array([ 0.,  0.,  0.,  0.,  0.])
In [28]:
np.ones((3,3))
Out[28]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
In [29]:
np.random.rand(5,2)
Out[29]:
array([[ 0.95867801,  0.36665502],
       [ 0.12711337,  0.17199144],
       [ 0.06886422,  0.08173259],
       [ 0.07495435,  0.73382897],
       [ 0.49609348,  0.4109899 ]])

Some linear algebra functions

In [30]:
np.diag(m)
Out[30]:
array([1, 4])
In [31]:
np.trace(m)
Out[31]:
5
In [32]:
m.T
Out[32]:
matrix([[1, 3],
        [2, 4]])
In [33]:
m1 = np.linalg.inv(m)
m1
Out[33]:
matrix([[-2. ,  1. ],
        [ 1.5, -0.5]])
In [34]:
np.linalg.det(m)
Out[34]:
-2.0000000000000004
In [35]:
np.linalg.det(m1)
Out[35]:
-0.49999999999999967
In [36]:
[eival, eivec] = np.linalg.eig(m)
In [37]:
eival
Out[37]:
array([-0.37228132,  5.37228132])
In [38]:
eivec
Out[38]:
matrix([[-0.82456484, -0.41597356],
        [ 0.56576746, -0.90937671]])

 

 

Leave a comment

Your email address will not be published. Required fields are marked *