3. Numpy Basics

3.1 Verifying the python version you are using


In [1]:
import sys
print(sys.version)


3.7.0 (default, Aug 20 2018, 21:19:42) 
[Clang 9.1.0 (clang-902.0.39.2)]

At this point anything above python 3.5 should be ok.

3.2 Import numpy and matplotlib


In [2]:
import numpy as np
print(np.__version__)

import matplotlib as mpl
from matplotlib import pyplot as plt
print(mpl.__version__)


1.15.4
3.0.2

Notes:

3.3 Create an array


In [3]:
x = [0,1,2,3,4,5,6]
a = [0.0, 1.0, 6.2, 5.333, 9, 4, 3.4]
b = np.array(a)
print(b)
print(type(b))


[0.    1.    6.2   5.333 9.    4.    3.4  ]
<class 'numpy.ndarray'>

Notes:

3.4 Use numpy.ndarray internal methods

Print the mean and the sum of the created array


In [4]:
b = np.array(a)
print(b.sum())
print(b.mean())


28.933
4.133285714285714

In [5]:
#plot it
fig = plt.figure()
plt.plot(x,b)
plt.plot(x, [b.mean()] * len(x))
plt.show()


Notes:

3.5 Exercice:

Compare the summation of two lists with the one of two numpy arrays and report the difference


In [ ]:

Notes:

3.6 Generate numpy arrays from scratch


In [6]:
print(np.zeros(5))
print(np.ones(5))

zeros = np.zeros((5,2))
print(zeros)
print(zeros.shape)


[0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1.]
[[0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]]
(5, 2)

Notes:

3.7 Set numpy array elements

Create a numpy array and set the elements in it as you iterate.


In [7]:
values = np.zeros(50)
size  = values.shape
print(size)

for i in range(size[0]):
    values[i] = i * 2
    
print(values)


(50,)
[ 0.  2.  4.  6.  8. 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32. 34.
 36. 38. 40. 42. 44. 46. 48. 50. 52. 54. 56. 58. 60. 62. 64. 66. 68. 70.
 72. 74. 76. 78. 80. 82. 84. 86. 88. 90. 92. 94. 96. 98.]

Notes:

Do the same for a two dimensional array


In [8]:
values = np.zeros((2,50))
size  = values.shape
print(size)

for i in range(size[1]):
    values[0,i] = i * 2
    values[1,i] = i / 2
    
print(values)


(2, 50)
[[ 0.   2.   4.   6.   8.  10.  12.  14.  16.  18.  20.  22.  24.  26.
  28.  30.  32.  34.  36.  38.  40.  42.  44.  46.  48.  50.  52.  54.
  56.  58.  60.  62.  64.  66.  68.  70.  72.  74.  76.  78.  80.  82.
  84.  86.  88.  90.  92.  94.  96.  98. ]
 [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5  5.   5.5  6.   6.5
   7.   7.5  8.   8.5  9.   9.5 10.  10.5 11.  11.5 12.  12.5 13.  13.5
  14.  14.5 15.  15.5 16.  16.5 17.  17.5 18.  18.5 19.  19.5 20.  20.5
  21.  21.5 22.  22.5 23.  23.5 24.  24.5]]

Notes:

3.8 Exercice:

Process mathematical functions of arrays. For this purpose use function like np.exp, np.sin, np.cos, np.log or np.sqrt functions present in the numpy module and apply it to a numpy array as previsouly described. Then plot them ...


In [ ]:

Notes:

3.9 Use linspace for quick array generation


In [9]:
x = np.linspace(-np.pi, np.pi, num = 10)
print(x)


[-3.14159265 -2.44346095 -1.74532925 -1.04719755 -0.34906585  0.34906585
  1.04719755  1.74532925  2.44346095  3.14159265]

Notes:

3.10 Exercice:

process the mean of the sinus between -pi and pi in one line (hint: np.sin)


In [ ]:

Notes: