Ejemplo n.º 1
0
        //static async Task Main(string[] args)
        static void Main(string[] args)
        {
            MLContext mlContext = new MLContext(seed: 0);

            TextLoader textLoader = null;

            textLoader = mlContext.Data.CreateTextLoader <IrisData>(hasHeader: false, separatorChar: ',');
            Microsoft.ML.IDataView dataView = textLoader.Load(path_data);


            string featuresColumnName = "Features";
            var    pipeline           = mlContext.Transforms
                                        .Concatenate
                                        (
                featuresColumnName,
                "SepalLength",
                "SepalWidth",
                "PetalLength",
                "PetalWidth"
                                        )
                                        .Append
                                        (
                mlContext.Clustering.Trainers.KMeans
                (
                    featuresColumnName,
                    null,
                    numberOfClusters: 3
                )
                                        );

            var model = pipeline.Fit(dataView);


            using (var fileStream = new FileStream(path_model, FileMode.Create, FileAccess.Write, FileShare.Write))
            {
                mlContext.Model.Save(model, textLoader, fileStream);
            }

            //Task.WaitAll();

            return;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 将<see cref="Microsoft.ML.IDataView"/>转换为<see cref="DataFrancis.IDataView"/>
 /// </summary>
 /// <param name="data">待转换的<see cref="IDataView"/></param>
 /// <param name="maxRows">读取的最大列数,如果这个值过大,可能会对性能产生影响,默认为全部读取</param>
 /// <returns></returns>
 public static IDataViewF ToIData(this IDataViewML data, int maxRows = int.MaxValue)
 => CreateDataObj.DataView(data.Preview(maxRows).RowView.Select(x => CreateDataObj.Data(x.Values, true)).ToArray());
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The UWP host has sent a request for something. Responses to the UWP app are set by
        /// the respective case handlers, and sent to the UWP Connection_RequestReceived handler
        /// via the AppServiceConnection.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async static void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            var deferral = args.GetDeferral();

            ValueSet message    = args.Request.Message;
            ValueSet returnData = new ValueSet();

            string verb = string.Empty;

            // get the command verb from the request message
            try
            {
                verb = message["verb"] as String;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);

                verb = string.Empty;
            }

            switch (verb)
            {
            // we received a request to make a prediction
            case "makePrediction":
            {
                try
                {
                    // get the parameters for the prediction from the message data
                    float sl = (float)message["sl"];
                    float sw = (float)message["sw"];
                    float pl = (float)message["pl"];
                    float pw = (float)message["pw"];

                    // we switch on the value of the verb in the UWP app that receives this valueSet
                    returnData.Add("verb", "PredictionResult");

                    // Use your model to make a prediction
                    // You can change these numbers to test different predictions
                    //var prediction = model.CreatePredictionEngine<IrisData, IrisPrediction>(mlContext).Predict(
                    //    new IrisData()
                    //    {
                    //        SepalLength = sl,
                    //        SepalWidth = sw,
                    //        PetalLength = pl,
                    //        PetalWidth = pw,
                    //    });

                    IrisData inputToTest = new IrisData()
                    {
                        SepalLength = sl,
                        SepalWidth  = sw,
                        PetalLength = pl,
                        PetalWidth  = pw,
                    };

                    var predictor = mlContext.Model.CreatePredictionEngine <IrisData, ClusterPrediction>(model);

                    var prediction = predictor.Predict(inputToTest);

                    // add the prediction to our response
                    returnData.Add("Cluster", prediction.PredictedClusterId);
                    returnData.Add("Distances", string.Join(" ", prediction.Distances));
                }
                catch (Exception ex)
                {
                    returnData.Add("verb", "PredictionError");
                    returnData.Add("exceptionMessage", ex.Message.ToString());
                }

                break;
            }

            // we received a request to build the model
            case "buildModel":
            {
                try
                {
                    // get the Iris data from the message
                    string contents = message["irisData"] as string;

                    // build the List<IrisData>, which is an IEnumerable

                    // clear the list of data from previous model builds
                    irisDataList.Clear();

                    var records = contents.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                    foreach (var record in records)
                    {
                        var fields = record.Split(',');

                        IrisData entry = new IrisData();
                        entry.SepalLength = float.Parse(fields[0]);
                        entry.SepalWidth  = float.Parse(fields[1]);
                        entry.PetalLength = float.Parse(fields[2]);
                        entry.PetalWidth  = float.Parse(fields[3]);
                        entry.Label       = fields[4];

                        irisDataList.Add(entry);
                    }

                    // If working in Visual Studio, make sure the 'Copy to Output Directory'
                    // property of iris-data.txt is set to 'Copy always'
                    //IDataView trainingDataView = mlContext.Data.LoadFromTextFile<IrisData>(path: @"MLDotNetWin32\iris-data.txt", hasHeader: false, separatorChar: ',');


                    Microsoft.ML.IDataView trainingDataView = mlContext.Data.LoadFromEnumerable <IrisData>(irisDataList);

                    // Transform your data and add a learner
                    // Assign numeric values to text in the "Label" column, because only
                    // numbers can be processed during model training.
                    // Add a learning algorithm to the pipeline. e.g.(What type of iris is this?)
                    // Convert the Label back into original text (after converting to number in step 3)
                    //
                    // This code is from the old tutorial in ML.net version 0.11
                    //var pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
                    //    .Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
                    //    .AppendCacheCheckpoint(mlContext)
                    //    .Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumnName: "Label", featureColumnName: "Features"))
                    //    .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

                    // this is the new ML.Net 1.0 way of doing things.
                    string featuresColumnName = "Label";
                    var    pipeline           = mlContext.Transforms
                                                .Concatenate(featuresColumnName, "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
                                                .Append(mlContext.Clustering.Trainers.KMeans(featuresColumnName, numberOfClusters: 3));

                    // Train your model based on the data set
                    model = pipeline.Fit(trainingDataView);

                    // let UWP know our model built correctly
                    returnData.Add("verb", "modelOk");
                }
                catch (Exception ex)
                {
                    returnData.Add("verb", "modelFailure");
                    returnData.Add("exceptionMessage", ex.Message);
                }

                break;
            }

            default:
            {
                returnData.Add("verb", "APIError");
                returnData.Add("exceptionMessage", "Bad or No Verb");
                break;
            }
            }

            try
            {
                // Return the data to the caller.
                await args.Request.SendResponseAsync(returnData);
            }
            catch (Exception e)
            {
                // Your exception handling code here.
                Debug.WriteLine(e.Message);
            }
            finally
            {
                // Complete the deferral so that the platform knows that we're done responding to the app service call.
                // Note for error handling: this must be called even if SendResponseAsync() throws an exception.
                deferral.Complete();
            }
        }