public async Task <PredictionModel <TInput, TOutput> > TrainAsync() { //1.通过反射获取Tinput的attributes Type ti = typeof(TInput), to = typeof(TOutput); //2.获取input属性 List <string> inputColoums = new List <string>(); Array.ForEach(ti.GetFields(), new Action <System.Reflection.FieldInfo>(p => { inputColoums.Add(p.Name); })); //3.获取output属性 string outputColoum = to.GetFields().Length > 0 ? to.GetFields()[0].Name : null; //4.聚合输入输出层参数名称 ColumnConcatenator coloums = new ColumnConcatenator(outputColoum, inputColoums.ToArray()); //4.构建学习机 //LearningPipeline pipeline = new LearningPipeline(); //pipeline.Add()\ //CollectionDataSource.Create(new List<Input>() { new Input { Number1 = 1, String1 = "1" } }) LearningPipeline pipeline = new LearningPipeline { coloums, new LogisticRegressionBinaryClassifier() }; PredictionModel <TInput, TOutput> model = pipeline.Train <TInput, TOutput>(); //model写入zip file await model.WriteAsync(_workDirectory + System.DateTime.Now.ToLongDateString() + ".zip"); //返回model对象 return(model); }
static void Main(string[] args) { //step2:创建一个管道并且加载你的数据 var pipeline = new LearningPipeline(); string dataPath = "SourceData/iris-data.txt"; var loaderData = new TextLoader <IrisData>(dataPath, separator: ","); pipeline.Add(loaderData); //step3:转换数据 //因为在模型训练的时候只能处理数字,所以在Label列中将数值分配给文本 var dictionarizer = new Dictionarizer("Label"); pipeline.Add(dictionarizer); var concatenator = new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"); pipeline.Add(concatenator); //step4:添加学习者 //添加一个学习算法到管道中,这是一种分类方案(这是什么类型的Iris) pipeline.Add(new StochasticDualCoordinateAscentClassifier()); //在步骤三转换为数字之后将Label转换回原始文本 pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" }); //step5:根据数据集来训练模型 var model = pipeline.Train <IrisData, IrisPrediction>(); var prediction = model.Predict(new IrisData() { SepalLength = 10.8f, SepalWidth = 5.1f, PetalLength = 2.55f, PetalWidth = 0.3f }); Console.WriteLine($"Predicted flower type is {prediction.PredictedLabels}"); Console.Read(); }
public static async Task <PredictionModel <TaxiTrip, TaxiTripFarePrediction> > TrainModel() { var pipeline = new LearningPipeline(); var loadData = new TextLoader(_dataPath).CreateFrom <TaxiTrip>(useHeader: true, separator: ','); var copyLabels = new ColumnCopier(("FareAmount", "Label")); var convertToNumeric = new CategoricalOneHotVectorizer("VendorId", "RateCode", "PaymentType"); var features = new ColumnConcatenator("Features", "VendorId", "RateCode", "PassengerCount", "TripDistance", "PaymentType"); pipeline.Add(loadData); pipeline.Add(copyLabels); pipeline.Add(convertToNumeric); pipeline.Add(features); pipeline.Add(new FastTreeRegressor()); var model = pipeline.Train <TaxiTrip, TaxiTripFarePrediction>(); await model.WriteAsync(_modelPath); return(model); }