将 ai.classify 与 pandas 配合使用

ai.classify 函数使用生成 AI 根据所选的自定义标签对输入文本进行分类,只需一行代码。

注释

  • 本文介绍如何将 ai.classify 与 pandas 配合使用。 若要将 ai.classify 与 PySpark 配合使用,请参阅 本文
  • 请参阅 本概述文章中的其他 AI 函数。
  • 了解如何自定义 AI 函数的配置

概述

ai.classify 函数扩展了 pandas Series 类。 若要将用户提供的标签分配给每个输入行,请对 pandas DataFrame 的文本列调用函数。

该函数返回一个 pandas Series,其中包含分类标签,该标签可以存储在新的 DataFrame 列中。

小窍门

建议将 ai.classify 函数与至少两个输入标签一起使用。

Syntax

df["classification"] = df["input"].ai.classify("category1", "category2", "category3")

参数

Name Description
labels
必选
一个或多个 字符串 ,表示要与输入文本值匹配的分类标签集。

退货

该函数返回一个 pandas Series ,其中包含每个输入文本行的分类标签。 如果无法对文本值进行分类,则对应的标签为 null

Example

# This code uses AI. Always review output for mistakes. 
# Read terms: https://azure.microsoft.com/support/legal/preview-supplemental-terms/.

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)

此示例代码单元提供以下输出:

数据帧的屏幕截图,其中包含“说明”和“类别”列。“category”列列出每个说明的类别名称。