liveBook · Manning

Zero-shot learning

As explained in Starting Data Analytics with Generative AI and Python

A zero-shot learning model performs a task or makes predictions for categories without task-specific examples for those categories during inference.

Need to find positive reviews in Portuguese, but have no labeled examples for training a new classifier? Zero-shot learning lets a pretrained model classify text using candidate labels you supply at inference time. It draws on knowledge from prior training rather than examples labeled for this particular review task. section 6.2.8

A practical workflow starts by asking an AI assistant how to extract positive Portuguese reviews with a zero-shot model. Install `transformers`, then inspect and adapt the suggested Python code to your DataFrame. The example uses a Hugging Face classification pipeline with `positive` and `negative` as candidate labels; it keeps each review’s top label and filters for positive results. section 6.2.8

Listing 6.5 Sentiment analysis with a zero-shot learning model
from transformers import pipeline
import pandas as pd
# Load the zero-shot classification pipeline
classifier = pipeline("zero-shot-classification", 
model="facebook/bart-large-mnli")
# Specify the candidate labels
candidate_labels = ["positive", "negative"]
# Define a function to classify a single review
def classify_review(review):
    result = classifier(review, candidate_labels=candidate_labels, 
    hypothesis_template="This review is {}.", multi_label=False)
    return result['labels'][0]
# Apply the classification to each review
df['sentiment'] = df['review_comment_message'].apply(classify_review)
# Filter the DataFrame to only include positive reviews
positive_reviews_df = df[df['sentiment'] == 'positive']

The listing shows the model, candidate labels, hypothesis template, and DataFrame filter needed to reproduce the workflow. section 6.2.8

Do not treat a plausible label as proof of accuracy. On 500 non-empty reviews, evaluation against `review_score` gave the zero-shot model 0.87 sensitivity and 0.74 specificity. A multilingual sentiment model scored 0.79 and 0.93 respectively: the zero-shot approach found more positive reviews but was less effective at excluding non-positive ones. Test candidate approaches on your data before choosing one. section 6.2.8 section 6.2.9

Table 6.3 Quality of sentiment analysis with different pretrained models
Sentiment analysis ( n=500)
Number of reviews classified as positive
Sensitivity
Specificity
Reference review score
320 (63%)
true positives
1
1
Basic keyword search
163 (33%)
0.45
0.9
ChatGPT 4 API
250 (50%)
0.74
0.93
Suboptimal model
193 (39%)
0.56
0.93
Translated inputs
295 (59%)
0.86
0.89
Multilingual model
265 (53%)
0.79
0.93
Zero-shot learning model
325 (65%)
0.87
0.74

The comparison table places the zero-shot results alongside multilingual classification, translated inputs, keyword search, and an API-based approach. section 6.2.9

Flexibility is the attraction, not a guarantee: a single zero-shot model can tackle tasks without task-specific labeled training data, but a model fine-tuned for a nuanced domain may perform better. The named model is an example, not necessarily the best choice for every dataset. section 6.2.4 section 6.2.8

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Works through a classification pipeline, candidate labels, filtering, and evaluation; does not develop model internals.
Examples
Uses Portuguese customer reviews, an AI-generated Python workflow, and sensitivity and specificity results from 500 reviews.
What it assumes
Assumes a pandas DataFrame containing review text; teaches the required library installation and zero-shot classification setup.
What you can do after
Apply it via Hugging Face Transformers to filter positive reviews, then evaluate results against review scores before selecting a method.
How it connects
Links zero-shot classification to sentiment analysis, multilingual models, pretrained models, fine-tuning, and natural language inference.

Also explained in

Compare how 10 books explain zero-shot learning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.