예제 #1
0
    public static void Main(string[] args)
    {
        // load the data
        var user_mapping   = new EntityMapping();
        var item_mapping   = new EntityMapping();
        var training_data  = ItemRecommendation.Read(args[0], user_mapping, item_mapping);
        var relevant_users = training_data.AllUsers;         // users that will be taken into account in the evaluation
        var relevant_items = training_data.AllItems;         // items that will be taken into account in the evaluation
        var test_data      = ItemRecommendation.Read(args[1], user_mapping, item_mapping);

        // set up the recommender
        var recommender = new MostPopular();

        recommender.Feedback = training_data;
        recommender.Train();

        // measure the accuracy on the test data set
        var results = ItemPredictionEval.Evaluate(recommender, test_data, training_data, relevant_users, relevant_items);

        foreach (var key in results.Keys)
        {
            Console.WriteLine("{0}={1}", key, results[key]);
        }

        // make a prediction for a certain user and item
        Console.WriteLine(recommender.Predict(user_mapping.ToInternalID(1), item_mapping.ToInternalID(1)));
    }
예제 #2
0
    static void LoadData()
    {
        TimeSpan loading_time = Utils.MeasureTime(delegate() {
            // training data
            training_data = ItemRecommendation.Read(Path.Combine(data_dir, training_file), user_mapping, item_mapping);

            // relevant users and items
            if (relevant_users_file != null)
            {
                relevant_users = new HashSet <int>(user_mapping.ToInternalID(Utils.ReadIntegers(Path.Combine(data_dir, relevant_users_file))));
            }
            else
            {
                relevant_users = training_data.AllUsers;
            }
            if (relevant_items_file != null)
            {
                relevant_items = new HashSet <int>(item_mapping.ToInternalID(Utils.ReadIntegers(Path.Combine(data_dir, relevant_items_file))));
            }
            else
            {
                relevant_items = training_data.AllItems;
            }

            if (!(recommender is MyMediaLite.ItemRecommendation.Random))
            {
                ((ItemRecommender)recommender).Feedback = training_data;
            }

            // user attributes
            if (recommender is IUserAttributeAwareRecommender)
            {
                if (user_attributes_file == null)
                {
                    Usage("Recommender expects --user-attributes=FILE.");
                }
                else
                {
                    ((IUserAttributeAwareRecommender)recommender).UserAttributes = AttributeData.Read(Path.Combine(data_dir, user_attributes_file), user_mapping);
                }
            }

            // item attributes
            if (recommender is IItemAttributeAwareRecommender)
            {
                if (item_attributes_file == null)
                {
                    Usage("Recommender expects --item-attributes=FILE.");
                }
                else
                {
                    ((IItemAttributeAwareRecommender)recommender).ItemAttributes = AttributeData.Read(Path.Combine(data_dir, item_attributes_file), item_mapping);
                }
            }
            if (filtered_eval)
            {
                if (item_attributes_file == null)
                {
                    Usage("--filtered-evaluation expects --item-attributes=FILE.");
                }
                else
                {
                    item_attributes = AttributeData.Read(Path.Combine(data_dir, item_attributes_file), item_mapping);
                }
            }

            // user relation
            if (recommender is IUserRelationAwareRecommender)
            {
                if (user_relations_file == null)
                {
                    Usage("Recommender expects --user-relation=FILE.");
                }
                else
                {
                    ((IUserRelationAwareRecommender)recommender).UserRelation = RelationData.Read(Path.Combine(data_dir, user_relations_file), user_mapping);
                    Console.WriteLine("relation over {0} users", ((IUserRelationAwareRecommender)recommender).NumUsers);                     // TODO move to DisplayDataStats
                }
            }

            // item relation
            if (recommender is IItemRelationAwareRecommender)
            {
                if (user_relations_file == null)
                {
                    Usage("Recommender expects --item-relation=FILE.");
                }
                else
                {
                    ((IItemRelationAwareRecommender)recommender).ItemRelation = RelationData.Read(Path.Combine(data_dir, item_relations_file), item_mapping);
                    Console.WriteLine("relation over {0} items", ((IItemRelationAwareRecommender)recommender).NumItems);                     // TODO move to DisplayDataStats
                }
            }

            // test data
            if (test_ratio == 0)
            {
                if (test_file != null)
                {
                    test_data = ItemRecommendation.Read(Path.Combine(data_dir, test_file), user_mapping, item_mapping);
                }
            }
            else
            {
                var split     = new PosOnlyFeedbackSimpleSplit <PosOnlyFeedback <SparseBooleanMatrix> >(training_data, test_ratio);
                training_data = split.Train[0];
                test_data     = split.Test[0];
            }
        });

        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "loading_time {0,0:0.##}", loading_time.TotalSeconds));
    }
