分享方式:


microsoftml.rx_predict:使用 Microsoft 機器學習模型進行評分

使用方式

microsoftml.rx_predict(model,
    data: typing.Union[revoscalepy.datasource.RxDataSource.RxDataSource,
    pandas.core.frame.DataFrame],
    output_data: typing.Union[revoscalepy.datasource.RxDataSource.RxDataSource,
    str] = None, write_model_vars: bool = False,
    extra_vars_to_write: list = None, suffix: str = None,
    overwrite: bool = False, data_threads: int = None,
    blocks_per_read: int = None, report_progress: int = None,
    verbose: int = 1,
    compute_context: revoscalepy.computecontext.RxComputeContext.RxComputeContext = None,
    **kargs)

Description

使用以 revoscalepy 資料來源訓練的 Microsoft ML 機器學習模型,報告資料框架或 revoscalepy 資料來源中每個執行個體的計分結果。

詳細資料

預設會在輸出中報告下列項目:針對二元分類器,會對三個變數計分:PredictedLabel、Score 和 Probability;針對 oneClassSvm 和迴歸分類器,則為 Score;針對多元分類器,則為 PredictedLabel,以及前面加上 Score 之每個類別的變數。

引數

model

從 Microsoft ML 模型傳回的模型資訊物件。 例如,從 rx_fast_treesrx_logistic_regression 傳回的物件。

data

revoscalepy 資料來源物件、資料框架或 .xdf 檔案的路徑。

output_data

輸出文字或 XDF 檔案名稱,或者具有寫入功能的 RxDataSource,可用來儲存已轉換的資料。 若為 None,則會傳回資料框架。 預設值為 None

write_model_vars

若為 True,除了計分變數之外,也會將模型中的變數寫入至輸出資料集。 如果來自輸入資料集的變數已在模型中轉換,也會包含轉換後的變數。 預設值是 False

extra_vars_to_write

要包含在 output_data 中的 None 或輸入資料中其他變數名稱的字元向量。 若 write_model_varsTrue,也會包含模型變數。 預設值是 None

尾碼

字元字串,指定要附加至所建立計分變數的尾碼,如果沒有尾碼,則指定 None。 預設值是 None

overwrite

若為 True,則會覆寫現有的 output_data;若為 False,則不會覆寫現有的 output_data。 預設值是 False

data_threads

整數,指定資料管線中所需的平行處理原則程度。 若為 None,則會在內部決定使用的執行緒數目。 預設值為 None

blocks_per_read

指定要針對從資料來源讀取之每個資料區塊讀取的區塊數目。

report_progress

指定資料列處理進度報告層級的整數值:

  • 0:未報告進度。

  • 1:已列印和更新處理的資料列數目。

  • 2:報告已處理的資料列數目與時間。

  • 3:報告已處理的資料列數目與所有時間。

預設值是 1

verbose

指定所需輸出數量的整數值。 若為 0,則計算期間不會列印任何詳細資訊輸出。 整數值 14 提供越來越多的資訊量。 預設值是 1

compute_context

設定用來執行計算的內容,以有效的 revoscalepy.RxComputeContext 指定。 目前支援本機和 revoscalepy.RxInSqlServer 計算內容。

kargs

傳送至計算引擎的其他引數。

傳回

資料框架或 revoscalepy.RxDataSource 物件,代表建立的輸出資料。 根據預設,計分二元分類器的輸出會包含三個變數:PredictedLabelScoreProbabilityrx_oneclass_svm 和迴歸會包含一個變數:Score;而多元分類器則會包含 PredictedLabel,以及前面加上 Score 之每個類別的變數。 若提供 suffix,則會將其新增至這些輸出變數名稱的結尾。

另請參閱

rx_featurizerevoscalepy.rx_data_steprevoscalepy.rx_import

二元分類範例

'''
Binary Classification.
'''
import numpy
import pandas
from microsoftml import rx_fast_linear, rx_predict
from revoscalepy.etl.RxDataStep import rx_data_step
from microsoftml.datasets.datasets import get_dataset

infert = get_dataset("infert")

import sklearn
if sklearn.__version__ < "0.18":
    from sklearn.cross_validation import train_test_split
else:
    from sklearn.model_selection import train_test_split

