In [2]:
import numpy as np
import matplotlib.pyplot as plt

Exercice Numpy

1. Array creation

  • Create a 1D array with values from 0 to 10 and in steps of 0.1. Check the shape of the array:
In [ ]:
 
  • Create an array of normally distributed numbers with mean $\mu=0$ and standard deviation $\sigma=0.5$. It should have 20 rows and as many columns as there are elements in xarray. Call it normal_array:
In [ ]:
 
  • Check the type of normal_array:
In [ ]:
 

2. Array mathematics

  • Using xarray as x-variable, create a new array yarray as y-variable using the function $y = 10* cos(x) * e^{-0.1x}$:
In [ ]:
 
  • Create array_abs by taking the absolute value of array_mul:
In [ ]:
 
  • Create a boolan array (logical array) where all positions $>0.3$ in array_abs are True and the others False
In [ ]:
 
  • Create a standard deviation projection along the second dimension (columns) of array_abs. Check that the dimensions are the ones you expected. Also are the values around the value you expect?
In [ ]:
 

3. Plotting

  • Use a line plot to plot yarray vs xarray:
In [ ]:
 
  • Try to change the color of the plot to red and to have markers on top of the line as squares:
In [ ]:
 
  • Plot the normal_array as an imagage and change the colormap to 'gray':
In [ ]:
 
  • Assemble the two above plots in a figure with one row and two columns grid:
In [ ]:
 

4. Indexing

  • Create new arrays where you select every second element from xarray and yarray. Plot them on top of xarray and yarray.
In [ ]:
 
  • Select all values of yarray that are larger than 0. Plot those on top of the regular xarray and yarrayplot.
In [ ]:
 
  • Flip the order of xarray use it to plot yarray:
In [ ]:
 

5. Combining arrays

  • Create an array filled with ones with the same shape as normal_array. Concatenate it to normal_array along the first dimensions and plot the result:
In [ ]:
 
  • yarray represents a signal. Each line of normal_array represents a possible random noise for that signal. Using broadcasting, try to create an array of noisy versions of yarray using normal_array. Finally, plot it:
In [ ]: