Homework 7. numpy

Here you can find the solution of the homework 7 of the 'Herramientas computacionales' course at uniades (Section 6) Course, Homework.

As we are going to use numpy, we import it to use numpy.array structure,

In [1]:
import numpy as np

Create an array from $0$ to $10$ including the limits,

In [2]:
x=np.linspace(0,10,1000)

It has the expected lenght

In [3]:
len(x)
Out[3]:
1000

We create some noisy signal by adding random numbers to a sine function,

In [4]:
y=np.sin(x)+(0.4*(np.random.random(len(x))-0.2))

We use matplotlib as auxiliar visualization,

In [5]:
import matplotlib.pylab as plt
In [6]:
plt.plot(x,y)
plt.savefig('fig1.pdf')
plt.show()

We use some of the numpy built-in functions to find the mean, variance, maximum and minimum.

In [7]:
np.mean(y)
Out[7]:
0.29902630965512633
In [8]:
np.var(y)
Out[8]:
0.4522265938880142
In [9]:
np.max(y)
Out[9]:
1.3145964014080123
In [10]:
np.min(y)
Out[10]:
-1.0743402522984065

In order to get the value of $x$ in which $y$ is max, we use the function numpy.argmax

In [11]:
np.argmax(y)
Out[11]:
778

And we use it to get the x of that cellm

In [12]:
x[np.argmax(y)]
Out[12]:
7.787787787787788

We implement two different ways to calculate the variance and average, one is based on the list structure and the other using the numpy structure.

The skewness and Kurtosis functions are made by using the numpy structure.

In [13]:
def variance(x):
    ave=average(x)
    suma=0.
    for i in range(len(x)):
        suma+=(x[i]-ave)**2
    suma/=len(x)
    return suma
In [14]:
def variance2(x):
    ave=average2(x)
    suma=np.sum((x-ave)**2.)/len(x)
    return suma
In [15]:
def average(x):
    suma=0.
    for i in range(len(x)):
        suma+=x[i]
    suma=suma/len(x)
    return suma
In [16]:
def average2(x):
    suma=np.sum(x)/len(x)
    return suma
In [17]:
def skewness(x):
    ave=average2(x)
    suma=np.sum((x-ave)**3.)/len(x)
    return suma
In [18]:
def Kurtosis(x):
    ave=average2(x)
    suma=np.sum((x-ave)**4.)/len(x)
    return suma

We use them,

In [19]:
%time
average(y)
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs
Out[19]:
0.2990263096551267
In [20]:
%time
average2(y)
CPU times: user 2 µs, sys: 1 µs, total: 3 µs
Wall time: 6.2 µs
Out[20]:
0.29902630965512633
In [21]:
%time
variance(y)
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs
Out[21]:
0.45222659388801456
In [22]:
%time
variance2(y)
CPU times: user 3 µs, sys: 1 µs, total: 4 µs
Wall time: 5.72 µs
Out[22]:
0.4522265938880142
In [23]:
%time
np.var(y)
CPU times: user 3 µs, sys: 1 µs, total: 4 µs
Wall time: 5.01 µs
Out[23]:
0.4522265938880142
In [24]:
skewness(y)
Out[24]:
-0.11617066301547643
In [25]:
Kurtosis(y)
Out[25]:
0.3829253953325561

We use again matplotlibto see the distribution of the $y$ points,

In [26]:
plt.hist(y,bins=25)
plt.savefig('fig2.pdf')
plt.show()

We perform some transformations on the data,

In [27]:
y2=np.sort(y)
In [28]:
plt.plot(x,y)
plt.plot(x,y2)
plt.savefig('fig3.pdf')
plt.show()
In [29]:
y3=y*y2
plt.plot(x,y)
plt.plot(x,y2)
plt.plot(x,y3)
plt.savefig('fig4.pdf')
plt.show
Out[29]:
<function matplotlib.pyplot.show(*args, **kw)>

And we calculate the percentile 5 and 95

In [30]:
np.percentile(y3,q=[5,95])
Out[30]:
array([-0.67891694,  1.03440143])