Skip to content

Commit bf0e059

Browse files
prathyusha12345CESARDELATORRE
prathyusha12345
authored andcommitted
Update PeekViewToConsole method to to use Preview() (#251)
* Updated PeekViewToConsole method to display data using Preview() * Removed TObservation class as specifying this class restricts only the columns in class to be displayed. * All the samples are working without issue.So removing this commented code. * Changed the commented line.
1 parent 13d2480 commit bf0e059

File tree

8 files changed

+25
-45
lines changed
  • samples/csharp
    • common
    • end-to-end-apps/MulticlassClassification-GitHubLabeler/GitHubLabeler/GitHubLabelerConsoleApp
    • getting-started
      • BinaryClassification_SentimentAnalysis/SentimentAnalysis/SentimentAnalysisConsoleApp
      • Clustering_CustomerSegmentation/CustomerSegmentation.Train
      • Clustering_Iris/IrisClustering/IrisClusteringConsoleApp
      • MulticlassClassification_HeartDisease/HeartDiseaseConsoleApp
      • Regression_BikeSharingDemand/BikeSharingDemand/BikeSharingDemandConsoleApp
      • Regression_TaxiFarePrediction/TaxiFarePrediction/TaxiFarePredictionConsoleApp

8 files changed

+25
-45
lines changed

samples/csharp/common/ConsoleHelper.cs

+18-38
Original file line numberDiff line numberDiff line change
@@ -166,52 +166,32 @@ public static void PrintClusteringMetrics(string name, ClusteringMetrics metrics
166166
Console.WriteLine($"*************************************************");
167167
}
168168

169-
public static List<TObservation> PeekDataViewInConsole<TObservation>(MLContext mlContext, IDataView dataView, IEstimator<ITransformer> pipeline, int numberOfRows = 4)
170-
where TObservation : class, new()
169+
public static void PeekDataViewInConsole(MLContext mlContext, IDataView dataView, IEstimator<ITransformer> pipeline, int numberOfRows = 4)
171170
{
172-
string msg = string.Format("Peek data in DataView: Showing {0} rows with the columns specified by TObservation class", numberOfRows.ToString());
171+
string msg = string.Format("Peek data in DataView: Showing {0} rows with the columns", numberOfRows.ToString());
173172
ConsoleWriteHeader(msg);
174173

175174
//https://github.com/dotnet/machinelearning/blob/master/docs/code/MlNetCookBook.md#how-do-i-look-at-the-intermediate-data
176175
var transformer = pipeline.Fit(dataView);
177176
var transformedData = transformer.Transform(dataView);
178177

179-
// 'transformedData' is a 'promise' of data, lazy-loading. Let's actually read it.
180-
// Convert to an enumerable of user-defined type.
181-
182-
//Preview<TSource>(this IDataReader<TSource> reader, TSource source, int maxRows = 100);
183-
184-
var someRows = mlContext.CreateEnumerable<TObservation>(transformedData, reuseRowObject: false)
185-
// Take the specified number of rows
186-
.Take(numberOfRows)
187-
// Convert to List
188-
.ToList();
189-
// v0.9
190-
//
191-
//var someRows = transformedData.AsEnumerable<TObservation>(mlContext, reuseRowObject: false)
192-
// // Take the specified number of rows
193-
// .Take(numberOfRows)
194-
// // Convert to List
195-
// .ToList();
196-
197-
someRows.ForEach(row =>
198-
{
199-
string lineToPrint = "Row--> ";
200-
201-
var fieldsInRow = row.GetType().GetFields(BindingFlags.Instance |
202-
BindingFlags.Static |
203-
BindingFlags.NonPublic |
204-
BindingFlags.Public);
205-
foreach (FieldInfo field in fieldsInRow)
206-
{
207-
lineToPrint += $"| {field.Name}: {field.GetValue(row)}";
208-
}
209-
Console.WriteLine(lineToPrint);
210-
});
211-
212-
return someRows;
213-
}
178+
// 'transformedData' is a 'promise' of data, lazy-loading. call Preview
179+
//and iterate through the returned collection from preview.
180+
181+
var preViewTransformedData = transformedData.Preview(maxRows: numberOfRows);
214182

183+
foreach (var row in preViewTransformedData.RowView)
184+
{
185+
var ColumnCollection = row.Values;
186+
string lineToPrint = "Row--> ";
187+
foreach (KeyValuePair<string, object> column in ColumnCollection)
188+
{
189+
lineToPrint += $"| {column.Key}:{column.Value}";
190+
}
191+
Console.WriteLine(lineToPrint + "\n");
192+
}
193+
}
194+
215195
public static List<float[]> PeekVectorColumnDataInConsole(MLContext mlContext, string columnName, IDataView dataView, IEstimator<ITransformer> pipeline, int numberOfRows = 4)
216196
{
217197
string msg = string.Format("Peek data in DataView: : Show {0} rows with just the '{1}' column", numberOfRows, columnName );

samples/csharp/end-to-end-apps/MulticlassClassification-GitHubLabeler/GitHubLabeler/GitHubLabelerConsoleApp/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void BuildAndTrainModel(string DataSetLocation, string ModelPath,
6565
.AppendCacheCheckpoint(mlContext); //In this sample, only when using OVA (Not SDCA) the cache improves the training time, since OVA works multiple times/iterations over the same data
6666

6767
// (OPTIONAL) Peek data (such as 2 records) in training DataView after applying the ProcessPipeline's transformations into "Features"
68-
Common.ConsoleHelper.PeekDataViewInConsole<GitHubIssue>(mlContext, trainingDataView, dataProcessPipeline, 2);
68+
Common.ConsoleHelper.PeekDataViewInConsole(mlContext, trainingDataView, dataProcessPipeline, 2);
6969
//Common.ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, "Features", trainingDataView, dataProcessPipeline, 2);
7070

7171
// STEP 3: Create the selected training algorithm/trainer

samples/csharp/getting-started/BinaryClassification_SentimentAnalysis/SentimentAnalysis/SentimentAnalysisConsoleApp/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private static ITransformer BuildTrainEvaluateAndSaveModel(MLContext mlContext)
4949
var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: DefaultColumnNames.Features, inputColumnName:nameof(SentimentIssue.Text));
5050

5151
// (OPTIONAL) Peek data (such as 2 records) in training DataView after applying the ProcessPipeline's transformations into "Features"
52-
ConsoleHelper.PeekDataViewInConsole<SentimentIssue>(mlContext, trainingDataView, dataProcessPipeline, 2);
52+
ConsoleHelper.PeekDataViewInConsole(mlContext, trainingDataView, dataProcessPipeline, 2);
5353
ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, DefaultColumnNames.Features, trainingDataView, dataProcessPipeline, 1);
5454

