appunti3s:numpy
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
appunti3s:numpy [2020/07/08 23:54] – profpro | appunti3s:numpy [2020/07/17 07:08] – profpro | ||
---|---|---|---|
Line 5: | Line 5: | ||
https:// | https:// | ||
- | Il python classico | + | Il python |
numpy permette di usare un nuovo tipo di array | numpy permette di usare un nuovo tipo di array | ||
- | < | + | < |
- | noti anche con il termine "ndarray" | + | * **ndarray** = N-dimensional array, ndarray, sono noti anche con il termine "array", cosa che potrebbe causare confusione se non si usasse il nome del modulo (numpy) come prefisso (numpy.array) |
- | Vediamo alcuni esempi per definire un array | + | ==Vediamo alcuni esempi per definire un array== |
- | <code> | + | * dtype = data type |
+ | |||
+ | <file python 1.py> | ||
import numpy as np | import numpy as np | ||
a = [[ 1., 0., 0.], [ 0., 1., 2.]] | a = [[ 1., 0., 0.], [ 0., 1., 2.]] | ||
b = np.array([1.2, | b = np.array([1.2, | ||
- | c = np.array( [ [1.0,2.0], [3.0,4.0] ], dtype=complex ) | + | c = np.array( [ [1.0,2.0], [3.0,4.0] ], dtype=complex ) # oppure int32 |
d = np.zeros((3, | d = np.zeros((3, | ||
e = np.ones((3, 4)) | e = np.ones((3, 4)) | ||
+ | del e # libera la memoria | ||
+ | |||
+ | </ | ||
+ | |||
+ | * shape = definisce la forma (le dimensioni) dell' | ||
+ | * nel precedente esempio | ||
+ | * la variabile a, ha due assi, il primo ha lunghezza 3, il secondo ha lunghezza 2 | ||
+ | * la variabile b ha due assi, il primo ha lunghezza 3, il secondo ha lunghezza 1, quindi si dice che ha un solo asse. | ||
+ | * Per comprendere questo termine (numero di assi) basta immaginare che la variabile b possa essere usata per contenere i punti dell' | ||
+ | |||
+ | Questa istruzione preleva solo alcuni valori, tutti e tre della seconda colonna. | ||
+ | c[0:2, 1] | ||
+ | c[:, | ||
+ | |||
+ | ==Iterazione== | ||
+ | |||
+ | usando un indice si stampano intere righe alla volta | ||
+ | <code python> | ||
+ | >>> | ||
+ | ... | ||
</ | </ | ||
+ | |||
+ | usando flat si stampano i singoli elementi | ||
+ | <code python> | ||
+ | >>> | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | ==Esempio in cui si vuole valutare una funzione== | ||
+ | |||
+ | <file python 2.py> | ||
+ | from numpy import pi | ||
+ | x = np.linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points | ||
+ | f = np.sin(x) | ||
+ | b = np.fromfunction(f, | ||
+ | y,x = np.ogrid[ -1.4: | ||
+ | </ | ||
+ | |||
+ | ====Esempio di operatori==== | ||
+ | |||
+ | == matrix product == | ||
+ | |||
+ | Scopriamo che è semplice fare le operazioni sui singoli elementi (potenze) | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | array([ 1.44, 12.25, 26.01]) | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==Prodotto vettoriale o cross product== | ||
+ | |||
+ | Di solito si moltiplicano due array 1 D e si ottiene un terzo array 1 D, ortogonale. | ||
+ | |||
+ | <code python> | ||
+ | numpy.cross(a, | ||
+ | </ | ||
+ | |||
+ | ==Prodotto scalare o dot product== | ||
+ | |||
+ | Ha un comportamento diverso, a seconda dei fattori utilizzati | ||
+ | |||
+ | * usando il tipo scalare 0 D, equivale a fare il prodotto con asterisco * | ||
+ | * usando il tipo array 1 D, equivale a fare il prodotto scalare, o somma dei prodotti | ||
+ | * usando il tipo array 2 D, ......... | ||
+ | |||
+ | < | ||
+ | fare un esempio.... | ||
+ | </ | ||
+ | Quando gli array sono 2D è preferibile usare @ | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | </ | ||
+ | |||
+ | == operazioni scalari == | ||
+ | |||
+ | <code python> | ||
+ | b.sum() | ||
+ | b.sum(axis=0) | ||
+ | b.sum(axis=1) | ||
+ | </ | ||
+ | |||
+ | == Randomizzare == | ||
+ | |||
+ | Anche numpy ha il proprio generatore di numeri causali | ||
+ | |||
+ | <file python 4.py> | ||
+ | import numpy.random as np_r | ||
+ | rng = np_r.default_rng() | ||
+ | vals = rng.standard_normal(10) | ||
+ | more_vals = rng.standard_normal(10) | ||
+ | </ | ||
+ | |||
+ | ==Copia e vista== | ||
+ | |||
+ | Non esistono puntatori, ma si usano! | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | True | ||
+ | >>> | ||
+ | >>> | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | </ | ||
+ | |||
+ | Modificando i dati di A, si modifica anche C! | ||
+ | Modificando i dati di C, si modifica anche A! | ||
+ | Modificando gli attributi (flag) di C NON si modificano i parametri di A! | ||
+ | |||
+ | == Deep copy == | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | </ | ||
+ | |||
+ | ==Array di Indici di Array== | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | array([ 1, 1, 9, 64, 25]) | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ |
appunti3s/numpy.txt · Last modified: 2020/08/02 17:27 by profpro