// Evaluate Non-negative Matrix Factorization public void EvaluateNMF(ITransformer model) { Console.WriteLine("\nEvaluating NMF..."); MLContext mlContext = new MLContext(); // get test data DataManager dm = new DataManager(); // test data IDataView testData = dm.GetDataView(ModelChoice.NMF, mlContext, DataPurpose.TEST); Data[] test_data = dm.GetRecipes(ModelChoice.NMF, DataPurpose.TEST); // train data IDataView trainData = dm.GetDataView(ModelChoice.NMF, mlContext, DataPurpose.TRAIN); Data[] train_data = dm.GetRecipes(ModelChoice.NMF, DataPurpose.TRAIN); // features string[] features = dm.GetFeatures(); int[] recipeArray = testData.GetColumn <int>(testData.Schema["recipeId"]).ToArray(); Results results = new Results(0); Recommender recommender = new Recommender(); // distinct test recipes int[] distinct_recipes = recipeArray.Distinct().ToArray(); // for each test recipe foreach (int r in distinct_recipes) { Recommendation[] recommendations = new Recommendation[features.Length]; Data[] recipe = test_data.Where(d => d.recipeId == r && d.score == 1).ToArray(); Data[] trecipe = train_data.Where(d => d.recipeId == r && d.score == 1).ToArray(); // get recipe r Data[] combined = recipe.Concat(trecipe).ToArray(); int[] current_recipe = dm.GetRecipe(combined.ToArray()); // iterate through all features for (int i = 0; i < dm.GetFeatures().Length; i++) { // make prediction (get score) double prediction = recommender.SinglePrediction(mlContext, model, i, r); // save score of ingredient recommendations[i] = new Recommendation(new Ingredient(i, features[i]), prediction); } // sort recommendations = recommendations.OrderByDescending(d => d.score).ToArray(); results = GetResults(results, recommendations, current_recipe); } // Display accuracy results results.ShowResults(); Console.WriteLine(); }
// Evaluate different models static void Evaluate(ModelChoice choice) { Evaluation eval = new Evaluation(); // Evaluate Non-negative Matrix Factorization (12 minutes) if (choice.Equals(ModelChoice.NMF)) { Recommender nmf = new Recommender(); eval.EvaluateNMF(nmf.GetModel()); } // Evaluate Naive Bayes (5 seconds each) else if (choice.Equals(ModelChoice.NB)) { NaiveBayes nb = new NaiveBayes(); double[][] model = nb.GetModel(); Console.WriteLine("1: Evaluate ALL versions of Naive Bayes"); Console.WriteLine("2: Evaluate only the BEST version of Naive Bayes (normalization, laplace, uniform prior)"); string option = Console.ReadLine(); // ALL if (option == "1") { eval.EvaluateNB(model, false, false, true); eval.EvaluateNB(model, true, false, true); // with laplace smoothing eval.EvaluateNB(model, false, true, true); // with normalization (best results) eval.EvaluateNB(model, true, true, true); // with laplace smoothing and normalization // uniform prior eval.EvaluateNB(model, false, false, false); eval.EvaluateNB(model, true, false, false); // with laplace smoothing eval.EvaluateNB(model, false, true, false); // with normalization (best results) } // BEST eval.EvaluateNB(model, true, true, false); // with laplace smoothing, normalization, and uniform prior } // Evaluate KNN (35 seconds) else if (choice.Equals(ModelChoice.KNN)) { eval.EvaluateKNN(6, DistanceChoice.Jaccard, Voting.Unweighted); } // Evaluate Modified KNN (35 seconds) else { eval.EvaluateModifiedKNN(DistanceChoice.Jaccard_Similarity, Voting.Unweighted); } }