コード例 #1
0
        // Evaluate Modified KNN (dynamic k)
        public void EvaluateModifiedKNN(DistanceChoice distance_choice, Voting voting)
        {
            Console.WriteLine("Evalulating Modified KNN...");
            Results results = new Results(0);

            KNN         knn = new KNN();
            MLContext   ml  = new MLContext();
            DataManager dm  = new DataManager();

            // get test data
            Data[] test_data = dm.GetRecipes(ModelChoice.KNN, DataPurpose.TEST);
            // get features (ingredients)
            string[] ingrNames = dm.GetFeatures();

            // group data by recipeId
            IGrouping <int, Data>[] recipes = test_data.GroupBy(d => d.recipeId).ToArray();

            // keep track of the k values
            int[] new_ks = new int[recipes.Length];
            int   index  = 0;

            // iterate through all test recipes
            foreach (IGrouping <int, Data> recipe in recipes)
            {
                // current recipe
                int[] current_recipe = dm.GetRecipe(recipe.ToArray());

                // get recommendations
                Recommendation[] recommendations = knn.GetRecommendations_ModifiedKNN(current_recipe, distance_choice, voting, ref new_ks[index]);
                index++;

                results = GetResults(results, recommendations, current_recipe);
            }

            Console.WriteLine("\nMin k: " + new_ks.Min());
            Console.WriteLine("Max k: " + new_ks.Max());
            Console.WriteLine("Avg k: " + new_ks.Average() + "\n");

            results.ShowResults();
        }
コード例 #2
0
        // Get ingredients sorted by what is most recommended
        public Recommendation[] GetRecommendations(ModelChoice model_choice, int[] recipe)
        {
            Recommendation[] recommendations = null;

            if (model_choice.Equals(ModelChoice.NB))
            {
                NaiveBayes nb = new NaiveBayes();
                recommendations = nb.RecipeRecommendations(nb.GetModel(), recipe, true, true, false);
            }
            else if (model_choice.Equals(ModelChoice.KNN))
            {
                KNN knn = new KNN();
                recommendations = knn.GetRecommendations(6, DistanceChoice.Jaccard, recipe, Voting.Unweighted);
            }
            else if (model_choice.Equals(ModelChoice.MKNN))
            {
                KNN knn   = new KNN();
                int new_k = 0;
                recommendations = knn.GetRecommendations_ModifiedKNN(recipe, DistanceChoice.Jaccard_Similarity, Voting.Unweighted, ref new_k);
            }

            return(recommendations);
        }
コード例 #3
0
        // Ingredients to ADD to or REMOVE from a recipe
        public void TopRecommendations(int top, string[] recipe_str, ModelChoice model_choice, bool add, bool include_recipe_ingrs)
        {
            DataManager dm = new DataManager();

            // get training data
            Data[] data = dm.GetRecipes(model_choice, DataPurpose.TRAIN);

            Console.WriteLine("You model choice: " + model_choice.ToString());

            // input recipe
            int[] recipe = new int[recipe_str.Length];
            for (int i = 0; i < recipe_str.Length; i++)
            {
                try
                {
                    // trim and make lowercase
                    recipe_str[i] = recipe_str[i].Trim().ToLower();

                    // find ingredient
                    recipe[i] = data.Where(d => d.ingredient.name.Equals(recipe_str[i])).ToArray()[0].ingredient.id;
                }
                catch
                {
                    // get features (ingredients)
                    string[] features = GetAllIngredients(false);

                    bool found = false;
                    // try finding a similar ingredient
                    foreach (string ingr in features)
                    {
                        if (ingr.StartsWith(recipe_str[i]) || ingr.Contains(recipe_str[i]))
                        {
                            recipe_str[i] = ingr;
                            recipe[i]     = data.Where(d => d.ingredient.name.Equals(ingr)).ToArray()[0].ingredient.id;
                            found         = true;
                            break;
                        }
                    }
                    // ingredient not found
                    if (found == false)
                    {
                        Console.WriteLine("Ingredient [" + recipe_str[i] + "] was not found");
                        return;
                    }
                }
            }

            Console.Write("Your recipe: ");
            PrintRecipe(recipe_str);

            // keep track of ingredient recommendations
            Recommendation[] recommendations = null;
            // Naive Bayes
            if (model_choice.Equals(ModelChoice.NB))
            {
                NaiveBayes nb = new NaiveBayes();
                recommendations = nb.RecipeRecommendations(nb.GetModel(), recipe, true, true, false);
            }
            // k Nearest Neighbors
            else if (model_choice.Equals(ModelChoice.KNN))
            {
                KNN knn = new KNN();
                recommendations = knn.GetRecommendations(6, DistanceChoice.Jaccard, recipe, Voting.Unweighted);
            }
            // Modified k Nearest Neighbors (dynamic k)
            else if (model_choice.Equals(ModelChoice.MKNN))
            {
                KNN knn   = new KNN();
                int new_k = 0;
                recommendations = knn.GetRecommendations_ModifiedKNN(recipe, DistanceChoice.Jaccard_Similarity, Voting.Unweighted, ref new_k);
            }
            else
            {
                return;
            }
            // Ingredients to Add
            if (add == true)
            {
                Console.WriteLine("Your recommendations:");

                for (int i = 0; i < top; i++)
                {
                    // skip ingredients in recipe
                    if (include_recipe_ingrs == false && recipe_str.Contains(recommendations[i].ingredient.name))
                    {
                        top++;
                        continue;
                    }
                    Console.WriteLine(recommendations[i].ingredient.name);
                }
            }
            // Ingredients to Remove
            else
            {
                Console.WriteLine("Ingredients ordered by what to remove first:");
                // only keep scores of ingredients in input recipe
                recommendations = recommendations.Where(d => recipe.Contains(d.ingredient.id)).ToArray();
                // sort by score
                recommendations = recommendations.OrderBy(d => d.score).ToArray();
                for (int i = 0; i < recipe.Length; i++)
                {
                    Console.WriteLine(recommendations[i].ingredient.name);
                }
            }
            Console.WriteLine();
        }