public static void getChartSeries(IDictionary<double, PredictorData> predictorData, PredictionModel model, ChartDataSeries tseries, ChartDataSeries pseries, bool isPace) { tseries.Points.Clear(); pseries.Points.Clear(); foreach (PredictorData t in predictorData.Values) { if (t.result.ContainsKey(model)) { TimePredictionResult r = t.result[model]; float x = (float)UnitUtil.Distance.ConvertFrom(t.Distance); if (!x.Equals(float.NaN)) { if (tseries.Points.IndexOfKey(x) == -1) { tseries.Points.Add(x, new PointF(x, (float)r.PredictedTime)); } float y = (float)UnitUtil.PaceOrSpeed.ConvertFrom(isPace, t.Distance/r.PredictedTime); if (pseries.Points.IndexOfKey(x) == -1) { pseries.Points.Add(x, new PointF(x, y)); } } } } }
public static String Name(PredictionModel model) { switch (model) { default: case PredictionModel.DAVE_CAMERON: return "Dave Cameron"; case PredictionModel.PETE_RIEGEL: return "Pete Riegel"; case PredictionModel.WAVA: return "WAVA"; case PredictionModel.VDOT: return "Vdot"; case PredictionModel.ELINDER: return "Elinder"; case PredictionModel.PURDY: return "Purdy Points"; } }
public static ChartColors Colors(PredictionModel model) { switch (model) { default: case PredictionModel.DAVE_CAMERON: return new ChartColors(Color.FromArgb(0xff, 0xff, 0x00, 0x00), Color.FromArgb(0x70, 0xf0, 0xc0, 0xc0), Color.FromArgb(0xc8, 0xf0, 0xa0, 0xa0)); case PredictionModel.PETE_RIEGEL: return new ChartColors(Color.FromArgb(0xff, 0x00, 0x26, 0xff), Color.FromArgb(0x70, 0xcc, 0xdd, 0xff), Color.FromArgb(0xc8, 0x77, 0x88, 0xff)); case PredictionModel.WAVA: return new ChartColors(Color.FromArgb(0xff, 0xe8, 0xc9, 0x00), Color.FromArgb(0x70, 0xee, 0xdd, 0xcc), Color.FromArgb(0xc8, 0xff, 0xee, 0x77)); case PredictionModel.VDOT: return new ChartColors(Color.FromArgb(0xff, 0x00, 0xf0, 0x8a), Color.FromArgb(0x70, 0xcc, 0xff, 0xdd), Color.FromArgb(0xc8, 0x77, 0xff, 0x99)); case PredictionModel.ELINDER: return new ChartColors(Color.FromArgb(0xff, 0xff, 0x00, 0xdc), Color.FromArgb(0x70, 0xff, 0xcc, 0xee), Color.FromArgb(0xc8, 0xff, 0x77, 0xee)); case PredictionModel.PURDY: return new ChartColors(Color.FromArgb(0xff, 0x88, 0x88, 0x88), Color.FromArgb(0x70, 0xdd, 0xdd, 0xdd), Color.FromArgb(0xc8, 0xbb, 0xbb, 0xbb)); } }
public static async Task <PredictionModel <ExchangeRate, ExchangeRatePrediction> > Train() { var pipeline = new LearningPipeline(); pipeline.Add(new TextLoader(_datapath).CreateFrom <ExchangeRate>(useHeader: true, separator: ',')); pipeline.Add(new ColumnCopier(("USDToCAD", "Label"))); pipeline.Add(new CategoricalOneHotVectorizer("USDToEUR", "USDToJPY", "USDToCZK", "USDToGBP", "USDToAUD", "USDToBRL", "USDToCNY", "USDToZAR", "USDToMXN", "USDToARS", "USDToCHF", "USDToINR", "USDToVND", "USDToZMW", "USDToIDR", "USDToIQD", "USDToIRR", "DateYear", "DateMonth", "DayofMonth", "DayOfWeek")); pipeline.Add(new ColumnConcatenator("Features", "USDToEUR", "USDToJPY", "USDToCZK", "USDToGBP", "USDToAUD", "USDToBRL", "USDToCNY", "USDToZAR", "USDToMXN", "USDToARS", "USDToCHF", "USDToINR", "USDToVND", "USDToZMW", "USDToIDR", "USDToIQD", "USDToIRR", "DateYear", "DateMonth", "DayofMonth", "DayOfWeek")); pipeline.Add(new FastTreeRegressor()); PredictionModel <ExchangeRate, ExchangeRatePrediction> model = pipeline.Train <ExchangeRate, ExchangeRatePrediction>(); await model.WriteAsync(_modelpath); return(model); }
//aka NeighborhoodBasedRecs public static List <string> CollaborativeBasedRecommendation(int userId, int take) { List <string> itemIds = new List <string>(); //TODO 3 - replace the following lines int neighborhoodSize = 15; double minSim = 0.0; int maxCandidates = 100; //inside this we do the implict rating of events for the user... Hashtable userRatedItems = GetRatedItems(userId, 100); if (userRatedItems.Count == 0) { return(new List <string>()); } //this is the mean rating a user gave double ratingSum = 0; foreach (double r in userRatedItems.Values) { ratingSum += r; } double userMean = ratingSum / userRatedItems.Count; //get similar items List <SimilarItem> candidateItems = GetCandidateItems(userRatedItems.Keys, minSim); //sort by similarity desc, take only max candidates candidateItems = candidateItems.OrderByDescending(c => c.similarity).Take(maxCandidates).ToList(); Hashtable recs = new Hashtable(); List <PredictionModel> precRecs = new List <PredictionModel>(); foreach (SimilarItem candidate in candidateItems) { int target = candidate.Target; double pre = 0; double simSum = 0; List <SimilarItem> ratedItems = candidateItems.Where(c => c.Target == target).Take(neighborhoodSize).ToList(); if (ratedItems.Count > 1) { foreach (SimilarItem simItem in ratedItems) { try { string source = userRatedItems[simItem.sourceItemId].ToString(); //rating of the movie - userMean; double r = double.Parse(source) - userMean; pre += simItem.similarity * r; simSum += simItem.similarity; if (simSum > 0) { PredictionModel p = new PredictionModel(); p.Prediction = userMean + pre / simSum; p.Items = ratedItems; precRecs.Add(p); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } //sort based on the prediction, only take x of them List <PredictionModel> sortedItems = precRecs.OrderByDescending(c => c.Prediction).Take(take).ToList(); //get first model's items... foreach (PredictionModel pm in sortedItems) { foreach (SimilarItem ri in pm.Items) { if (ri.targetItemId != null) { itemIds.Add(ri.targetItemId.ToString()); break; } } } return(itemIds); }
private async Task <PredictionModel <PivotData, ClusteringPrediction> > LoadModel() { ConsoleWriteHeader("Read model"); Console.WriteLine($"Model location: {modelLocation}"); return(await PredictionModel.ReadAsync <PivotData, ClusteringPrediction>(modelLocation)); }
// <Snippet4> public static PredictionModel <SentimentData, SentimentPrediction> TrainAndPredict() // </Snippet4> { // LearningPipeline allows us to add steps in order to keep everything together // during the learning process. // <Snippet5> var pipeline = new LearningPipeline(); // </Snippet5> // The TextLoader loads a dataset with comments and corresponding postive or negative sentiment. // When you create a loader you specify the schema by passing a class to the loader containing // all the column names and their types. This will be used to create the model, and train it. // <Snippet6> pipeline.Add(new TextLoader <SentimentData>(_dataPath, useHeader: false, separator: "tab")); // </Snippet6> // TextFeaturizer is a transform that will be used to featurize an input column. // This is used to format and clean the data. // <Snippet7> pipeline.Add(new TextFeaturizer("Features", "SentimentText")); //</Snippet7> //add a FastTreeBinaryClassifier, the decision tree learner for this project, and //three hyperparameters to be used for tuning decision tree performance // <Snippet8> pipeline.Add(new FastTreeBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 }); // </Snippet8> // We train our pipeline based on the dataset that has been loaded, transformed // <Snippet9> PredictionModel <SentimentData, SentimentPrediction> model = pipeline.Train <SentimentData, SentimentPrediction>(); // </Snippet9> //add some comments to test the trained model's predictions // <Snippet10> IEnumerable <SentimentData> sentiments = new[] { new SentimentData { SentimentText = "Contoso's 11 is a wonderful experience", Sentiment = 0 }, new SentimentData { SentimentText = "The acting in this movie is very bad", Sentiment = 0 }, new SentimentData { SentimentText = "Joe versus the Volcano Coffee Company is a great film.", Sentiment = 0 } }; // </Snippet10> // Now that we have a model, use that to predict the positive // or negative sentiment of the comment data. // <Snippet11> IEnumerable <SentimentPrediction> predictions = model.Predict(sentiments); // </Snippet11> // <Snippet12> Console.WriteLine(); Console.WriteLine("Sentiment Predictions"); Console.WriteLine("---------------------"); // </Snippet12> // Build pairs of (sentiment, prediction) // <Snippet13> var sentimentsAndPredictions = sentiments.Zip(predictions, (sentiment, prediction) => (sentiment, prediction)); // </Snippet13> // <Snippet14> foreach (var item in sentimentsAndPredictions) { Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(item.prediction.Sentiment ? "Positive" : "Negative")}"); } Console.WriteLine(); // </Snippet14> //return the model we trained to use for evaluation // <Snippet15> return(model); // </Snippet15> }
//private void saveDateTimeOfUser(string userID, string connectionString, string loginTime, string date, string publicIP, string publicMAC) //{ // SqlConnection con; // SqlCommand cmd; // con = new SqlConnection(connectionString); // string currentHostname = System.Environment.MachineName.ToString(); // con.Open(); // try // { // cmd = new SqlCommand("INSERT INTO [dbo].[LogAnalysis] (UserID, LoginTime, LoginDate, IpAddress , MacAddress , hostname) VALUES (@UserID, @LoginTime, @LoginDate , @IPAddress , @MACAddress , @HostName)", con); // cmd.Parameters.AddWithValue("@UserID", userID); // cmd.Parameters.AddWithValue("@LoginTime", loginTime); // cmd.Parameters.AddWithValue("@LoginDate", date.ToString()); // cmd.Parameters.AddWithValue("@IPAddress", publicIP); // cmd.Parameters.AddWithValue("@MACAddress", publicMAC); // cmd.Parameters.AddWithValue("@HostName", currentHostname); // cmd.ExecuteNonQuery(); // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // } // finally // { // con.Close(); // } //} private void ForgotPassword3NextButton_Click(object sender, RoutedEventArgs e) { string selected_ForgotPasswordCode = UserModel.UserModel.twoFAcode; UserModel.UserModel cm = UserModel.UserModel._currentUserModel; string userID = cm.userID; if (ForgotPasswordCodeTextBox.Text == selected_ForgotPasswordCode) { MessageBox.Show("Correct!"); string date = AlgorithmLibary.PredictionModel.getCurrentDate(); string loginTime = DateTime.Now.ToString("HH.mm"); string publicIP = PredictionModel.getCurrentPublicIP(); string publicMAC = PredictionModel.getCurrentMAC(); Console.WriteLine(publicMAC + "HELLO"); string riskLevelStatement = "Low"; //Use the same class for saveDateTime Method - Justin Changed at 1:20 am on 6/8/2017 UserModel.UserModel.saveDateTimeOfUser(userID, connectionString, loginTime, date, publicIP, publicMAC); string exist = UserModel.UserModel.checkFollowUp(userID, connectionString); string selected_UserID = (App.Current as App).LoginUserID; try { string connectionString = conSettings.ConnectionString; con = new SqlConnection(connectionString); con.Open(); cmd = new SqlCommand("DELETE FROM [dbo].[FailedAttempt] where UserID = '" + selected_UserID + "'", con); cmd.ExecuteNonQuery(); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message); } finally { con.Close(); } if (exist != null) { UserModel.UserModel.updateFollowUp(userID, connectionString, "False"); } else { UserModel.UserModel.saveFollowUp(userID, connectionString, "False"); } Page cloud = new StartupPage(); PredictionModel.SessionRiskValue = riskLevelStatement; this.NavigationService.Navigate(cloud); } else { MessageBox.Show("Invalid code! Please Try Again"); //Remove the statement below because it will conflict with my fe counter++; if (counter > 3) { MessageBox.Show("More than 3 attempts! Account will be locked now!"); string exist = UserModel.UserModel.checkFollowUp(userID, connectionString); if (exist != null) { UserModel.UserModel.updateFollowUp(userID, connectionString, "True"); } else { UserModel.UserModel.saveFollowUp(userID, connectionString, "True"); } Page LoginPage = new LoginPage(); this.NavigationService.Navigate(LoginPage); } } }
/// <summary> /// This function creates a prediction engine from the model located in the <paramref name="modelPath"/>. /// </summary> private async Task <PredictionModel <CountryData, CountrySalesPrediction> > CreatePredictionEngineAsync(string modelPath) { PredictionModel <CountryData, CountrySalesPrediction> model = await PredictionModel.ReadAsync <CountryData, CountrySalesPrediction>(modelPath); return(model); }
/// <summary> /// Setup the predictor. /// </summary> public void CalcPrediction() { if (_pm.SelectedProject.Configuration.SubsystemConfigDict.ContainsKey(ConfigKey)) { PredictionModelInput predInput = new PredictionModelInput(_pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].SubsystemConfig.SubSystem); if (_pm.SelectedProject.Configuration.DeploymentOptions.Duration <= 0) { predInput.DeploymentDuration = 1; } else { predInput.DeploymentDuration = _pm.SelectedProject.Configuration.DeploymentOptions.Duration; } // Absorption predInput.Temperature = _pm.SelectedProject.Configuration.Commands.CWT; predInput.Salinity = _pm.SelectedProject.Configuration.Commands.CWS; predInput.XdcrDepth = _pm.SelectedProject.Configuration.Commands.CTD; predInput.CEI = _pm.SelectedProject.Configuration.Commands.CEI.ToSecondsD(); predInput.CWPON = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CWPON; predInput.CBTON = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CBTON; predInput.CBTTBP = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CBTTBP; predInput.CBTBB_TransmitPulseType = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CBTBB_Mode; //predInput.CBTBB_TransmitPulseType = Commands.AdcpSubsystemCommands.eCBTBB_Mode.NARROWBAND_LONG_RANGE; // DEFAULT to narrowband because it autoswitches now predInput.CWPTBP = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CWPTBP; predInput.CWPBN = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CWPBN; predInput.CWPBS = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CWPBS; predInput.CWPBL = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CWPBL; predInput.CWPBB_TransmitPulseType = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CWPBB_TransmitPulseType; predInput.CWPBB_LagLength = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CWPBB_LagLength; predInput.CWPP = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CWPP; predInput.BatteryType = _pm.SelectedProject.Configuration.DeploymentOptions.BatteryType; predInput.CBI_BurstInterval = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CBI_BurstInterval.ToSecondsD(); predInput.CBI_SamplesPerBurst = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CBI_NumEnsembles; predInput.CBI_IsInterleaved = _pm.SelectedProject.Configuration.SubsystemConfigDict[ConfigKey].Commands.CBI_BurstPairFlag; // Create Prediction model PredictionModel Predictor = new PredictionModel(); // Set the prediction values PredictionModel.PredictedRanges ranges = Predictor.GetPredictedRange(predInput); PredictedBottomRange = ranges.BottomTrack; PredictedProfileRange = ranges.WaterProfile; ProfileFirstBinPosition = ranges.FirstBinPosition.ToString("0.0000"); double maxVel = Predictor.GetMaxVelocity(predInput); MaximumVelocity = maxVel.ToString("0.0000"); double std = Predictor.GetStandardDeviation(predInput); StandardDeviation = std.ToString("0.0000"); double numBatts = Predictor.BatteryUsage(predInput); NumberBatteryPacks = numBatts; if (CBI_NumEnsembles > 0) { NumberBytes = Predictor.GetDataStorageBurst(predInput); } else { NumberBytes = Predictor.GetDataStorage(predInput); } if (RangeVM != null) { // Update all the properites RangeVM.WaterProfileRange = ranges.WaterProfile; RangeVM.BottomTrackRange = ranges.BottomTrack; RangeVM.WpBlank = predInput.CWPBL; RangeVM.WpFirstBinRange = ranges.FirstBinPosition; if (_pm.IsProjectSelected && _pm.SelectedProject.Configuration != null) { RangeVM.DepthToBottom = predInput.XdcrDepth; } } //this.NotifyOfPropertyChange(() => this.CWPP); //this.NotifyOfPropertyChange(() => this.CWPTBP); //this.NotifyOfPropertyChange(() => this.CWPBS); //this.NotifyOfPropertyChange(() => this.CWPBN); //this.NotifyOfPropertyChange(() => this.CWPBL); //this.NotifyOfPropertyChange(() => this.CWPBB_TransmitPulseType); //this.NotifyOfPropertyChange(() => this.CWPBB_LagLength); //this.NotifyOfPropertyChange(() => this.CBTON); //this.NotifyOfPropertyChange(() => this.CBI_BurstInterval); //this.NotifyOfPropertyChange(() => this.DataSize); //this.NotifyOfPropertyChange(() => this.NumberBatteryPackStr); //this.NotifyOfPropertyChange(() => this.PredictedBottomRangeStr); //this.NotifyOfPropertyChange(() => this.PredictedProfileRangeStr); //this.NotifyOfPropertyChange(() => this.ProfileFirstBinPosition); //this.NotifyOfPropertyChange(() => this.MaximumVelocity); //this.NotifyOfPropertyChange(() => this.StandardDeviation); } }
public async Task start() { bool alwayCreateModel = true; //是否建立模型文档 string labelColumn = "student"; //需要计算的字段 student string[] oneHotColumns = new string[] { }; //oneHot编码字段 string[] features = new string[] { "teacher", "classCount" }; //特征列, "classCount" "student" var curTypeNameIndex = 4; //幼儿园 1小学 4中学 5大学 6驾校 7教育其他 var tableName = "go007_School"; //条件筛选项 var studentExistQuery = Query.And(Query.Exists("student_new", true), Query.NE("student_new", "")); var teachExistQuery = Query.And(Query.Exists("teacher", true), Query.NE("teacher", "")); var classExistQuery = Query.And(Query.Exists("classCount_new", true), Query.NE("classCount_new", "")); var studentNoExistQuery = Query.Or(Query.Exists("student_new", false), Query.EQ("student_new", "")); var teachNoExistQuery = Query.Or(Query.Exists("teacher", false), Query.EQ("teacher", "")); var classNoExistQuery = Query.Or(Query.Exists("classCount_new", false), Query.EQ("classCount_new", "")); var conStr = GetConnectionStr("192.168.1.121", "SimpleCrawler"); var dataop = new MongoRepositoryHelperA3(conStr); //训练评估查询语句 var traningQuery = Query.And(Query.EQ("typeNameIndex", curTypeNameIndex), teachExistQuery, studentExistQuery, classExistQuery); //预测语句 var predicQuery = Query.And(Query.EQ("typeNameIndex", curTypeNameIndex), teachExistQuery, classExistQuery, studentNoExistQuery); var allExistSchoolDocList = dataop.FindByQuery(tableName, traningQuery).SetFields("id", "typeNameIndex", "teacher", "classCount_new", "cityGuid", "student_new", "buildingArea").OrderBy(c => c["id"]).ToList(); var allCount = allExistSchoolDocList.Count(); int traningCount = (int)(allCount * 0.7); //评估的数据源个数 var traningSchoolList = new List <School>(); //训练数据 var evaluateSchoolList = new List <School>(); //评估数据 foreach (var traningSchool in allExistSchoolDocList) { var curSchool = new School() { // buildingArea = traningSchool.Float("buildingArea"), // cityGuid = traningSchool.Text("cityGuid"), classCount = traningSchool.Float("classCount_new"), student = traningSchool.Float("student_new"), teacher = traningSchool.Float("teacher"), typeNameIndex = traningSchool.Float("typeNameIndex") }; if (traningSchoolList.Count <= traningCount) { traningSchoolList.Add(curSchool); } else { evaluateSchoolList.Add(curSchool); } } PredictionModel <School, SchoolStudentPrediction> model = null; ///模型地址 var modelFileName = $"{ModelDirectory}/{string.Join("_", features)}-{labelColumn}-{traningSchoolList.Count()}.zip"; if (!alwayCreateModel && File.Exists(modelFileName)) { model = await PredictionModel.ReadAsync <School, SchoolStudentPrediction>(modelFileName); // await在你的Main方法中添加一个方法意味着该Main方法必须具有async修饰符并返回a Task: // using System.Threading.Tasks; // 评估模型 } if (model == null) { //PredictionModel<School, SchoolStudentPrediction> model = TrainAsync(); //改变的返回类型Train方法意味着你必须一个补充await,以调用codde Train在Method下面的代码如下所示: model = await Train(traningSchoolList, modelFileName, labelColumn, oneHotColumns, features); } Evaluate(evaluateSchoolList, model); var prediction1 = model.Predict(TestTrips.Trip1); Console.WriteLine($"classCount:{TestTrips.Trip1.classCount}teacher:{TestTrips.Trip1.teacher} Predicted fare: {(int)prediction1.student} fare: {TestTrips.Trip1.student}"); Console.WriteLine("确认模型后按回车开始"); Console.ReadKey(); // var query = Query.And(Query.Exists("student", true), Query.Exists("teacher", true), Query.Exists("classCount", false)); var allSchoolList = dataop.FindByQuery(tableName, predicQuery).SetFields("student_new", "teacher", "typeNameIndex", "classCount_new", "cityGuid", "predicColumns", "predicColumns", "buildingArea").ToList(); foreach (var school in allSchoolList) { /// 比如没有班级字段通过模型的学生和老师个数来预测班级数 var typeNameIndex = school.Int("typeNameIndex"); var student = school.Int("student_new"); var teacher = school.Int("teacher"); var classCount = school.Int("classCount_new"); var predicBaseColumns = school.Contains("predicBaseColumns") ? school["predicBaseColumns"] as BsonArray ?? new BsonArray(): new BsonArray(); var predicColumns = school.Contains("predicColumns") ? school["predicColumns"] as BsonArray ?? new BsonArray() : new BsonArray(); School Trip1 = new School { typeNameIndex = typeNameIndex, classCount = classCount, teacher = teacher, student = student, //cityGuid = school.Text("cityGuid") }; var prediction = model.Predict(Trip1); var doc = new BsonDocument(); if (prediction.student != 0) { doc.Add($"{labelColumn}_new", (int)prediction.student); #region 操作日志字段 foreach (var feature in features) { //基于字段 if (!predicBaseColumns.Contains(feature)) { predicBaseColumns.Add(feature); } } ///预测的字段 if (!predicColumns.Contains(labelColumn)) { predicColumns.Add(labelColumn); } doc.Add("isPredic", 1);//是否预测字段 doc.Add("predicColumns", predicColumns); doc.Add("predicBaseColumns", predicBaseColumns); DBChangeQueue.Instance.EnQueue(new StorageData() { Name = tableName, Document = doc, Query = Query.EQ("_id", ObjectId.Parse(school.Text("_id"))), Type = StorageType.Update }); #endregion } Console.WriteLine($"student:{Trip1.student}teacher:{Trip1.teacher} classCount:{Trip1.classCount} Predicted fare: {(int)prediction.student}"); } StartDBChangeProcessQuick(dataop); Console.WriteLine("操作结束"); }
public static PredictTime Predictor(PredictionModel model) { switch (model) { default: case PredictionModel.DAVE_CAMERON: return Predict.Cameron; case PredictionModel.PETE_RIEGEL: return Predict.Riegel; case PredictionModel.WAVA: return Predict.Wava; case PredictionModel.VDOT: return Predict.Vdot; case PredictionModel.ELINDER: return Predict.Elinder; case PredictionModel.PURDY: return Predict.Purdy; } }
private static bool load() { //Backwards compatibility, read old preferences file String prefsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "PerformancePredictorPlugin" + Path.DirectorySeparatorChar + "preferences.xml"; if (!File.Exists(prefsPath)) return false; XmlDocument document = new XmlDocument(); XmlReader reader = new XmlTextReader(prefsPath); document.Load(reader); try { XmlNode elm = document.ChildNodes[0]["view"]; windowSize = new Size(int.Parse(elm.Attributes["viewWidth"].Value), int.Parse(elm.Attributes["viewHeight"].Value)); model = (PredictionModel) Enum.Parse(typeof(PredictionModel), elm.Attributes["metric"].Value); showChart = bool.Parse(elm.Attributes["showChart"].Value); parseDistances(elm.Attributes["distances"].Value.Split(';')); hsPercentOfDistance = int.Parse(elm.Attributes["percentOfDistance"].Value); minPercentOfDistance = int.Parse(elm.Attributes["minPercentOfDistance"].Value); //showPrediction = bool.Parse(elm.Attributes["showPrediction"].Value); showPace = bool.Parse(elm.Attributes["showPace"].Value); } catch (Exception) { reader.Close(); return false; } reader.Close(); return true; }
public static void ReadOptions(XmlDocument xmlDoc, XmlNamespaceManager nsmgr, XmlElement pluginNode) { String attr, attr2; attr = pluginNode.GetAttribute(xmlTags.settingsVersion); if (attr.Length > 0) { settingsVersion = (Int16)XmlConvert.ToInt16(attr); } if (0 == settingsVersion) { // No settings in Preferences.System found, try read old files load(); } attr = pluginNode.GetAttribute(xmlTags.predictionView); if (attr.Length > 0) { try { predictionView = (PredictionView)Enum.Parse(typeof(PredictionView), attr); } catch { } } //else //{ // attr = pluginNode.GetAttribute(xmlTags.showPrediction); // if (attr.Length > 0) // { // bool isPrediction = XmlConvert.ToBoolean(attr); // if (isPrediction) // { // predictionView = PredictionView.TimePrediction; // } // else // { // predictionView = PredictionView.Training; // } // } //} attr = pluginNode.GetAttribute(xmlTags.elinderBreakEvenTime); if (attr.Length > 0) { elinderBreakEvenTime = TimeSpan.FromSeconds(Settings.parseFloat(attr)); } attr = pluginNode.GetAttribute(xmlTags.riegelFatigueFactor); if (attr.Length > 0) { riegelFatigueFactor = Settings.parseFloat(attr); } attr = pluginNode.GetAttribute(xmlTags.idealBmi); if (attr.Length > 0) { idealBmi = Settings.parseFloat(attr); } attr = pluginNode.GetAttribute(xmlTags.idealShoe); if (attr.Length > 0) { idealShoe = Settings.parseFloat(attr); } attr = pluginNode.GetAttribute(xmlTags.showPace); if (attr.Length > 0) { showPace = XmlConvert.ToBoolean(attr); } attr = pluginNode.GetAttribute(xmlTags.showChart); if (attr.Length > 0) { showChart = XmlConvert.ToBoolean(attr); } attr = pluginNode.GetAttribute(xmlTags.minPercentOfDistance); if (attr.Length > 0) { minPercentOfDistance = XmlConvert.ToInt16(attr); } attr = pluginNode.GetAttribute(xmlTags.hsPercentOfDistance); if (attr.Length > 0) { hsPercentOfDistance = XmlConvert.ToInt16(attr); } attr = pluginNode.GetAttribute(xmlTags.model); if (attr.Length > 0) { model = (PredictionModel)Enum.Parse(typeof(PredictionModel), attr); } attr = pluginNode.GetAttribute(xmlTags.showToolBar); if (attr.Length > 0) { showToolBar = XmlConvert.ToBoolean(attr); } attr = pluginNode.GetAttribute(xmlTags.distances); if (attr.Length > 0) { distances = parseDistances(attr.Split(';')); } attr = pluginNode.GetAttribute(xmlTags.viewWidth); attr2 = pluginNode.GetAttribute(xmlTags.viewHeight); if (attr.Length > 0 && attr2.Length > 0) { windowSize = new Size(XmlConvert.ToInt16(attr), XmlConvert.ToInt16(attr2)); } }
private static void PaintChart(PredictionModel <TaxiTrip, TaxiTripFarePrediction> model, string testDataSetPath, int numberOfRecordsToRead, string[] args) { string chartFileName = ""; using (var pl = new PLStream()) { // use SVG backend and write to SineWaves.svg in current directory if (args.Length == 1 && args[0] == "svg") { pl.sdev("svg"); chartFileName = "TaxiRegressionDistribution.svg"; pl.sfnam(chartFileName); } else { pl.sdev("pngcairo"); chartFileName = "TaxiRegressionDistribution.png"; pl.sfnam(chartFileName); } // use white background with black foreground pl.spal0("cmap0_alternate.pal"); // Initialize plplot pl.init(); // set axis limits const int xMinLimit = 0; const int xMaxLimit = 40; //Rides larger than $40 are not shown in the chart const int yMinLimit = 0; const int yMaxLimit = 40; //Rides larger than $40 are not shown in the chart pl.env(xMinLimit, xMaxLimit, yMinLimit, yMaxLimit, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes); // Set scaling for mail title text 125% size of default pl.schr(0, 1.25); // The main title pl.lab("Measured", "Predicted", "Distribution of Taxi Fare Prediction"); // plot using different colors // see http://plplot.sourceforge.net/examples.php?demo=02 for palette indices pl.col0(1); int totalNumber = numberOfRecordsToRead; var testData = new TaxiTripCsvReader().GetDataFromCsv(testDataSetPath, totalNumber).ToList(); //This code is the symbol to paint char code = (char)9; // plot using other color //pl.col0(9); //Light Green //pl.col0(4); //Red pl.col0(2); //Blue double yTotal = 0; double xTotal = 0; double xyMultiTotal = 0; double xSquareTotal = 0; for (int i = 0; i < testData.Count; i++) { var x = new double[1]; var y = new double[1]; var FarePrediction = model.Predict(testData[i]); x[0] = testData[i].FareAmount; y[0] = FarePrediction.FareAmount; //Paint a dot pl.poin(x, y, code); xTotal += x[0]; yTotal += y[0]; double multi = x[0] * y[0]; xyMultiTotal += multi; double xSquare = x[0] * x[0]; xSquareTotal += xSquare; double ySquare = y[0] * y[0]; Console.WriteLine($"-------------------------------------------------"); Console.WriteLine($"Predicted : {FarePrediction.FareAmount}"); Console.WriteLine($"Actual: {testData[i].FareAmount}"); Console.WriteLine($"-------------------------------------------------"); } // Regression Line calculation explanation: // https://www.khanacademy.org/math/statistics-probability/describing-relationships-quantitative-data/more-on-regression/v/regression-line-example double minY = yTotal / totalNumber; double minX = xTotal / totalNumber; double minXY = xyMultiTotal / totalNumber; double minXsquare = xSquareTotal / totalNumber; double m = ((minX * minY) - minXY) / ((minX * minX) - minXsquare); double b = minY - (m * minX); //Generic function for Y for the regression line // y = (m * x) + b; double x1 = 1; //Function for Y1 in the line double y1 = (m * x1) + b; double x2 = 39; //Function for Y2 in the line double y2 = (m * x2) + b; var xArray = new double[2]; var yArray = new double[2]; xArray[0] = x1; yArray[0] = y1; xArray[1] = x2; yArray[1] = y2; pl.col0(4); pl.line(xArray, yArray); // end page (writes output to disk) pl.eop(); // output version of PLplot pl.gver(out var verText); Console.WriteLine("PLplot version " + verText); } // the pl object is disposed here // Open Chart File In Microsoft Photos App (Or default app, like browser for .svg) Console.WriteLine("Showing chart..."); var p = new Process(); string chartFileNamePath = @".\" + chartFileName; p.StartInfo = new ProcessStartInfo(chartFileNamePath) { UseShellExecute = true }; p.Start(); }
public List <MetricItem> EvaluateModel(Conductor_Shared.Version pVersion, PredictionModel pModel) { try { //check if version defines a special bucketing algorithm int bucketing = BucketingType; switch (pVersion.DatasetType) { case DatasetType.Generic: bucketing = 1; break; case DatasetType.Cargo2000: bucketing = 2; break; default: bucketing = 1; break; } //get raw data var data = fsManager.GetModelResults(pVersion, pModel); if (data != null && data.Count > 1) { //generate lines List <Line> lines = Line.GetLinesFromData(data, pVersion.DatasetType == DatasetType.Generic); //generate buckets from lines List <Bucket> buckets = Bucketing.CreateBuckets(BucketGranularity, BucketingType, lines); List <MetricItem> metrics = new List <MetricItem>(); if (buckets.Any(t => !double.IsNaN(t.MCC))) { metrics.Add(new MetricItem() { Name = "MCC", Value = buckets.Where(t => !double.IsNaN(t.MCC)).Average(t => t.MCC) }); } if (buckets.Any(t => !double.IsNaN(t.Accuracy))) { metrics.Add(new MetricItem() { Name = "Accuracy", Value = buckets.Where(t => !double.IsNaN(t.Accuracy)).Average(t => t.Accuracy) }); } if (buckets.Any(t => !double.IsNaN(t.Precision))) { metrics.Add(new MetricItem() { Name = "Precision", Value = buckets.Where(t => !double.IsNaN(t.Precision)).Average(t => t.Precision) }); } if (buckets.Any(t => !double.IsNaN(t.Recall))) { metrics.Add(new MetricItem() { Name = "Recall", Value = buckets.Where(t => !double.IsNaN(t.Recall)).Average(t => t.Recall) }); } if (buckets.Any(t => !double.IsNaN(t.FMeasure))) { metrics.Add(new MetricItem() { Name = "FMetric", Value = buckets.Where(t => !double.IsNaN(t.FMeasure)).Average(t => t.FMeasure) }); } if (buckets.Any(t => !double.IsNaN(t.Specificity))) { metrics.Add(new MetricItem() { Name = "Speceficity", Value = buckets.Where(t => !double.IsNaN(t.Specificity)).Average(t => t.Specificity) }); } if (buckets.Any(t => !double.IsNaN(t.NegativePredictedValue))) { metrics.Add(new MetricItem() { Name = "Negative Predictions", Value = buckets.Where(t => !double.IsNaN(t.NegativePredictedValue)).Average(t => t.NegativePredictedValue) }); } if (buckets.Any(t => !double.IsNaN(t.FalsePositiveRate))) { metrics.Add(new MetricItem() { Name = "False Positive Rate", Value = buckets.Where(t => !double.IsNaN(t.FalsePositiveRate)).Average(t => t.FalsePositiveRate) }); } if (buckets.Any(t => !double.IsNaN(t.MAE))) { metrics.Add(new MetricItem() { Name = "MAE", Value = buckets.Where(t => !double.IsNaN(t.MAE)).Average(t => t.MAE) }); } if (buckets.Any(t => !double.IsNaN(t.MSE))) { metrics.Add(new MetricItem() { Name = "MSE", Value = buckets.Where(t => !double.IsNaN(t.MSE)).Average(t => t.MSE) }); } if (buckets.Any(t => !double.IsNaN(t.RMSE))) { metrics.Add(new MetricItem() { Name = "RMSE", Value = buckets.Where(t => !double.IsNaN(t.RMSE)).Average(t => t.RMSE) }); } if (buckets.Any(t => !double.IsNaN(t.RAE))) { metrics.Add(new MetricItem() { Name = "RAE", Value = buckets.Where(t => !double.IsNaN(t.RAE)).Average(t => t.RAE) }); } if (buckets.Any(t => !double.IsNaN(t.RSE))) { metrics.Add(new MetricItem() { Name = "RSE", Value = buckets.Where(t => !double.IsNaN(t.RSE)).Average(t => t.RSE) }); } if (buckets.Any(t => !double.IsNaN(t.RRSE))) { metrics.Add(new MetricItem() { Name = "RRSE", Value = buckets.Where(t => !double.IsNaN(t.RRSE)).Average(t => t.RRSE) }); } return(metrics); } } catch (Exception ex) { NotifyNewLogMessageEvent($"something went wrong while evaluating {pVersion.ToString()} {pModel.ModelFileName}: {ex.Message}"); } return(null); }
public void TrainAndPredictIrisModelTest() { string dataPath = GetDataPath("iris.txt"); var pipeline = new LearningPipeline(); pipeline.Add(new TextLoader <IrisData>(dataPath, useHeader: true, separator: "tab")); pipeline.Add(new ColumnConcatenator(outputColumn: "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); pipeline.Add(new StochasticDualCoordinateAscentClassifier()); PredictionModel <IrisData, IrisPrediction> model = pipeline.Train <IrisData, IrisPrediction>(); IrisPrediction prediction = model.Predict(new IrisData() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); Assert.Equal(1, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(0, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisData() { SepalLength = 3.1f, SepalWidth = 5.5f, PetalLength = 2.2f, PetalWidth = 6.4f, }); Assert.Equal(0, prediction.PredictedLabels[0], 2); Assert.Equal(0, prediction.PredictedLabels[1], 2); Assert.Equal(1, prediction.PredictedLabels[2], 2); prediction = model.Predict(new IrisData() { SepalLength = 3.1f, SepalWidth = 2.5f, PetalLength = 1.2f, PetalWidth = 4.4f, }); Assert.Equal(.2, prediction.PredictedLabels[0], 1); Assert.Equal(.8, prediction.PredictedLabels[1], 1); Assert.Equal(0, prediction.PredictedLabels[2], 2); // Note: Testing against the same data set as a simple way to test evaluation. // This isn't appropriate in real-world scenarios. string testDataPath = GetDataPath("iris.txt"); var testData = new TextLoader <IrisData>(testDataPath, useHeader: true, separator: "tab"); var evaluator = new ClassificationEvaluator(); evaluator.OutputTopKAcc = 3; ClassificationMetrics metrics = evaluator.Evaluate(model, testData); Assert.Equal(.98, metrics.AccuracyMacro); Assert.Equal(.98, metrics.AccuracyMicro, 2); Assert.Equal(.06, metrics.LogLoss, 2); Assert.InRange(metrics.LogLossReduction, 94, 96); Assert.Equal(1, metrics.TopKAccuracy); Assert.Equal(3, metrics.PerClassLogLoss.Length); Assert.Equal(0, metrics.PerClassLogLoss[0], 1); Assert.Equal(.1, metrics.PerClassLogLoss[1], 1); Assert.Equal(.1, metrics.PerClassLogLoss[2], 1); ConfusionMatrix matrix = metrics.ConfusionMatrix; Assert.Equal(3, matrix.Order); Assert.Equal(3, matrix.ClassNames.Count); Assert.Equal("0", matrix.ClassNames[0]); Assert.Equal("1", matrix.ClassNames[1]); Assert.Equal("2", matrix.ClassNames[2]); Assert.Equal(49, matrix[0, 0]); Assert.Equal(49, matrix["0", "0"]); Assert.Equal(0, matrix[0, 1]); Assert.Equal(0, matrix["0", "1"]); Assert.Equal(0, matrix[0, 2]); Assert.Equal(0, matrix["0", "2"]); Assert.Equal(0, matrix[1, 0]); Assert.Equal(0, matrix["1", "0"]); Assert.Equal(48, matrix[1, 1]); Assert.Equal(48, matrix["1", "1"]); Assert.Equal(2, matrix[1, 2]); Assert.Equal(2, matrix["1", "2"]); Assert.Equal(0, matrix[2, 0]); Assert.Equal(0, matrix["2", "0"]); Assert.Equal(1, matrix[2, 1]); Assert.Equal(1, matrix["2", "1"]); Assert.Equal(49, matrix[2, 2]); Assert.Equal(49, matrix["2", "2"]); }
void Start() { CLAB = GameObject.Find("CLAB"); predictionModel = gameObject.GetComponent <PredictionModel>(); }
public static void defaults() { predictionView = PredictionView.TimePrediction; idealBmi = 18.5f; idealShoe = 0.100f; showPace = true; showChart = false;//show chart by default minPercentOfDistance = 10; hsPercentOfDistance = 40; model = PredictionModelUtil.Default; elinderBreakEvenTime = TimeSpan.FromHours(2); riegelFatigueFactor = 1.06f; showToolBar = true; distances.Clear(); //Some distances removed by default //addDistance(100, Length.Units.Meter, false); //addDistance(200, Length.Units.Meter, false); //addDistance(400, Length.Units.Meter, false); //addDistance(500, Length.Units.Meter, false); //addDistance(800, Length.Units.Meter, false); addDistance(1, Length.Units.Kilometer, false); //addDistance(1.5, Length.Units.Kilometer, false); addDistance(1, Length.Units.Mile, false); addDistance(2, Length.Units.Kilometer, false); //addDistance(3, Length.Units.Kilometer, false); addDistance(2, Length.Units.Mile, false); //addDistance(4, Length.Units.Kilometer, false); //addDistance(3, Length.Units.Mile, false); addDistance(5, Length.Units.Kilometer, false); //addDistance(4, Length.Units.Mile, false); //addDistance(8, Length.Units.Kilometer, false); addDistance(5, Length.Units.Mile, false); addDistance(10, Length.Units.Kilometer, false); //addDistance(15, Length.Units.Kilometer, false); addDistance(10, Length.Units.Mile, false); addDistance(20, Length.Units.Kilometer, false); addDistance(21097.5, Length.Units.Meter, true); //addDistance(15, Length.Units.Mile, false); addDistance(25, Length.Units.Kilometer, false); addDistance(30, Length.Units.Kilometer, false); addDistance(20, Length.Units.Mile, false); //addDistance(25, Length.Units.Mile, false); addDistance(42195, Length.Units.Meter, true); windowSize = new Size(800, 600); }
public static void TestModel(PredictionModel <NumberData, NumberPrediction> model) { Random rd = new Random(); int pp = 0; int pn = 0; int np = 0; int nn = 0; int randomGuessesRight = 0; int modelGetsRight = 0; int N = 10000; Hashtable seenBefore = new Hashtable(); for (int i = 0; i < N;) { BigInteger a = new BigInteger(rd.Next()); BigInteger b = new BigInteger(rd.Next()); BigInteger c = a * b + 1; if (seenBefore.ContainsKey(c)) { continue; } seenBefore.Add(c, true); string trueValue = ""; string predictedValue = ""; Predict(model, c, out trueValue, out predictedValue); if (trueValue.Equals("Prime") && predictedValue.Equals("Prime")) { pp++; } else if (trueValue.Equals("Prime") && predictedValue.Equals("NotPrime")) { pn++; } else if (trueValue.Equals("NotPrime") && predictedValue.Equals("Prime")) { np++; } else if (trueValue.Equals("NotPrime") && predictedValue.Equals("NotPrime")) { nn++; } i++; //Try here a = new BigInteger(rd.Next()); b = new BigInteger(rd.Next()); c = a * b - 1; bool isPrime = IsPrimeMillerRabin(c); Predict(model, c, out trueValue, out predictedValue); bool randomGuess = rd.Next(0, (int)Math.Log(N)) == 0; if ((isPrime && randomGuess) || (!isPrime && !randomGuess)) { randomGuessesRight++; } if ((isPrime && predictedValue.Equals("Prime")) || (!isPrime && predictedValue.Equals("NotPrime"))) { modelGetsRight++; } } Console.WriteLine("Confusion Matrix for N = {0}:", N); Console.WriteLine("Positive-Positive: {0}", pp); Console.WriteLine("Positive-Negative: {0}", pn); Console.WriteLine("Negative-Positive: {0}", np); Console.WriteLine("Negative-Negative: {0}", nn); Console.WriteLine("Precision: {0}%", (100.0 * pp) / (pp + np)); Console.WriteLine("Recall: {0}%", (100.0 * pp) / (pp + pn)); Console.WriteLine("Accuracy Random Guesses: {0}%", (100.0 * randomGuessesRight) / N); Console.WriteLine("Accuracy Model: {0}%", (100.0 * modelGetsRight) / N); }
static void Predict(PredictionModel <WineData, WinePrediction> model) { using (var environment = new TlcEnvironment()) { var textLoader = new Microsoft.ML.Data.TextLoader(TestDataPath).CreateFrom <WineData>(useHeader: true, separator: ',', trimWhitespace: false); var experiment = environment.CreateExperiment(); var output = textLoader.ApplyStep(null, experiment) as ILearningPipelineDataStep; experiment.Compile(); textLoader.SetInput(environment, experiment); experiment.Run(); var data = experiment.GetOutput(output.Data); var wineDatas = new List <WineData>(); using (var cursor = data.GetRowCursor((a => true))) { var getters = new ValueGetter <float>[] { cursor.GetGetter <float>(0), cursor.GetGetter <float>(1), cursor.GetGetter <float>(2), cursor.GetGetter <float>(3), cursor.GetGetter <float>(4), cursor.GetGetter <float>(5), cursor.GetGetter <float>(6), cursor.GetGetter <float>(7), cursor.GetGetter <float>(8), cursor.GetGetter <float>(9), cursor.GetGetter <float>(10), cursor.GetGetter <float>(11), cursor.GetGetter <float>(12) }; while (cursor.MoveNext()) { float value0 = 0; float value1 = 0; float value2 = 0; float value3 = 0; float value4 = 0; float value5 = 0; float value6 = 0; float value7 = 0; float value8 = 0; float value9 = 0; float value10 = 0; float value11 = 0; float value12 = 0; getters[0](ref value0); getters[1](ref value1); getters[2](ref value2); getters[3](ref value3); getters[4](ref value4); getters[5](ref value5); getters[6](ref value6); getters[7](ref value7); getters[8](ref value8); getters[9](ref value9); getters[10](ref value10); getters[11](ref value11); getters[12](ref value12); var wdata = new WineData() { FixedAcidity = value0, VolatileAcidity = value1, CitricACID = value2, ResidualSugar = value3, Chlorides = value4, FreeSulfurDioxide = value5, TotalSulfurDioxide = value6, Density = value7, PH = value8, Sulphates = value9, Alcohol = value10, Quality = value11, Id = value12, }; wineDatas.Add(wdata); } } var predictions = model.Predict(wineDatas); var wineDataAndPredictions = wineDatas.Zip(predictions, (wineData, prediction) => (wineData, prediction)); Console.WriteLine($"Wine Id: {wineDataAndPredictions.Last().wineData.Id}, Quality: {wineDataAndPredictions.Last().wineData.Quality} | Prediction: { wineDataAndPredictions.Last().prediction.PredictionQuality}"); Console.WriteLine(); } }
public ImageWithLabelPrediction(PredictionModel pred, string label) { Label = label; Score = pred.Score; PredictedLabelValue = pred.PredictedLabelValue; }
public static PredictionModel <SentimentData, SentimentPrediction> TrainAndPredict() { // LearningPipeline allows you to add steps in order to keep everything together // during the learning process. // <Snippet5> var pipeline = new LearningPipeline(); // </Snippet5> // The TextLoader loads a dataset with comments and corresponding postive or negative sentiment. // When you create a loader, you specify the schema by passing a class to the loader containing // all the column names and their types. This is used to create the model, and train it. // <Snippet6> pipeline.Add(new TextLoader <SentimentData>(_dataPath, useHeader: true, separator: "tab")); // </Snippet6> // TextFeaturizer is a transform that is used to featurize an input column. // This is used to format and clean the data. // <Snippet7> pipeline.Add(new TextFeaturizer("Features", "SentimentText")); //</Snippet7> // Adds a FastTreeBinaryClassifier, the decision tree learner for this project, and // three hyperparameters to be used for tuning decision tree performance. // <Snippet8> pipeline.Add(new FastTreeBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 }); // </Snippet8> // Train the pipeline based on the dataset that has been loaded, transformed. // <Snippet9> PredictionModel <SentimentData, SentimentPrediction> model = pipeline.Train <SentimentData, SentimentPrediction>(); // </Snippet9> // Adds some comments to test the trained model's predictions. // <Snippet10> IEnumerable <SentimentData> sentiments = new[] { new SentimentData { SentimentText = "Please refrain from adding nonsense to Wikipedia." }, new SentimentData { SentimentText = "He is the best, and the article should say that." } }; // </Snippet10> // Now that you have a model, use that to predict the positive // or negative sentiment of the comment data. // <Snippet11> IEnumerable <SentimentPrediction> predictions = model.Predict(sentiments); // </Snippet11> // <Snippet12> Console.WriteLine(); Console.WriteLine("Sentiment Predictions"); Console.WriteLine("---------------------"); // </Snippet12> // Builds pairs of (sentiment, prediction) // <Snippet13> var sentimentsAndPredictions = sentiments.Zip(predictions, (sentiment, prediction) => (sentiment, prediction)); // </Snippet13> // <Snippet14> foreach (var item in sentimentsAndPredictions) { Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(item.prediction.Sentiment ? "Positive" : "Negative")}"); } Console.WriteLine(); // </Snippet14> // Returns the model we trained to use for evaluation. // <Snippet15> return(model); // </Snippet15> }
protected IEnumerable <ImageNetData> PredictDataUsingModel(string testLocation, string imagesFolder, PredictionModel <ImageNetData, ImageNetPrediction> model) { ConsoleWriteHeader("Classificate images"); Console.WriteLine($"Images folder: {imagesFolder}"); Console.WriteLine($"Training file: {testLocation}"); Console.WriteLine(" "); model.TryGetScoreLabelNames(out string[] labels); var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder).ToList(); // add an extra image to "really" test the model testData = testData.Concat(new[] { new ImageNetData() { ImagePath = Path.Combine(imagesFolder, "teddy5.jpg") } }).ToList(); foreach (var sample in testData) { var probs = model.Predict(sample).PredictedLabels; var imageData = new ImageNetDataProbability() { ImagePath = sample.ImagePath, }; (imageData.Label, imageData.Probability) = GetLabel(labels, probs); imageData.ConsoleWriteLine(); yield return(imageData); } }
private static IEnumerable <ClusteringPrediction> PredictDataUsingModel(IEnumerable <PivotData> preProcessData, PredictionModel <PivotData, ClusteringPrediction> model) { ConsoleWriteHeader("Calculate customer segmentation"); var predictions = model.Predict(preProcessData); return(predictions); }
public void TrainAndPredictSentimentModelTest() { string dataPath = GetDataPath(SentimentDataPath); var pipeline = new LearningPipeline(); pipeline.Add(new Data.TextLoader(dataPath) { Arguments = new TextLoaderArguments { Separator = new[] { '\t' }, HasHeader = true, Column = new[] { new TextLoaderColumn() { Name = "Label", Source = new [] { new TextLoaderRange(0) }, Type = Runtime.Data.DataKind.Num }, new TextLoaderColumn() { Name = "SentimentText", Source = new [] { new TextLoaderRange(1) }, Type = Runtime.Data.DataKind.Text } } } }); pipeline.Add(new TextFeaturizer("Features", "SentimentText") { KeepDiacritics = false, KeepPunctuations = false, TextCase = TextNormalizerTransformCaseNormalizationMode.Lower, OutputTokens = true, StopWordsRemover = new PredefinedStopWordsRemover(), VectorNormalizer = TextTransformTextNormKind.L2, CharFeatureExtractor = new NGramNgramExtractor() { NgramLength = 3, AllLengths = false }, WordFeatureExtractor = new NGramNgramExtractor() { NgramLength = 2, AllLengths = true } }); pipeline.Add(new FastTreeBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 }); pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" }); PredictionModel <SentimentData, SentimentPrediction> model = pipeline.Train <SentimentData, SentimentPrediction>(); IEnumerable <SentimentData> sentiments = new[] { new SentimentData { SentimentText = "Please refrain from adding nonsense to Wikipedia." }, new SentimentData { SentimentText = "He is a CHEATER, and the article should say that." } }; IEnumerable <SentimentPrediction> predictions = model.Predict(sentiments); Assert.Equal(2, predictions.Count()); Assert.True(predictions.ElementAt(0).Sentiment.IsFalse); Assert.True(predictions.ElementAt(1).Sentiment.IsTrue); string testDataPath = GetDataPath(SentimentTestPath); var testData = new Data.TextLoader(testDataPath) { Arguments = new TextLoaderArguments { Separator = new[] { '\t' }, HasHeader = true, Column = new[] { new TextLoaderColumn() { Name = "Label", Source = new [] { new TextLoaderRange(0) }, Type = Runtime.Data.DataKind.Num }, new TextLoaderColumn() { Name = "SentimentText", Source = new [] { new TextLoaderRange(1) }, Type = Runtime.Data.DataKind.Text } } } }; var evaluator = new BinaryClassificationEvaluator(); BinaryClassificationMetrics metrics = evaluator.Evaluate(model, testData); Assert.Equal(.5556, metrics.Accuracy, 4); Assert.Equal(.8, metrics.Auc, 1); Assert.Equal(.87, metrics.Auprc, 2); Assert.Equal(1, metrics.Entropy, 3); Assert.Equal(.6923, metrics.F1Score, 4); Assert.Equal(.969, metrics.LogLoss, 3); Assert.Equal(3.083, metrics.LogLossReduction, 3); Assert.Equal(1, metrics.NegativePrecision, 3); Assert.Equal(.111, metrics.NegativeRecall, 3); Assert.Equal(.529, metrics.PositivePrecision, 3); Assert.Equal(1, metrics.PositiveRecall); ConfusionMatrix matrix = metrics.ConfusionMatrix; Assert.Equal(2, matrix.Order); Assert.Equal(2, matrix.ClassNames.Count); Assert.Equal("positive", matrix.ClassNames[0]); Assert.Equal("negative", matrix.ClassNames[1]); Assert.Equal(9, matrix[0, 0]); Assert.Equal(9, matrix["positive", "positive"]); Assert.Equal(0, matrix[0, 1]); Assert.Equal(0, matrix["positive", "negative"]); Assert.Equal(8, matrix[1, 0]); Assert.Equal(8, matrix["negative", "positive"]); Assert.Equal(1, matrix[1, 1]); Assert.Equal(1, matrix["negative", "negative"]); }
protected IEnumerable <ImageNetPrediction> GetPredictions(string testLocation, string imagesFolder, PredictionModel <ImageNetData, ImageNetPrediction> model) { var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder); foreach (var sample in testData) { yield return(model.Predict(sample)); } }
public async Task Evaluate() { var model = await PredictionModel.ReadAsync<ImageNetData, ImageNetPrediction>(modelLocation); var predictions = GetPredictions(dataLocation, imagesFolder, model).ToArray(); ShowMetrics(dataLocation, model); }
/// <summary> /// This function creates a prediction engine from the model located in the <paramref name="modelPath"/>. /// </summary> private PredictionModel <StockPredictionData, StockPrediction> CreatePredictionEngineAsync(string modelPath) { PredictionModel <StockPredictionData, StockPrediction> model = PredictionModel.ReadAsync <StockPredictionData, StockPrediction>(modelPath).Result; return(model); }
/// <summary> /// Predict samples using saved model /// </summary> /// <param name="outputModelPath">Model file path</param> /// <returns></returns> public static async Task TestPrediction(string outputModelPath = "country_month_fastTreeTweedie.zip") { Console.WriteLine("*********************************"); Console.WriteLine("Testing country forecasting model"); // Read the model that has been previously saved by the method SaveModel var model = await PredictionModel.ReadAsync <CountryData, CountrySalesPrediction>(outputModelPath); // Build sample data var dataSample = new CountryData() { country = "United Kingdom", month = 10, year = 2017, med = 323.4F, max = 616.96F, min = 145.9800F, std = 3041.14133F, prev = 930338.28F, count = 1705, sales = 1075435.72F, }; // Predict sample data var prediction = model.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month: {dataSample.month + 1}, year: {dataSample.year} - Real value (US$): {Math.Pow(6.207962F, 10)}, Forecasting (US$): {Math.Pow(prediction.Score,10)}"); dataSample = new CountryData() { country = "United Kingdom", month = 11, year = 2017, med = 301.04F, max = 515.272F, min = 139.42800F, std = 6580.22797F, prev = 1075435.72F, count = 2387, sales = 1614217.83F, }; prediction = model.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month: {dataSample.month + 1}, year: {dataSample.year} - Forecasting (US$): {Math.Pow(prediction.Score,10)}"); dataSample = new CountryData() { country = "Germany", month = 10, year = 2017, med = 410.0349F, max = 912.8460F, min = 148.226F, std = 527.95676F, prev = 19553.63F, count = 58, sales = 33507.58F }; prediction = model.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month: {dataSample.month + 1}, year: {dataSample.year} - Real value (US$): {Math.Pow(4.483736F, 10)}, Forecasting (US$): {Math.Pow(prediction.Score,10)}"); dataSample = new CountryData() { country = "Germany", month = 11, year = 2017, med = 353.755F, max = 668.076F, min = 123.2400F, std = 403.04297F, prev = 33507.58F, count = 68, sales = 30460.45F, }; prediction = model.Predict(dataSample); Console.WriteLine($"Country: {dataSample.country}, month: {dataSample.month + 1}, year: {dataSample.year} - Forecasting (US$): {Math.Pow(prediction.Score,10)}"); }
public static async Task Main(string[] args) { string arg; Console.WriteLine("enter #train, #eval or #predict"); arg = Console.ReadLine(); if (arg == "train") { PredictionModel <ShakVector, ShakPrediction> model = await Train(); System.Threading.Thread.Sleep(3000); } else if (arg == "eval") { PredictionModel <ShakVector, ShakPrediction> model = await PredictionModel.ReadAsync <ShakVector, ShakPrediction>(_modelpath); System.Threading.Thread.Sleep(3000); Evaluate(model); } else if (arg == "predict") { PredictionModel <ShakVector, ShakPrediction> model = await PredictionModel.ReadAsync <ShakVector, ShakPrediction>(_modelpath); System.Threading.Thread.Sleep(3000); Console.WriteLine("Write ingridiants:"); string Tomato, Onion, Garlic, BellPepper, Eggs, Pepper, Salt, BulgerianCheese, Paprika, Water, Resek, Cumun, Eggplant, Tofu, FryingTimeBeforeTomatosMinutes, CookingAfterTomatosMinutes, CookingAfterEggsMinutes; Tomato = Console.ReadLine(); Onion = Console.ReadLine(); Garlic = Console.ReadLine(); BellPepper = Console.ReadLine(); Eggs = Console.ReadLine(); Pepper = Console.ReadLine(); Salt = Console.ReadLine(); BulgerianCheese = Console.ReadLine(); Paprika = Console.ReadLine(); Water = Console.ReadLine(); Resek = Console.ReadLine(); Cumun = Console.ReadLine(); Eggplant = Console.ReadLine(); Tofu = Console.ReadLine(); FryingTimeBeforeTomatosMinutes = Console.ReadLine(); CookingAfterTomatosMinutes = Console.ReadLine(); CookingAfterEggsMinutes = Console.ReadLine(); ShakVector newVec = new ShakVector { TomatoAmount = float.Parse(Tomato), OnionAmount = float.Parse(Onion), GarlicAmount = float.Parse(Garlic), BellPepperAmount = float.Parse(BellPepper), EggsAmount = float.Parse(Eggs), PepperAmount = float.Parse(Pepper), SaltAmount = float.Parse(Salt), BulgerianCheeseAmount = float.Parse(BulgerianCheese), PaprikaAmount = float.Parse(Paprika), WaterAmount = float.Parse(Water), TomatoResekAmount = float.Parse(Resek), CuminAmount = float.Parse(Cumun), EggplantAmount = float.Parse(Eggplant), TofuAmount = float.Parse(Tofu), FryingTimeBeforeTomatosMinutes = float.Parse(FryingTimeBeforeTomatosMinutes), CookingAfterEggsMinutes = float.Parse(CookingAfterEggsMinutes), CookingAfterTomatosMinutes = float.Parse(CookingAfterTomatosMinutes) }; Predict(model, newVec); } }
public async Task <string> TrainAsync(string trainpath, bool writeToDisk = true) { // pipeline encapsulates the data loading, data processing/featurization, and learning algorithm var pipeline = new LearningPipeline { // load from CSV --> SubCategory, Description, Bank, Amount, new TextLoader(trainpath).CreateFrom <BankStatementLineItem>(separator: ',', useHeader: true), //Converts input values (words, numbers, etc.) to index in a dictionary. new Dictionarizer(("SubCategory", "Label")), // convert the data columns to the feature. For that TextFeaturizer // ngram analysis over the transaction description new TextFeaturizer("Description", "Description") { TextCase = TextNormalizerTransformCaseNormalizationMode.Lower, WordFeatureExtractor = new NGramNgramExtractor { // Term frequency -- the number of times that term t occurs in document d Weighting = NgramTransformWeightingCriteria.Tf } }, new TextFeaturizer("Bank", "Bank") { TextCase = TextNormalizerTransformCaseNormalizationMode.Lower }, // feature column using bank and description new ColumnConcatenator("Features", "Bank", "Description"), //******************************************************************** // classifiers //******************************************************************** //new NaiveBayesClassifier(), new StochasticDualCoordinateAscentClassifier { Shuffle = false, NumThreads = 1 }, //new LightGbmClassifier(), //******************************************************************** //Transforms a predicted label column to its original values, unless it is of type bool new PredictedLabelColumnOriginalValueConverter { PredictedLabelColumn = "PredictedLabel" } }; //******************************************************************** // training //******************************************************************** Console.WriteLine("=============== Start training ==============="); var watch = Stopwatch.StartNew(); _model = pipeline.Train <BankStatementLineItem, PredictedLabel>(); watch.Stop(); Console.WriteLine($"=============== End training ==============="); Console.WriteLine($"training took {watch.ElapsedMilliseconds} milliseconds"); Console.WriteLine("The model is saved to {0}", PredictionModelWrapper.Model1Path); //******************************************************************** var converter = new OnnxConverter { Onnx = PredictionModelWrapper.Model1Path, Json = PredictionModelWrapper.Model1Path.Replace(".onnx", ".json"), Domain = "onnx" }; converter.Convert(_model); if (writeToDisk) { await _model.WriteAsync(PredictionModelWrapper.Model1Path); // Strip the version. var fileText = File.ReadAllText(converter.Json); fileText = Regex.Replace(fileText, "\"producerVersion\": \"([^\"]+)\"", "\"producerVersion\": \"##VERSION##\""); File.WriteAllText(converter.Json, fileText); } return(PredictionModelWrapper.Model1Path); }
/// <summary> /// This function creates a prediction engine from the model located in the <paramref name="modelPath"/>. /// </summary> private async Task <PredictionModel <ProductData, ProductUnitPrediction> > CreatePredictionEngineAsync(string modelPath) { PredictionModel <ProductData, ProductUnitPrediction> model = await PredictionModel.ReadAsync <ProductData, ProductUnitPrediction>(modelPath); return(model); }
public async Task LoadModel() { model = await PredictionModel.ReadAsync <ManifestDataTraining, ManifestPrediction>("model.train"); }
private async void Page_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { DatasetBox.IsChecked = false; PerceptronBox.IsChecked = false; LinearSvmBox.IsChecked = false; LogisticRegressionBox.IsChecked = false; SdcaBox.IsChecked = false; StartButton.IsEnabled = false; CalculateButton.IsEnabled = false; BusyIndicator.Visibility = Windows.UI.Xaml.Visibility.Visible; BusyIndicator.PlayAnimation(); // Prepare datasets. DatasetBox.IsChecked = true; var trainingDataLocation = await MlDotNet.FilePath(@"ms-appx:///Data/winequality_white_train.csv"); _testDataPath = await MlDotNet.FilePath(@"ms-appx:///Data/winequality_white_test.csv"); // Prepare diagram. PrepareDiagram(out ColumnSeries accuracySeries, out ColumnSeries entropySeries, out ColumnSeries f1ScoreSeries); //// This raises a ArgumentOutOfRangeException because of different Label type expected: //// var priorModel = await ViewModel.BuildAndTrain(trainingDataLocation, ViewModel.MLContext.BinaryClassification.Trainers.Prior()); //// https://github.com/dotnet/machinelearning/issues/3119 //// These raise an exception on System.Diagnostics.Process //// 'PlatformNotSupportedException: Retrieving information about local processes is not supported on this platform.' //// //// var fastTreeBinaryModel = await ViewModel.BuildAndTrain(trainingDataLocation, ViewModel.MLContext.BinaryClassification.Trainers.FastTree()); //// var fastForestBinaryModel = await ViewModel.BuildAndTrain(trainingDataLocation, ViewModel.MLContext.BinaryClassification.Trainers.FastForest()); //// https://github.com/dotnet/machinelearning/issues/2444 // Perceptron PerceptronBox.IsChecked = true; _perceptronBinaryModel = await ViewModel.BuildAndTrain(trainingDataLocation, ViewModel.MLContext.BinaryClassification.Trainers.AveragedPerceptron()); await ViewModel.Save(_perceptronBinaryModel, "perceptronModel.zip"); var nonCalibratedMetrics = await ViewModel.EvaluateNonCalibrated(_perceptronBinaryModel, _testDataPath); accuracySeries.Items.Add(new ColumnItem { CategoryIndex = 0, Value = nonCalibratedMetrics.Accuracy }); entropySeries.Items.Add(new ColumnItem { CategoryIndex = 0, Value = double.NaN }); // metrics.Entropy }); f1ScoreSeries.Items.Add(new ColumnItem { CategoryIndex = 0, Value = nonCalibratedMetrics.F1Score }); // Update diagram Diagram.InvalidatePlot(); // Linear SVM LinearSvmBox.IsChecked = true; _linearSvmModel = await ViewModel.BuildAndTrain(trainingDataLocation, ViewModel.MLContext.BinaryClassification.Trainers.LinearSvm()); await ViewModel.Save(_linearSvmModel, "linearSvmModel.zip"); nonCalibratedMetrics = await ViewModel.EvaluateNonCalibrated(_linearSvmModel, _testDataPath); accuracySeries.Items.Add(new ColumnItem { CategoryIndex = 1, Value = nonCalibratedMetrics.Accuracy }); entropySeries.Items.Add(new ColumnItem { CategoryIndex = 1, Value = double.NaN }); // metrics.Entropy }); f1ScoreSeries.Items.Add(new ColumnItem { CategoryIndex = 1, Value = nonCalibratedMetrics.F1Score }); // Update diagram Diagram.InvalidatePlot(); // Logistic Regression LogisticRegressionBox.IsChecked = true; _logisticRegressionModel = await ViewModel.BuildAndTrain(trainingDataLocation, ViewModel.MLContext.BinaryClassification.Trainers.LbfgsLogisticRegression()); await ViewModel.Save(_logisticRegressionModel, "logisticRegressionModel.zip"); var metrics = await ViewModel.Evaluate(_logisticRegressionModel, _testDataPath); accuracySeries.Items.Add(new ColumnItem { CategoryIndex = 2, Value = metrics.Accuracy }); entropySeries.Items.Add(new ColumnItem { CategoryIndex = 2, Value = metrics.Entropy }); f1ScoreSeries.Items.Add(new ColumnItem { CategoryIndex = 2, Value = metrics.F1Score }); // Update diagram Diagram.InvalidatePlot(); // Stochastic Dual Coordinate Ascent SdcaBox.IsChecked = true; _sdcabModel = await ViewModel.BuildAndTrain(trainingDataLocation, ViewModel.MLContext.BinaryClassification.Trainers.SdcaLogisticRegression()); await ViewModel.Save(_sdcabModel, "sdcabModel.zip"); metrics = await ViewModel.Evaluate(_sdcabModel, _testDataPath); accuracySeries.Items.Add(new ColumnItem { CategoryIndex = 3, Value = metrics.Accuracy }); entropySeries.Items.Add(new ColumnItem { CategoryIndex = 3, Value = metrics.Entropy }); f1ScoreSeries.Items.Add(new ColumnItem { CategoryIndex = 3, Value = metrics.F1Score }); // Update diagram Diagram.InvalidatePlot(); StartButton.IsEnabled = true; CalculateButton.IsEnabled = true; BusyIndicator.Visibility = Windows.UI.Xaml.Visibility.Collapsed; BusyIndicator.PauseAnimation(); }
/* public ActionResult PlayLists() { * ViewBag.usuario = spotifyUser.DisplayName; * return View(db.Musicas.Where(m => m.idUsuario.Equals(spotifyUser.UserId)).ToList()); * }*/ public ActionResult GenerateNameSortList(string access_token, string error) { if (error != null || error == "access_denied") { return(View("Error")); } if (string.IsNullOrEmpty(access_token)) { return(View()); } try { _spotifyApi.Token = access_token; SpotifyService spotifyService = new SpotifyService(_spotifyApi); //Get user_id and user displayName spotifyUser = spotifyService.GetUserProfile(); ViewBag.UserName = spotifyUser.DisplayName; Tracks tocadasRecentemente = spotifyService.GetRecentlyPlayed(); List <Audio> metaAudios = spotifyService.GetAudioTracks(tocadasRecentemente, spotifyUser); // GeraPlayLists(); /*HttpContext.Current.Server.MapPath("~/Content/Dados/"); * using (var sw = new StreamWriter(path + nomeArquivo + "musicas.csv"))*/ string _dataPath = System.Web.HttpContext.Current.Server.MapPath("~/Content/Dados/" + spotifyUser.UserId + "_" + spotifyUser.DisplayName + "musicas.csv"); string _dataPath2 = System.Web.HttpContext.Current.Server.MapPath("~/Content/Dados/dataSpotify.csv"); string _modelPath = System.Web.HttpContext.Current.Server.MapPath("~/Content/Dados/p1.zip"); string _modelPath2 = System.Web.HttpContext.Current.Server.MapPath("~/Content/Dados/p2.zip"); string nomeArq = spotifyUser.UserId + "_" + spotifyUser.DisplayName + "musicas.csv"; var model1 = PredictionModel.ReadAsync <Musica, ClusterPrediction>(_modelPath).Result; var model2 = PredictionModel.ReadAsync <Musica, ClusterPrediction>(_modelPath2).Result; var rec = spotifyService.geraplay(_dataPath, _dataPath2, model1, model2); rec.Nome = spotifyUser.DisplayName; rec.IdUsuario = spotifyUser.UserId; foreach (var musicasp1 in rec.P1) { musicasp1.IdUsuario = spotifyUser.UserId; // db.Musicas.Add(musicasp1); } foreach (var musicasp2 in rec.P2) { musicasp2.IdUsuario = spotifyUser.UserId; //db.Musicas.Add(musicasp2); } foreach (var musicasp3 in rec.P3) { musicasp3.IdUsuario = spotifyUser.UserId; //db.Musicas.Add(musicasp3); } // db.SaveChanges(); TempData["rec"] = rec; TempData["nomeArqui"] = nomeArq; return(RedirectToAction("Index", "Musicas")); // return RedirectToAction("Playlists", rec); // Post(access_token, error, playlistProntas); //return View("Teste", playlistProntas); } catch (Exception e) { return(View("Error")); } }
private void predict_Click(PredictionModel pred) { Settings.Model = pred; syncToolBarToState(); //setView(); predictorView.setData(); trainingView.RefreshData(); extrapolateView.RefreshData(); }
public JsonResult Predict(PredictionModel model) { model.UserId = User.Identity.GetUserId(); return(Json((new PredictionBiz()).AddOrUpdatePrediction(model))); }