The following example demonstrates the idea, using skmultilearn's implementation of LabelPowerset.
# generate a sample multilabel dataset
from sklearn.datasets import make_multilabel_classification
from skmultilearn.problem_transform import LabelPowerset
from sklearn.ensemble import RandomForestClassifier
import numpy.random as random
from scipy import sparse
random.seed(123)
X, y = make_multilabel_classification(n_classes=8,
n_labels=2, # average no. of labels per instance
allow_unlabeled=False,
random_state=1)
y[0:2]
# array([[0, 0, 0, 1, 0, 0, 0, 1],
# [0, 1, 0, 1, 0, 0, 1, 1]])
classifier = LabelPowerset(
classifier = RandomForestClassifier(),
require_dense = [False, True]
)
classifier.fit(X, y)
classifier.unique_combinations_
# {'1': 0,
# '7': 1,
# '1,3,7': 2,
# '0,6': 3,
# '3': 4,
# '0': 5,
# '6': 6,
# '0,1,7': 7,
# '1,4,7': 8,
# '0,4': 9,
# '4': 10,
# '1,3,4,7': 11,
# '1,6': 12,
# '0,5': 13,
# '1,3': 14,
# '3,4': 15,
# '1,4': 16,
# '1,7': 17,
# '0,1': 18,
# '0,1,3': 19,
# '1,3,5': 20,
# '3,6,7': 21,
# '5,7': 22,
# '0,3': 23,
# '1,6,7': 24,
# '0,6,7': 25,
# '3,5': 26,
# '3,7': 27}
def get_OR_prob(X, classes=None):
pred = classifier.classifier.predict_proba(X.reshape(1,-1))[0]
for combination_id in range(len(pred)):
if sorted(classes) ==
sorted(classifier.reverse_combinations_[combination_id]):
return pred[combination_id]
return 0