机器学习新手求教:如何使用Logistic Regression实现文本分类?
Hey there! Glad you're diving into text classification with Logistic Regression—great pick for a beginner since it’s intuitive, efficient, and works surprisingly well for many text tasks. Let’s walk through this step by step, using Python (the standard for ML work) to turn your 200 training text files into a working classifier.
Raw text is messy—capitalization, punctuation, and filler words like "the" or "and" don’t help the model learn patterns. We need to clean it first:
- Convert all text to lowercase to avoid treating "Hello" and "hello" as different words
- Strip out punctuation and special characters (they add no meaningful signal)
- Remove stopwords (common words that don’t carry classification weight)
- Optional: Lemmatize words (reduce them to their root form, e.g., "running" → "run") to shrink your vocabulary size
Here’s a quick preprocessing function you can use:
import nltk from nltk.corpus import stopwords from nltk.stem import WordNetLemmatizer import string # Download required NLTK resources (run once) nltk.download('stopwords') nltk.download('wordnet') def preprocess_text(text): # Lowercase everything text = text.lower() # Remove punctuation text = text.translate(str.maketrans('', '', string.punctuation)) # Split text into individual words words = text.split() # Filter out stopwords stop_words = set(stopwords.words('english')) words = [word for word in words if word not in stop_words] # Lemmatize to root words lemmatizer = WordNetLemmatizer() words = [lemmatizer.lemmatize(word) for word in words] # Rejoin words into a single string return ' '.join(words)
Assuming your training texts are stored in a folder like train_texts/ (each file is one sample) and you have a corresponding train_labels.txt (each line is the label for the matching text file), here’s how to load them:
import os # Load training texts train_dir = 'train_texts/' train_texts = [] for filename in sorted(os.listdir(train_dir)): # Sort to match label order! with open(os.path.join(train_dir, filename), 'r', encoding='utf-8') as f: raw_text = f.read() cleaned_text = preprocess_text(raw_text) train_texts.append(cleaned_text) # Load training labels with open('train_labels.txt', 'r') as f: train_labels = [int(line.strip()) for line in f] # Repeat for test data test_dir = 'test_texts/' test_texts = [] for filename in sorted(os.listdir(test_dir)): with open(os.path.join(test_dir, filename), 'r', encoding='utf-8') as f: raw_text = f.read() cleaned_text = preprocess_text(raw_text) test_texts.append(cleaned_text) with open('test_labels.txt', 'r') as f: test_labels = [int(line.strip()) for line in f]
Pro tip: Make sure your files are sorted in the same order as your labels—otherwise your model will learn wrong mappings!
Machines can’t understand text directly, so we need to convert it into numbers. TF-IDF (Term Frequency-Inverse Document Frequency) is a great choice here—it measures how important a word is to a sample relative to the entire dataset.
from sklearn.feature_extraction.text import TfidfVectorizer # Initialize TF-IDF vectorizer (limit to top 1000 most common words to avoid overfitting) tfidf_vectorizer = TfidfVectorizer(max_features=1000) # Fit the vectorizer on training data (learns word frequencies) then transform both sets X_train = tfidf_vectorizer.fit_transform(train_texts) X_test = tfidf_vectorizer.transform(test_texts) # Assign labels to variables for clarity y_train = train_labels y_test = test_labels
Now we’re ready to train the model! Scikit-learn makes this super straightforward:
from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, classification_report # Initialize the model (default parameters work well for starters) logreg_model = LogisticRegression() # Train on the training data logreg_model.fit(X_train, y_train)
Let’s see how the model does on your test set:
# Predict labels for test data y_pred = logreg_model.predict(X_test) # Calculate overall accuracy accuracy = accuracy_score(y_test, y_pred) print(f"Test Set Accuracy: {accuracy:.2f}") # Get a detailed breakdown of performance per class print("\nClassification Report:") print(classification_report(y_test, y_pred))
If your accuracy isn’t where you want it to be, try these tweaks:
- Adjust the TF-IDF parameters: Try increasing
max_features(e.g., 2000) or addingngram_range=(1,2)to include word pairs (like "machine learning") - Tune the Logistic Regression model: Use
GridSearchCVto test different values ofC(regularization strength) to prevent overfitting - Double-check your preprocessing: Maybe you missed domain-specific stopwords, or lemmatization isn’t handling your text correctly
内容的提问来源于stack exchange,提问作者mohamed Salah

