4.0 Numpy Advanced

4.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.

4.2 Import numpy


In [2]:
import numpy as np
np.__version__


Out[2]:
'1.15.4'

In [3]:
import matplotlib as mpl
from matplotlib import pyplot as plt
mpl.__version__


Out[3]:
'3.0.2'

Notes:

4.3 Set a dummy numpy array elements

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


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

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


(2, 50)
[[ 0.00000000e+00  2.00000000e+00  4.00000000e+00  6.00000000e+00
   8.00000000e+00  1.00000000e+01  1.20000000e+01  1.40000000e+01
   1.60000000e+01  1.80000000e+01  2.00000000e+01  2.20000000e+01
   2.40000000e+01  2.60000000e+01  2.80000000e+01  3.00000000e+01
   3.20000000e+01  3.40000000e+01  3.60000000e+01  3.80000000e+01
   4.00000000e+01  4.20000000e+01  4.40000000e+01  4.60000000e+01
   4.80000000e+01  5.00000000e+01  5.20000000e+01  5.40000000e+01
   5.60000000e+01  5.80000000e+01  6.00000000e+01  6.20000000e+01
   6.40000000e+01  6.60000000e+01  6.80000000e+01  7.00000000e+01
   7.20000000e+01  7.40000000e+01  7.60000000e+01  7.80000000e+01
   8.00000000e+01  8.20000000e+01  8.40000000e+01  8.60000000e+01
   8.80000000e+01  9.00000000e+01  9.20000000e+01  9.40000000e+01
   9.60000000e+01  9.80000000e+01]
 [ 0.00000000e+00  4.79425539e-01  8.41470985e-01  9.97494987e-01
   9.09297427e-01  5.98472144e-01  1.41120008e-01 -3.50783228e-01
  -7.56802495e-01 -9.77530118e-01 -9.58924275e-01 -7.05540326e-01
  -2.79415498e-01  2.15119988e-01  6.56986599e-01  9.37999977e-01
   9.89358247e-01  7.98487113e-01  4.12118485e-01 -7.51511205e-02
  -5.44021111e-01 -8.79695760e-01 -9.99990207e-01 -8.75452175e-01
  -5.36572918e-01 -6.63218974e-02  4.20167037e-01  8.03784427e-01
   9.90607356e-01  9.34895056e-01  6.50287840e-01  2.06467482e-01
  -2.87903317e-01 -7.11785342e-01 -9.61397492e-01 -9.75626005e-01
  -7.50987247e-01 -3.42480618e-01  1.49877210e-01  6.05539870e-01
   9.12945251e-01  9.96829794e-01  8.36655639e-01  4.71639003e-01
  -8.85130929e-03 -4.87174512e-01 -8.46220404e-01 -9.98082028e-01
  -9.05578362e-01 -5.91357530e-01]]

Notes:

4.4 Save the numpy array to file

It is practical to save the processed arrays and numpy has built in functions for this


In [5]:
np.save('np_file.npy', values)
np.savetxt('txt_file.txt', np.transpose(values))

Notes:

4.5 Load numpy array from file


In [6]:
values_from_text = np.loadtxt("txt_file.txt")
values_from_np   = np.load("np_file.npy")

print(values_from_text[0,0] == values_from_np[0,0])
print(values_from_text)


