本文說明如何在 Microsoft Fabric 中使用可解釋的提升機器 (EBM) 來定型回歸模型。 EBM 是一種機器學習技術,結合了梯度提升的威力,並強調模型可解釋性。 EBM 會建立混合決策樹模型,類似於漸層提升,但具有特別的著重點在於產生人類易於閱讀的模型。 EBM 可為這些預測提供精確的預測和清楚、直覺的說明。 EBM 非常適合涉及醫療保健、財務和法規合規性的應用程式,其中了解驅動模型決策的基礎因素至關重要。
在 SynapseML 中,您可以使用由 Apache Spark 提供支持的解釋性提升機器的具可擴展性的實作,以訓練新模型。 本教學課程說明如何使用 Apache Spar,在 Microsoft Fabric 內套用可解釋提升機器的延展性和可解釋性。 使用可解釋性提升機器與 Microsoft Fabric 的功能目前正在預覽中。
重要
這項功能處於預覽狀態。
可解釋提升機器的優點
EBM 提供獨特的可解譯性和預測能力混合,當機器學習模型的透明度和理解性至關重要時,它成為理想的選擇。 透過EBM,用戶可以針對驅動預測的基礎因素建置寶貴的見解,然後他們可以瞭解模型為何做出特定決策或預測。 這對於在 AI 系統中建置信任至關重要。
他們在數據中發現複雜關聯性的能力,同時提供清楚且可解釋的結果,使得EBM在財務、醫療保健、詐騙偵測等方面非常有用。在這些領域,模型可解釋性不僅有用,而且通常是法規需求。 最後,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")
載入資料
下列代碼段會載入並準備標準 Wine Quality數據集,其可作為有用的回歸工作數據集。 它示範如何載入、操作和轉換數據集,以搭配基於Spark的機器學習任務使用。 轉換程式牽涉到將 Pandas 數據框架(使用 as_frame 自變數時由 Sklearn 傳回)轉換為 Spark 數據框架,因為 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 ML 風格的學習者來說,將特徵組織成向量值列是至關重要的。 在此案例中,請將此數據行稱為「功能」。此數據列包含已載入之數據框架中的所有資料行,但目標變數除外。 此代碼段示範如何使用 VectorAssembler 資源正確建構功能,以進行後續的 Spark ML 模型化:
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)
訓練模型
下列代碼段使用 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數據框架而言,可能會相當龐大)。
下列代碼段示範如何設定及使用 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.05929027436479602,-0.06788488062509922,-0.0385850430666259,-0.2761907140329337,-0.0423377816119861,0.03582834632321236,0.07759833436021146,-0.08428610897153033,-0.01322508472067107,-0.05477604157900576,0.08087667928468423,0.09010794901713073,-0.09521961842295387]