Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The ai.classify function uses generative AI to categorize input text according to custom labels you choose, with a single line of code.
Note
- This article covers using ai.classify with pandas. To use ai.classify with PySpark, see this article.
- See other AI functions in this overview article.
- Learn how to customize the configuration of AI functions.
Overview
The ai.classify function extends the pandas Series class. To assign user-provided labels to each input row, call the function on a text column of a pandas DataFrame.
The function returns a pandas Series that contains classification labels, which can be stored in a new DataFrame column.
Tip
We recommend using the ai.classify function with at least two input labels.
Syntax
df["classification"] = df["input"].ai.classify("category1", "category2", "category3")
Parameters
| Name | Description |
|---|---|
labels Required |
One or more strings that represent the set of classification labels to match to input text values. |
Returns
The function returns a pandas Series that contains a classification label for each input text row. If a text value can't be classified, the corresponding label is null.
Example
# This code uses AI. Always review output for mistakes.
df = pd.DataFrame([
"This duvet, lovingly hand-crafted from all-natural fabric, is perfect for a good night's sleep.",
"Tired of friends judging your baking? With these handy-dandy measuring cups, you'll create culinary delights.",
"Enjoy this *BRAND NEW CAR!* A compact SUV perfect for the professional commuter!"
], columns=["descriptions"])
df["category"] = df['descriptions'].ai.classify("kitchen", "bedroom", "garage", "other")
display(df)
This example code cell provides the following output:
Related content
Detect sentiment with ai.analyze_sentiment.
Generate vector embeddings with ai.embed.
Extract entities with ai_extract.
Fix grammar with ai.fix_grammar.
Answer custom user prompts with ai.generate_response.
Calculate similarity with ai.similarity.
Summarize text with ai.summarize.
Translate text with ai.translate.
Learn more about the full set of AI functions.
Customize the configuration of AI functions.
Did we miss a feature you need? Suggest it on the Fabric Ideas forum.