Пример #1
0
        public ModelController(IConfiguration configuration)
        {
            _configuration = configuration;

            _context = new MLContext();
            var modelPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "wine.zip");

            if (!System.IO.File.Exists(modelPath))
            {
                var blob = BlobConnection.GetBlobReference(_configuration["blobConnectionString"], "models", "wine.zip");

                blob.DownloadToFileAsync(modelPath, System.IO.FileMode.CreateNew).RunSynchronously();
            }

            using (var stream = System.IO.File.OpenRead(modelPath))
            {
                _model = _context.Model.Load(stream);
            }
        }
Пример #2
0
        static async Task Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("config.json");

            var configuration = builder.Build();

            _sqlConnectionString = configuration["connectionString"];

            var fileData = ReadFromFile("./winequality.csv");

            AddDataToDatabase(fileData);

            var dbData = ReadFromDatabase();

            var context = new MLContext();

            var mlData = context.Data.LoadFromEnumerable(dbData);

            var trainTestData = context.Regression.TrainTestSplit(mlData, testFraction: 0.2);

            var dataPreview = trainTestData.TrainSet.Preview();

            var pipeline = context.Transforms.Categorical.OneHotEncoding("TypeOneHot", "Type")
                           .Append(context.Transforms.Concatenate("Features", "FixedAcidity", "VolatileAcidity", "CitricAcid",
                                                                  "ResidualSugar", "Chlorides", "FreeSulfurDioxide", "TotalSulfurDioxide", "Density", "Ph", "Sulphates",
                                                                  "Alcohol"))
                           .Append(context.Transforms.CopyColumns(("Label", "Quality")))
                           .Append(context.Regression.Trainers.FastTree());

            var model = pipeline.Fit(trainTestData.TrainSet);

            var blob = BlobConnection.GetBlobReference(configuration["blobConnectionString"], "models", fileName);

            using (var stream = File.Create(fileName))
            {
                context.Model.Save(model, stream);
            }

            await blob.UploadFromFileAsync(fileName);
        }