使用量
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 目前不支援處理因子資料。
論點
科爾斯
一個字串或變數名稱清單,要轉換。 若 dict,鍵代表將建立的新變數名稱。
output_kind
一個字串,用來指定輸出類型的類型。
"Bag":輸出多集合向量。 若輸入欄位為類別向量,輸出包含一個向量,每個欄位的值為該類別在輸入向量中的出現次數。 若輸入欄包含單一類別,指示向量與袋向量等價"Ind"輸出指示向量。 輸入欄是類別向量,輸出欄位中每個槽位包含一個指示向量。"Key": 輸出一個索引。 輸出為類別的整數 ID(介於 1 與字典中類別數量之間)。"Bin": 輸出一個向量,該向量是該類別的二元表示。
預設值為 "Ind"。
max_num_terms
一個整數,指定字典中應包含的最大類別數量。 預設值是 1000000。
條款
詞彙或類別的可選字元向量。
排序
一個字串,用來指定排序條件。
"Occurrence":依發生次數排序分類。 最常見的是第一。"Value":依值排序類別。
text_key_values
關鍵值元資料是否應該是文字,不論實際輸入類型為何。
卡格族
額外參數送入計算引擎。
Returns
一個定義轉換的物件。
範例
'''
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