True
[[ 0.00000000e+00  0.00000000e+00]
 [ 2.00000000e+00  4.79425539e-01]
 [ 4.00000000e+00  8.41470985e-01]
 [ 6.00000000e+00  9.97494987e-01]
 [ 8.00000000e+00  9.09297427e-01]
 [ 1.00000000e+01  5.98472144e-01]
 [ 1.20000000e+01  1.41120008e-01]
 [ 1.40000000e+01 -3.50783228e-01]
 [ 1.60000000e+01 -7.56802495e-01]
 [ 1.80000000e+01 -9.77530118e-01]
 [ 2.00000000e+01 -9.58924275e-01]
 [ 2.20000000e+01 -7.05540326e-01]
 [ 2.40000000e+01 -2.79415498e-01]
 [ 2.60000000e+01  2.15119988e-01]
 [ 2.80000000e+01  6.56986599e-01]
 [ 3.00000000e+01  9.37999977e-01]
 [ 3.20000000e+01  9.89358247e-01]
 [ 3.40000000e+01  7.98487113e-01]
 [ 3.60000000e+01  4.12118485e-01]
 [ 3.80000000e+01 -7.51511205e-02]
 [ 4.00000000e+01 -5.44021111e-01]
 [ 4.20000000e+01 -8.79695760e-01]
 [ 4.40000000e+01 -9.99990207e-01]
 [ 4.60000000e+01 -8.75452175e-01]
 [ 4.80000000e+01 -5.36572918e-01]
 [ 5.00000000e+01 -6.63218974e-02]
 [ 5.20000000e+01  4.20167037e-01]
 [ 5.40000000e+01  8.03784427e-01]
 [ 5.60000000e+01  9.90607356e-01]
 [ 5.80000000e+01  9.34895056e-01]
 [ 6.00000000e+01  6.50287840e-01]
 [ 6.20000000e+01  2.06467482e-01]
 [ 6.40000000e+01 -2.87903317e-01]
 [ 6.60000000e+01 -7.11785342e-01]
 [ 6.80000000e+01 -9.61397492e-01]
 [ 7.00000000e+01 -9.75626005e-01]
 [ 7.20000000e+01 -7.50987247e-01]
 [ 7.40000000e+01 -3.42480618e-01]
 [ 7.60000000e+01  1.49877210e-01]
 [ 7.80000000e+01  6.05539870e-01]
 [ 8.00000000e+01  9.12945251e-01]
 [ 8.20000000e+01  9.96829794e-01]
 [ 8.40000000e+01  8.36655639e-01]
 [ 8.60000000e+01  4.71639003e-01]
 [ 8.80000000e+01 -8.85130929e-03]
 [ 9.00000000e+01 -4.87174512e-01]
 [ 9.20000000e+01 -8.46220404e-01]
 [ 9.40000000e+01 -9.98082028e-01]
 [ 9.60000000e+01 -9.05578362e-01]
 [ 9.80000000e+01 -5.91357530e-01]]

Notes:

4.6 Exercice:

Create a numpy array from -pi to pi and save the sin of it to file. The read this file again and print the result in the notebook.


In [ ]:

Notes:

5. Numpy array slicing

5.1 General slicing


In [7]:
values = values_from_text
print(values.shape)

x_0 = values[0]
print(x_0)

x_1 = values[:,0]
print(x_1)

y_1 = values[:,1]
print(y_1)


(50, 2)
[0. 0.]
[ 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.47942554  0.84147098  0.99749499  0.90929743  0.59847214
  0.14112001 -0.35078323 -0.7568025  -0.97753012 -0.95892427 -0.70554033
 -0.2794155   0.21511999  0.6569866   0.93799998  0.98935825  0.79848711
  0.41211849 -0.07515112 -0.54402111 -0.87969576 -0.99999021 -0.87545217
 -0.53657292 -0.0663219   0.42016704  0.80378443  0.99060736  0.93489506
  0.65028784  0.20646748 -0.28790332 -0.71178534 -0.96139749 -0.97562601
 -0.75098725 -0.34248062  0.14987721  0.60553987  0.91294525  0.99682979
  0.83665564  0.471639   -0.00885131 -0.48717451 -0.8462204  -0.99808203
 -0.90557836 -0.59135753]

In [8]:
fig = plt.figure()
plt.plot(x_1, y_1)
plt.show()


Notes:

5.2 Precise slicing


In [9]:
indices = [5,10, 15 ,20]

x = values[indices,0]
print(x)

y = values[indices,1]
print(y)


[10. 20. 30. 40.]
[ 0.59847214 -0.95892427  0.93799998 -0.54402111]

In [10]:
fig = plt.figure()
plt.plot(x, y)
plt.show()


Notes:

5.3 Conditional slicing


In [11]:
indices = np.where(values[:,1] > -0.5)[0]
print("indices: ",indices)

x = values[indices,0]
print("x: ",x)

y = values[indices,1]
print("y: ",y)


