共用方式為


在處理期間檢查中繼資料

了解如何在 ML.NET 中,於載入、處理和模型定型步驟期間檢查中繼資料。 中繼資料是機器學習服務管線中每個階段的輸出。

請考慮下列住房數據:

HousingData[] housingData = new HousingData[]
{
    new HousingData
    {
        Size = 600f,
        HistoricalPrices = new float[] { 100000f ,125000f ,122000f },
        CurrentPrice = 170000f
    },
    new HousingData
    {
        Size = 1000f,
        HistoricalPrices = new float[] { 200000f, 250000f, 230000f },
        CurrentPrice = 225000f
    },
    new HousingData
    {
        Size = 1000f,
        HistoricalPrices = new float[] { 126000f, 130000f, 200000f },
        CurrentPrice = 195000f
    },
    new HousingData
    {
        Size = 850f,
        HistoricalPrices = new float[] { 150000f,175000f,210000f },
        CurrentPrice = 205000f
    },
    new HousingData
    {
        Size = 900f,
        HistoricalPrices = new float[] { 155000f, 190000f, 220000f },
        CurrentPrice = 210000f
    },
    new HousingData
    {
        Size = 550f,
        HistoricalPrices = new float[] { 99000f, 98000f, 130000f },
        CurrentPrice = 180000f
    }
};

在 ML.NET 中,您可以透過各種方式檢查如本文所述載入至 IDataView 的中間數據,詳細資訊請參見以下各節。

將 IDataView 轉換成 IEnumerable

其中一個檢查 IDataView 最快速的方式便是將它轉換成 IEnumerable。 若要進行這項轉換,請使用 CreateEnumerable 方法。

若要最佳化效能,請將 reuseRowObject 設為 true。 這麼做會以目前數據列的數據填入相同的物件,而不是為數據集中的每個數據列建立新的物件。

// Create an IEnumerable of HousingData objects from IDataView
IEnumerable<HousingData> housingDataEnumerable =
    mlContext.Data.CreateEnumerable<HousingData>(data, reuseRowObject: true);

// Iterate over each row
foreach (HousingData row in housingDataEnumerable)
{
    // Do something (print out Size property) with current Housing Data object being evaluated
    Console.WriteLine(row.Size);
}

使用 IEnumerable 存取特定索引

若您只需要存取一部分的資料或特定索引,請使用 CreateEnumerable,並將 reuseRowObject 參數的值設為 false,為資料集中所要求的每個資料列建立新物件。 接著,將 IEnumerable 轉換成陣列或清單。

警告

CreateEnumerable 的結果轉換成數位或清單,會將所有所要求的 IDataView 數據列載入記憶體,這可能會影響效能。

一旦所有集合都已建立完成,您便可以在資料上執行作業。 下列代碼段會採用數據集中的前三個數據列,並計算目前的平均價格。

// Create an Array of HousingData objects from IDataView
HousingData[] housingDataArray =
    mlContext.Data.CreateEnumerable<HousingData>(data, reuseRowObject: false)
        .Take(3)
        .ToArray();

// Calculate Average CurrentPrice of First Three Elements
HousingData firstRow = housingDataArray[0];
HousingData secondRow = housingDataArray[1];
HousingData thirdRow = housingDataArray[2];
float averageCurrentPrice = (firstRow.CurrentPrice + secondRow.CurrentPrice + thirdRow.CurrentPrice) / 3;

檢查單一資料行中的值

在模型建置流程的任何一個時間點,IDataView 單一資料行中的值可透過使用 GetColumn 方法進行存取。 GetColumn 方法會將單一資料行中的所有值作為 IEnumerable 傳回。

IEnumerable<float> sizeColumn = data.GetColumn<float>("Size").ToList();

一次檢查一列 IDataView 值

IDataView 會延遲評估。 若要逐一查看 IDataView,而不將其轉換成 IEnumerable(如本文件先前章節所示範),請使用 DataViewRowCursor 方法並將您 GetRowCursorDataViewSchema 作為參數傳遞,來建立 IDataView。 然後,若要逐一查看資料列,請使用 MoveNext 指標方法,搭配 ValueGetter 委派來從每個資料行擷取個別的值。

重要

基於效能考慮,ML.NET 中的向量會使用 VBuffer,而不是原生集合類型 (也就是 Vectorfloat[])。

// Get DataViewSchema of IDataView
DataViewSchema columns = data.Schema;

// Create DataViewCursor
using (DataViewRowCursor cursor = data.GetRowCursor(columns))
{
    // Define variables where extracted values will be stored to
    float size = default;
    VBuffer<float> historicalPrices = default;
    float currentPrice = default;

    // Define delegates for extracting values from columns
    ValueGetter<float> sizeDelegate = cursor.GetGetter<float>(columns[0]);
    ValueGetter<VBuffer<float>> historicalPriceDelegate = cursor.GetGetter<VBuffer<float>>(columns[1]);
    ValueGetter<float> currentPriceDelegate = cursor.GetGetter<float>(columns[2]);

    // Iterate over each row
    while (cursor.MoveNext())
    {
        //Get values from respective columns
        sizeDelegate.Invoke(ref size);
        historicalPriceDelegate.Invoke(ref historicalPrices);
        currentPriceDelegate.Invoke(ref currentPrice);
    }
}

預覽數據子集的前置處理或定型結果

警告

請不要在生產程式碼中使用 Preview,因為它旨在用於偵錯,可能會降低效能。

模型建置流程是實驗性且反覆性的。 若要查看數據子集在預先處理或訓練機器學習模型後的效果,請使用可傳回 DataDebuggerPreviewPreview 方法。 結果是具有 ColumnViewRowView 屬性的對象,這些屬性既是 IEnumerable,又包含特定數據行或數據列中的值。 使用 maxRows 參數指定要套用轉換的資料列數。

資料偵錯工具預覽物件

檢查 IDataView 的結果看起來類似下圖:

資料偵錯工具預覽資料列檢視