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)
Description
文本转换,可以在训练模型前对数据进行。
详细信息
该 featurize_text 变换从给定的文本语料库中生成一组连续单词序列的计数,称为n-grams。
它有两种方式可以做到这一点:
构建一个n-gram字典,并用字典中的ID作为袋子中的索引;
对每个n-Gram进行哈希,并用哈希值作为袋子中的索引。
哈希的目的是将可变长度的文本文档转换为等长的数值特征向量,支持降维,并加快特征权重的查找速度。
文本变换应用于文本输入列。 它提供语言检测、标记化、塞字移除、文本规范化和功能生成。 默认支持以下语言:英语、法语、德语、荷兰语、意大利语、西班牙语和日语。
n-gram 以计数向量表示,向量槽对应于 n-gram(用 n_gram创建)或其哈希值(用 n_gram_hash创建)。 将nGram嵌入向量空间可以高效地比较其内容。
向量中的槽值可以通过以下因素加权:
术语频率 ——该槽位在文本中的出现次数
反文档频率 ——一个比率(相对槽频率的对数),通过确定该槽在全文中的常见或稀有度来衡量该槽所提供的信息。
术语频率-反向文档频率 ——产品术语频率和逆文档频率。
Arguments
cols
一个用于转换的变量字符串或变量列表。 如果 dict,这些键代表要创建的新变量名称。
语言
指定数据集中使用的语言。 支持以下值:
"AutoDetect"用于自动语言检测。"English""French""German""Dutch""Italian""Spanish""Japanese"
stopwords_remover
明确了使用该使用哪种停止词去除器。 支持三种选项:
无:不使用停止词去除剂。
predefined:使用预编译的语言专用停止词列表,包含 Microsoft Office 中最常见的词。custom:用户自定义的停止词列表。 它接受以下选项:stopword。
默认值为无。
大小写
采用不变文化规则的文本大小写。 取以下值:
"Lower""Upper""None"
默认值为 "Lower"。
keep_diacritics
False 去除变音符号; True 以保留变音符号。 默认值为 False。
keep_punctuations
False 去除标点符号; True 为了保留标点符号。 默认值为 True。
keep_numbers
False 去除数字; True 为了保留人数。 默认值为 True。
字典
允许列出的术语词典,接受以下选项:
term:一个可选的词项或范畴特征向量。dropUnknowns:掉落物品。sort: 规定矢量化时如何排序物品。 支持两种顺序:-
"occurrence": 物品按遇到的顺序出现。 -
"value":项目根据默认比较进行排序。 例如,文本排序会区分大小写(例如,先是'A',然后是'Z',再是'a')。
-
默认值为无。 注意,注定词列表优先于词典允许列表,因为在词典术语被允许列出之前,停止词已被删除。
word_feature_extractor
指定“特征提取参数”一词。 有两种不同的特征提取机制:
n_gram():基于计数的特征提取(相当于WordBag)。 它接受以下选项:max_num_terms和weighting。n_gram_hash():基于哈希的特征提取(相当于WordHashBag)。 它接受以下选项:hash_bits、、seedordered和invert_hash。
默认值为 n_gram。
char_feature_extractor
指定字符特征提取参数。 有两种不同的特征提取机制:
n_gram():基于计数的特征提取(相当于WordBag)。 它接受以下选项:max_num_terms和weighting。n_gram_hash():基于哈希的特征提取(相当于WordHashBag)。 它接受以下选项:hash_bits、、seedordered和invert_hash。
默认值为无。
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())
输出:
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-gram 提取器
microsoftml.n_gram:使用 n 元语法 将文本转换为特征
microsoftml.n_gram_hash:使用哈希 n 元语法 将文本转换为特征