定型可解釋提升機器 - 迴歸 (預覽)
在本文中,您將了解如何在 Microsoft Fabric 中使用可解釋提升機器 (EBM) 來定型迴歸模型。 EBM 是一種機器學習技術,結合了梯度提升的威力,並強調模型可解釋性。 它會建立一個決策樹,類似於梯度提升,但其獨特的重點是產生人類可讀的模型。 EBM 不僅提供精確的預測,也提供這些預測清晰直觀的說明。 它們非常適合需要了解驅動模型決策基礎因素的應用程式,例如醫療保健、財務和監管合規性。
在 SynapseML 中,您可以使用 Apache Spark 所提供可解釋提升機器的可調整實作,以定型新模型。 本教學課程會引導您利用 Apache Spark,在 Microsoft Fabric 內套用可解釋提升機器的可擴縮性和可解釋性。 搭配 Microsoft Fabric 使用可解釋提升機器目前處於預覽狀態。
重要
這項功能處於預覽狀態。
可解釋提升機器的優點
當機器學習模型的透明度和理解性至關重要時,EBM 提供獨特的可解譯性和預測能力混合,使其成為理想的選擇。 透過 EBM,使用者可以深入了解驅動預測的基礎因素,讓他們了解模型為何做出特定決策或預測,這對於在 AI 系統中建置信任至關重要。
它們能夠發掘資料中的複雜關係,同時提供清晰且可解釋的結果,使其在財務、醫療保健和詐欺偵測等領域具有寶貴價值。 在這些欄位中,模型可解釋性不僅可取,而且通常是法規要求。 最後,選擇 EBM 可以在模型效能與透明度之間取得平衡,確保 AI 解決方案準確、易於理解且可靠。
必要條件
取得 Microsoft Fabric 訂用帳戶。 或註冊免費的 Microsoft Fabric 試用版。
登入 Microsoft Fabric。
使用首頁左下方的體驗切換器,切換至 Fabric。
- 選取 +,再選取 [筆記本],然後在工作區中建立新的筆記本。
匯入 mlflow
MLflow 能讓您追蹤模型的參數和結果。 下列程式碼片段示範如何使用 MLflow 進行實驗和追蹤。
ebm-wine-quality
值是記錄資訊的實驗名稱。
# Import MLflow
import mlflow
# Create a new experiment for EBM Wine Quality
mlflow.set_experiment("ebm-wine-quality")
載入資料
下列程式碼片段會載入並準備標準葡萄酒品質資料集,以作為迴歸工作精簡但有價值的資料集。 請務必強調,轉換程序牽涉將 Pandas DataFrame (使用 as_frame 引數時由 Sklearn 傳回) 轉換為 Spark DataFrame,如 Spark ML 樣式定型器所需:
import sklearn
# Load the Wine Quality Data Set using the as_frame argument for Pandas compatibility.
bunch = sklearn.datasets.load_wine(as_frame=True)
# Extract the data into a Pandas dataframe.
pandas_df = bunch['data']
# Add the target variable to the Pandas dataframe.
pandas_df['target'] = bunch['target']
# Convert the Pandas dataframe to a Spark dataframe.
df = spark.createDataFrame(pandas_df)
# Display the resulting Spark dataframe.
display(df)
此程式碼片段示範如何載入、操作和轉換資料集,以搭配 Spark 型機器學習工作使用。
準備資料
與 Spark ML 樣式學習者一樣,在向量值資料行內組織特徵是不可或缺的。 在此情況下,請將此資料行稱為「特徵」,其中包含所載入 DataFrame 中的所有資料行,但目標變數除外:
from pyspark.ml.feature import VectorAssembler
# Define the name of the target variable column.
labelColumnName = 'target'
# Create a VectorAssembler to consolidate features.
assembler = VectorAssembler(outputCol='features')
# Specify the input columns, excluding the target column.
assembler.setInputCols([c for c in df.columns if c != labelColumnName])
# Transform the dataframe to include the 'features' column.
df_with_features = assembler.transform(df)
此程式碼片段說明如何使用 VectorAssembler 來正確建構特徵,以利後續 Spark ML 型模型化的功能。
定型模型
下列程式碼會使用 Synapse ML 程式庫來起始建立 EBM 迴歸模型的程序。 首先,初始化 EBM 迴歸估算器,指定其用於迴歸工作。 接下來,設定標籤資料行名稱,以確保模型知道要預測的資料行。 最後,將模型調整為前置處理的資料集:
# Import the EBMRegression estimator from Synapse ML.
from synapse.ml.ebm import EbmRegression
# Create an instance of the EBMRegression estimator.
estimator = EbmRegression()
# Set the label column for the regression task.
estimator.setLabelCol(labelColumnName)
# Fit the EBM regression model to the prepared dataset.
model = estimator.fit(df_with_features)
檢視全域說明
若要將模型的整體說明視覺化,您可以取得視覺效果包裝函式,並利用 interpret
程式庫的 show
方法。 視覺效果包裝函式可做為橋樑,促進模型的視覺效果體驗。 其具體做法如下:
# Get the visualization wrapper for the model.
wrap = model.getVizWrapper()
# Generate the global explanation.
explanation = wrap.explain_global()
接下來,匯入 interpret
程式庫並使用 show
方法來顯示說明:
import interpret
interpret.show(explanation)
「重要性」一詞代表每個詞彙 (特徵或互動) 對預測的平均絕對貢獻 (分數)。 這些貢獻會在定型資料集之間平均,並考慮到每個量化和樣本權數中的樣本數目 (如適用)。 前 15 個最重要的詞彙會顯示在說明中。
檢視局部說明
提供的說明位於全域層級,但在某些情況下,每個功能輸出也非常有價值。 定型器和模型都提供設定 FeaturesScoresCol
的功能,在填入時會導入另一個向量值資料行。 此資料行中的每個向量都會比對特徵資料行的長度,且每個值對應至相同索引的特徵。 這些值代表了每個特徵值對模型最終輸出的貢獻。
不同於全域說明,目前沒有與每個功能輸出的 interpret
視覺效果直接整合。 這是因為全域視覺效果主要會隨著特徵數目進行調整 (通常很小),而本機說明會隨著資料列數目進行調整 (如果為 Spark DataFrame,可能相當龐大)。
以下說明如何設定及使用 FeaturesScoresCol
:
# Set the FeaturesScoresCol to include per-feature outputs.
prediction = model.setFeatureScoresCol("featurescores").transform(df_with_features)
# For small datasets, you can collect the results to a single machine without issues.
# However, for larger datasets, caution should be exercised when collecting all rows locally.
# In this example, we convert to Pandas for easy local inspection.
predictions_pandas = prediction.toPandas()
predictions_list = prediction.collect()
為了說明目的,讓我們列印第一個範例的詳細資料:
# Extract the first example from the collected predictions.
first = predictions_list[0]
# Print the lengths of the features and feature scores.
print('Length of the features is', len(first['features']), 'while the feature scores have length', len(first['featurescores']))
# Print the values of the features and feature scores.
print('Features are', first['features'])
print('Feature scores are', first['featurescores'])
此程式碼片段示範如何存取和列印預測中第一個範例的特徵值和對應的特徵分數。 此程式碼會產生下列輸出:
Length of the features is 13 while the feature scores have length 13
Features are [14.23, 1.71, 2.43, 15.6, 127.0, 2.8, 3.06, 0.28, 2.29, 5.64, 1.04, 3.92, 1065.0]
Feature scores are [-0.06610139373422304, -0.06386890875478829, 0.006784629513340544, -0.27503132406909486, -0.017971992178296585, 0.027848245358365248, 0.08691003021839885, -0.09550122309042419, -0.0068259112648438175, -0.04053278237133137, 0.07148173894260551, 0.07739120309898403, -0.0867647572984993]