使用量
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-gram。
它有兩種方式可以做到這點:
建立一個 n-gram 字典,並以字典中的 ID 作為袋子中的索引;
對每個 n 個 n 個 gram 進行雜湊,並以雜湊值作為袋子中的索引。
雜湊的目的是將可變長度的文字文件轉換為等長的數值特徵向量,以支援降維,並加快特徵權重的查詢速度。
文字轉換會套用在文字輸入欄位。 它提供語言偵測、標記化、停止字移除、文字正規化及功能生成功能。 預設支援以下語言:英語、法語、德語、荷蘭語、義大利語、西班牙語和日語。
n-gram 以計數向量表示,向量槽對應於 n-gram(使用 n_gram)或其雜湊值(使用 n_gram_hash)。 將 ngram 嵌入向量空間,可以有效率地比較其內容。
向量中的槽值可依以下因素加權:
詞彙頻率 -該槽位在文本中的出現次數
反向文件頻率 - 一個比率(相對槽位頻率的對數),透過判斷該槽在整篇文本中的常見或稀有度來衡量該槽所提供的資訊。
項頻率-反向文件頻率 -產品項項、詞語頻率與反向文件頻率。
論點
科爾斯
一個字串或變數名稱清單,要轉換。 若 dict,鍵代表將建立的新變數名稱。
語言
指定資料集中所使用的語言。 支援以下數值:
"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。
字典
允許列出的詞典,接受以下選項:
term:一個可選的詞彙或類別向量。dropUnknowns:掉落物品。sort: 規定向量化時如何排序項目。 支援兩種排序:-
"occurrence": 物品依遭遇順序出現。 -
"value": 項目依預設比較排序。 例如,文字排序會區分大小寫(例如,先是「A」接著是「Z」,再是「a」)。
-
預設值是 None。 請注意,stopword 清單優先於字典允許清單,因為在字典術語被允許列出前,stopword 會被移除。
word_feature_extractor
指定特徵提取參數一詞。 特徵提取機制有兩種不同:
n_gram():基於計數的特徵擷取(相當於 WordBag)。 它接受以下選項:max_num_terms和weighting。n_gram_hash():基於雜湊的特徵擷取(相當於 WordHashBag)。 它接受以下選項:hash_bits、、seedorderedinvert_hash和 。
預設值為 n_gram。
char_feature_extractor
指定字符特徵擷取參數。 特徵提取機制有兩種不同:
n_gram():基於計數的特徵擷取(相當於 WordBag)。 它接受以下選項:max_num_terms和weighting。n_gram_hash():基於雜湊的特徵擷取(相當於 WordHashBag)。 它接受以下選項:hash_bits、、seedorderedinvert_hash和 。
預設值是 None。
vector_normalizer
透過重新縮放到單位範數,逐一正規化向量(列)。 取以下其中一個值:
"None""L2""L1""LInf"
預設值為 "L2"。
卡格族
額外參數送入計算引擎。
Returns
一個定義轉換的物件。
範例
'''
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-gram 將文字轉換成特徵
microsoftml.n_gram_hash:使用哈希 n-gram 將文字轉換成特徵