DataOperationsCatalog.ShuffleRows 方法

定義

隨機顯示 的資料 input 列。

public Microsoft.ML.IDataView ShuffleRows (Microsoft.ML.IDataView input, int? seed = default, int shufflePoolSize = 1000, bool shuffleSource = true);
member this.ShuffleRows : Microsoft.ML.IDataView * Nullable<int> * int * bool -> Microsoft.ML.IDataView
Public Function ShuffleRows (input As IDataView, Optional seed As Nullable(Of Integer) = Nothing, Optional shufflePoolSize As Integer = 1000, Optional shuffleSource As Boolean = true) As IDataView

參數

input
IDataView

輸入資料。

seed
Nullable<Int32>

隨機種子。 如果未指定,隨機狀態會改為衍生自 MLContext

shufflePoolSize
Int32

要保留在集區中的資料列數目。 將此設定為 1 會關閉集區隨機顯示,而且 ShuffleRows(IDataView, Nullable<Int32>, Int32, Boolean) 只會以隨機順序讀取 input 來執行隨機隨機顯示。

shuffleSource
Boolean

如果 false 為 ,轉換不會嘗試以隨機順序讀取 input ,而且只會使用共用來隨機讀取。 如果 的 input 屬性為 falseCanShuffle 這個參數就沒有任何作用。

傳回

範例

using System;
using System.Collections.Generic;
using Microsoft.ML;

namespace Samples.Dynamic
{
    public static class ShuffleRows
    {
        // Sample class showing how to shuffle rows in 
        // IDataView.
        public static void Example()
        {
            // Create a new context for ML.NET operations. It can be used for
            // exception tracking and logging, as a catalog of available operations
            // and as the source of randomness.
            var mlContext = new MLContext();

            // Get a small dataset as an IEnumerable.
            var enumerableOfData = GetSampleTemperatureData(5);
            var data = mlContext.Data.LoadFromEnumerable(enumerableOfData);

            // Before we apply a filter, examine all the records in the dataset.
            Console.WriteLine($"Date\tTemperature");
            foreach (var row in enumerableOfData)
            {
                Console.WriteLine($"{row.Date.ToString("d")}" +
                    $"\t{row.Temperature}");
            }
            Console.WriteLine();
            // Expected output:
            //  Date    Temperature
            //  1/2/2012        36
            //  1/3/2012        36
            //  1/4/2012        34
            //  1/5/2012        35
            //  1/6/2012        35

            // Shuffle the dataset.
            var shuffledData = mlContext.Data.ShuffleRows(data, seed: 123);

            // Look at the shuffled data and observe that the rows are in a
            // randomized order.
            var enumerable = mlContext.Data
                .CreateEnumerable<SampleTemperatureData>(shuffledData,
                reuseRowObject: true);

            Console.WriteLine($"Date\tTemperature");
            foreach (var row in enumerable)
            {
                Console.WriteLine($"{row.Date.ToString("d")}" +
                $"\t{row.Temperature}");
            }
            // Expected output:
            //  Date    Temperature
            //  1/4/2012        34
            //  1/2/2012        36
            //  1/5/2012        35
            //  1/3/2012        36
            //  1/6/2012        35
        }

        private class SampleTemperatureData
        {
            public DateTime Date { get; set; }
            public float Temperature { get; set; }
        }

        /// <summary>
        /// Get a fake temperature dataset.
        /// </summary>
        /// <param name="exampleCount">The number of examples to return.</param>
        /// <returns>An enumerable of <see cref="SampleTemperatureData"/>.</returns>
        private static IEnumerable<SampleTemperatureData> GetSampleTemperatureData(
            int exampleCount)

        {
            var rng = new Random(1234321);
            var date = new DateTime(2012, 1, 1);
            float temperature = 39.0f;

            for (int i = 0; i < exampleCount; i++)
            {
                date = date.AddDays(1);
                temperature += rng.Next(-5, 5);
                yield return new SampleTemperatureData
                {
                    Date = date,
                    Temperature =
                    temperature
                };

            }
        }
    }
}

備註

ShuffleRows(IDataView, Nullable<Int32>, Int32, Boolean) 會使用串流方法隨機顯示任何輸入 IDataView 的資料列。 為了不要在記憶體中載入整個資料集,資料列集 shufflePoolSize 區將用來隨機選取要輸出的資料列。 集區是從 中的 input 第一個 shufflePoolSize 資料列建構。 然後,系統會從集區隨機產生資料列,並將資料列取代為下一個資料列 input ,直到產生所有資料列為止,產生與隨機順序相同的新 IDataView 大小 input , 如果 的 CanShuffleinput 屬性為 true,則它也會以隨機順序讀入集區,並提供兩個隨機性來源。

適用於