Text Classifier learns the characteristics of each label from the training data to assign labels to new data.

They generally feature:

  1. Input

    • document,
    • predefined set of classes
  2. Output

    • predefined class

Here are the ML models for text classification:

Classification predicts a category. Regression predicts a numerical target, such as a review score. Logistic regression is a classifier despite its name.

Classification needs labels. When there are no labels, documents are grouped by similarity instead, which is what K-Means Clustering, Hierarchical Clustering (HC) and Fuzzy Clustering do. Once a classifier has produced an output, its quality is measured with Evaluation Metrics.


Code

From text to predicted labels

from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
 
texts = ["good fun film", "good story", "bad dull film", "bad story"]
labels = [1, 1, 0, 0]
classifier = make_pipeline(CountVectorizer(), MultinomialNB())
classifier.fit(texts, labels)
print(classifier.predict(["good film", "bad film"]).tolist())  # [1, 0]

Read the pipeline:

  • texts contains four training documents; labels contains their sentiment classes
  • CountVectorizer learns the vocabulary and converts documents into count rows
  • MultinomialNB learns from those rows and their labels
  • fit(texts, labels) performs training
  • predict applies the same fitted vocabulary to the new documents before classification
  • predict also uese the same pipeline under make_pipeline as training
  • Each new document produces one class label

Use a separate test set to measure performance. The tiny example shows how the two processing steps connect.