pandas で ai.classify を使用する

ai.classify関数は、指定したラベルを使用して各入力行を分類します。

  • この記事では、pandas を使用して ai.classify について説明します。 PySpark については、「 PySpark で ai.classify を使用する」を参照してください。
  • すべての AI 関数と前提条件については、 AI Functions の概要に関するページを参照してください。
  • Pandas を使用して AI Functions の既定の構成を変更します。

概要

ai.classify 関数は、pandas Series クラスを拡張します。 ユーザー指定のラベルを各入力行に割り当てるには、 pandas DataFrame のテキスト列で関数を呼び出します。

この関数は、新しい DataFrame 列に格納できる分類ラベルを含む pandas Series を返します。

ヒント

ai.classify関数を少なくとも 2 つの入力ラベルと共に使用することをお勧めします。

構文

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

パラメーター

名前 Description
labels
必須
入力テキスト値に一致する分類ラベルのセットを表す 1 つ以上の 文字列

返品ポリシー

この関数は、各入力テキスト行の分類ラベルを含む pandas Series を返します。 テキスト値を分類できない場合は、対応するラベルが 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)

Output:

マルチモーダル入力

画像、PDF、またはテキスト ファイルを分類するには、入力列にファイル パス文字列が含まれている場合に column_type="path" を設定します。 サポートされているファイルの種類とセットアップについては、「 AI Functions でマルチモーダル入力を使用する」を参照してください。

# This code uses AI. Always review output for mistakes.

file_path_series = aifunc.list_file_paths("/lakehouse/default/Files")
custom_df = pd.DataFrame({"file_path": file_path_series})

custom_df["highest_degree"] = custom_df["file_path"].ai.classify(
    "Master", "PhD", "Bachelor", "Other",
)
display(custom_df)

aifunc.list_file_paths()を使用してファイル パス列を作成すると、返されたyarl.URL オブジェクトがファイル パスとして自動的に検出されます。 列にプレーンな文字列 URL が含まれている場合にのみ、 column_type="path" を指定する必要があります。

aifunc.loadを使用してファイルを DataFrame に取り込み、ファイル パス列を分類することもできます。

# This code uses AI. Always review output for mistakes.

df, schema = aifunc.load("/lakehouse/default/Files")
df["category"] = df["file_path"].ai.classify("Master", "PhD", "Bachelor", "Other")
display(df)

aifunc.loadを使用すると、自動的に検出されるyarl.URLオブジェクトがファイル パス列に含まれます。 プレーン文字列 URL の場合は、 column_type="path"設定します。