Create an IMDB Sentiment Classifier with Scikit‑LLM & Groq in 500 Lines
This tutorial walks through building a complete sentiment‑analysis pipeline for the IMDB movie‑review dataset using Scikit‑LLM and Groq, from environment setup and data sampling to custom text cleaning, zero‑shot LLM classification, model fitting, prediction, and evaluation with a 95% accuracy report.
Prerequisites and Environment Setup
Install the Scikit‑LLM library and configure it to use Groq’s OpenAI‑compatible endpoint.
pip install scikit-llm from skllm.config import SKLLMConfig
SKLLMConfig.set_gpt_url("https://api.groq.com/openai/v1")
SKLLMConfig.set_openai_key("YOUR-API-KEY")Data Acquisition
Load the public IMDB sentiment CSV from GitHub, then sample 500 rows for a lightweight demo.
import pandas as pd
from sklearn.model_selection import train_test_split
url = "https://raw.githubusercontent.com/Ankit152/IMDB-sentiment-analysis/master/IMDB-Dataset.csv"
df = pd.read_csv(url)
df_sampled = df.sample(n=500, random_state=42)
X = df_sampled["review"]
y = df_sampled["sentiment"] # 'positive' or 'negative'
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)Text‑Cleaning Transformer
Define a function that removes HTML tags and collapses whitespace, then wrap it with FunctionTransformer so it can be used inside a scikit‑learn pipeline.
from sklearn.preprocessing import FunctionTransformer
def clean_text_data(texts):
series = pd.Series(texts).astype(str)
cleaned = series.str.replace(r'<[^>]+>', ' ', regex=True) # strip HTML tags
cleaned = cleaned.str.strip().str.replace(r'\s+', ' ', regex=True) # collapse spaces
return cleaned.tolist()
text_cleaner = FunctionTransformer(clean_text_data)Sentiment‑Analysis Pipeline
Chain the cleaner with a zero‑shot LLM classifier that calls Groq’s Llama 3.1 8B model. The fit call does not train the model; it only registers the two class labels present in y_train.
from sklearn.pipeline import Pipeline
from skllm.models.gpt.classification.zero_shot import ZeroShotGPTClassifier
sentiment_pipeline = Pipeline([
("cleaner", text_cleaner),
("llm_classifier", ZeroShotGPTClassifier(model="custom_url::llama-3.1-8b-instant"))
])
print("Fitting the pipeline...")
sentiment_pipeline.fit(X_train, y_train)Evaluation
Run predictions on the held‑out test set and compute a scikit‑learn classification report. The sample loop prints three truncated reviews with their true and predicted labels.
from sklearn.metrics import classification_report
predictions = sentiment_pipeline.predict(X_test)
print(classification_report(y_test, predictions))
for review, actual, pred in zip(X_test[:3], y_test[:3], predictions[:3]):
short = review[:100] + "..."
print(f"Review: {short}")
print(f"Actual: {actual} | Predicted: {pred}") --- Classification Report ---
precision recall f1-score support
negative 0.95 0.97 0.96 60
positive 0.95 0.93 0.94 40
accuracy 0.95 100
macro avg 0.95 0.95 0.95 100
weighted avg 0.95 0.95 0.95 100
--- Sample Predictions ---
Review: I saw mommy...well, she wasn't exactly kissing Santa Clause; he has his hand on her thigh and wicked...
Actual: negative | Predicted: negative
Review: This entry is certainly interesting for series fans (like myself), but yet it is mostly incomprehens...
Actual: negative | Predicted: negative
Review: Ingrid Bergman (Cleo Dulaine) has never been so beautiful. Gary Cooper as "Cleent" so perfectly cast...
Actual: positive | Predicted: positiveThe pipeline achieves approximately 0.95 accuracy on the 100‑sample test split, demonstrating that a classic scikit‑learn workflow can be extended to zero‑shot LLM‑driven sentiment classification with minimal code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Data Party THU
Official platform of Tsinghua Big Data Research Center, sharing the team's latest research, teaching updates, and big data news.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
