J'ai un tableau numpy comme maintenant, je veux diviser chaque élément inférieur à 5 par 2. Le résultat doit être P> [[0.5, 1, 1.5],
[2, 5, 6],
[7, 8, 9]]
4 Réponses :
Vous pouvez le faire par indexation logique:
>>> import numpy as np >>> x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = float) >>> x[x<5] = x[x<5]/2 >>> x array([[0.5, 1. , 1.5], [2. , 5. , 6. ], [7. , 8. , 9. ]])
Utiliser sortie: p> numpy.where code>:
numpy.where(np_array >=5, np_array, np_array/2)
Voici ce que vous pourriez faire
x[x<5] = x[x<5]/2