在處理期間檢查中繼資料
了解如何在 ML.NET 中,於載入、處理和模型定型步驟期間檢查中繼資料。 中繼資料是機器學習服務管線中每個階段的輸出。
您可以在 ML.NET 中透過各種方式檢查與以下內容相似且會載入 IDataView
的中繼資料。
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
}
};
將 IDataView 轉換成 IEnumerable
其中一個檢查 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
(如本文件先前章節所示範),請使用 GetRowCursor
方法並將您 IDataView
的 DataViewSchema 作為參數傳遞,來建立 DataViewRowCursor
。 然後,若要逐一查看資料列,請使用 MoveNext
指標方法,搭配 ValueGetter
委派來從每個資料行擷取個別的值。
重要
基於效能考量,ML.NET 中的向量會使用 VBuffer
,而非原生集合類型 (即 Vector
、float[]
等)。
// 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
,因為它旨在用於偵錯,可能會降低效能。
模型建置流程是實驗性且反覆性的。 若要預覽預先處理之後或針對資料子集定型機器學習模型之後資料的結果,請使用 Preview
方法,該方法會傳回 DataDebuggerPreview
。 結果是一個具備 ColumnView
和 RowView
屬性的物件,這兩個屬性都是 IEnumerable
,且包含特定資料行或資料列中的值。 使用 maxRows
參數指定要套用轉換的資料列數。
檢查 IDataView
的結果看起來會與下列內容相似:
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應