'sklearn.ensemble.RandomForestClassifier with non-consistent output

I have a trained sklearn randomforest multi-label classifier, in the training set, one class is always present, which means you expect the classifier to always return 1 for this class. This happens, however the classifier returns [1] instead of [0, 1]. See output bellow:

[array([[0.05, 0.95]]), array([[0.97, 0.03]]), 
array([[0.95, 0.05]]), array([[1., 0.]]), array([[1., 0.]]), 
array([[1., 0.]]), array([[0.65, 0.35]]), array([[1.]])]

Why is this the case, and how do I prevent this from happening? In the example, it is the result of only a single input however in my case I have a full data frame as input which I transform into class predictions. This is not possible if one of the arrays has only a single dimension: [1] instead of two dimensions [0,1] like the predictions for the other classes.

Can this be changed with a setting in sklearn?

Extra clarification why I have a training set with only positive class samples: This is part of a recommender system and sometimes a product is bought every time by every type of customer.



Solution 1:[1]

May be this code can help you:

dates_array_a = ["2021-12-07","2021-12-08","2021-12-09","2021-12-10","2021-12-11","2021-12-12","2021-12-13","2021-12-14"];
dates_array_b = [ ['2021-12-07', 100], ['2021-12-10', 555], ['2021-12-13', 750] ];

let lastKnownKey;
const result = dates_array_a.map((dateA) => {
  const [date, key] = dates_array_b.find(([dateB]) => dateB === dateA) ?? [null, null]
  lastKnownKey = key ?? lastKnownKey;
  return `${dateA} ${lastKnownKey}`;
});

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 A1exandr Belan