I am trying to classify text to multi labels and it is working good but as i want to consider the predicted labels below .5 threshold, it changed predict()
to predict_proba()
to get the all the probalities of labels and select the values based on different threshold but I am not able to transform binary probalities value of each label to actual text label.
Here is reproducible code:
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import MultiLabelBinarizer
X_train = np.array(["new york is a hell of a town",
"new york was originally dutch",
"the big apple is great",
"new york is also called the big apple",
"nyc is nice",
"people abbreviate new york city as nyc",
"the capital of great britain is london",
"london is in the uk",
"london is in england",
"london is in great britain",
"it rains a lot in london",
"london hosts the british museum",
"new york is great and so is london",
"i like london better than new york"])
y_train_text = [["new york"],["new york"],["new york"],["new york"],["new york"],
["new york"],["london"],["london"],["london"],["london"],
["london"],["london"],["new york","london"],["new york","london"]]
X_test = np.array(['nice day in nyc',
'welcome to london',
'london is rainy',
'it is raining in britian',
'it is raining in britian and the big apple',
'it is raining in britian and nyc',
'hello welcome to new york. enjoy it here and london too'])
target_names = ['New York', 'London']
lb = MultiLabelBinarizer()
Y = lb.fit_transform(y_train_text)
classifier = Pipeline([
('tfidf', TfidfVectorizer()),
('clf', OneVsRestClassifier(LinearSVC()))])
classifier.fit(X_train, Y)
predicted = classifier.predict_proba(X_test)
This gives me probability values of labels for every X_test value
Now when i tried lb.inverse_transform(predicted[0])
to get the actual labels of first X_test, it didn't work.
Any help guys, what I am doing wrong and how to get desired results.
Note:The above is dummy data but i have 500 labels
out of which each particular text can have not more than 5 labels
.