{"id":1611,"date":"2022-10-30T15:33:23","date_gmt":"2022-10-30T22:33:23","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1611"},"modified":"2022-10-30T15:33:23","modified_gmt":"2022-10-30T22:33:23","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-4-2-2-2","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2022\/10\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-4-2-2-2\/","title":{"rendered":"#308 Predict prices using regression with ML.NET using C#"},"content":{"rendered":"<p>Data file: Download the and place to Data folder two files: taxi-fare-test.csv and taxi-fare-train.csv<\/p>\n<p>TaxiTrip.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ &lt;Snippet1&gt;\r\nusing Microsoft.ML.Data;\r\n\/\/ &lt;\/Snippet1&gt;\r\n\r\nnamespace TaxiFarePrediction\r\n{\r\n    \/\/ &lt;Snippet2&gt;\r\n    public class TaxiTrip\r\n    {\r\n        &#x5B;LoadColumn(0)]\r\n        public string VendorId;\r\n\r\n        &#x5B;LoadColumn(1)]\r\n        public string RateCode;\r\n\r\n        &#x5B;LoadColumn(2)]\r\n        public float PassengerCount;\r\n\r\n        &#x5B;LoadColumn(3)]\r\n        public float TripTime;\r\n\r\n        &#x5B;LoadColumn(4)]\r\n        public float TripDistance;\r\n\r\n        &#x5B;LoadColumn(5)]\r\n        public string PaymentType;\r\n\r\n        &#x5B;LoadColumn(6)]\r\n        public float FareAmount;\r\n    }\r\n\r\n    public class TaxiTripFarePrediction\r\n    {\r\n        &#x5B;ColumnName(&quot;Score&quot;)]\r\n        public float FareAmount;\r\n    }\r\n    \/\/ &lt;\/Snippet2&gt;\r\n}\r\n<\/pre>\n<p>Program.cs<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/Install Microsoft.ML\r\n\/\/Install Microsoft.ML.FastTree\r\n\/\/ &lt;Snippet1&gt;\r\nusing System;\r\nusing System.IO;\r\nusing Microsoft.ML;\r\n\/\/ &lt;\/Snippet1&gt;\r\n\r\nnamespace TaxiFarePrediction\r\n{\r\n    class Program\r\n    {\r\n        \/\/ &lt;Snippet2&gt;\r\n        static readonly string _trainDataPath = Path.Combine(Environment.CurrentDirectory, &quot;Data&quot;, &quot;taxi-fare-train.csv&quot;);\r\n        static readonly string _testDataPath = Path.Combine(Environment.CurrentDirectory, &quot;Data&quot;, &quot;taxi-fare-test.csv&quot;);\r\n        static readonly string _modelPath = Path.Combine(Environment.CurrentDirectory, &quot;Data&quot;, &quot;Model.zip&quot;);\r\n        \/\/ &lt;\/Snippet2&gt;\r\n\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Console.WriteLine(Environment.CurrentDirectory);\r\n\r\n            \/\/ &lt;Snippet3&gt;\r\n            MLContext mlContext = new MLContext(seed: 0);\r\n            \/\/ &lt;\/Snippet3&gt;\r\n\r\n            \/\/ &lt;Snippet5&gt;\r\n            var model = Train(mlContext, _trainDataPath);\r\n            \/\/ &lt;\/Snippet5&gt;\r\n\r\n            \/\/ &lt;Snippet14&gt;\r\n            Evaluate(mlContext, model);\r\n            \/\/ &lt;\/Snippet14&gt;\r\n\r\n            \/\/ &lt;Snippet20&gt;\r\n            TestSinglePrediction(mlContext, model);\r\n            \/\/ &lt;\/Snippet20&gt;\r\n        }\r\n\r\n        public static ITransformer Train(MLContext mlContext, string dataPath)\r\n        {\r\n            \/\/ &lt;Snippet6&gt;\r\n            IDataView dataView = mlContext.Data.LoadFromTextFile&lt;TaxiTrip&gt;(dataPath, hasHeader: true, separatorChar: ',');\r\n            \/\/ &lt;\/Snippet6&gt;\r\n\r\n            \/\/ &lt;Snippet7&gt;\r\n            var pipeline = mlContext.Transforms.CopyColumns(outputColumnName: &quot;Label&quot;, inputColumnName: &quot;FareAmount&quot;)\r\n                    \/\/ &lt;\/Snippet7&gt;\r\n                    \/\/ &lt;Snippet8&gt;\r\n                    .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: &quot;VendorIdEncoded&quot;, inputColumnName: &quot;VendorId&quot;))\r\n                    .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: &quot;RateCodeEncoded&quot;, inputColumnName: &quot;RateCode&quot;))\r\n                    .Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: &quot;PaymentTypeEncoded&quot;, inputColumnName: &quot;PaymentType&quot;))\r\n                    \/\/ &lt;\/Snippet8&gt;\r\n                    \/\/ &lt;Snippet9&gt;\r\n                    .Append(mlContext.Transforms.Concatenate(&quot;Features&quot;, &quot;VendorIdEncoded&quot;, &quot;RateCodeEncoded&quot;, &quot;PassengerCount&quot;, &quot;TripDistance&quot;, &quot;PaymentTypeEncoded&quot;))\r\n                    \/\/ &lt;\/Snippet9&gt;\r\n                    \/\/ &lt;Snippet10&gt;\r\n                    .Append(mlContext.Regression.Trainers.FastTree());\r\n            \/\/ &lt;\/Snippet10&gt;\r\n\r\n            Console.WriteLine(&quot;=============== Create and Train the Model ===============&quot;);\r\n\r\n            \/\/ &lt;Snippet11&gt;\r\n            var model = pipeline.Fit(dataView);\r\n            \/\/ &lt;\/Snippet11&gt;\r\n\r\n            Console.WriteLine(&quot;=============== End of training ===============&quot;);\r\n            Console.WriteLine();\r\n            \/\/ &lt;Snippet12&gt;\r\n            return model;\r\n            \/\/ &lt;\/Snippet12&gt;\r\n        }\r\n\r\n        private static void Evaluate(MLContext mlContext, ITransformer model)\r\n        {\r\n            \/\/ &lt;Snippet15&gt;\r\n            IDataView dataView = mlContext.Data.LoadFromTextFile&lt;TaxiTrip&gt;(_testDataPath, hasHeader: true, separatorChar: ',');\r\n            \/\/ &lt;\/Snippet15&gt;\r\n\r\n            \/\/ &lt;Snippet16&gt;\r\n            var predictions = model.Transform(dataView);\r\n            \/\/ &lt;\/Snippet16&gt;\r\n            \/\/ &lt;Snippet17&gt;\r\n            var metrics = mlContext.Regression.Evaluate(predictions, &quot;Label&quot;, &quot;Score&quot;);\r\n            \/\/ &lt;\/Snippet17&gt;\r\n\r\n            Console.WriteLine();\r\n            Console.WriteLine($&quot;*************************************************&quot;);\r\n            Console.WriteLine($&quot;*       Model quality metrics evaluation         &quot;);\r\n            Console.WriteLine($&quot;*------------------------------------------------&quot;);\r\n            \/\/ &lt;Snippet18&gt;\r\n            Console.WriteLine($&quot;*       RSquared Score:      {metrics.RSquared:0.##}&quot;);\r\n            \/\/ &lt;\/Snippet18&gt;\r\n            \/\/ &lt;Snippet19&gt;\r\n            Console.WriteLine($&quot;*       Root Mean Squared Error:      {metrics.RootMeanSquaredError:#.##}&quot;);\r\n            \/\/ &lt;\/Snippet19&gt;\r\n            Console.WriteLine($&quot;*************************************************&quot;);\r\n        }\r\n\r\n        private static void TestSinglePrediction(MLContext mlContext, ITransformer model)\r\n        {\r\n            \/\/Prediction test\r\n            \/\/ Create prediction function and make prediction.\r\n            \/\/ &lt;Snippet22&gt;\r\n            var predictionFunction = mlContext.Model.CreatePredictionEngine&lt;TaxiTrip, TaxiTripFarePrediction&gt;(model);\r\n            \/\/ &lt;\/Snippet22&gt;\r\n            \/\/Sample:\r\n            \/\/vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount\r\n            \/\/VTS,1,1,1140,3.75,CRD,15.5\r\n            \/\/ &lt;Snippet23&gt;\r\n            var taxiTripSample = new TaxiTrip()\r\n            {\r\n                VendorId = &quot;VTS&quot;,\r\n                RateCode = &quot;1&quot;,\r\n                PassengerCount = 1,\r\n                TripTime = 1140,\r\n                TripDistance = 3.75f,\r\n                PaymentType = &quot;CRD&quot;,\r\n                FareAmount = 0 \/\/ To predict. Actual\/Observed = 15.5\r\n            };\r\n            \/\/ &lt;\/Snippet23&gt;\r\n            \/\/ &lt;Snippet24&gt;\r\n            var prediction = predictionFunction.Predict(taxiTripSample);\r\n            \/\/ &lt;\/Snippet24&gt;\r\n            \/\/ &lt;Snippet25&gt;\r\n            Console.WriteLine($&quot;**********************************************************************&quot;);\r\n            Console.WriteLine($&quot;Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5&quot;);\r\n            Console.WriteLine($&quot;**********************************************************************&quot;);\r\n            \/\/ &lt;\/Snippet25&gt;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n*************************************************\r\n*       Model quality metrics evaluation\r\n*------------------------------------------------\r\n*       RSquared Score:      0.71\r\n*       Root Mean Squared Error:      5.86\r\n*************************************************\r\n**********************************************************************\r\nPredicted fare: 14.7698, actual fare: 15.5\r\n**********************************************************************\r\n<\/pre>\n<p><a href=\"https:\/\/github.com\/gantovnik\/wordpress_examples\/tree\/main\/ex308\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/gantovnik\/wordpress_examples\/tree\/main\/ex308<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data file: Download the and place to Data folder two files: taxi-fare-test.csv and taxi-fare-train.csv TaxiTrip.cs \/\/ &lt;Snippet1&gt; using Microsoft.ML.Data; \/\/ &lt;\/Snippet1&gt; namespace TaxiFarePrediction { \/\/ &lt;Snippet2&gt; public class TaxiTrip { &#x5B;LoadColumn(0)] public string VendorId; &#x5B;LoadColumn(1)] public string RateCode; &#x5B;LoadColumn(2)] public float PassengerCount; &#x5B;LoadColumn(3)] public float TripTime; &#x5B;LoadColumn(4)] public float TripDistance; &#x5B;LoadColumn(5)] public string PaymentType; &#x5B;LoadColumn(6)] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[50],"tags":[],"class_list":["post-1611","post","type-post","status-publish","format-standard","hentry","category-c"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-pZ","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1599,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/10\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-4-2\/","url_meta":{"origin":1611,"position":0},"title":"#306 Analyze sentiment of website comments with binary classification in ML.NET using C#","author":"gantovnik","date":"2022-10-27","format":false,"excerpt":"Data file: yelp_labelled.txt SentimentData.cs [code language=\"csharp\"] using Microsoft.ML.Data; \/\/ <\/SnippetAddUsings> namespace SentimentAnalysis { \/\/ <SnippetDeclareTypes> public class SentimentData { [LoadColumn(0)] public string SentimentText; [LoadColumn(1), ColumnName(\"Label\")] public bool Sentiment; } public class SentimentPrediction : SentimentData { [ColumnName(\"PredictedLabel\")] public bool Prediction { get; set; } public float Probability { get; set; }\u2026","rel":"","context":"In &quot;C#&quot;","block_context":{"text":"C#","link":"https:\/\/gantovnik.com\/bio-tips\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1596,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/10\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-4\/","url_meta":{"origin":1611,"position":1},"title":"#305 Line plot using C#","author":"gantovnik","date":"2022-10-26","format":false,"excerpt":"Temperature.cs [code language=\"python\"] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WindowsFormsApp1 { public class Temperature { public string Location { get; set; } public decimal M1 { get; set; } public decimal M2 { get; set; } public decimal M3 { get; set; } public decimal\u2026","rel":"","context":"In &quot;C#&quot;","block_context":{"text":"C#","link":"https:\/\/gantovnik.com\/bio-tips\/category\/c\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/10\/2022-10-26_041420-300x276.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1603,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/10\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-4-2-2\/","url_meta":{"origin":1611,"position":2},"title":"#307 Categorize support issues using multiclass classification with ML.NET using C#","author":"gantovnik","date":"2022-10-28","format":false,"excerpt":"Data file: Download the and place to Data folder two files: issues_train.tsv and the issues_test.tsv GitHubIssueData.cs [code language=\"csharp\"] \/\/ <SnippetAddUsings> using Microsoft.ML.Data; \/\/ <\/SnippetAddUsings> namespace GitHubIssueClassification { \/\/ <SnippetDeclareTypes> public class GitHubIssue { [LoadColumn(0)] public string ID { get; set; } [LoadColumn(1)] public string Area { get; set; } [LoadColumn(2)]\u2026","rel":"","context":"In &quot;C#&quot;","block_context":{"text":"C#","link":"https:\/\/gantovnik.com\/bio-tips\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2094,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/409-rain-drops-animation-using-matplotlib-in-python\/","url_meta":{"origin":1611,"position":3},"title":"#409 Rain drops &#8211; animation using matplotlib in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation # Fixing random state for reproducibility np.random.seed(19680801) # Create new Figure and an Axes which fills it. fig = plt.figure(figsize=(7, 7)) ax = fig.add_axes([0, 0, 1, 1], frameon=False) ax.set_xlim(0, 1), ax.set_xticks([]) ax.set_ylim(0, 1), ax.set_yticks([]) # Create\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex409.gif?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1686,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-4-2-2-2-2-2-3\/","url_meta":{"origin":1611,"position":4},"title":"#319 Scatter plot using ScottPlot.WinForms and C#","author":"gantovnik","date":"2022-11-29","format":false,"excerpt":"1) Install the ScottPlot.WinForms NuGet package 2) Drag a FormsPlot from the toolbox onto the form Form1.cs [code language=\"csharp\"] namespace WinFormsAppEx319 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void formsPlot1_Load(object sender, EventArgs e) { \/\/ generate some random X\/Y data int pointCount =\u2026","rel":"","context":"In &quot;C#&quot;","block_context":{"text":"C#","link":"https:\/\/gantovnik.com\/bio-tips\/category\/c\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex319.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex319.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex319.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex319.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":926,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/06\/166-solution-of-a-differential-equation-using-bubnov-galerkin-method-with-sympy-package\/","url_meta":{"origin":1611,"position":5},"title":"#166 Solution of a differential equation using Bubnov-Galerkin method with Sympy package","author":"gantovnik","date":"2021-06-15","format":false,"excerpt":"#166 Solution of a differential equation using Bubnov-Galerkin method with Sympy package The problem and solution in this pdf file: ex166 [code language=\"python\"] import sympy from matplotlib import pyplot as plt import seaborn as sns import numpy as np from sympy.utilities.lambdify import lambdify from sympy import simplify from sympy import\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=1611"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1611\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}