Usage
microsoftml.featurize_text(cols: [str, dict, list], language: ['AutoDetect',
'English', 'French', 'German', 'Dutch', 'Italian', 'Spanish',
'Japanese'] = 'English', stopwords_remover=None, case: ['Lower',
'Upper', 'None'] = 'Lower', keep_diacritics: bool = False,
keep_punctuations: bool = True, keep_numbers: bool = True,
dictionary: dict = None, word_feature_extractor={'Name': 'NGram',
'Settings': {'Weighting': 'Tf', 'MaxNumTerms': [10000000],
'NgramLength': 1, 'AllLengths': True, 'SkipLength': 0}},
char_feature_extractor=None, vector_normalizer: ['None', 'L1', 'L2',
'LInf'] = 'L2', **kargs)
설명
모델 학습 전에 데이터에 대해 수행할 수 있는 텍스트 변환.
세부 정보
변환은 featurize_text 주어진 텍스트 말뭉치에서 연속된 단어들의 연속 숫자 백(n-그램)을 생성합니다.
이를 실현하는 방법은 두 가지가 있습니다:
n그램 사전을 만들고 사전의 ID를 가방 내 인덱스로 사용하세요;
각 n-그램을 해시하고 해시 값을 백 내 인덱스로 사용하세요.
해싱의 목적은 가변 길이의 텍스트 문서를 동일한 길이의 수치 특징 벡터로 변환하고, 차원 감소를 지원하며, 특징 가중치 조회를 더 빠르게 하는 것입니다.
텍스트 변환은 텍스트 입력 열에 적용됩니다. 언어 감지, 토큰화, 스톱워드 제거, 텍스트 정규화 및 기능 생성 기능을 제공합니다. 기본적으로 영어, 프랑스어, 독일어, 네덜란드어, 이탈리아어, 스페인어, 일본어를 지원합니다.
n그램은 카운트 벡터로 표현되며, 벡터 슬롯은 n그램(로 생성 n_gram)이나 해시(로 생성 n_gram_hash)에 대응합니다. 벡터 공간에 ngram을 삽입하면 그 내용을 효율적으로 비교할 수 있습니다.
벡터 내 슬롯 값은 다음과 같은 요소들로 가중치를 부여할 수 있습니다:
용어 빈도 - 텍스트 내 슬롯의 등장 횟수입니다
역문서 빈도 - 슬롯이 제공하는 정보를 전체 텍스트에서 얼마나 흔한지, 희귀한지 측정하는 비율(역상대 슬롯 빈도의 로그)입니다.
항 빈도-역문서 빈도 - 곱항 용어 빈도와 역문서 빈도.
Arguments
cols
변환할 변수 이름 문자열 또는 목록입니다. 만약 이면 dict, 키는 새로 생성할 변수들의 이름을 나타냅니다.
language
데이터 세트에서 사용되는 언어를 지정합니다. 지원되는 값은 다음과 같습니다.
"AutoDetect": 자동 언어 감지를 위한 것입니다."English""French""German""Dutch""Italian""Spanish""Japanese"
stopwords_remover
사용할 스톱워드 제거제를 명시합니다. 지원되는 세 가지 옵션이 있습니다:
없음: 스톱워드 제거제는 사용하지 않습니다.
predefined: Microsoft Office에서 가장 자주 쓰는 단어들을 포함하는 사전 컴파일된 언어별 스톱워드 목록이 사용됩니다.custom: 사용자가 정의한 스톱워드 목록입니다. 다음 옵션을 수용합니다:stopword.
기본 값은 None입니다.
케이스
불변 문화 규칙을 이용한 텍스트 케이싱. 다음과 같은 값이 있습니다:
"Lower""Upper""None"
기본값은 "Lower"입니다.
keep_diacritics
False 발음 부호를 제거하고; True 발음 부호를 유지하기 위해서. 기본값은 False입니다.
keep_punctuations
False 구두점 제거; True 구두점을 유지하기 위해서입니다. 기본값은 True입니다.
keep_numbers
False 숫자를 제거하고; True 숫자를 유지하기 위해서였다. 기본값은 True입니다.
dictionary
다음과 같은 옵션을 허용하는 허용 명시된 용어 사전:
term: 선택적 용어 또는 범주의 문자 벡터입니다.dropUnknowns: 아이템을 떨어뜨리세요.sort벡터화 시 아이템 순서를 지정합니다. 두 가지 순서가 지원됩니다:-
"occurrence": 아이템은 만난 순서대로 나타납니다. -
"value": 항목들은 기본 비교에 따라 정렬됩니다. 예를 들어, 텍스트 정렬은 대소문자에 구분됩니다(예: 'A', 'Z', 'a').
-
기본 값은 None입니다. 스톱워드 목록이 사전 허용 목록보다 우선임을 유의하는데, 이는 사전 용어가 허용되기 전에 스톱워드가 제거되기 때문입니다.
word_feature_extractor
특징 추출 인자라는 단어를 명시합니다. 특징 추출 메커니즘에는 두 가지가 있습니다:
n_gram(): 카운트 기반 특징 추출(워드백과 동등). 다음과 같은 선택지를 받아들입니다:max_num_terms와weighting.n_gram_hash(): 해싱 기반 특징 추출(WordHashBag과 동등). 다음과 같은 선택지를 받아들입니다:hash_bits,seed,ordered그리고invert_hash.
기본값은 n_gram입니다.
char_feature_extractor
차자 특징 추출 논자를 명시합니다. 특징 추출 메커니즘에는 두 가지가 있습니다:
n_gram(): 카운트 기반 특징 추출(워드백과 동등). 다음과 같은 선택지를 받아들입니다:max_num_terms와weighting.n_gram_hash(): 해싱 기반 특징 추출(WordHashBag과 동등). 다음과 같은 선택지를 받아들입니다:hash_bits,seed,ordered그리고invert_hash.
기본 값은 None입니다.
vector_normalizer
벡터(행)를 단위 노름으로 재조정하여 개별적으로 정규화합니다. 다음 값 중 하나를 취합니다:
"None""L2""L1""LInf"
기본값은 "L2"입니다.
kargs
컴퓨팅 엔진으로 전송된 추가 인수입니다.
Returns
변환을 정의하는 개체입니다.
Example
'''
Example with featurize_text and rx_logistic_regression.
'''
import numpy
import pandas
from microsoftml import rx_logistic_regression, featurize_text, rx_predict
from microsoftml.entrypoints._stopwordsremover_predefined import predefined
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"]))
out_model = rx_logistic_regression("like ~ review_tran",
data=train_reviews,
ml_transforms=[
featurize_text(cols=dict(review_tran="review"),
stopwords_remover=predefined(),
keep_punctuations=False)])
# Use the model to score.
score_df = rx_predict(out_model, data=test_reviews, extra_vars_to_write=["review"])
print(score_df.head())
Output:
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.
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: 11
improvement criterion: Mean Improvement
L1 regularization selected 3 of 11 weights.
Not training a calibrator because it is not needed.
Elapsed time: 00:00:00.3725934
Elapsed time: 00:00:00.0131199
Beginning processing data.
Rows Read: 10, Read Time: 0, Transform Time: 0
Beginning processing data.
Elapsed time: 00:00:00.0635453
Finished writing 10 rows.
Writing completed.
review PredictedLabel Score Probability
0 This is great True 0.443986 0.609208
1 I hate it False -0.668449 0.338844
2 Love it True 0.994339 0.729944
3 Really like it True 0.443986 0.609208
4 I hate it False -0.668449 0.338844
N-그램 추출기
microsoftml.n_gram: n-gram 사용하여 텍스트를 기능으로 변환
microsoftml.n_gram_hash: 해시된 n-gram 사용하여 텍스트를 기능으로 변환
스톱워드 제거기
microsoftml.custom
제거합니다.: 사용자 지정 중지 단어 microsoftml.predefined
제거합니다.: 미리 정의된 중지 단어
관련 콘텐츠
- microsoftml.n_gram: n-gram 사용하여 텍스트를 기능으로 변환
- microsoftml.n_gram_hash: 해시된 n-gram 사용하여 텍스트를 기능으로 변환
- microsoftml.custom
제거합니다.: 사용자 지정 중지 단어 - microsoftml.predefined
제거합니다.: 미리 정의된 중지 단어 - microsoftml.get_sentiment: 감정 분석