次の方法で共有


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 の範囲で指定する必要があります。 既定値は 16 です。

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

注文

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

invert_hash

スロット名の生成に使用できるキーの数の制限を指定する整数。 0 は反転ハッシュがないことを意味します。-1 は制限がないことを意味します。 ゼロ値を指定するとパフォーマンスが向上しますが、意味のある係数名を取得するには 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