infertdf = infert.as_df()
infertdf["isCase"] = infertdf.case == 1
data_train, data_test, y_train, y_test = train_test_split(infertdf, infertdf.isCase)

forest_model = rx_fast_linear(
    formula=" isCase ~ age + parity + education + spontaneous + induced ",
    data=data_train)
    
# RuntimeError: The type (RxTextData) for file is not supported.
score_ds = rx_predict(forest_model, data=data_test,
                     extra_vars_to_write=["isCase", "Score"])
                     
# Print the first five rows
print(rx_data_step(score_ds, number_rows_read=5))

輸出:

Automatically adding a MinMax normalization transform, use 'norm=Warn' or 'norm=No' to turn this behavior off.
Beginning processing data.
Rows Read: 186, Read Time: 0, Transform Time: 0
Beginning processing data.
Beginning processing data.
Rows Read: 186, Read Time: 0.001, Transform Time: 0
Beginning processing data.
Beginning processing data.
Rows Read: 186, Read Time: 0.001, Transform Time: 0
Beginning processing data.
Using 2 threads to train.
Automatically choosing a check frequency of 2.
Auto-tuning parameters: maxIterations = 8064.
Auto-tuning parameters: L2 = 2.666837E-05.
Auto-tuning parameters: L1Threshold (L1/L2) = 0.
Using best model from iteration 590.
Not training a calibrator because it is not needed.
Elapsed time: 00:00:00.6058289
Elapsed time: 00:00:00.0084728
Beginning processing data.
Rows Read: 62, Read Time: 0, Transform Time: 0
Beginning processing data.
Elapsed time: 00:00:00.0302359
Finished writing 62 rows.
Writing completed.
Rows Read: 5, Total Rows Processed: 5, Total Chunk Time: 0.001 seconds 
  isCase PredictedLabel     Score  Probability
0  False           True  0.576775     0.640325
1  False          False -2.929549     0.050712
2   True          False -2.370090     0.085482
3  False          False -1.700105     0.154452
4  False          False -0.110981     0.472283

迴歸範例

'''
Regression.
'''
import numpy
import pandas
from microsoftml import rx_fast_trees, rx_predict
from revoscalepy.etl.RxDataStep import rx_data_step
from microsoftml.datasets.datasets import get_dataset

airquality = get_dataset("airquality")

import sklearn
if sklearn.__version__ < "0.18":
    from sklearn.cross_validation import train_test_split
else:
    from sklearn.model_selection import train_test_split

airquality = airquality.as_df()


######################################################################
# Estimate a regression fast forest
# Use the built-in data set 'airquality' to create test and train data

df = airquality[airquality.Ozone.notnull()]
df["Ozone"] = df.Ozone.astype(float)

data_train, data_test, y_train, y_test = train_test_split(df, df.Ozone)

airFormula = " Ozone ~ Solar_R + Wind + Temp "

# Regression Fast Forest for train data
ff_reg = rx_fast_trees(airFormula, method="regression", data=data_train)

# Put score and model variables in data frame
score_df = rx_predict(ff_reg, data=data_test, write_model_vars=True)
print(score_df.head())

# Plot actual versus predicted values with smoothed line
# Supported in the next version.
# rx_line_plot(" Score ~ Ozone ", type=["p", "smooth"], data=score_df)

輸出:

'unbalanced_sets' ignored for method 'regression'
Not adding a normalizer.
Making per-feature arrays
Changing data from row-wise to column-wise
Beginning processing data.
Rows Read: 87, Read Time: 0.001, Transform Time: 0
Beginning processing data.
Warning: Skipped 4 instances with missing features during training
Processed 83 instances
Binning and forming Feature objects
Reserved memory for tree learner: 22620 bytes
Starting to train ...
Not training a calibrator because it is not needed.
Elapsed time: 00:00:00.0390764
Elapsed time: 00:00:00.0080750
Beginning processing data.
Rows Read: 29, Read Time: 0.001, Transform Time: 0
Beginning processing data.
Elapsed time: 00:00:00.0221875
Finished writing 29 rows.
Writing completed.
   Solar_R  Wind  Temp      Score
0    290.0   9.2  66.0  33.195541
1    259.0  15.5  77.0  20.906796
2    276.0   5.1  88.0  76.594643
3    139.0  10.3  81.0  31.668842
4    236.0  14.9  81.0  43.590839