Commit 81cdd749 authored by Mauro Donega's avatar Mauro Donega
Browse files

first commit

parent acf13248
This diff is collapsed.
%% Cell type:markdown id: tags:
# Generate random numbers from a gaussian, store them into a numpy array and compute mean, median and mode
%% Cell type:code id: tags:
``` python
import numpy as np
```
%% Cell type:code id: tags:
``` python
mu,sigma = 0,5
X = np.random.normal(mu, sigma, 11)
```
%% Cell type:code id: tags:
``` python
print X
print "size of the array = ", np.size(X)
```
%% Output
[-1.26455776 1.38453698 0.73973744 -1.15468409 1.53464198 5.19383441
4.68666505 -3.37484882 -2.02648859 -1.23785353 -3.25495203]
size of the array = 11
%% Cell type:markdown id: tags:
# Compute the mean
%% Cell type:code id: tags:
``` python
#compute the mean
sum = 0
for x in X:
sum += x
print "mean = ", sum/np.size(X)
```
%% Output
mean = 0.111457367391
%% Cell type:code id: tags:
``` python
# mean from numpy
print np.mean(X)
```
%% Output
0.111457367391
%% Cell type:markdown id: tags:
# Compute the median
%% Cell type:code id: tags:
``` python
np.sort(X)
```
%% Output
array([-3.37484882, -3.25495203, -2.02648859, -1.26455776, -1.23785353,
-1.15468409, 0.73973744, 1.38453698, 1.53464198, 4.68666505,
5.19383441])
%% Cell type:code id: tags:
``` python
np.median(X)
```
%% Output
-1.1546840894861969
%% Cell type:markdown id: tags:
# Compute the mode
%% Cell type:code id: tags:
``` python
# to show what is the mode we can cast to integers the previous array and use a new library scipy.stats
from scipy.stats import mode
intX = X.astype(int)
print intX
```
%% Output
[-1 1 0 -1 1 5 4 -3 -2 -1 -3]
%% Cell type:code id: tags:
``` python
mode(intX) # this will give you back the mode and the number of times it appears
```
%% Output
ModeResult(mode=array([-1]), count=array([3]))
%% Cell type:code id: tags:
``` python
# plot the distribution to see visually the mode
import matplotlib.pyplot as plt
%matplotlib inline
n, bins, patches = plt.hist(intX, 21, facecolor='blue')
plt.show()
```
%% Output
%% Cell type:code id: tags:
``` python
```
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
%% Cell type:markdown id: tags:
# Error propagation
%% Cell type:code id: tags:
``` python
from uncertainties import ufloat
import math
```
%% Cell type:code id: tags:
``` python
x = ufloat(1.0,0.1)
y = ufloat(1.5,0.1)
print x
print y
```
%% Output
1.00+/-0.10
1.50+/-0.10
%% Cell type:markdown id: tags:
# scale