1

I was trying to do the example of ML.net to predict New York taxi fares, but when I finished the tutorial had the message: Program does not contain a static 'Main' method suitable for an entry point

Here the code that I did:

Class Program.cs

using System;
using System.IO;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Models;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
using System.Threading.Tasks;

namespace TaxiFarePrediction2
{
    public class Program
    {
        static readonly string _datapath = Path.Combine(Environment.CurrentDirectory, "Data", "taxi-fare-train.csv");
        static readonly string _testdatapath = Path.Combine(Environment.CurrentDirectory, "Data", "taxi-fare-test.csv");
        static readonly string _modelpath = Path.Combine(Environment.CurrentDirectory, "Data", "Model.zip");


        static async Task Main(string[] args)
        {
            PredictionModel<TaxiTrip, TaxiTripFarePrediction> model = await Train();
            Evaluate(model);
            TaxiTripFarePrediction prediction = model.Predict(TestTrips.Trip1);
            Console.WriteLine("Predicted fare: {0}, actual fare: 29.5", prediction.FareAmount);
        }


        public static async Task<PredictionModel<TaxiTrip, TaxiTripFarePrediction>> Train()
        {
            var pipeline = new LearningPipeline
            {
                new TextLoader(_datapath).CreateFrom<TaxiTrip>(useHeader: true, separator: ','),
                new ColumnCopier(("FareAmount", "Label")),
                new CategoricalOneHotVectorizer(
                    "VendorId",
                    "RateCode",
                    "PaymentType"),
                new ColumnConcatenator(
                    "Features",
                    "VendorId",
                    "RateCode",
                    "PassengerCount",
                    "TripDistance",
                    "PaymentType"),
                new FastTreeRegressor()
            };
            PredictionModel<TaxiTrip, TaxiTripFarePrediction> model = pipeline.Train<TaxiTrip, TaxiTripFarePrediction>();
            await model.WriteAsync(_modelpath);
            return model;
        }

        private static void Evaluate(PredictionModel<TaxiTrip, TaxiTripFarePrediction> model)
        {
            var testData = new TextLoader(_testdatapath).CreateFrom<TaxiTrip>(useHeader: true, separator: ',');
            var evaluator = new RegressionEvaluator();
            RegressionMetrics metrics = evaluator.Evaluate(model, testData);
            Console.WriteLine($"Rms = {metrics.Rms}");
            Console.WriteLine($"RSquared = {metrics.RSquared}");
        }


    }
}

class TaxiTrip.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ML.Runtime.Api;

namespace TaxiFarePrediction2
{
    public class TaxiTrip
    {
        [Column("0")]
        public string VendorId;

        [Column("1")]
        public string RateCode;

        [Column("2")]
        public float PassengerCount;

        [Column("3")]
        public float TripTime;

        [Column("4")]
        public float TripDistance;

        [Column("5")]
        public string PaymentType;

        [Column("6")]
        public float FareAmount;
    }

    public class TaxiTripFarePrediction
    {
        [ColumnName("Score")]
        public float FareAmount;
    }
}

class TestTrips.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ML.Runtime.Api;

namespace TaxiFarePrediction2
{
    public class TaxiTrip
    {
        [Column("0")]
        public string VendorId;

        [Column("1")]
        public string RateCode;

        [Column("2")]
        public float PassengerCount;

        [Column("3")]
        public float TripTime;

        [Column("4")]
        public float TripDistance;

        [Column("5")]
        public string PaymentType;

        [Column("6")]
        public float FareAmount;
    }

    public class TaxiTripFarePrediction
    {
        [ColumnName("Score")]
        public float FareAmount;
    }
}

The tutorial is in : https://learn.microsoft.com/en-us/dotnet/machine-learning/tutorials/taxi-fare

please help me to do this example.

1 Answers1

4

Main method can not support asycn before c# 7.1, You can start main once and than create tasks in main method that can be async if you are using earlier versions.

You can writesomething mentioned by Chris Moschini

class Program
{
static void Main(string[] args)
{
    Task.Run(async () =>
    {
        // Do any async anything you need here without worry
    }).GetAwaiter().GetResult();
}

The link you posted clearly mention about c# version specified ...

Because the async Main method is the feature added in C# 7.1 and the default language version of the project is C# 7.0, you need to change the language vers ion to C# 7.1 or higher. To do that, right-click the project node in Solution Explorer and select Properties. Select the Build tab and select the Advanced button. In the dropdown, select C# 7.1 (or a higher version). Select the OK button.

A good read on main with async

mukesh kudi
  • 719
  • 6
  • 20
  • 1
    Don't know if that is the case but the code you pasted seems exactly like in the following answer: https://stackoverflow.com/a/24601591/6400526. If that is the case then this is very discouraged on SO. A better way is to write your own code or just give the credit. Something like: "As written in this answer (link) you can do this: (code)" – Gilad Green Aug 27 '18 at 06:35
  • Much better :) now it is a good answer – Gilad Green Aug 27 '18 at 06:44
  • thanks for your help! It works well now :) – johnnyLuis26 Aug 27 '18 at 19:31
  • @johnnyLuis26 welcome !!! If this will help you in solving your issue, please accept it as the answer. – mukesh kudi Aug 27 '18 at 20:04