File tree Expand file tree Collapse file tree 1 file changed +5
-3
lines changed Expand file tree Collapse file tree 1 file changed +5
-3
lines changed Original file line number Diff line number Diff line change @@ -200,6 +200,10 @@ def predict_proba(self, X):
200
200
weights = _get_weights (neigh_dist , self .weights )
201
201
if weights is None :
202
202
weights = np .ones_like (neigh_ind )
203
+ else :
204
+ # Some weights may be infinite (zero distance), which can cause
205
+ # downstream NaN values when used for normalization.
206
+ weights [np .isinf (weights )] = np .finfo ('f' ).max
203
207
204
208
all_rows = np .arange (X .shape [0 ])
205
209
probabilities = []
@@ -214,9 +218,7 @@ def predict_proba(self, X):
214
218
# normalize 'votes' into real [0,1] probabilities
215
219
normalizer = proba_k .sum (axis = 1 )[:, np .newaxis ]
216
220
normalizer [normalizer == 0.0 ] = 1.0
217
- with np .errstate (invalid = 'ignore' ):
218
- proba_k /= normalizer
219
- proba_k [np .isnan (proba_k )] = 1.0
221
+ proba_k /= normalizer
220
222
221
223
probabilities .append (proba_k )
222
224
You can’t perform that action at this time.
0 commit comments