microsoftml.categorical_hash: テキスト列をハッシュし、カテゴリに変換する

使用法

microsoftml.categorical_hash(cols: [str, dict, list],
    hash_bits: int = 16, seed: int = 314489979,
    ordered: bool = True, invert_hash: int = 0,
    output_kind: ['Bag', 'Ind', 'Key', 'Bin'] = 'Bag', **kargs)

説明

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

説明

categorical_hash では、値をハッシュし、バッグのインデックスとしてハッシュを使用することで、カテゴリ値をインジケーター配列に変換します。 入力列がベクトルの場合、それに対して 1 つのインジケーター バッグが返されます。 categorical_hash では、現在、係数データの処理はサポートされていません。

引数

cols

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

hash_bits

ハッシュするビットの数を指定する整数。 1 から 30 までの数にする必要があります (1 と 30 も含まれます)。 既定値は 16 です。

seed

ハッシュ シードを指定する整数。 既定値は 314489979 です。

ordered

各用語の位置をハッシュに含める場合は True。 それ以外の場合は False。 既定値は True です。

invert_hash

スロット名を生成するために使用できるキー数の制限を指定する整数。 0 は逆ハッシュがないことを意味し、-1 は制限がないことを意味します。 0 の値を使用するとパフォーマンスが向上しますが、意味のある係数の名前を取得するには 0 以外の値が必要です。 既定値は 0 です。

output_kind

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

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

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

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

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

既定値は "Bag" です。

kargs

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

戻り値

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

関連項目

categorical

'''
Example on rx_logistic_regression and categorical_hash.
'''
import numpy
import pandas
from microsoftml import rx_logistic_regression, categorical_hash, rx_predict
from microsoftml.datasets.datasets import get_dataset

movie_reviews = get_dataset("movie_reviews")

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 hash transform.
out_model = rx_logistic_regression("like ~ reviewCat",
                data=train_reviews,
                ml_transforms=[categorical_hash(cols=dict(reviewCat="review"))])
                
# Weights are similar to categorical.
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())

出力:

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: 65537
improvement criterion: Mean Improvement
L1 regularization selected 3 of 65537 weights.
Not training a calibrator because it is not needed.
Elapsed time: 00:00:00.1209392
Elapsed time: 00:00:00.0190134
OrderedDict([('(Bias)', 0.2132447361946106), ('f1783', -0.7939924597740173), ('f38537', 0.1968022584915161)])
Beginning processing data.
Rows Read: 10, Read Time: 0, Transform Time: 0
Beginning processing data.
Elapsed time: 00:00:00.0284223
Finished writing 10 rows.
Writing completed.
           review PredictedLabel     Score  Probability
0   This is great           True  0.213245     0.553110
1       I hate it          False -0.580748     0.358761
2         Love it           True  0.213245     0.553110
3  Really like it           True  0.213245     0.553110
4       I hate it          False -0.580748     0.358761