/// <summary> /// Creates an instance of <see cref="PredictionFunctionDataFrame"/>. /// </summary> /// <param name="env">The host environment.</param> /// <param name="transformer">The model (transformer) to use for prediction.</param> /// <param name="inputSchema">Input schema.</param> /// <param name="conc">Number of threads.</param> public PredictionFunctionDataFrame(IHostEnvironment env, ITransformer transformer, DataViewSchema inputSchema) { Contracts.CheckValue(env, nameof(env)); env.CheckValue(transformer, nameof(transformer)); var df = new DataFrame(transformer.GetOutputSchema(inputSchema), 0); var tr = transformer.Transform(df) as IDataTransform; _fastValueMapperObject = new ValueMapperDataFrameFromTransform(env, tr); _fastValueMapper = _fastValueMapperObject.GetMapper <DataFrame, DataFrame>(); }
private static void CallMachineLearningDotNet() { sample_ml = new SomatotypeInputData() { Height = 191.7, Mass = 82.0, BreadthHumerus = 7.3, BreadthFemur = 10.1, GirthArmUpper = 33.2, GirthCalfStanding = 36, SkinfoldTriceps = 7, SkinfoldSubscapular = 6, SkinfoldMedialCalf = 4, SkinfoldSupraspinale = 9 }; mlContext = new MLContext(seed: 0); // Train/learn model = TrainLearnMLdotnet(mlContext, _trainDataPath); EvaluateTestMLdotnet(mlContext, model); EvaluateTestSinglePredictionMLdotnet(mlContext, model); }
public static ITransformer TrainLearnMLdotnet(MLContext mlContext, string path) { IDataView dataView = null; dataView = mlContext .Data .LoadFromTextFile <SomatotypeInputData> ( path, hasHeader: true, separatorChar: ',' ); DataOperationsCatalog.TrainTestData dataSplit; dataSplit = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.25); trainData = dataSplit.TrainSet; testData = dataSplit.TestSet; IEnumerable <SomatotypeInputData> list = null; list = mlContext .Data .CreateEnumerable <SomatotypeInputData>(dataView, reuseRowObject: false) .ToList(); for (int i = 0; i < list.Count(); i++) { Console.WriteLine($" Id = {list.ElementAt(i).Id}"); Console.WriteLine($" EndomorphicComponent = {list.ElementAt(i).EndomorphicComponent}"); Console.WriteLine($" MesomorphicComponent = {list.ElementAt(i).MesomorphicComponent}"); Console.WriteLine($" EctomorphicComponent = {list.ElementAt(i).EctomorphicComponent}"); } Microsoft.ML.Transforms.ColumnCopyingEstimator pipeline = null; pipeline = mlContext.Transforms.CopyColumns ( outputColumnName: "Label", inputColumnName: "EndomorphicComponent" ); pipeline.Append ( mlContext.Transforms.Concatenate ( "Features", "Height", "Mass", "BreadthHumerus", "BreadthFemur", "GirthArmUpper", "GirthCalfStanding", "SkinfoldSubscapular", "SkinfoldTriceps", "SkinfoldSupraspinale", "SkinfoldMedialCalf" ) ); pipeline.Append(mlContext.Regression.Trainers.FastTree()); Microsoft.ML.Transforms.ColumnCopyingTransformer model_1 = pipeline.Fit(trainData); return(model = model_1); }
/// <summary> /// Create an instance of the 'prediction function', or 'prediction machine', from a model /// denoted by <paramref name="transformer"/>. /// It will be accepting instances of <typeparamref name="TSrc"/> as input, and produce /// instances of <typeparamref name="TDst"/> as output. /// </summary> public static PredictionFunctionDataFrame MakePredictionFunctionDataFrame(this ITransformer transformer, IHostEnvironment env, DataViewSchema inputSchema) => new PredictionFunctionDataFrame(env, transformer, inputSchema);