How to use String as a Predicted Column RRS feed

  • Question

  • Goal:
    Make a prediction on PaymentType. The target name or predicted column is a string value.

    I retrieve a error message that is "ArgumentOutOfRangeException: Training label column 'Label' type isn't suitable for regression: Text. Type must be R4 or R8. Parameter name: data"

    Whar part from the source code am I missing?

    Thank you!


    I'm new in


    using Microsoft.ML;
    using Microsoft.ML.Data;
    using Microsoft.ML.Models;
    using Microsoft.ML.Trainers;
    using Microsoft.ML.Transforms;
    using System;
    using System.Threading.Tasks;
    namespace TaxiFarePrediction
        class Program
            const string _datapath = @".\Data\taxi-fare-train.csv";
            const string _testdatapath = @".\Data\taxi-fare-test.csv";
            const string _modelpath = @".\Data\";
            static async Task Main(string[] args)
                PredictionModel<TaxiTrip, TaxiTripFarePrediction> model = await Train();
                var prediction = model.Predict(TestTrips.Trip1);
                Console.WriteLine("Predicted fare: {0}", prediction.PaymentType);
            static async Task<PredictionModel<TaxiTrip, TaxiTripFarePrediction>> Train()
                // Create learning pipeline
                var pipeline = new LearningPipeline
                    // Load and transform data
                    new TextLoader(_datapath).CreateFrom<TaxiTrip>(separator: ','),
                     // Labeling
                    new ColumnCopier(("PaymentType", "Label")),
                    // Feature engineering
                    new CategoricalOneHotVectorizer("VendorId",
                     // Combine features in a single vector
                    new ColumnConcatenator("Features",
                    // Add learning algorithm
                    new FastTreeRegressor()
                // Train the model
                PredictionModel<TaxiTrip, TaxiTripFarePrediction> model = pipeline.Train<TaxiTrip, TaxiTripFarePrediction>();
                // Save the model to a zip file
                await model.WriteAsync(_modelpath);
                return model;
            private static void Evaluate(PredictionModel<TaxiTrip, TaxiTripFarePrediction> model)
                // Load test data
                var testData = new TextLoader(_datapath).CreateFrom<TaxiTrip>(useHeader: true, separator: ',');
                // Evaluate test data
                var evaluator = new RegressionEvaluator();
                RegressionMetrics metrics = evaluator.Evaluate(model, testData);
                // Display regression evaluation metrics
                Console.WriteLine("Rms=" + metrics.Rms);
                Console.WriteLine("RSquared = " + metrics.RSquared);

    namespace TaxiFarePrediction
        static class TestTrips
            internal static readonly TaxiTrip Trip1 = new TaxiTrip
                VendorId = "VTS",
                RateCode = "1",
                PassengerCount = 1,
                TripDistance = 10.33f,
                PaymentType = "", 
                FareAmount = 7,
                //FareAmount = 0 // predict it. actual = 29.5

    using Microsoft.ML.Runtime.Api;
    namespace TaxiFarePrediction
        public class TaxiTrip
            public string VendorId;
            public string RateCode;
            public float PassengerCount;
            public float TripTime;
            public float TripDistance;
            public string PaymentType;
            public float FareAmount;

    using Microsoft.ML.Runtime.Api;
    namespace TaxiFarePrediction
        public class TaxiTripFarePrediction
            public float FareAmount;
            public string PaymentType;

    Thursday, October 24, 2019 12:51 PM

All replies

  • Hi Sakura,

    Are you following the quickstart from documentation? I ran the solution as mentioned in the documentation and it works as expected. 

    It looks like the issue is from ColumnCopier, Could you try to use CopyColumns as mentioned in the documentation? The error seen in this case is explained in this issue. You can try to vectorize the string values and try the same if columncopier is used.


    Friday, October 25, 2019 8:53 AM