static void Main(string[] args) { string dataset = "SalaryData.csv" //MLNetUtilities.GetDataPathByDatasetName("SalaryData.csv") ; string testDataset = "SalaryData-test.csv" //MLNetUtilities.GetDataPathByDatasetName("SalaryData-test.csv") ; LocalEnvironment env = new LocalEnvironment(); Microsoft.ML.StaticPipe.DataReader < IMultiStreamSource, ( Microsoft.ML.StaticPipe.Scalar <float> YearsExperience, Microsoft.ML.StaticPipe.Scalar <float> Target ) > reader = TextLoader.CreateReader ( env, ctx => ( YearsExperience: ctx.LoadFloat(0), Target: ctx.LoadFloat(1) ), hasHeader: true, separator: ',' ); Microsoft.ML.StaticPipe.DataView < ( Microsoft.ML.StaticPipe.Scalar <float> YearsExperience, Microsoft.ML.StaticPipe.Scalar <float> Target ) > data = reader.Read(new MultiFileSource(dataset)); var regression = new RegressionContext(env); var pipeline = reader.MakeNewEstimator() .Append(r => ( r.Target, Prediction: regression.Trainers.FastTree(label: r.Target, features: r.YearsExperience.AsVector()) )); Microsoft.ML.Core.Data.ITransformer model = pipeline.Fit(data).AsDynamic; var predictionFunc = model.MakePredictionFunction <SalaryData, SalaryPrediction>(env); var prediction = predictionFunc.Predict(new SalaryData { YearsExperience = 8 }); Console.WriteLine($"Predicted salary - {String.Format("{0:C}", prediction.PredictedSalary)}"); Console.Read(); }
/// <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, Schema inputSchema, int conc = 1) { 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, conc: conc); _fastValueMapper = _fastValueMapperObject.GetMapper <DataFrame, DataFrame>(); }
public PredictionViewModel() { _home = new Home(); Title = "Emlak Tahmin"; var context = new MLContext(); var assembly = IntrospectionExtensions.GetTypeInfo(typeof(PredictionViewModel)).Assembly; using (var stream = assembly.GetManifestResourceStream("TeknolotEmlak.MLModels.model.zip")) { _model = context.Model.Load(stream); _predictionEngine = _model.CreatePredictionEngine <Home, HousePricePrediction>(context); } this.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(this.Price)) { return; } Price = (decimal)_predictionEngine.Predict(_home).Price; }; }
/// <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, Schema inputSchema, int conc = 1) => new PredictionFunctionDataFrame(env, transformer, inputSchema, conc);