Share via


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)

Description

모델을 학습시키기 전에 데이터에서 수행할 수 있는 범주 변환입니다.

세부 정보

categorical 변환은 텍스트 열에서 작동하는 데이터 세트를 통과하여 범주 사전을 작성합니다. 각 행에 대해 입력 열에 나타나는 전체 텍스트 문자열이 범주로 정의됩니다. 범주 변환의 출력은 표시기 벡터입니다. 이 벡터의 각 슬롯은 사전의 범주에 해당하므로 길이는 작성된 사전의 크기입니다. 범주 변환은 하나 이상의 열에 적용할 수 있습니다. 이 경우 적용되는 각 열에 대해 별도의 사전을 작성합니다.

categorical은 현재 요소 데이터를 처리하는 데 지원되지 않습니다.

인수

cols

변환할 문자열 또는 변수 이름 목록입니다. dict이면 키는 만들 새 변수의 이름을 나타냅니다.

output_kind

출력 종류를 지정하는 문자열입니다.

  • "Bag": 다중 집합 벡터를 출력합니다. 입력 열이 범주의 벡터인 경우 출력에 하나의 벡터가 포함됩니다. 여기서 각 슬롯의 값은 입력 벡터의 범주 발생 횟수입니다. 입력 열에 단일 범주가 포함된 경우 표시기 벡터와 백 벡터가 동일합니다.

  • "Ind": 표시기 벡터를 출력합니다. 입력 열은 범주의 벡터이며 출력에는 입력 열의 슬롯당 하나의 표시기 벡터가 포함됩니다.

  • "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