1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using Microsoft . ML ;
4
- using Microsoft . ML . SamplesUtils ;
5
4
6
5
namespace Samples . Dynamic
7
6
{
8
- /// <summary>
9
- /// Sample class showing how to use ShuffleRows.
10
- /// </summary>
11
7
public static class DataViewEnumerable
12
8
{
9
+ // A simple case of creating IDataView from IEnumerable.
13
10
public static void Example ( )
14
11
{
15
12
// Create a new context for ML.NET operations. It can be used for exception tracking and logging,
16
13
// as a catalog of available operations and as the source of randomness.
17
14
var mlContext = new MLContext ( ) ;
18
15
19
16
// Get a small dataset as an IEnumerable.
20
- IEnumerable < DatasetUtils . SampleTemperatureData > enumerableOfData = DatasetUtils . GetSampleTemperatureData ( 5 ) ;
17
+ IEnumerable < SampleTemperatureData > enumerableOfData = GetSampleTemperatureData ( 5 ) ;
21
18
22
19
// Load dataset into an IDataView.
23
20
IDataView data = mlContext . Data . LoadFromEnumerable ( enumerableOfData ) ;
24
21
25
22
// We can now examine the records in the IDataView. We first create an enumerable of rows in the IDataView.
26
- var rowEnumerable = mlContext . Data . CreateEnumerable < DatasetUtils . SampleTemperatureData > ( data , reuseRowObject : true ) ;
23
+ var rowEnumerable = mlContext . Data . CreateEnumerable < SampleTemperatureData > ( data , reuseRowObject : true ) ;
27
24
28
25
// SampleTemperatureDataWithLatitude has the definition of a Latitude column of type float.
29
26
// We can use the parameter ignoreMissingColumns to true to ignore any missing columns in the IDataView.
30
27
// The produced enumerable will have the Latitude field set to the default for the data type, in this case 0.
31
- var rowEnumerableIgnoreMissing = mlContext . Data . CreateEnumerable < DatasetUtils . SampleTemperatureDataWithLatitude > ( data ,
28
+ var rowEnumerableIgnoreMissing = mlContext . Data . CreateEnumerable < SampleTemperatureDataWithLatitude > ( data ,
32
29
reuseRowObject : true , ignoreMissingColumns : true ) ;
33
30
34
31
Console . WriteLine ( $ "Date\t Temperature") ;
@@ -55,5 +52,38 @@ public static void Example()
55
52
// 1/5/2012 35 0
56
53
// 1/6/2012 35 0
57
54
}
55
+
56
+ private class SampleTemperatureData
57
+ {
58
+ public DateTime Date { get ; set ; }
59
+ public float Temperature { get ; set ; }
60
+ }
61
+
62
+ private class SampleTemperatureDataWithLatitude
63
+ {
64
+ public float Latitude { get ; set ; }
65
+ public DateTime Date { get ; set ; }
66
+ public float Temperature { get ; set ; }
67
+ }
68
+
69
+ /// <summary>
70
+ /// Get a fake temperature dataset.
71
+ /// </summary>
72
+ /// <param name="exampleCount">The number of examples to return.</param>
73
+ /// <returns>An enumerable of <see cref="SampleTemperatureData"/>.</returns>
74
+ private static IEnumerable < SampleTemperatureData > GetSampleTemperatureData ( int exampleCount )
75
+ {
76
+ var rng = new Random ( 1234321 ) ;
77
+ var date = new DateTime ( 2012 , 1 , 1 ) ;
78
+ float temperature = 39.0f ;
79
+
80
+ for ( int i = 0 ; i < exampleCount ; i ++ )
81
+ {
82
+ date = date . AddDays ( 1 ) ;
83
+ temperature += rng . Next ( - 5 , 5 ) ;
84
+ yield return new SampleTemperatureData { Date = date , Temperature = temperature } ;
85
+ }
86
+ }
58
87
}
59
88
}
89
+
0 commit comments