indices:  [ 0  1  2  3  4  5  6  7 12 13 14 15 16 17 18 19 25 26 27 28 29 30 31 32
 37 38 39 40 41 42 43 44 45]
x:  [ 0.  2.  4.  6.  8. 10. 12. 14. 24. 26. 28. 30. 32. 34. 36. 38. 50. 52.
 54. 56. 58. 60. 62. 64. 74. 76. 78. 80. 82. 84. 86. 88. 90.]
y:  [ 0.          0.47942554  0.84147098  0.99749499  0.90929743  0.59847214
  0.14112001 -0.35078323 -0.2794155   0.21511999  0.6569866   0.93799998
  0.98935825  0.79848711  0.41211849 -0.07515112 -0.0663219   0.42016704
  0.80378443  0.99060736  0.93489506  0.65028784  0.20646748 -0.28790332
 -0.34248062  0.14987721  0.60553987  0.91294525  0.99682979  0.83665564
  0.471639   -0.00885131 -0.48717451]

In [12]:
fig = plt.figure()
plt.plot(x, y)
plt.show()


Notes:

6. Numpy array Sorting

6.1 One dimension


In [13]:
to_sort = np.random.rand(10)
print(to_sort)


[0.74438005 0.66759891 0.99776074 0.71703393 0.44051449 0.80043255
 0.06930568 0.57756323 0.97322278 0.73689581]

In [14]:
to_sort.sort()
print(to_sort)


[0.06930568 0.44051449 0.57756323 0.66759891 0.71703393 0.73689581
 0.74438005 0.80043255 0.97322278 0.99776074]

Notes:

6.2 Two dimension


In [15]:
to_sort = np.random.rand(2,10)
print(to_sort)


[[0.59338804 0.09881164 0.50221478 0.90890098 0.39580175 0.99807353
  0.88899061 0.07625014 0.94579776 0.55559513]
 [0.17560908 0.83344852 0.14880046 0.16277757 0.1659511  0.28763826
  0.44091318 0.3692674  0.52122957 0.984774  ]]

In [16]:
to_sort.sort(axis=1)
print(to_sort)


[[0.07625014 0.09881164 0.39580175 0.50221478 0.55559513 0.59338804
  0.88899061 0.90890098 0.94579776 0.99807353]
 [0.14880046 0.16277757 0.1659511  0.17560908 0.28763826 0.3692674
  0.44091318 0.52122957 0.83344852 0.984774  ]]

Notes:

6.3 Sort whole array by one column


In [17]:
to_sort = np.random.rand(3,10)
print(to_sort)


[[0.28421131 0.45620536 0.88139488 0.21765097 0.78513718 0.14862175
  0.65422715 0.17078262 0.06942061 0.28684477]
 [0.5851557  0.98355313 0.73677578 0.77824204 0.31860526 0.01115127
  0.22403211 0.83552439 0.23995272 0.43048175]
 [0.18612104 0.41598837 0.353467   0.87297748 0.89724348 0.29244869
  0.67882874 0.08087711 0.99904277 0.53680573]]

In [18]:
#investigate the axis we want to sort after
print("The axis to sort: \n",to_sort[1])

sort_indices = to_sort[1].argsort()
print("The indexes after the sort: \n",sort_indices)

#proceed the sort using the slicing method we just introduced
to_sort = to_sort[:,sort_indices]
print("The sorted full array:\n ",to_sort)


The axis to sort: 
 [0.5851557  0.98355313 0.73677578 0.77824204 0.31860526 0.01115127
 0.22403211 0.83552439 0.23995272 0.43048175]
The indexes after the sort: 
 [5 6 8 4 9 0 2 3 7 1]
The sorted full array:
  [[0.14862175 0.65422715 0.06942061 0.78513718 0.28684477 0.28421131
  0.88139488 0.21765097 0.17078262 0.45620536]
 [0.01115127 0.22403211 0.23995272 0.31860526 0.43048175 0.5851557
  0.73677578 0.77824204 0.83552439 0.98355313]
 [0.29244869 0.67882874 0.99904277 0.89724348 0.53680573 0.18612104
  0.353467   0.87297748 0.08087711 0.41598837]]

Notes: