Remplacer dans une matrice les valeurs "inf" avec numpy de python

Published: 06 février 2018

DMCA.com Protection Status

Exemple de comment remplacer dans une matrice les valeurs inf avec numpy de python

from numpy import inf

import numpy as np

data = np.random.rand(4,4)

data[3,1] = inf
data[1,2] = inf
data[1,0] = -inf

print(data)

print('---------- replace inf value ----------')

data[data == inf] = 0
data[data == -inf] = 0

print(data)

donne par exemple

[[ 0.71609474  0.4647556   0.4534842   0.58247166]
 [       -inf  0.16399704         inf  0.50330687]
 [ 0.25337134  0.48485982  0.45901885  0.68636133]
 [ 0.52786728         inf  0.15045737  0.34464057]]

---------- replace inf value ----------

[[ 0.71609474  0.4647556   0.4534842   0.58247166]
 [ 0.          0.16399704  0.          0.50330687]
 [ 0.25337134  0.48485982  0.45901885  0.68636133]
 [ 0.52786728  0.          0.15045737  0.34464057]]

Références

Liens Site
Replace -inf with zero value stackoverflow
numpy.isinf scipy doc