共用方式為


教學課程:使用語意連結探索語意模型中的關聯性

本教學課程說明如何從 Jupyter Notebook 與 Power BI 互動,並透過 SemPy 連結庫的協助偵測數據表之間的關聯性。

在本教學課程中,您會了解如何:

  • 使用語意連結的 Python 連結庫 (SemPy) 探索語意模型中的關聯性(Power BI 數據集)。
  • 使用支援與 Power BI 整合的 SemPy 元件,並協助自動化數據品質分析。 這些元件包括:
    • FabricDataFrame - 使用其他語意信息增強的 pandas 類似結構。
    • 將語意模型從 Fabric 工作區提取至筆記本的函式。
    • 函式會自動化功能相依性的假設評估,以及識別語意模型中關聯性違規的函式。

必要條件

  • 從左側瀏覽窗格中選取 [工作區 ],以尋找並選取您的工作區。 此工作區會變成您目前的工作區。

  • 網狀架構範例 GitHub 存放庫下載客戶獲利率範例.pbix客戶獲利率範例 (auto.pbix 語意模型),並將其上傳至您的工作區。

在筆記本中跟著

powerbi_relationships_tutorial.ipynb 筆記本會伴隨本教學課程。

若要開啟本教學課程隨附的筆記本,請遵循準備系統以進行數據科學教學課程中的指示,將筆記本匯入您的工作區。

如果您想要複製並貼上此頁面中的程式碼,您可以 建立新的筆記本

開始執行程序代碼之前,請務必將 Lakehouse 附加至筆記本

設定筆記本

在本節中,您會使用必要的模組和數據來設定筆記本環境。

  1. 使用%pip筆記本內的內嵌安裝功能從 PyPI 安裝SemPy

    %pip install semantic-link
    
  2. 執行稍後需要的必要 SemPy 模組匯入:

    import sempy.fabric as fabric
    
    from sempy.relationships import plot_relationship_metadata
    from sempy.relationships import find_relationships
    from sempy.fabric import list_relationship_violations
    
  3. 匯入 pandas 以強制執行可協助輸出格式設定的組態選項:

    import pandas as pd
    pd.set_option('display.max_colwidth', None)
    

探索語意模型

本教學課程使用標準範例語意模型客戶獲利率 Sample.pbix 如需語意模型的描述,請參閱 Power BI 的客戶獲利率範例。

  • 使用 SemPy 的 函 list_datasets 式來探索您目前工作區中的語意模型:

    fabric.list_datasets()
    

在此筆記本的其餘部分,您會使用兩個版本的客戶獲利率範例語意模型:

  • 客戶獲利率範例:語意模型來自具有預先定義數據表關聯性的Power BI範例
  • 客戶獲利率範例 (auto):相同的數據,但關聯性僅限於 Power BI 將自動偵測的那些數據。

使用其預先定義的語意模型擷取範例語意模型

  1. 使用 SemPy 的 函式,載入預先定義並儲存在 客戶獲利率範例 語意模型中的 list_relationships 關聯性。 此函式會從表格式物件模型列出:

    dataset = "Customer Profitability Sample"
    relationships = fabric.list_relationships(dataset)
    relationships
    
  2. relationships使用 SemPy 的 函plot_relationship_metadata式,將 DataFrame 視覺化為圖形:

    plot_relationship_metadata(relationships)
    

    顯示語意模型中數據表之間關聯性繪圖的螢幕快照。

此圖表顯示此語意模型中數據表之間關聯性的「基礎真相」,因為它會反映主題專家在Power BI 中定義它們的方式。

補充關聯性探索

如果您從Power BI自動偵測到的關聯性開始,就會有一個較小的集合。

  1. 視覺化 Power BI 在語意模型中自動偵測到的關聯性:

    dataset = "Customer Profitability Sample (auto)"
    autodetected = fabric.list_relationships(dataset)
    plot_relationship_metadata(autodetected)
    

    此螢幕快照顯示Power BI在語意模型中自動偵測到的關聯性。

    Power BI 的自動偵測遺漏了許多關聯性。 此外,兩個自動偵測到的關聯性語意不正確:

    • Executive[ID] ->Industry[ID]
    • BU[Executive_id] ->Industry[ID]
  2. 將關聯性列印為資料表:

    autodetected
    

    數據表的 Industry 關聯性不正確會出現在索引 3 和 4 的數據列中。 使用這項資訊來移除這些數據列。

  3. 捨棄未正確識別的關聯性。

    autodetected.drop(index=[3,4], inplace=True)
    autodetected
    

    現在您有正確但不完整的關聯性。

  4. 使用 plot_relationship_metadata將這些不完整的關聯性視覺化:

    plot_relationship_metadata(autodetected)
    

    螢幕快照,顯示移除不正確關聯性之後的視覺效果。

  5. 使用 SemPy 的 list_tablesread_table 函式,從語意模型載入所有資料表:

    tables = {table: fabric.read_table(dataset, table) for table in fabric.list_tables(dataset)['Name']}
    
    tables.keys()
    
  6. 使用 find_relationships尋找數據表之間的關聯性,並檢閱記錄輸出,以取得此函式運作方式的一些見解:

    suggested_relationships_all = find_relationships(
        tables,
        name_similarity_threshold=0.7,
        coverage_threshold=0.7,
        verbose=2
    )
    
  7. 將新探索到的關聯性視覺化:

    plot_relationship_metadata(suggested_relationships_all)
    

    顯示新探索關聯性視覺效果的螢幕快照。

    SemPy 能夠偵測所有關聯性。

  8. exclude使用 參數,將搜尋限制為先前未識別的其他關聯性:

    additional_relationships = find_relationships(
        tables,
        exclude=autodetected,
        name_similarity_threshold=0.7,
        coverage_threshold=0.7
    )
    
    additional_relationships
    

驗證關聯性

  1. 首先,從 客戶獲利率範例 語意模型載入數據:

    dataset = "Customer Profitability Sample"
    tables = {table: fabric.read_table(dataset, table) for table in fabric.list_tables(dataset)['Name']}
    
    tables.keys()
    
  2. 使用函 list_relationship_violations 式檢查主要和外鍵值的重疊。 提供函式的 list_relationships 輸出做為 輸入給 list_relationship_violations

    list_relationship_violations(tables, fabric.list_relationships(dataset))
    

    關聯性違規提供一些有趣的見解。 例如,中的 Fact[Product Key] 七個值中有一個不存在, Product[Product Key]而這個遺漏的索引鍵是 50

探勘數據分析是一個令人興奮的程序,數據清理也是如此。 根據您查看數據的方式、您想要詢問的內容等等,數據一律會隱藏。 語意連結提供您可用來達成更多數據的新工具。

查看語意連結 /SemPy 的其他教學課程: