Академический Документы
Профессиональный Документы
Культура Документы
Introduction
UMR MISTEA Montpellier France
2016
Pascal.Neveu@supagro.inra.fr 1/73
Plan
Introduction.......................... p 3
Python for sciences............... p 11
Structure and types............... p 16
Data manipulations............... p 40
Matrix Operations................. p 50
Exercises.............................. p 78
Practices and performance.... p 59
Pascal.Neveu@supagro.inra.fr 2/73
Introduction
Python:
- Powerful programming language
- Available on different platforms
- Effective approach to object-oriented programming
- Dynamic typing and interpreted
- Easy to learn, (help function) and Web resources
Some Spanish references:
http://es.wikipedia.org/wiki/Python
http://docs.python.org.ar/tutorial/contenido.html
Current versions: 3.3 or 2.7 ( 3.3 vs 2.7?)
Pascal.Neveu@supagro.inra.fr 3/73
Introduction
Invoking python
- Script mode
pn@pn-laptop:~/PYTHON$ cat prog1.py
print 2+3
pn@pn-laptop:~/PYTHON$ python prog1.py
5
- Interactive mode
pn@pn-laptop:~/PYTHON$ python
Python 2.6.5 (r265:79063, Oct 1 2012, 22:04:36)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" ...
>>> 1+2
3
Pascal.Neveu@supagro.inra.fr 4/73
Introduction: list
>>>
>>>
[1,
>>>
1
>>>
3
numbers=[1,2,3]
numbers
2, 3]
numbers[0]
numbers[2]
Pascal.Neveu@supagro.inra.fr 5/73
Introduction: if statement
>>> x=2
>>> if x>1 : x=x+1
...
>>> x
3
>>> if x < 0:
...
x = 0
...
print Negative changed to zero
... elif x == 0:
...
print Zero
... else:
...
print More
...
More
Pascal.Neveu@supagro.inra.fr 6/73
Pascal.Neveu@supagro.inra.fr 7/73
>>> range(3)
[0, 1, 2]
>>> for i in range(3):
...
print i,is a ,words[i]
...
0 is a cat
1 is a dog
2 is a horse
Pascal.Neveu@supagro.inra.fr 8/73
Pascal.Neveu@supagro.inra.fr 9/73
Pascal.Neveu@supagro.inra.fr 10/73
Pascal.Neveu@supagro.inra.fr 11/73
bool
int
int8
uint8
float
float16
float32
complex
string
http://docs.scipy.org/doc/numpy/user/basics.types.html
Pascal.Neveu@supagro.inra.fr 16/73
Predefined constants
True
False
e
1j
inf
nan
pi
Pascal.Neveu@supagro.inra.fr 17/73
Main structures
Pascal.Neveu@supagro.inra.fr 18/73
Matrix:
Matlab behavior
A*B : matrix product
dimension 2 (only)
Pascal.Neveu@supagro.inra.fr 19/73
Vector creating
Unidimensional array (ordering values)
>>> a=array([1,2,3,4])
>>> a
array([1, 2, 3, 4])
>>> size(a)
4
# index from 0
>>> a[2]
3
# select elements 4 and 2
>>> a[[3,1]]
array([4, 2])
Pascal.Neveu@supagro.inra.fr 20/73
Vector generating
>>> v=r_[1:11]
array([ 1, 2,
3,
4,
5,
6,
7,
8,
9, 10])
>>> v=linspace(0,pi,5)
array([ 0. , 0.7853981, 1.5707963, 2.3561944, 3.1415926])
>>> v=arange(5)
array([0, 1, 2, 3, 4])
>>> v=arange(1,11,3)
array([ 1, 4, 7, 10])
>>> v=arange(1,11,2.5)
array([ 1. , 3.5, 6. ,
8.5])
Pascal.Neveu@supagro.inra.fr 22/73
Pascal.Neveu@supagro.inra.fr 23/73
float64, 48 bytes
int64, 48 bytes
int32, 24 bytes
int16, 12 bytes
Pascal.Neveu@supagro.inra.fr 24/73
Matrix generating
Pascal.Neveu@supagro.inra.fr 27/73
8, 10],
7, 9]])
8, 10,
1,
3,
5,
7,
9])
Pascal.Neveu@supagro.inra.fr 28/73
3.,
6.,
1.,
4.,
2.,
5.,
3.],
6.]])
Pascal.Neveu@supagro.inra.fr 29/73
3,
4])
Pascal.Neveu@supagro.inra.fr 30/73
1, 12,
4, 5,
3],
6]])
1, 12,
4, 5,
3],
6]])
>>> B=A.copy()
>>> A[0,1] = 3
>>> B
matrix([[ 1, 12,
[ 4, 5,
3],
6]])
Pascal.Neveu@supagro.inra.fr 31/73
Logical operators
==
!=
>
>=
<
<=
~
not
and
or
xor
equal
not equal
greater
greater and equal
lower
lower and equal
not
not
logical and
logical or
exclusive OR
Pascal.Neveu@supagro.inra.fr 32/73
Pascal.Neveu@supagro.inra.fr 33/73
Logical array
>>> A
array([[1, 2, 3],
[4, 5, 6]])
>>> A > 2
array([[False, False,
[ True, True,
True],
True]], dtype=bool)
>>> sum(A>2)
4
Pascal.Neveu@supagro.inra.fr 34/73
File writing
In [18]: A=arange(5)
In [19]: savetxt(fichier.txt,A)
# os: operating system interface module
In [20]: import os
In [21]: os.system(cat fichier.txt)
0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
4.000000000000000000e+00
Out[21]: 0
Pascal.Neveu@supagro.inra.fr 35/73
File reading
In [22]: B=loadtxt(fichier.txt)
In [23]: B
Out[23]: array([ 0.,
1.,
2.,
3.,
4.])
In [24]: B=B.astype(int)
Pascal.Neveu@supagro.inra.fr 36/73
Explicit reference
Attention
>>> import numpy as np
>>> A = np.array([[1,2,3],[4,5,6]])
>>> sum(A)
array([5, 7, 9])
>>> np.sum(A)
21
Pascal.Neveu@supagro.inra.fr 37/73
Constant manipulation
>>> sqrt(-1)
1j
>>> A = array([1,nan,2])
>>> A
array([ 1., NaN,
2.])
>>> max(A)
nan
>>> sum(A)
nan
>>> 1/inf
0.0
Pascal.Neveu@supagro.inra.fr 38/73
Attention:
>>> nan == nan
False
>>> isnan(nan)
True
Pascal.Neveu@supagro.inra.fr 39/73
Manipulations
Data manipulation, two ways:
with index
with logical
2,
3,
4,
5,
6,
7,
8,
9, 10])
>>> v[:]
array([ 1,
2,
3,
4,
5,
6,
7,
8,
9, 10])
>>> v[2:]
array([ 3,
4,
5,
6,
7,
8,
9, 10])
>>> v[2:-1]
array([3, 4, 5, 6, 7, 8, 9])
Pascal.Neveu@supagro.inra.fr 40/73
Manipulations (cont.)
>>> v[2:-3]
array([3, 4, 5, 6, 7])
>>> v[::2]
array([1, 3, 5, 7, 9])
>>> v[1::2]
array([ 2, 4,
6,
8, 10])
>>> v[[3,1]]
array([4, 2])
Pascal.Neveu@supagro.inra.fr 41/73
Manipulations (cont.)
Array manipulations with logical
>>> v
array([ 1,
2,
3,
4,
5,
6,
7,
8,
9, 10])
>>> v>2
array([False, False, True, True, True, True, True,
True, True, True], dtype=bool)
>>> v[v>2]
array([ 3,
4,
5,
6,
7,
8,
9, 10])
>>> v[(v>2)*(v<5)]
array([3, 4])
>>> v[((v>2)&(v<5))]
array([3, 4])
Pascal.Neveu@supagro.inra.fr 42/73
2,
5,
3],
6]])
>>> A[1,2]
6
>>> A[(0,1),1]
array([2, 5])
>>> A[1,(1,2)]
array([5, 6])
>>> A[:,1]
array([2, 5])
>>> A[[1,0],[1,2]]
array([5, 3])
Pascal.Neveu@supagro.inra.fr 43/73
>>> A<3
array([[ True, True, False],
[False, False, False]], dtype=bool)
>>> A[A<3]
array([1, 2])
>>> A[A<3]=10
>>> A
array([[10, 10,
[ 4, 5,
3],
6]])
Pascal.Neveu@supagro.inra.fr 44/73
Structure manipulation
>>> a=arange(8)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a.reshape(2,-1)
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> a.reshape(-1,2)
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
Pascal.Neveu@supagro.inra.fr 45/73
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a.reshape(-1,2).transpose()
>>> a.reshape(-1,2).T
array([[0, 2, 4, 6],
[1, 3, 5, 7]])
Pascal.Neveu@supagro.inra.fr 46/73
Matrix operations
Pascal.Neveu@supagro.inra.fr 47/73
Pascal.Neveu@supagro.inra.fr 49/73
Pascal.Neveu@supagro.inra.fr 50/73
Pascal.Neveu@supagro.inra.fr 51/73
0.52,
0.99,
0.5 ,
0.13,
>>> A[A>0.7]
array([ 0.99,
0.59,
0.88,
0.58,
0.03,
0.88,
0.3 ,
0.26,
0.29,
0.19,
0.44,
0.77,
0.41,
0.63,
0.05],
0.41],
0.26],
0.38]])
0.77])
>>> where(A>.6)
(array([1, 1, 1, 3]), array([0, 1, 3, 3]))
Pascal.Neveu@supagro.inra.fr 52/73
0.52,
0.99,
0.5 ,
0.13,
0.59,
0.88,
0.58,
0.03,
0.3 ,
0.26,
0.29,
0.19,
0.44,
0.77,
0.41,
0.63,
0.05],
0.41],
0.26],
0.38]])
>>> sort(A.flatten())
array([ 0.03, 0.05, 0.13, 0.19, 0.26, 0.26, 0.29,...
0.3 , 0.38, 0.41, 0.41, 0.44, 0.5 , 0.52, 0.58,...
0.59, 0.63, 0.77, 0.88, 0.99])
>>> sort(A)
array([[ 0.05,
[ 0.26,
[ 0.26,
[ 0.03,
0.3 ,
0.41,
0.29,
0.13,
0.44,
0.77,
0.41,
0.19,
0.52,
0.88,
0.5 ,
0.38,
0.59],
0.99],
0.58],
0.63]])
Pascal.Neveu@supagro.inra.fr 53/73
>>> sort(A,axis=0)
array([[ 0.13, 0.03,
[ 0.5 , 0.58,
[ 0.52, 0.59,
[ 0.99, 0.88,
0.19,
0.26,
0.29,
0.3 ,
0.41,
0.44,
0.63,
0.77,
0.05],
0.26],
0.38],
0.41]])
Pascal.Neveu@supagro.inra.fr 54/73
Pascal.Neveu@supagro.inra.fr 55/73
A program
import sys
from numpy import *
from scipy import *
import matplotlib.pyplot as plt
from scipy.integrate import odeint
# modele de "peche"
# x(t)=rx(t)*(1-x(t)/K)-qEx(t)
# 2 arguments r and E
# r : reproduction
# E : effort
# syntax checking
if ( size(sys.argv) != 3 ):
print Syntax error \n usage : +sys.argv[0]+ r E
exit(0)
r=float( sys.argv[1] ) # r
E=float( sys.argv[2] ) # E
Pascal.Neveu@supagro.inra.fr 58/73
A program (cont.)
# cond initials
x0=([8.0])
# temps
t=arange(0.0,80,0.003)
# capturability
q=0.25
# max
K=20.0
# axis labels
xl = plt.xlabel(temps)
yl = plt.ylabel(Biomass)
# graphic title visualize parameter value
titre=parameters : r=+sys.argv[1]+
... et E=+sys.argv[2]
Pascal.Neveu@supagro.inra.fr 59/73
A program (cont.)
ttl = plt.title(titre)
# diff eq of the modele
def funci(x,t):
return r*x*(1-x/K)-q*E*x
# integration init cond
y=odeint(funci,x0,t)
x0 and time
# visualisation
plt.plot(t,y)
plt.show()
Pascal.Neveu@supagro.inra.fr 60/73
Exercices
- Install python 2.7
- Install numpy, scipy, matplotlib and ipython modules
- Create v1 as numerical vector with values from 1 to 12
- Calculate the sum of v1 elements
- Calculate the number of v1 elements that are strictly greater than 5
- Calculate the sum of v1 elements that are strictly greater than 5
- Create a vector V of length 100 that contains random values in [0,1]
Use rand from numpy.random library.
- Give a simple instruction to get 12! value (factorial)
- Create the matrix A
array([[ 0,
[ 4,
[ 8,
1, 2, 3],
5, 6, 7],
9, 10, 11]])
Pascal.Neveu@supagro.inra.fr 61/73
Exercices (cont.)
- Calculate row means of A
Sergio has 3 daughters, we know:
The sum of 3 daughter ages is 66 years.
The double of the age of one of them is equal to the sum
of the 2 others.
The older has 12 years less than the sum of ages of
the 2 youngest daughters.
Give ages of the 3 daughters.
- x and y row vectors. We need to remove
values of vector x that are already in vector y.
For example, if x=[2, 1, 4, 5, 7, 6] and if
y = [1, 4, 7]. We want x= [2, 5, 6]
x=random(20)*3 and y=x**2+random(20)
make the plot of y vs x.
locate on the graphics the curve maximum
Pascal.Neveu@supagro.inra.fr 62/73
0.13654671,
0.15800227,
0.33486046,
0.46035374,
...
0.90776494])
>>> all(x>.002)
True
>>> any(x>.9)
True
>>> any(x>.999)
False
Pascal.Neveu@supagro.inra.fr 63/73
>>> t1=time.clock()
>>> n=0
>>> for i in range(2000000):
...
if a[i]>.5 : n=n+1
...
>>> n
1001192
>>> t2=time.clock()
>>> t2-t1
1.47
>>> t1=time.clock()
>>> n=sum(a>.5)
>>> n
1001192
>>> t2=time.clock()
>>> t2-t1
0.030000000000000027
Pascal.Neveu@supagro.inra.fr 64/73
>>> t1=time.clock()
>>> for i in range(2000000):
...
if a[i]>.5 : a[i]=1
...
>>> t2=time.clock()
>>> t2-t1
1.5600000000000001
>>> a[(a>.5)]=1
>>>
>>> t3=time.clock()
>>> t3-t2
0.020000000000000018
Pascal.Neveu@supagro.inra.fr 65/73
>>> t1=time.clock()
>>> for i in range(2000000):
...
if a[i]>.5 : a[i]=log(a[i])
...
>>> t2=time.clock()
>>> t2-t1
3.9399999999999995
>>> a[(a>.5)]=log(a[(a>.5)])
>>>
>>> t3=time.clock()
>>> t3-t2
0.010000000000000675
Pascal.Neveu@supagro.inra.fr 66/73
all vs sum
>>> t1=time.clock()
>>> n=sum(a>0)
>>>
>>> t2=time.clock()
>>> t2-t1
0.020000000000000018
>>> all(a>0)
True
>>> t3=time.clock()
>>> t3-t2
0.0099999999999997868
Pascal.Neveu@supagro.inra.fr 67/73
t1=time.clock()
for i in range(1000000-2):
y[i]=x[i]+x[i+1]+x[i+2]
t2=time.clock()
t2-t1
>>> t1=time.clock()
>>> y=x[:-2]+x[1:-1]+x[2:]
>>> t2=time.clock()
>>> t2-t1
0.020000000000000018
Pascal.Neveu@supagro.inra.fr 69/73
Pascal.Neveu@supagro.inra.fr 70/73
Pascal.Neveu@supagro.inra.fr 71/73