python 13 lines · 1 tab

Text vectorization with TF-IDF for strong classical baselines

1 tab
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report

pipeline = Pipeline([
    ('vectorizer', TfidfVectorizer(max_features=30_000, ngram_range=(1, 2), min_df=3)),
    ('model', LogisticRegression(max_iter=2000, class_weight='balanced')),
])

pipeline.fit(train_texts, train_labels)
predictions = pipeline.predict(valid_texts)
print(classification_report(valid_labels, predictions, digits=3))
1 file · python Explain with highlit

Before I fine-tune transformers, I almost always try a TF-IDF baseline. It is fast, interpretable, and often surprisingly competitive for moderate text classification tasks. If a linear model over sparse features is already good enough, that is usually the correct production choice.


Related snips

python
from gensim.models import Word2Vec

sentences = [
    ['customer', 'refund', 'payment', 'issue'],
    ['login', 'authentication', 'password', 'reset'],
    ['delivery', 'shipment', 'tracking', 'delay'],

Word embeddings with gensim for semantic similarity tasks

word-embeddings gensim nlp
by Dr. Elena Vasquez 1 tab
python
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_absolute_error, root_mean_squared_error

models = {
    'linear': LinearRegression(),
    'ridge': Ridge(alpha=1.0),

Regression workflows with linear, ridge, lasso, and elastic net

scikit-learn regression ridge
by Dr. Elena Vasquez 1 tab
python
from datasets import Dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer

model_name = 'distilbert-base-uncased'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=3)

Fine tuning transformer models for domain text classification

hugging-face fine-tuning transformers
by Dr. Elena Vasquez 1 tab
python
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

Encoding categorical variables without creating leakage

categorical-encoding preprocessing scikit-learn
by Dr. Elena Vasquez 1 tab
python
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline

model_name = 'distilbert-base-uncased-finetuned-sst-2-english'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

Using Hugging Face transformers for modern NLP inference

hugging-face transformers nlp
by Dr. Elena Vasquez 1 tab
python
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier

grid_search = GridSearchCV(
    estimator=RandomForestClassifier(random_state=42, n_jobs=-1),
    param_grid={

Hyperparameter tuning with GridSearchCV and randomized search

hyperparameter-tuning gridsearch scikit-learn
by Dr. Elena Vasquez 1 tab

Share this code

Here's the card — post it anywhere.

Text vectorization with TF-IDF for strong classical baselines — share card
Link copied