예제 #3
0
    public static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Handlers.UnhandledExceptionHandler);

        // check number of command line parameters
        if (args.Length < 4)
        {
            Usage("Not enough arguments.");
        }

        // read command line parameters
        RecommenderParameters parameters = null;

        try     { parameters = new RecommenderParameters(args, 4); }
        catch (ArgumentException e)     { Usage(e.Message); }

        // other parameters
        string data_dir             = parameters.GetRemoveString("data_dir");
        string relevant_items_file  = parameters.GetRemoveString("relevant_items");
        string item_attributes_file = parameters.GetRemoveString("item_attributes");
        string user_attributes_file = parameters.GetRemoveString("user_attributes");
        //string save_mapping_file    = parameters.GetRemoveString( "save_model");
        int  random_seed = parameters.GetRemoveInt32("random_seed", -1);
        bool no_eval     = parameters.GetRemoveBool("no_eval", false);
        bool compute_fit = parameters.GetRemoveBool("compute_fit", false);

        if (random_seed != -1)
        {
            MyMediaLite.Util.Random.InitInstance(random_seed);
        }

        // main data files and method
        string trainfile       = args[0].Equals("-") ? "-" : Path.Combine(data_dir, args[0]);
        string testfile        = args[1].Equals("-") ? "-" : Path.Combine(data_dir, args[1]);
        string load_model_file = args[2];
        string method          = args[3];

        // set correct recommender
        switch (method)
        {
        case "BPR-MF-ItemMapping":
            recommender = Recommender.Configure(bprmf_map, parameters, Usage);
            break;

        case "BPR-MF-ItemMapping-Optimal":
            recommender = Recommender.Configure(bprmf_map_bpr, parameters, Usage);
            break;

        case "BPR-MF-ItemMapping-Complex":
            recommender = Recommender.Configure(bprmf_map_com, parameters, Usage);
            break;

        case "BPR-MF-ItemMapping-kNN":
            recommender = Recommender.Configure(bprmf_map_knn, parameters, Usage);
            break;

        case "BPR-MF-ItemMapping-SVR":
            recommender = Recommender.Configure(bprmf_map_svr, parameters, Usage);
            break;

        case "BPR-MF-UserMapping":
            recommender = Recommender.Configure(bprmf_user_map, parameters, Usage);
            break;

        case "BPR-MF-UserMapping-Optimal":
            recommender = Recommender.Configure(bprmf_user_map_bpr, parameters, Usage);
            break;

        default:
            Usage(string.Format("Unknown method: '{0}'", method));
            break;
        }

        if (parameters.CheckForLeftovers())
        {
            Usage(-1);
        }

        // ID mapping objects
        var user_mapping = new EntityMapping();
        var item_mapping = new EntityMapping();

        // training data
        training_data        = ItemRecommendation.Read(Path.Combine(data_dir, trainfile), user_mapping, item_mapping);
        recommender.Feedback = training_data;

        // relevant items
        if (!relevant_items_file.Equals(string.Empty))
        {
            relevant_items = new HashSet <int>(item_mapping.ToInternalID(Utils.ReadIntegers(Path.Combine(data_dir, relevant_items_file))));
        }
        else
        {
            relevant_items = training_data.AllItems;
        }

        // user attributes
        if (recommender is IUserAttributeAwareRecommender)
        {
            if (user_attributes_file.Equals(string.Empty))
            {
                Usage("Recommender expects user_attributes=FILE.");
            }
            else
            {
                ((IUserAttributeAwareRecommender)recommender).UserAttributes = AttributeData.Read(Path.Combine(data_dir, user_attributes_file), user_mapping);
            }
        }

        // item attributes
        if (recommender is IItemAttributeAwareRecommender)
        {
            if (item_attributes_file.Equals(string.Empty))
            {
                Usage("Recommender expects item_attributes=FILE.");
            }
            else
            {
                ((IItemAttributeAwareRecommender)recommender).ItemAttributes = AttributeData.Read(Path.Combine(data_dir, item_attributes_file), item_mapping);
            }
        }

        // test data
        test_data = ItemRecommendation.Read(Path.Combine(data_dir, testfile), user_mapping, item_mapping);

        TimeSpan seconds;

        Recommender.LoadModel(recommender, load_model_file);

        // set the maximum user and item IDs in the recommender - this is important for the cold start use case
        recommender.MaxUserID = user_mapping.InternalIDs.Max();
        recommender.MaxItemID = item_mapping.InternalIDs.Max();

        DisplayDataStats();

        Console.Write(recommender.ToString() + " ");

        if (compute_fit)
        {
            seconds = Utils.MeasureTime(delegate() {
                int num_iter = recommender.NumIterMapping;
                recommender.NumIterMapping = 0;
                recommender.LearnAttributeToFactorMapping();
                Console.Error.WriteLine();
                Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "iteration {0} fit {1}", -1, recommender.ComputeFit()));

                recommender.NumIterMapping = 1;
                for (int i = 0; i < num_iter; i++, i++)
                {
                    recommender.IterateMapping();
                    Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "iteration {0} fit {1}", i, recommender.ComputeFit()));
                }
                recommender.NumIterMapping = num_iter;                 // restore
            });
        }
        else
        {
            seconds = Utils.MeasureTime(delegate() {
                recommender.LearnAttributeToFactorMapping();
            });
        }
        Console.Write("mapping_time " + seconds + " ");

        if (!no_eval)
        {
            seconds = EvaluateRecommender(recommender, test_data, training_data);
        }
        Console.WriteLine();
    }