Opérations arithmétiques
30 min
Niveau 8
Introduction
Les tableaux d'entrée pour l'exécution d'opérations arithmétiques telles que add(), subtract(), multiply() et divide() doivent être soit de la même forme, soit conformes aux règles de diffusion des tableaux.
Exemple
import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
print 'First array:'
print a
print '\n'
print 'Second array:'
b = np.array([10,10,10])
print b
print '\n'
print 'Add the two arrays:'
print np.add(a,b)
print '\n'
print 'Subtract the two arrays:'
print np.subtract(a,b)
print '\n'
print 'Multiply the two arrays:'
print np.multiply(a,b)
print '\n'
print 'Divide the two arrays:'
print np.divide(a,b)
Il produira le résultat suivant -
First array:
[
[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]
]
Second array:
[10 10 10]
Add the two arrays:
[
[ 10. 11. 12.]
[ 13. 14. 15.]
[ 16. 17. 18.]
]
Subtract the two arrays:
[
[-10. -9. -8.]
[ -7. -6. -5.]
[ -4. -3. -2.]
]
Multiply the two arrays:
[
[ 0. 10. 20.]
[ 30. 40. 50.]
[ 60. 70. 80.]
]
Divide the two arrays:
[
[ 0. 0.1 0.2]
[ 0.3 0.4 0.5]
[ 0.6 0.7 0.8]
]
Voyons maintenant quelques-unes des autres fonctions arithmétiques importantes disponibles dans NumPy.
Besoin d'aide ?
Rejoignez notre communauté officielle et ne restez plus seul à bloquer sur un problème !