1.预备训练数据
2.根据训练数据生成模型
3.预测
实现:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Models;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Runtime.Learners;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
namespace _03_SG_HDB_Price
{
public static class Predictor
{
static readonly string _datapath = Path.Combine(Environment.CurrentDirectory, "train.csv");
static readonly string _testdatapath = Path.Combine(Environment.CurrentDirectory, "test.csv");
static readonly string _modelpath = Path.Combine(Environment.CurrentDirectory, "Model.zip");
public class PriceData
{
[Column(ordinal: "0", name: "quarter")]
public string quarter;
[Column(ordinal: "1", name: "index")]
public float index;
}
public class PricePrediction
{
[ColumnName("Score")]
public float index;
}
public static async Task<PricePrediction> Predict(PriceData tt)
{
var model = await Train();
Evaluate(model);
return model.Predict(tt);
}
private static async Task<PredictionModel<PriceData, PricePrediction>> Train()
{
var pipeline = new LearningPipeline
{
new TextLoader(_datapath).CreateFrom<PriceData>(useHeader: true, separator: ','),
new ColumnCopier(("index", "Label")),
new CategoricalOneHotVectorizer(
"quarter"),
new ColumnConcatenator("Features","quarter","index"),
new FastTreeRegressor()
};
PredictionModel<PriceData, PricePrediction> model = pipeline.Train<PriceData, PricePrediction>();
await model.WriteAsync(_modelpath);
return model;
}
private static void Evaluate(PredictionModel<PriceData, PricePrediction> model)
{
var testData = new TextLoader(_testdatapath).CreateFrom<PriceData>(useHeader: true, separator: ',');
var evaluator = new RegressionEvaluator();
RegressionMetrics metrics = evaluator.Evaluate(model, testData);
Console.WriteLine($"Rms = {metrics.Rms}");
Console.WriteLine($"RSquared = {metrics.RSquared}");
}
}
}
2. 调用:
using System;
namespace _03_SG_HDB_Price
{
class Program
{
static void Main(string[] args)
{
var prediction = Predictor.Predict(new Predictor.PriceData()
{
quarter = "2030-Q4",
index = 0// predict it
}).Result;
Console.WriteLine("Predicted SG hdb house price index: {0}", prediction.index);
Console.ReadLine();
}
}
}
---------------------
作者:_iorilan
来源:CSDN
原文:https://blog.csdn.net/lan_liang/article/details/84680463
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|