## Archive for the ‘python’ Category

The numpy module features some useful functions for statistics, like “mean()” and “median()”:

https://docs.scipy.org/doc/numpy/reference/routines.statistics.html

For example let´s consider a 2D array with age and height of some people and print out some statistics:

#! /usr/bin/env python import numpy as np #age, height in meters person = [[11,1.56],[4, 0.80], [44, 1.88], [23, 1.68], [55, 1.74]] np_person = np.array(person) print(np_person) age = np_person[:,0] height = np_person[:,1] #average print("average age: " + str(np.mean(age))) print("average height: " + str(np.mean(height))) #the standard deviation is also rounded to two decimals only. std_height= round(np.std(height),2) print("standard deviation of the height: "+ str(std_height)) #correlation corr = np.corrcoef(np_person[:,0], np_person[:,1]) print("Correlation: " + str(corr))

The code can be also found on github:

https://github.com/lauraliparulo/python-scripts/blob/master/statistics/person_stats.py

If you want to execute python scripts with Linux you need to add the shebang line: “#! /usr/bin/env python”

It must be added on top of the file.

The shebang will allow you to run the script as any other script. Among the many options to run it, assuming the script name is “script”, one is:

> ./script.py

The file must be made executable:

```
```> sudo chmod +x script.py

Assuming we want to print an homogenous array created with the numpy module, a script might include the module import too:

#! /usr/bin/env python import numpy as np array1 = np.array([1,2,3,4]) print(array1) print(type(array1))

It will print the following lines:

[1 2 3 4]

<type ‘numpy.ndarray’>