5555
// STEP 3: Set the training algorithm, then create and config the modelBuilder

samples/csharp/getting-started/Clustering_CustomerSegmentation/CustomerSegmentation.Train/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void Main(string[] args)
5252
));
5353

5454
// (Optional) Peek data in training DataView after applying the ProcessPipeline's transformations
55-
Common.ConsoleHelper.PeekDataViewInConsole<PivotObservation>(mlContext, pivotDataView, dataProcessPipeline, 10);
55+
Common.ConsoleHelper.PeekDataViewInConsole(mlContext, pivotDataView, dataProcessPipeline, 10);
5656
Common.ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, "Features", pivotDataView, dataProcessPipeline, 10);
5757

5858
//STEP 3: Create the training pipeline

samples/csharp/getting-started/Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static void Main(string[] args)
4545
var dataProcessPipeline = mlContext.Transforms.Concatenate(DefaultColumnNames.Features, nameof(IrisData.SepalLength), nameof(IrisData.SepalWidth), nameof(IrisData.PetalLength), nameof(IrisData.PetalWidth));
4646

4747
// (Optional) Peek data in training DataView after applying the ProcessPipeline's transformations
48-
Common.ConsoleHelper.PeekDataViewInConsole<IrisData>(mlContext, trainingDataView, dataProcessPipeline, 10);
48+
Common.ConsoleHelper.PeekDataViewInConsole(mlContext, trainingDataView, dataProcessPipeline, 10);
4949
Common.ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, DefaultColumnNames.Features, trainingDataView, dataProcessPipeline, 10);
5050

5151
// STEP 3: Create and train the model

samples/csharp/getting-started/MulticlassClassification_HeartDisease/HeartDiseaseConsoleApp/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static void BuildTrainEvaluateAndSaveModel(MLContext mlContext)
4343
.AppendCacheCheckpoint(mlContext);
4444

4545
// (OPTIONAL) Peek data (such as 5 records) in training DataView after applying the ProcessPipeline's transformations into "Features"
46-
ConsoleHelper.PeekDataViewInConsole<HeartData>(mlContext, trainingDataView, dataProcessPipeline, 5);
46+
ConsoleHelper.PeekDataViewInConsole(mlContext, trainingDataView, dataProcessPipeline, 5);
4747
ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, DefaultColumnNames.Features, trainingDataView, dataProcessPipeline, 5);
4848

4949
var trainer = mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumn: DefaultColumnNames.Label, featureColumn: DefaultColumnNames.Features);

samples/csharp/getting-started/Regression_BikeSharingDemand/BikeSharingDemand/BikeSharingDemandConsoleApp/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void Main(string[] args)
3939
.AppendCacheCheckpoint(mlContext);
4040

4141
// (Optional) Peek data in training DataView after applying the ProcessPipeline's transformations
42-
Common.ConsoleHelper.PeekDataViewInConsole<DemandObservation>(mlContext, trainingDataView, dataProcessPipeline, 10);
42+
Common.ConsoleHelper.PeekDataViewInConsole(mlContext, trainingDataView, dataProcessPipeline, 10);
4343
Common.ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, DefaultColumnNames.Features, trainingDataView, dataProcessPipeline, 10);
4444

4545
// Definition of regression trainers/algorithms to use

samples/csharp/getting-started/Regression_TaxiFarePrediction/TaxiFarePrediction/TaxiFarePredictionConsoleApp/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private static ITransformer BuildTrainEvaluateAndSaveModel(MLContext mlContext)
7373
, nameof(TaxiTrip.TripTime), nameof(TaxiTrip.TripDistance)));
7474

7575
// (OPTIONAL) Peek data (such as 5 records) in training DataView after applying the ProcessPipeline's transformations into "Features"
76-
ConsoleHelper.PeekDataViewInConsole<TaxiTrip>(mlContext, trainingDataView, dataProcessPipeline, 5);
76+
ConsoleHelper.PeekDataViewInConsole(mlContext, trainingDataView, dataProcessPipeline, 5);
7777
ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, DefaultColumnNames.Features, trainingDataView, dataProcessPipeline, 5);
7878

7979
// STEP 3: Set the training algorithm, then create and config the modelBuilder - Selected Trainer (SDCA Regression algorithm)

0 commit comments

Comments
 (0)