void CheckBufferAndPredict() { //Ensure the buffer is full for (int i = 0; i < buffer.Length; i++) { if (buffer[i] == null) { return; } } //If the predictor is in its prediction state, predict and pass on the result if (trainingId == ID_PREDICT) { var prediction = predictor.Predict(buffer); if (prediction != NO_PREDICTION) { Logger.Log(string.Format("Predicted: {0}", prediction)); Add(new TrainedEvent(prediction)); } } //Otherwise add the data to the training data set at the corresponding label else { predictor.AddTrainingData(trainingId, buffer); } //Reset the buffer buffer = new EEGEvent[types.Length]; }
private string HandlePredictCommand(PredictOption opts) { JObject currencies = _reader.ReadCurrencies(); StringBuilder stringBuilder = new StringBuilder(); if (!currencies.ContainsKey(opts.From)) { stringBuilder.AppendLine($"Currency code {opts.From} is invalid."); } if (!currencies.ContainsKey(opts.To)) { stringBuilder.AppendLine($"Currency code {opts.To} is invalid."); } if (stringBuilder.Length > 0) { throw new ArgumentException(stringBuilder.ToString()); } PredictorOption option = new PredictorOption { FromDate = DateTime.ParseExact("15/01/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture), ToDate = DateTime.ParseExact("15/12/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture), PredictDate = DateTime.ParseExact("15/01/2017", "dd/MM/yyyy", CultureInfo.InvariantCulture), FromCurrency = opts.From, ToCurrency = opts.To }; decimal rate = _predictor.Predict(option); string result = $"The predicted currency exchange from {opts.From} to {opts.To} for 15/1/2017 is {rate}"; return(result); }
/// <summary> /// Train a model on a single example, /// </summary> /// <typeparam name="TOutput"></typeparam> /// <param name="trainerMaker"></param> /// <param name="checker"></param> private static void TrivialHelper <TOutput>(Func <ITrainerHost, ITrainer <Instances, IPredictorProducing <TOutput> > > trainerMaker, Action <TOutput, TOutput> checker) { // The following simple instance should result in a "trivial" predictor for binary classification, regression, and multiclass, I think. ListInstances instances = new ListInstances(); instances.AddInst(new Float[] { (Float)0.0 }, (Float)0); instances.CopyMetadata(null); ITrainerHost host = new TrainHost(new Random(1), 0); var trainer = trainerMaker(host); trainer.Train(instances); IPredictor <Instance, TOutput> predictor = (IPredictor <Instance, TOutput>)trainer.CreatePredictor(); IPredictor <Instance, TOutput> loadedPredictor = default(IPredictor <Instance, TOutput>); using (Stream stream = new MemoryStream()) { using (RepositoryWriter writer = RepositoryWriter.CreateNew(stream, false)) { ModelSaveContext.SaveModel(writer, predictor, "foo"); writer.Commit(); } stream.Position = 0; using (RepositoryReader reader = RepositoryReader.Open(stream, false)) { ModelLoadContext.LoadModel(out loadedPredictor, reader, "foo"); } Assert.AreNotEqual(default(IPredictor <Instance, TOutput>), loadedPredictor, "did not load expected model"); } TOutput result = predictor.Predict(instances[0]); TOutput loadedResult = loadedPredictor.Predict(instances[0]); checker(result, loadedResult); }
public float Predict(List <float> values, List <float> predictions, float?desiredValue = null) { float basePrediction = BasePredictor.Predict(values, predictions, desiredValue); if (values == null || values.Count < 2) { return(basePrediction); } var firstDerivative = AdditiveFirstDerivativePredictor.Differentiate(values); float derivativePrediction = DerivativePredictor.Predict(firstDerivative, new List <float>(), desiredValue); if (firstDerivative.Count < 2) { return(basePrediction + derivativePrediction); } var secondDerivative = AdditiveFirstDerivativePredictor.Differentiate(values); float secondDerivativePrediction = SecondDerivativePredictor.Predict(secondDerivative, new List <float>(), desiredValue); return(basePrediction + derivativePrediction + secondDerivativePrediction); }
public override void Execute() { dynamic inputs = GetInputValue("inputs"); LoadModel(); SetOutputValue("outputs", PredictorInstance.Predict(inputs)); //Global.KerasService.LoadModel(model); //Global.KerasService.LoadWeights(weights); //SetOutputValue("outputs", Global.KerasService.Predict(inputs, shape)); }
public IPredictionModel BuildModel(IDataFrame dataFrame, string dependentFeatureName, IModelBuilderParams additionalParams) { if (!(additionalParams is IRandomForestModelBuilderParams)) { throw new ArgumentException("Invalid parameters passed to Random Forest Model builder!"); } var randomForestParams = additionalParams as IRandomForestModelBuilderParams; var trees = new IDecisionTreeNode[randomForestParams.TreesCount]; var oobErrors = new double[randomForestParams.TreesCount]; var columnsCountToTake = featuresToUseCountCalculator(dataFrame.ColumnsCount - 1); var featureColumns = dataFrame.ColumnNames.Except(new[] { dependentFeatureName }).ToList(); var randomizer = new Random(); var locker = new object(); Parallel.For( 0, randomForestParams.TreesCount, i => { var localRandomizer = new Random(i.GetHashCode()); var randomlySelectedIndices = Enumerable.Range(0, dataFrame.RowCount) .Select(_ => localRandomizer.Next(0, dataFrame.RowCount)) .ToList(); var outOfBagIndices = Enumerable.Range(0, dataFrame.RowCount).Except(randomlySelectedIndices).ToList(); var columnsToTake = new List <string>(); columnsToTake = featureColumns.Shuffle(localRandomizer).Take(columnsCountToTake).ToList(); columnsToTake.Add(dependentFeatureName); var baggedTestData = dataFrame.Slice(randomlySelectedIndices, columnsToTake); var oobTestData = dataFrame.Slice(outOfBagIndices, columnsToTake); var oobExpected = oobTestData.GetColumnVector <TPredictionVal>(dependentFeatureName).Values; var decisionTree = decisionTreeModelBuilder.BuildModel( baggedTestData, dependentFeatureName, decisionTreeModelBuilderParamsFactory()); var prediction = decisionTreePredictor.Predict(oobTestData, decisionTree, dependentFeatureName); //TODO: AAA !!! Later on add support for calculating variable importance!!! var oobError = dataQualityMeasure.CalculateError(oobExpected, prediction); trees[i] = decisionTree as IDecisionTreeNode; oobErrors[i] = oobError; }); return(new RandomForestModel(trees, oobErrors)); }
public IActionResult Predict([FromQuery] string authId, int amount) { try { var user = _userRepository.GetByAuthId(authId); var recommendations = _predictor.Predict(user.Id, amount); return(Ok(recommendations)); } catch (Exception e) { _logger.LogError(e, e.Message); return(StatusCode(500, e.Message)); } }
public IList<TPredictionVal> Predict(IDataFrame queryDataFrame, IPredictionModel model, string dependentFeatureName) { if (!(model is IRandomForestModel)) { throw new ArgumentException("Invalid model passed to Random Forest Predictor"); } var randomForestModel = (IRandomForestModel)model; var weightedPredictons = new Tuple<IList<TPredictionVal>, double>[randomForestModel.DecisionTrees.Count]; Parallel.For( 0, weightedPredictons.Length, i => { var predictions = decisionTreePredictor.Predict( queryDataFrame, randomForestModel.DecisionTrees[i], dependentFeatureName); var weight = 1 - randomForestModel.OutOfBagErrors[i]; weightedPredictons[i] = new Tuple<IList<TPredictionVal>, double>(predictions, weight); }); var predictionVotes = new Dictionary<int, IDictionary<TPredictionVal, double>>(); foreach (var weightedPrediction in weightedPredictons) { for (int rowIdx = 0; rowIdx < queryDataFrame.RowCount; rowIdx++) { var predictedVal = weightedPrediction.Item1[rowIdx]; var weight = useVotingWeightedByOob ? weightedPrediction.Item2 : 1.0; if (!predictionVotes.ContainsKey(rowIdx)) { predictionVotes.Add(rowIdx, new Dictionary<TPredictionVal, double>()); } if (!predictionVotes[rowIdx].ContainsKey(predictedVal)) { predictionVotes[rowIdx].Add(predictedVal, 0.0); } predictionVotes[rowIdx][predictedVal] += weight; } } var results = predictionVotes.Select( rowVotes => rowVotes.Value.OrderByDescending(weightedPredictions => weightedPredictions.Value).First().Key) .ToList(); return results; }
public async Task <LabelSuggestion> PredictUsingModelsFromStorageQueue(string owner, string repo, int number) { if (_regex == null) { _regex = new Regex(@"@[a-zA-Z0-9_//-]+"); } if (!_modelHolder.IsIssueEngineLoaded || !_modelHolder.IsPrEngineLoaded) { throw new InvalidOperationException("load engine before calling predict"); } var iop = await _gitHubClientWrapper.GetIssue(owner, repo, number); bool isPr = iop.PullRequest != null; string body = iop.Body ?? string.Empty; var userMentions = _regex.Matches(body).Select(x => x.Value).ToArray(); LabelSuggestion labelSuggestion = null; if (isPr && !_useIssueLabelerForPrsToo) { var prModel = await CreatePullRequest(owner, repo, iop.Number, iop.Title, iop.Body, userMentions, iop.User.Login); labelSuggestion = _predictor.Predict(prModel, _logger); _logger.LogInformation("predicted with pr model the new way"); _logger.LogInformation(string.Join(",", labelSuggestion.LabelScores.Select(x => x.LabelName))); return(labelSuggestion); } var issueModel = CreateIssue(iop.Number, iop.Title, iop.Body, userMentions, iop.User.Login); labelSuggestion = _predictor.Predict(issueModel, _logger); _logger.LogInformation("predicted with issue model the new way"); _logger.LogInformation(string.Join(",", labelSuggestion.LabelScores.Select(x => x.LabelName))); return(labelSuggestion); }
public async Task RunAsync() { Console.Write("From Currency: "); string fromCurrency = Console.ReadLine(); Console.Write("To Currency: "); string toCurrency = Console.ReadLine(); if (string.IsNullOrWhiteSpace(fromCurrency)) { Console.WriteLine("Please enter value for From Currency"); return; } if (string.IsNullOrWhiteSpace(toCurrency)) { Console.WriteLine("Please enter value for To Currency"); return; } if (!await ValidateCurrencyAsync(fromCurrency)) { Console.WriteLine($"From Currency: {fromCurrency} currency is invalid."); return; } if (!await ValidateCurrencyAsync(toCurrency)) { Console.WriteLine($"To Currency: {fromCurrency} currency is invalid."); return; } if (string.Equals(fromCurrency, toCurrency, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine($"From Currency and To Currency should be different."); return; } int month = 13; var rates = await GetRatesAsync(fromCurrency.ToUpper(), toCurrency.ToUpper()); var result = _predictor.Predict(month, rates); Console.WriteLine($"The predicted currency exchange from {fromCurrency.ToUpper()} to {toCurrency.ToUpper()} for 15/1/2017 is {result}"); }
public float Predict(List <float> values, List <float> predictions, float?desiredValue = null) { if (values == null || values.Count == 0) { return(.5f); } if (values.Count < 2) { return(values[0]); } List <float> firstDerivative = AdditiveFirstDerivativePredictor.Differentiate(values); float derivativePrediction = DerivativePredictor.Predict(firstDerivative, new List <float>(), desiredValue); return(values[values.Count - 1] + derivativePrediction); }
public int GetPrediction(int i, int j, int[,] matrix) { if (i == 0 && j == 0) { return(new Predictors.HalfRange(range).Predict(0, 0, 0)); } if (i == 0) { return(firstRowPredictor.Predict(matrix[0, j - 1], 0, 0)); } if (j == 0) { return(firstColumnPredictor.Predict(0, matrix[i - 1, 0], 0)); } return(predictor.Predict(matrix[i, j - 1], matrix[i - 1, j], matrix[i - 1, j - 1])); }
private static void PrintMseError(IEnumerable<Game> games, IPredictor predictor) { foreach (Game g in games) { double pred = predictor.Predict(g); totalSquaredError += (pred - g.GoalDiff) * (pred - g.GoalDiff); totalLinearError += Math.Abs(pred - g.GoalDiff); if (Math.Sign(g.GoalDiff) == 0) totalSignError += 0.5d; else if (Math.Sign(pred) != Math.Sign(g.GoalDiff)) totalSignError += 1d; n++; Console.WriteLine("Predicted {0:0.00} was {1:0.00}", pred, g.GoalDiff); } Console.WriteLine("MSE {0:0.00}", totalSquaredError / (double)n); Console.WriteLine("MLE {0:0.00}", totalLinearError / (double)n); Console.WriteLine("Total Sign Error {0:0.00}", totalSignError / (double)n); }
public IList <IDataQualityReport <TPredictionResult> > CrossValidate( IPredictionModelBuilder modelBuilder, IModelBuilderParams modelBuilderParams, IPredictor <TPredictionResult> predictor, IDataQualityMeasure <TPredictionResult> qualityMeasure, IDataFrame dataFrame, string dependentFeatureName, double percetnagOfTrainData, int folds) { var trainingDataCount = (int)Math.Round(percetnagOfTrainData * dataFrame.RowCount); var testDataCount = dataFrame.RowCount - trainingDataCount; var shuffledAllIndices = dataFrame.RowIndices.Shuffle(_randomizer); var maxWindowsCount = dataFrame.RowCount / testDataCount; var iterationAccuracies = new List <IDataQualityReport <TPredictionResult> >(); var currentWindowNo = 0; for (var i = 0; i < folds; i++) { if (currentWindowNo == maxWindowsCount) { currentWindowNo = 0; shuffledAllIndices = shuffledAllIndices.Shuffle(); } var offset = currentWindowNo * testDataCount; var trainingIndices = shuffledAllIndices.Skip(offset).Take(trainingDataCount).ToList(); var trainingData = dataFrame.GetSubsetByRows(trainingIndices); var testIndices = shuffledAllIndices.Except(trainingIndices).ToList(); var testData = dataFrame.GetSubsetByRows(testIndices); IPredictionModel model = modelBuilder.BuildModel(trainingData, dependentFeatureName, modelBuilderParams); IList <TPredictionResult> predictions = predictor.Predict(testData, model, dependentFeatureName); IList <TPredictionResult> expected = testData.GetColumnVector <TPredictionResult>(dependentFeatureName); IDataQualityReport <TPredictionResult> qualityReport = qualityMeasure.GetReport(expected, predictions); iterationAccuracies.Add(qualityReport); currentWindowNo++; } return(iterationAccuracies); }
public Dictionary <string, string> GetPredictions(int week) { Dictionary <string, string> p = new Dictionary <string, string>(); List <ITeam> allTeams = _league.Conferences.SelectMany(c => c.Divisions).SelectMany(d => d.Teams).ToList(); foreach (ITeam team in allTeams) { IGame game = team.Games.Where(g => g.Week == week).FirstOrDefault(); if (game == null) { continue; } ITeam homeTeam = _leagueLastWeek.GetTeam(game.HomeTeam); ITeam awayTeam = _leagueLastWeek.GetTeam(game.AwayTeam); string key = awayTeam.ID + " at " + homeTeam.ID; if (!p.ContainsKey(key)) { int prediction = _predictor.Predict(homeTeam, awayTeam); if (prediction > 0) { p[key] = homeTeam.ID; } else if (prediction < 0) { p[key] = awayTeam.ID; } else { p[key] = PickRandom(homeTeam, awayTeam).ID; } } } return(p); }
/// <summary> /// Predict with pretrained model with tlc client /// </summary> /// <param name="svmFeature"></param> /// <returns></returns> private float[] Predict(float[] svmFeature) { var instance = new Microsoft.TMSN.TMSNlearn.Instance(svmFeature); return((float[])predictor.Predict(instance)); }
public void Predict(float?desiredValue = null) { _predictions.Add(_predictor.Predict(_values, _predictions, desiredValue)); }
public double Predict(int t) { return(Predictor.Predict(Inputs.Column(t))); }