I want to apply the inverse_transform
of MultiLabelBinarizer to a single sample, e.g.:
labels = ['Architektur & Garten',
'Ganzheitliches Bewusstsein',
'Glaube & Ethik',
'Kinderbuch & Jugendbuch',
'Künste',
'Literatur & Unterhaltung',
'Ratgeber',
'Sachbuch']
samples = []
for l in labels:
samples.append([l])
from sklearn.preprocessing import MultiLabelBinarizer
m = MultiLabelBinarizer()
m.fit_transform(samples)
If I now apply the MultiLabelBinarizer to a matrix it works:
s = np.array([[0, 1, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 1]])
m.inverse_transform(s)
[('Ganzheitliches Bewusstsein',), ('Sachbuch',)]
If I however try to apply it to a single sample, i.e. a vector it fails:
import numpy as np
s = np.array([0, 1, 0, 0, 0, 0, 0, 0])
m.inverse_transform(s)
--> 957 if yt.shape[1] != len(self.classes_):
958 raise ValueError('Expected indicator for {0} classes, but got {1}'
959 .format(len(self.classes_), yt.shape[1]))