Understanding Multilabel Text Classification and the related process
Tags: machinelearning, python, textclassification, scikitlearn
Published: 2019-11-15 11:45:33

   Multilabel text classification and its process can confuse even the intermediate developer. Here is a basic guide on what is multilabel text classification and how to go ahead with the process.

## What is Multilabel?

Multilabel means each sample have multiple target labels. E.g. If you write some content on the quora, it automatically tags multiple topics to your content.
 
## What is text Classification?

Text Classification means a classification task with more than two classes, each label is mutually exclusive. The classification makes the assumption that each sample is assigned to one and only one label.
 
Steps of the process:

    1. Make dataset or download the dataset
    2. Preprocess dataset
    3. Feature Extraction
    4. Train model
 
### 1. Make dataset or download dataset:

We use the StackOverflow data set for this task. Which is available on kaggle. 

You can download from this link -[https://www.kaggle.com/stackoverflow/stacksample](https://www.kaggle.com/stackoverflow/stacksample)
 
### 2.Preprocess dataset:

There is a lot of effort needed in text classification for data preprocessing.

**2.1 Steps for preprocess:**

* Tokenize content
* Remove stop words
* Remove Punctuation
* Apply Lemmatization
* Apply stemming
 
There are a lot of libraries available for preprocessing. We used nltk for this.
 
First of all, we have to load the dataset. In [python](https://www.botreetechnologies.com/python), pandas are the best library to deal with large datasets.
 
Example: **NOTE - use this and it should show github code.**

{% gist https://gist.github.com/Mayur-btc/7ae090d5d6c600ec07904f7ed7110dc3.js %}

Sometimes there are some words frequently used in the text document which are not important for our task. So we have to remove those words. By using the nltk library we can create the frequency distribution of the words and remove such words which are not important for our task.
 
**Example:**

{% gist https://gist.github.com/Mayur-btc/5ce6d8d45136ac3c8220cd5f4e9c1579.js %}
 
 
In our dataset, we have to extract the text data from the HTML tag. To remove the HTML tag and extract the text from it we use the **“Beutifulsoup”** library.

**Example:**

{% gist https://gist.github.com/Mayur-btc/f8e95d44f6f118cff99d566fadc9518f.js %}
 
Great! we done data set loading task and some preprocess steps. Now we have tokenized the text and remove the stop words from the text. Also, we have to do stemming on a word.

**Stemming:** In linguistic morphology and information retrieval, stemming is the process of reducing inflected words to their word stem, base or root form—generally a written word form. For example, if the word is "run", then the inverted algorithm might automatically generate the forms "running", "runs", "runned", and "runly". 

In our task we use a list of stop words from the nltk and also use a stemming method from the nltk.

**Example:**

{% gist https://gist.github.com/Mayur-btc/6189b3d72b7df1d2ea1957422c49e49f.js %}
 
This problem is a multi-label problem. So we have to apply one method on target labels. We have to convert all label in the form of binary. Apply MultiLabelBinarizer from sklearn for this conversion.
 
**Example:**

{% gist https://gist.github.com/Mayur-btc/fad6d3c0775d358ebb1786aa7dfa754f.js %}
 
### 3.Feature Extraction:

When the input data to an algorithm is too large to be processed and it is suspected to be redundant, then it can be transformed into a reduced set of features (also named a feature vector). Determining a subset of the initial features is called feature selection. The selected features are expected to contain the relevant information from the input data so that the desired task can be performed by using this reduced representation instead of the complete initial data.

We use the sklearn library for Feature Extraction. There is lots of algorithm for that. e.g. **TfidfVectorizer** , **CountVectorizer** etc.
 
we use TfidfVectorizer for this task.
 
**3.1 What is TF-IDF?**

TF-IDF score represents the relative importance of a term in the document and the entire corpus. TF-IDF score is composed of two terms: the first computes the normalized Term Frequency (TF), the second term is the Inverse Document Frequency (IDF), computed as the logarithm of the number of documents in the corpus divided by the number of documents where the specific term appears.
 
TF(t) = (Number of times term t appears in a document) / (Total number of terms in the document)

IDF(t) = log_e(Total number of documents / Number of documents with term t in it)
 
If we want to save this TfidfVectorizer then we have to use joblib from sklearn lib. We can save vectorize in form of pickle format.

**Example:**

{% gist https://gist.github.com/Mayur-btc/087423f3b7738133be9d0c117e108989.js %}

###Read Also - [Top Expertise Requirement For A Full Stack Developer](https://www.botreetechnologies.com/blog/top-expertise-requirement-for-a-full-stack-developer)
 
### 4.Train model:

There are lots of algorithms in [machine learning](https://www.botreetechnologies.com/machine-learning-solutions) to train your model. To train our model we use OneVsRestClassifier and SVM algorithm for that.
 
**4.1 OneVsRestClassifier:**

Also known as one-vs-all, this strategy consists of fitting one classifier per class. For each classifier, the class is fitted against all the other classes. In addition to its computational efficiency (only n_classes classifiers are needed), one advantage of this approach is its interpretability. Since each class is represented by one and one classifier only, it is possible to gain knowledge about the class by inspecting its corresponding classifier. This is the most commonly used strategy for multiclass classification and is a fair default choice.

This strategy can also be used for multi label learning, where a classifier is used to predict multiple labels for instance, by fitting on a 2-d matrix in which cell [i, j] is 1 if the sample I have label j and 0 otherwise.
In the multi label learning literature, OvR is also known as the binary relevance method.
 
**4.2 LinearSVC:**

Similar to SVC with parameter kernel=’ linear’, but implemented in terms of liblinear rather than libsvm, so it has more flexibility in the choice of penalties and loss functions and should scale better to large numbers of samples.
This class supports both dense and sparse input and the multiclass support is handled according to a one-vs-the-rest scheme.
 
Sometimes we use this training model for our purpose. We can use this model in django project or create one api and use it for multi-purposes. I share some code how can we load this model again and predict the unseen data. Reach out to learn more about the [best web development company in New York](https://www.botreetechnologies.com/usa/web-development-company-new-york) for the various ways to improve or build the quality of projects and across your company.

**Example:**

{% gist https://gist.github.com/Mayur-btc/80cb85defd909097ccbade676a24d953 %}