Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created October 10, 2018 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/387904adeb425a914b3ffd97c1c23515 to your computer and use it in GitHub Desktop.
Save elbruno/387904adeb425a914b3ffd97c1c23515 to your computer and use it in GitHub Desktop.
MLNet060SampleProgram.cs
static void Main(string[] args)
{
var dataPath = "AgeRangeData.csv";
var env = new LocalEnvironment();
var reader = TextLoader.CreateReader(env, ctx => (
Age: ctx.LoadFloat(1),
Label: ctx.LoadText(3)),
separator: ',', hasHeader: true);
var trainData = reader.Read(new MultiFileSource(dataPath));
var classification = new MulticlassClassificationContext(env);
var learningPipeline = reader.MakeNewEstimator()
.Append(r => (
r.Label,
Predictions: classification.Trainers.Sdca
(label: r.Label.ToKey(),
features: r.Age.AsVector())))
.Append(r => r.Predictions.predictedLabel.ToValue());
var model = learningPipeline.Fit(trainData);
var predictionFunc = model.AsDynamic.MakePredictionFunction<AgeRangeNewApi, AgeRangePredictionNewApi>(env);
var example = new AgeRangeNewApi()
{
Age = 6,
Name = "John",
Gender = "M"
};
var prediction = predictionFunc.Predict(example);
Console.WriteLine("prediction: " + prediction.PredictedLabel);
Console.ReadLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment