You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RandomForest errors out with infinite values for predictions #34668
This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.
Describe the bug and give evidence about its user-facing impact
Decision trees have decision rules of the form x <= value and similar. Such rules are well defined for infinite values, and it should be possible to make predictions on them, but scikit-learn refuses such inputs.
Hi, I was looking into your issue and here are my findings.
I was able to reproduce the error. The value error thrown is coming from an assertion statement _assert_all_finite from the validation layer rather than prediction logic/model issue. Once the predict method is hit, the input is gone through a validation layer and the param ensure_all_finite = "allows_nan". It allows nan and rest of the values are checked for finiteness and when np.inf is encountered it raises an error. This is completely reproducible.
I tweaked the param ensure_all_finite to False to check whether the model predictions works fine without any crashes and yes it did so. Since it worked correctly for RandomForestRegressor, I tried decision tree to see if the behaviour is repeating across tree models and yes in Decision tree setting the ensure_all_finite = False is getting past the validation layer and predictions ran successfully.
From these experiments, it seems that the reported behavior is coming from the current input validation rather than an immediate limitation in the prediction code for these estimators.
These are some insights I tried with the code. As for the maintainers if you need help in fixing this behaviour OR your suggesting the existing flow is intentional. I am good. Let me know if you need any help.
This needs decision: it's clearly intentional in the code, but I'm indeed not sure why this limitation is enforced.
For the fit, I think we can't accept them in the random splitter because of this: rand_uniform(min_feature_value, max_feature_value). For the bet splitter, only the order of values matters, so we could support it.
One thing to note: there are cases for which NaNs are not supported, and the current validation tools in scikit-learn don't allow for "NaN forbidden but inf/-inf allowed"... (we could change that though).
With all that said, I'm not sure if we really want to support that or if we prefer to consider it's a mistake from the user. @adam2392 do you have an opinion?
I don't have much of a use case, but it feels like an unnecessary blocker for something that should be theoretically possible.
Although infinite values do come up from time to time when calculating ratios as features, or as a more natural kind of "missing" label in some cases where they are more similar to lower or higher values.
Thanks for the work you've done so far. The goal of this comment is to set expectations.
Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.
Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.
shubhampotdar4122003-debug commented on Aug 16, 2026
Hi! Just checking in on this issue — has there been any decision on whether infinite values should be supported during prediction for tree-based estimators?
Happy to help investigate or work on it if the direction is decided.
Hello, I am answering here as I just opened (and it was closed) a similar issue.
I think it is more a question of why not than a why. E.G. infinite value support doesn't seem very hard to implement.
It seems to be a relatively minor change:
The splitter already tolerates infinite values without modifications. There seems to be a little modification to be done so we can have the correct split.
There seems to be a little need for removing the gating / test but nothing expensive. (and a good opportunity to get rid of the error relating to missing or infinite value).
Maybe the hard part would be the saving of trees, I am not sure infinite value can be properly saved / serialised.
Activity
prxshxnti commentedon Aug 7, 2026
Hi, I was looking into your issue and here are my findings.
I was able to reproduce the error. The value error thrown is coming from an assertion statement
_assert_all_finitefrom the validation layer rather than prediction logic/model issue. Once the predict method is hit, the input is gone through a validation layer and the param ensure_all_finite = "allows_nan". It allows nan and rest of the values are checked for finiteness and when np.inf is encountered it raises an error. This is completely reproducible.I tweaked the param
ensure_all_finiteto False to check whether the model predictions works fine without any crashes and yes it did so. Since it worked correctly for RandomForestRegressor, I tried decision tree to see if the behaviour is repeating across tree models and yes in Decision tree setting theensure_all_finite= False is getting past the validation layer and predictions ran successfully.From these experiments, it seems that the reported behavior is coming from the current input validation rather than an immediate limitation in the prediction code for these estimators.
These are some insights I tried with the code. As for the maintainers if you need help in fixing this behaviour OR your suggesting the existing flow is intentional. I am good. Let me know if you need any help.
cakedev0 commentedon Aug 10, 2026
This needs decision: it's clearly intentional in the code, but I'm indeed not sure why this limitation is enforced.
For the fit, I think we can't accept them in the random splitter because of this:
rand_uniform(min_feature_value, max_feature_value). For the bet splitter, only the order of values matters, so we could support it.One thing to note: there are cases for which NaNs are not supported, and the current validation tools in scikit-learn don't allow for "NaN forbidden but inf/-inf allowed"... (we could change that though).
With all that said, I'm not sure if we really want to support that or if we prefer to consider it's a mistake from the user. @adam2392 do you have an opinion?
adam2392 commentedon Aug 10, 2026
Hmm interesting.
What use case does support inf have for you? @david-cortes-intel
david-cortes-intel commentedon Aug 10, 2026
I don't have much of a use case, but it feels like an unnecessary blocker for something that should be theoretically possible.
Although infinite values do come up from time to time when calculating ratios as features, or as a more natural kind of "missing" label in some cases where they are more similar to lower or higher values.
github-actions commentedon Aug 10, 2026
Thanks for the work you've done so far. The goal of this comment is to set expectations.
Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.
Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.
shubhampotdar4122003-debug commentedon Aug 16, 2026
Hi! I would like to work on this issue. Are we open to allowing infinite values during prediction for tree-based models?
prxshxnti commentedon Sep 6, 2026
Hi! Just checking in on this issue — has there been any decision on whether infinite values should be supported during prediction for tree-based estimators?
Happy to help investigate or work on it if the direction is decided.
lorentzenchr commentedon Sep 21, 2026
Let‘s make decisions then.
If it is a minor FIX of the code, we should do it.
If this is a long, delicate, labour-intensive work, we shouldn‘t.
For me, this is evidence to not do it. If there is no use case, nothing is really blocked (and you can always set inf to a very large float anyway).
lcrmorin commentedon Sep 21, 2026
Hello, I am answering here as I just opened (and it was closed) a similar issue.
I think it is more a question of why not than a why. E.G. infinite value support doesn't seem very hard to implement.
It seems to be a relatively minor change: