✏️ Esercizi#
Molti di questi esercizi contengono concetti e funzioni che non sono stati spiegati in precedenza. Pertanto, è più sensato cercare di comprendere la soluzione fornita dopo aver letto attentamente il testo dell’esercizio, anziché cercare di risolverlo senza consultare preventivamente la soluzione.
import numpy as np
Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.
Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.
Esercizio 1. Creare un vettore di dimensione 10 i cui elementi sono tutti 0 tranne il quinto che è 1.
Z = np.zeros(10)
Z[4] = 1
print(Z)
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
Esercizio 2. Creare un vettore con valori compresi tra 10 e 49.
Z = np.arange(10,50)
print(Z)
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
Esercizio 3. Invertire un vettore (il primo elemento diventa l’ultimo).
Z = np.arange(50)
Z = Z[::-1]
print(Z)
[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2
1 0]
Esercizio 4. Trovare gli indici degli elementi non zero di [1, 2, 0, 0, 4, s0].
nz = np.nonzero([1, 2, 0, 0, 4, 0])
print(nz)
(array([0, 1, 4]),)
Esercizio 5. Crearere un array 10x10 con valori casuali e trovare i valori minimo e massimo.
Z = np.random.random((10,10))
Z
array([[0.0708204 , 0.95974688, 0.4672839 , 0.41922607, 0.98598493,
0.63473662, 0.78101736, 0.072342 , 0.49436298, 0.20856603],
[0.70646236, 0.91179868, 0.62443811, 0.9215931 , 0.0655805 ,
0.94999081, 0.03188679, 0.53062703, 0.4437942 , 0.92951406],
[0.43417452, 0.58559374, 0.72067867, 0.22881332, 0.12704034,
0.82579505, 0.35690228, 0.75433483, 0.14171088, 0.72261857],
[0.06762427, 0.99049463, 0.57682744, 0.75731278, 0.54196812,
0.46814389, 0.67032553, 0.32896111, 0.75681843, 0.61696947],
[0.54431492, 0.72534886, 0.47978425, 0.8639504 , 0.84069558,
0.03679093, 0.93505434, 0.44160958, 0.48523031, 0.68891511],
[0.16887746, 0.25311054, 0.82361984, 0.77013944, 0.10064537,
0.93515197, 0.99096414, 0.50058185, 0.99326829, 0.631005 ],
[0.41192784, 0.45320375, 0.17977229, 0.67765087, 0.1319186 ,
0.8403485 , 0.76193093, 0.86525157, 0.66634861, 0.34378106],
[0.42722908, 0.88530912, 0.74586119, 0.75315078, 0.77773695,
0.7699262 , 0.69627418, 0.46486345, 0.76283829, 0.96465821],
[0.56780298, 0.61918704, 0.06221124, 0.79327838, 0.16445736,
0.9371252 , 0.80718586, 0.69121677, 0.25488964, 0.25655655],
[0.50294098, 0.28118982, 0.98348398, 0.90001853, 0.74398534,
0.87228622, 0.38097335, 0.76163003, 0.93589898, 0.84295941]])
Zmin, Zmax = Z.min(), Z.max()
print(Zmin, Zmax)
0.03188678958750113 0.9932682942858698
Esercizio 6. Creare un vettore casuale di dimensione 30 e trovare il valore medio.
Z = np.random.random(30)
Z
array([0.48508621, 0.17234104, 0.49710369, 0.058412 , 0.32280899,
0.44068946, 0.31127164, 0.91661934, 0.40799206, 0.99502268,
0.70209845, 0.6785909 , 0.72484677, 0.55229031, 0.50926366,
0.56562666, 0.34770023, 0.50786573, 0.37492836, 0.61136544,
0.51608351, 0.5528491 , 0.2971558 , 0.79799982, 0.1292703 ,
0.77011866, 0.02108276, 0.79124021, 0.75452956, 0.97990278])
m = Z.mean()
print(m)
0.5264052039328897
Esercizio 7. Dato un array 1D, negare tutti gli elementi compresi tra 3 e 8, inplace.
Z = np.arange(11)
Z[(3 < Z) & (Z < 8)] *= -1
print(Z)
[ 0 1 2 3 -4 -5 -6 -7 8 9 10]
Esercizio 8. Qual è l’output di print(sum(range(5),-1))
?
print(sum(range(5),-1))
9
Esercizio 9. Creare un vettore casuale di dimensione 10 e sostituire il valore massimo con 0.
Z = np.random.random(10)
Z[Z.argmax()] = 0
print(Z)
[0.26073755 0.07938285 0.11947669 0.01694249 0.43450715 0.09003104
0.52898779 0.21089765 0.58457833 0. ]
Esercizio 10. Trovare il valore più vicino (a uno scalare dato) in un vettore.
Z = np.arange(100)
Z
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
v = np.random.uniform(0, 100)
v
83.56341911433401
index = (np.abs(Z - v)).argmin()
print(Z[index])
84
Esercizio 11. Convertire tutti gli elementi di un array numpy da float a integer.
a = np.array([[2.5, 3.8, 1.5], [4.7, 2.9, 1.56]])
o = a.astype("int")
print(o)
[[2 3 1]
[4 2 1]]
Esercizio 12. Convertire un array numpy binario (contenente solo 0 e 1) in un array numpy booleano.
a = np.array([[1, 0, 0], [1, 1, 1], [0, 0, 0]])
o = a.astype("bool")
print(o)
[[ True False False]
[ True True True]
[False False False]]
Esercizio 13. Unire orizzontalmente due array numpy aventi la stessa prima dimensione (cioè lo stesso numero di righe negli array 2D).
a1 = np.array([[1, 2, 3], [4, 5, 6]])
a2 = np.array([[7, 8, 9], [10, 11, 12]])
o = np.hstack((a1, a2))
print(o)
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
Esercizio 14. Dati due array numpy, estrarre gli indici in cui gli elementi nei due array corrispondono.
a = np.array([1, 2, 3, 4, 5])
b = np.array([1, 3, 2, 4, 5])
print(np.where(a == b))
(array([0, 3, 4]),)
Esercizio 15. Generare un array ripetendo un array più piccolo di 2 dimensioni 10 volte.
a = np.array([[1, 2, 3], [4, 5, 6]])
o = np.tile(a, 10)
print(o)
[[1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3]
[4 5 6 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6 4 5 6]]
Esercizio 16. Generare un array 5x5 di interi casuali compresi tra 0 (incluso) e 10 (escluso).
np.random.seed(123) # setting the seed
o = np.random.randint(0, 10, size=(5, 5))
print(o)
[[2 2 6 1 3]
[9 6 1 0 1]
[9 0 0 9 3]
[4 0 0 4 1]
[7 3 2 4 7]]
Esercizio 17. Verificare se uno qualsiasi degli elementi di un array dato è diverso da zero.
x = np.array([1, 0, 0, 0])
print(np.any(x))
x = np.array([0, 0, 0, 0])
print(np.any(x))
True
False
Esercizio 18. Creare un confronto elemento per elemento (uguale, uguale entro una tolleranza) di due array dati.
x = np.array([72, 79, 85, 90, 150, -135, 120, -10, 60, 100])
y = np.array([72, 79, 85, 90, 150, -135, 120, -10, 60, 100.000001])
print(np.equal(x, y))
print(np.allclose(x, y))
[ True True True True True True True True True False]
True
Esercizio 19. Trovare i dati mancanti in un array dato.
nums = np.array([[3, 2, np.nan, 1], [10, 12, 10, 9], [5, np.nan, 1, np.nan]])
nums
array([[ 3., 2., nan, 1.],
[10., 12., 10., 9.],
[ 5., nan, 1., nan]])
print(np.isnan(nums))
[[False False True False]
[False False False False]
[False True False True]]
Esercizio 20. Scrivi un programma NumPy per estrarre tutti i numeri da un array dato che sono inferiori e superiori ad un numero specificato.
nums = np.array([[5.54, 3.38, 7.99], [3.54, 4.38, 6.99], [1.54, 2.39, 9.29]])
n = 6
print(nums[nums < n])
[5.54 3.38 3.54 4.38 1.54 2.39]
Esercizio 21. Sostituire tutti i numeri in un array dato che sono uguali, minori e maggiori di un dato numero.
nums = np.array([[5.54, 3.38, 7.99], [3.54, 8.32, 6.99], [1.54, 2.39, 9.29]])
n = 8.32
r = 18.32
print(np.where(nums == n, r, nums))
[[ 5.54 3.38 7.99]
[ 3.54 18.32 6.99]
[ 1.54 2.39 9.29]]
print("\nReplace elements with of the said array which are less than", n, "with", r)
print(np.where(nums < n, r, nums))
Replace elements with of the said array which are less than 8.32 with 18.32
[[18.32 18.32 18.32]
[18.32 8.32 18.32]
[18.32 18.32 9.29]]
print("\nReplace elements with of the said array which are greater than", n, "with", r)
print(np.where(nums > n, r, nums))
Replace elements with of the said array which are greater than 8.32 with 18.32
[[ 5.54 3.38 7.99]
[ 3.54 8.32 6.99]
[ 1.54 2.39 18.32]]
Esercizio 22. Moltiplicare due array dati della stessa dimensione elemento per elemento.
nums1 = np.array([[2, 5, 2], [1, 5, 5]])
nums2 = np.array([[5, 3, 4], [3, 2, 5]])
print(nums1)
[[2 5 2]
[1 5 5]]
print(nums2)
[[5 3 4]
[3 2 5]]
print(np.multiply(nums1, nums2))
[[10 15 8]
[ 3 10 25]]