Text Classifier learns the characteristics of each label from the training data to assign labels to new data.
They generally feature:
-
Input
- document,
- predefined set of classes
-
Output
- predefined class
Here are the ML models for text classification:
- Naive Bayes (NB)
- Support Vector Machine (SVM)
- Extreme Learning Machines (ELM)
- Guassian Processes (GP)
- Logistic Regression
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:
textscontains four training documents;labelscontains their sentiment classesCountVectorizerlearns the vocabulary and converts documents into count rowsMultinomialNBlearns from those rows and their labelsfit(texts, labels)performs trainingpredictapplies the same fitted vocabulary to the new documents before classificationpredictalso uese the same pipeline undermake_pipelineas 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.