In Python, how to know what does each number reperesent in multilabel_binarizer

Viewed 34

I have multi-label multi-classifcation dataset number of labels exceed 1000 labels

ID   Feature1   Feature2   Feature3   Feature4   Labels
1    1034       3922       2300       3922       Toyota, Mazda
2    5855       3201       7820       2421       Nissan, Honda, Kia, Mazda
3    1383       1554       7771       5200       Mazda, Kia, Toyota
4    5858       6912       1800       1100       Toyota, Fiat, BMW, Kia, Mazda
5    3737       3822       3636       3820       BMW
6    2554       8442       9254       8338       VolksWagon, Fiat, Kia

I get everything working fine

except I would like to know what does each number in the multilabel_binarizer mean

I am using this code

multilabel_binarizer = MultiLabelBinarizer()
multilabel_binarizer.fit(df[Code])
z = multilabel_binarizer.transform(df[Code])
z_pivot = pd.DataFrame(z).T

which convert my labels into numbers

ID   Feature1   Feature2   Feature3   Feature4   0   1   2   3   ...   1058
1    1034       3922       2300       3922       1   0   0   1   ...   0    
2    5855       3201       7820       2421       0   0   1   0   ...   1 
3    1383       1554       7771       5200       1   1   0   0   ...   0
4    5858       6912       1800       1100       1   0   0   1   ...   0
5    3737       3822       3636       3820       0   0   0   0   ...   1
6    2554       8442       9254       8338       0   0   1   0   ...   0

I do calculations and work on the number of each label

At the end I do

                y_pred_Codes = multilabel_binarizer.inverse_transform(y_pred)

which converts numbers to labels

but still I do not know which number means which table

I need this as I extract a copy of the dataframe in between

so I want the map of each number what does it mean in the labels

something like this

0    Toyota
1    Fiat
2    BMW
3    Mazda
...  ...
1048 Nissan

how to do that?

1 Answers

Looking into the source code of MultiLabelBinarizer(), you can use

multilabel_binarizer.classes_

to get list of class in the order of label number. Or use

multilabel_binarizer._build_cache()

to get a dict, which is just a zip of classes_ attribute. Use

dict(zip(range(len(multilabel_binarizer.classes_)), multilabel_binarizer.classes_))

for dict with number as keys.

Related