Sobes.tech
Middle — Senior

We are solving a linear regression problem, all y > 0. Which of the listed algorithms can give a negative prediction: linear regression, KNN, gradient boosting, tree, random forest, neural network?

sobes.tech AI

Answer from AI

Among the listed algorithms:

  • Linear regression can give negative predictions because it is simply a linear combination of features without restrictions.
  • KNN (k-nearest neighbors) predicts the average or median of neighbors' values, so if neighbors have negative values, it can give negative predictions. But if all y > 0, then predictions will be > 0.
  • Gradient boosting, decision trees, random forest, and neural networks can give negative predictions if no restrictions are applied (for example, ReLU activations, output constraints, or transformations).

In summary: without additional restrictions, all listed models can predict negative values, except KNN, which will not give negative predictions if all training y > 0.

To avoid negative predictions, one can apply:

  • Transformation of the target variable (e.g., logarithm).
  • Constraints on the model output.
  • Special models that account for non-negativity.