0

I was wondering whether it is possible, to use a single label train-set to produce a multilabel output.

Using the modified the scikit learn example below. The train set contains a number of sentences, either labelled London or NY.

At the moment, the result using the train set, are either London or NY even for the sentences, including a reference to both cities.

Is there a way to make the algorithm produce two labels for the sentences containing both London and NY without touching the train set?

import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier
from sklearn import preprocessing

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"])
y_train_text = [["new york"],["new york"],["new york"],["new york"],["new york"],
                ["new york"],["london"],["london"],["london"],["london"],
                ["london"],["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',
                    "new york is great and so is london",
                    "i like london better than new york"])
target_names = ['New York', 'London']

lb = preprocessing.LabelBinarizer()
Y = lb.fit_transform(y_train_text)

classifier = Pipeline([
    ('vectorizer', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', OneVsRestClassifier(LinearSVC()))])

classifier.fit(X_train, Y)
predicted = classifier.predict(X_test)
all_labels = lb.inverse_transform(predicted)

for item, labels in zip(X_test, all_labels):
    print '%s => %s' % (item, ', '.join(labels))
ulrich
  • 3,547
  • 5
  • 35
  • 49
  • "make the algorithm produce two labels for the sentences containing both London and NY " I think what you want is a third label, which denotes the presence of both cities; the shape of the label vector must match that of the training vector on the 0th axis; AFIAK you can't have two labels for one data vector. Also, I'm pretty sure you'll need at least one example of that third class in your training set; in your code I don't see any examples including both cities. If you expect to be able to make a pred in a test set for a given label, you will need an example of that label in the the training – Ryan Aug 14 '15 at 12:28
  • The OP used `LabelBinarizer`, if `predicted` contains `[1, 1]`, that would denote both cities. – yangjie Aug 14 '15 at 13:14
  • @yangjie yes you are correct the question is how to make that happen with a training set which only contains single label examples. – ulrich Aug 14 '15 at 13:46

0 Answers0