microsoftml.categorical: 텍스트 열을 카테고리로 변환합니다

Usage

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 텍스트 열을 대상으로 한 데이터 세트를 통과하여 범주 사전을 만듭니다. 각 행에 대해 입력 열에 나타나는 전체 텍스트 문자열이 범주로 정의됩니다. 범주 변환의 출력은 지시 벡터입니다. 이 벡터의 각 슬롯은 사전의 범주에 해당하므로 길이는 기본 제공 사전의 크기입니다. 범주 변환은 하나 이상의 열에 적용할 수 있으며, 이 경우 적용된 각 열마다 별도의 사전을 생성합니다.

categorical 현재 요인 데이터 처리에 지원되지 않습니다.

Arguments

cols

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

output_kind

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

  • "Bag": 다중 집합 벡터를 출력합니다. 입력 열이 범주 벡터라면, 출력은 하나의 벡터를 포함하며, 각 슬롯의 값은 입력 벡터에서 해당 범주가 발생하는 횟수입니다. 입력 열에 단일 범주가 있을 경우, 지시 벡터와 백 벡터는 동등하다

  • "Ind": 지시 벡터를 출력합니다. 입력 열은 범주 벡터이며, 출력은 입력 열의 각 슬롯마다 하나의 지시 벡터를 포함합니다.

  • "Key": 는 지수를 출력합니다. 출력은 범주의 정수 ID(1과 사전의 범주 수 사이)입니다.

  • "Bin": 는 범주의 이진 표현인 벡터를 출력합니다.

기본값은 "Ind"입니다.

max_num_terms

사전에 포함할 최대 범주 수를 지정하는 정수입니다. 기본 설정은 1000000입니다.

약관

선택적 용어 또는 범주의 문자 벡터.

sort

정렬 기준을 지정하는 문자 문자열.

  • "Occurrence": 발생 횟수별로 카테고리를 정렬하세요. 가장 흔한 것은 첫 번째입니다.

  • "Value": 값별로 분류 정렬.

text_key_values

키 값 메타데이터가 실제 입력 유형과 상관없이 텍스트여야 하는지에 관한 문제입니다.

kargs

컴퓨팅 엔진으로 전송된 추가 인수입니다.

Returns

변환을 정의하는 개체입니다.

Example

'''
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())

Output:

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