Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Text classification

Text classification

Text Classification (Text Classification) is one of the most fundamental and important tasks in Natural Language Processing (NLP). Its goal is to automatically assign a given text document to one or more predefined categories.

Basic concepts

Text classification is like a librarian in a library, who needs to categorize books and place them on the correct shelves according to their content. In computing, we need to teach machines how to understand text content and make correct classification decisions.

Application scenarios

Text classification has wide applications in modern society:

  1. Sentiment analysis: Determine whether a review is positive or negative
  2. Spam Filtering: Distinguish normal emails from spam
  3. News Classification: Categorize news into sections such as sports, finance, and technology
  4. Intent recognition: Understand the real intent behind user queries
  5. Medical Diagnosis: Classify disease types based on symptom descriptions

Basic Process of Text Classification

A complete text classification system usually includes the following steps:

1. Text Preprocessing

Text preprocessing converts raw text into a form suitable for machine learning models:

Example

import

re

import

nltk

from

nltk.

corpus

import

stopwords

from

nltk.

stem

import

PorterStemmer

def

preprocess_text

(

text

)

:

   

Convert to lowercase

    text

=

text.

lower

(

)

   

Remove special characters and numbers

    text

=

re

.

sub

(

r

'[^a-zA-Z\s]'

,

''

,

text

)

   

Tokenization

    words

=

text.

split

(

)

   

Remove stop words

    stop_words

=

set

(

stopwords.

words

(

'english'

)

)

    words

=

[

word

for

word

in

words

if

word

not

in

stop_words

]

   

Stemming

    stemmer

=

PorterStemmer

(

)

    words

=

[

stemmer.

stem

(

word

)

for

word

in

words

]

   

return

' '

.

join

(

words

)

2. Feature Extraction

Convert text into numerical feature representations; common methods include:

Method Description Advantages Disadvantages
Bag-of-Words model (BoW) Count word frequencies Simple and intuitive Ignores word order and semantics
TF-IDF Considers word importance More precise than BoW Still ignores context
Word2Vec Word Vector Representation Captures semantic relationships Cannot handle polysemy
BERT Contextual Embeddings State-of-the-art representation High computational resource requirements

3. Classification Model Selection

Choose an appropriate classification algorithm based on task requirements and data characteristics:

  1. Traditional Machine Learning Methods:

  2. Naive Bayes
  3. Support Vector Machine (SVM)
  4. Logistic Regression
  5. Random Forest
  6. - Naive Bayes - Support Vector Machine (SVM) - Logistic Regression - Random Forest
    1. Deep Learning Methods:

    2. Convolutional Neural Network (CNN)
    3. Recurrent Neural Network (RNN/LSTM)
    4. Transformer models (BERT, etc.)
    5. - Convolutional Neural Network (CNN) - Recurrent Neural Network (RNN/LSTM) - Transformer models (BERT, etc.)

      Practical Example: News Classification

      Let us demonstrate how to implement text classification using Python through a practical example. We will use the 20 Newsgroups dataset, a classic news classification dataset.

      1. Data Preparation

      Example

      from

      sklearn.

      datasets

      import

      fetch_20newsgroups

      Select 4 categories as examples

      categories

      =

      [

      'alt.atheism'

      ,

      'soc.religion.christian'

      ,

      'comp.graphics'

      ,

      'sci.med'

      ]

      Load the training set and test set

      newsgroups_train

      =

      fetch_20newsgroups

      (

      subset

      =

      'train'

      ,

      categories

      =

      categories

      )

      newsgroups_test

      =

      fetch_20newsgroups

      (

      subset

      =

      'test'

      ,

      categories

      =

      categories

      )

      print

      (

      f

      "Number of training samples: {len(newsgroups_train.data)}"

      )

      print

      (

      f

      "Number of test samples: {len(newsgroups_test.data)}"

      )

      2. Feature Extraction (TF-IDF)

      Example

      from

      sklearn.

      feature_extraction

      .

      text

      import

      TfidfVectorizer

      Create a TF-IDF vectorizer

      vectorizer

      =

      TfidfVectorizer

      (

      max_features

      =

      5000

      )

      Transform the training set and test set

      X_train

      =

      vectorizer.

      fit_transform

      (

      newsgroups_train.

      data

      )

      X_test

      =

      vectorizer.

      transform

      (

      newsgroups_test.

      data

      )

      y_train

      =

      newsgroups_train.

      target

      y_test

      =

      newsgroups_test.

      target

      3. Model Training (Logistic Regression)

      Example

      from

      sklearn.

      linear_model

      import

      LogisticRegression

      from

      sklearn.

      metrics

      import

      accuracy_score

      ,

      classification_report

      Create and train the model

      model

      =

      LogisticRegression

      (

      max_iter

      =

      1000

      )

      model.

      fit

      (

      X_train

      ,

      y_train

      )

      Predict on the test set

      y_pred

      =

      model.

      predict

      (

      X_test

      )

      Evaluate the model

      print

      (

      f

      "Accuracy: {accuracy_score(y_test, y_pred):.2f}"

      )

      print

      (

      "\nClassification Report:"

      )

      print

      (

      classification_report

      (

      y_test

      ,

      y_pred

      ,

      target_names

      =

      newsgroups_test.

      target_names

      )

      )

      4. Result Analysis

      Typical output results might look like the following:

      准确率: 0.91
      
      
      
      Classification Report:
      
                              precision    recall  f1-score   support
      
      
      
                 alt.atheism       0.90      0.87      0.89       319
      
      soc.religion.christian       0.93      0.95      0.94       389
      
               comp.graphics       0.89      0.90      0.90       396
      
                     sci.med       0.92      0.91      0.92       398
      
      
      
                    accuracy                           0.91      1502
      
                   macro avg       0.91      0.91      0.91      1502
      
                weighted avg       0.91      0.91      0.91      1502
      
      

      Advanced Techniques and Challenges

      Handling Class Imbalance

      When some classes have far more samples than others, you can try:

      1. Resampling (oversample the minority class or undersample the majority class)
      2. Use class weights
      3. Try different evaluation metrics (e.g., F1-score instead of accuracy)

      Methods to Improve Model Performance

      1. Feature Engineering:

      2. Try different n-gram ranges
      3. Add part-of-speech features
      4. Use more advanced word embeddings
      5. - Try different n-gram ranges - Add part-of-speech features - Use more advanced word embeddings
        1. Model optimization:

        2. Hyperparameter tuning
        3. Model Ensembling
        4. Try deep learning models
        5. - Hyperparameter tuning - Model ensembling - Try deep learning models
          1. Data augmentation:

          2. Back Translation (Back Translation)
          3. Synonym Replacement
          4. Generative Adversarial Network (GAN)
          5. - Back Translation (Back Translation) - Synonym Replacement - Generative Adversarial Network (GAN)

            Common challenges

            1. Multi-label Classification: A document may belong to multiple categories
            2. Domain adaptation: Model performance degrades in new domains
            3. Few-shot Learning: Situations with limited labeled data
            4. Interpretability: Understand why a model makes a particular classification decision

            Summary and Learning Path

            Text classification is a fundamental NLP task; mastering it is crucial for understanding more complex NLP applications. Recommended learning path:

            1. Start with traditional machine learning methods (e.g., TF-IDF + SVM)
            2. Understand the concepts and applications of word embeddings (Word2Vec, GloVe)
            3. Learn the application of deep learning models (CNN, LSTM) in text classification
            4. Explore transfer learning with pre-trained language models (BERT, GPT)

            Through continuous practice and trying different methods, you will be able to build powerful and practical text classification systems.