8000 apply the fix closer to the source of the issue · rmurcek/scikit-learn@55ef5a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 55ef5a4

Browse files
apply the fix closer to the source of the issue
1 parent 392630d commit 55ef5a4

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

sklearn/neighbors/classification.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ def predict_proba(self, X):
200200
weights = _get_weights(neigh_dist, self.weights)
201201
if weights is None:
202202
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
203207

204208
all_rows = np.arange(X.shape[0])
205209
probabilities = []
@@ -214,9 +218,7 @@ def predict_proba(self, X):
214218
# normalize 'votes' into real [0,1] probabilities
215219
normalizer = proba_k.sum(axis=1)[:, np.newaxis]
216220
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
220222

221223
probabilities.append(proba_k)
222224

0 commit comments

Comments
 (0)
0