microsoftml.categorical: テキスト列をカテゴリに変換する

使用法

microsoftml.categorical(cols: [str, dict, list], output_kind: ['Bag', 'Ind',
    'Key', 'Bin'] = 'Ind', max_num_terms: int = 1000000,
    terms: int = None, sort: ['Occurrence', 'Value'] = 'Occurrence',
    text_key_values: bool = False, **kargs)

説明

モデルをトレーニングする前にデータに対して実行できるカテゴリ変換。

説明

categorical 変換では、テキスト列を操作するデータ セットを通過して、カテゴリのディクショナリを構築します。 各行について、入力列に表示されるテキスト文字列全体がカテゴリとして定義されます。 カテゴリ変換の出力はインジケーター ベクトルです。 このベクトル内の各スロットは辞書内のカテゴリに対応します。そのため、その長さは構築された辞書のサイズとなります。 カテゴリ変換は 1 つまたは複数の列に適用できます。その場合、適用される列ごとに個別の辞書が構築されます。

categorical は、現在、係数データを処理するためにサポートされていません。

引数

cols

変換する文字列または変数名のリスト。 dict の場合、キーは作成される新しい変数名を表します。

output_kind

出力の種類を指定する文字列。

  • "Bag": 複数セットのベクトルを出力します。 入力列がカテゴリのベクトルである場合、出力には 1 つのベクトルが含まれます。ここで各スロットの値は、入力ベクトル内のカテゴリの出現回数です。 入力列に 1 つのカテゴリが含まれている場合、インジケーター ベクトルとバッグ ベクトルは同等です

  • "Ind": インジケーター ベクトルを出力します。 入力列はカテゴリのベクトルであり、出力には入力列のスロットごとに 1 つのインジケーター ベクトルが含まれています。

  • "Key": インデックスを出力します。 出力はカテゴリの整数 ID (1 から、辞書内のカテゴリ数の間) です。

  • "Bin": カテゴリのバイナリ表現であるベクトルを出力します。

既定値は "Ind" です。

max_num_terms

辞書に含めるカテゴリの最大数を指定する整数。 既定値は 1000000 です。

terms

用語またはカテゴリの省略可能な文字ベクトル。

sort

並べ替え条件を指定する文字列。

  • "Occurrence": カテゴリを出現順に並べ替えます。 最も頻度の高いものが最初になります。

  • "Value": カテゴリを値で並べ替えます。

text_key_values

実際の入力の種類に関係なく、キー値のメタデータをテキストにする必要があるかどうか。

kargs

コンピューティング エンジンに送信される追加の引数。

戻り値

変換を定義するオブジェクト。

関連項目

categorical_hash

'''
Example on rx_logistic_regression and categorical.
'''
import numpy
import pandas
from microsoftml import rx_logistic_regression, categorical, rx_predict

train_reviews = pandas.DataFrame(data=dict(
    review=[
        "This is great", "I hate it", "Love it", "Do not like it", "Really like it",
        "I hate it", "I like it a lot", "I kind of hate it", "I do like it",
        "I really hate it", "It is very good", "I hate it a bunch", "I love it a bunch",
        "I hate it", "I like it very much", "I hate it very much.",
        "I really do love it", "I really do hate it", "Love it!", "Hate it!",
        "I love it", "I hate it", "I love it", "I hate it", "I love it"],
    like=[True, False, True, False, True, False, True, False, True, False,
        True, False, True, False, True, False, True, False, True, False, True,
        False, True, False, True]))
        
test_reviews = pandas.DataFrame(data=dict(
    review=[
        "This is great", "I hate it", "Love it", "Really like it", "I hate it",
        "I like it a lot", "I love it", "I do like it", "I really hate it", "I love it"]))

# Use a categorical transform: the entire string is treated as a category
out_model = rx_logistic_regression("like ~ reviewCat",
                data=train_reviews,
                ml_transforms=[categorical(cols=dict(reviewCat="review"))])
                
# Note that 'I hate it' and 'I love it' (the only strings appearing more than once)
# have non-zero weights.
print(out_model.coef_)

# Use the model to score.
source_out_df = rx_predict(out_model, data=test_reviews, extra_vars_to_write=["review"])
print(source_out_df.head())

出力:

Beginning processing data.
Rows Read: 25, Read Time: 0, Transform Time: 0
Beginning processing data.
Not adding a normalizer.
Beginning processing data.
Rows Read: 25, Read Time: 0, Transform Time: 0
Beginning processing data.
Beginning processing data.
Rows Read: 25, Read Time: 0, Transform Time: 0
Beginning processing data.
LBFGS multi-threading will attempt to load dataset into memory. In case of out-of-memory issues, turn off multi-threading by setting trainThreads to 1.
Warning: Too few instances to use 4 threads, decreasing to 1 thread(s)
Beginning optimization
num vars: 20
improvement criterion: Mean Improvement
L1 regularization selected 3 of 20 weights.
Not training a calibrator because it is not needed.
Elapsed time: 00:00:01.6550695
Elapsed time: 00:00:00.2259981
OrderedDict([('(Bias)', 0.21317288279533386), ('I hate it', -0.7937591671943665), ('I love it', 0.19668534398078918)])
Beginning processing data.
Rows Read: 10, Read Time: 0, Transform Time: 0
Beginning processing data.
Elapsed time: 00:00:00.1385248
Finished writing 10 rows.
Writing completed.
           review PredictedLabel     Score  Probability
0   This is great           True  0.213173     0.553092
1       I hate it          False -0.580586     0.358798
2         Love it           True  0.213173     0.553092
3  Really like it           True  0.213173     0.553092
4       I hate it          False -0.580586     0.358798