public static void Main(string[] args)
    {
        // load the data
        var training_data = RatingData.Read(args[0]);
        var test_data     = RatingData.Read(args[1]);

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

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

        // measure the accuracy on the test data set
        var results = recommender.Evaluate(test_data);

        Console.WriteLine("RMSE={0} MAE={1}", results["RMSE"], results["MAE"]);
        Console.WriteLine(results);

        // make a prediction for a certain user and item
        Console.WriteLine(recommender.Predict(1, 1));

        var bmf = new BiasedMatrixFactorization {
            Ratings = training_data
        };

        Console.WriteLine(bmf.DoCrossValidation());
    }
        [Test()] public void TestComputeCorrelations2()
        {
            // load data from disk
            var user_mapping = new Mapping();
            var item_mapping = new Mapping();
            var ratings      = RatingData.Read("../../../../data/ml-100k/u1.base", user_mapping, item_mapping);

            var p = new Pearson(ratings.AllUsers.Count, 200f);

            Assert.AreEqual(-0.02788301f, p.ComputeCorrelation(ratings, EntityType.ITEM, 45, 311), 0.00001);
        }
        IFoldInRatingPredictor CreateRecommender()
        {
            var training_data = RatingData.Read("../../../../data/ml-100k/u.data");
            var recommender   = new MatrixFactorization();

            recommender.Ratings    = training_data;
            recommender.NumFactors = 4;
            recommender.NumIter    = 5;
            recommender.Train();

            return(recommender);
        }
Пример #4
0
    private void CreateRecommender()
    {
        BiasedMatrixFactorization recommender = new BiasedMatrixFactorization();

        Console.Error.Write("Reading in ratings ... ");
        TimeSpan time = Wrap.MeasureTime(delegate() {
            recommender.Ratings = RatingData.Read(ratings_file, user_mapping, item_mapping);
        });

        Console.Error.WriteLine("done ({0,0:0.##}).", time.TotalSeconds.ToString(CultureInfo.InvariantCulture));

        //Console.Error.Write("Reading in additional ratings ... ");
        //string[] rating_files = Directory.GetFiles("../../saved_data/", "user-ratings-*");
        //Console.Error.WriteLine("done.");

        foreach (var indices_for_item in recommender.Ratings.ByItem)
        {
            if (indices_for_item.Count > 0)
            {
                movies_by_frequency.Add(new Tuple <int, float>(recommender.Ratings.Items[indices_for_item[0]], indices_for_item.Count));
            }
        }
        movies_by_frequency = movies_by_frequency.OrderByDescending(x => x.Item2).ToList();
        for (int i = 0; i < n_movies; i++)
        {
            top_n_movies.Add(movies_by_frequency[i].Item1);
        }

        Console.Error.Write("Loading prediction model ... ");
        recommender.UpdateUsers    = true;
        recommender.UpdateItems    = false;
        recommender.BiasReg        = 0.001f;
        recommender.Regularization = 0.045f;
        recommender.NumIter        = 60;
        time = Wrap.MeasureTime(delegate() {
            recommender.LoadModel(model_file);
        });
        Console.Error.WriteLine("done ({0,0:0.##}).", time.TotalSeconds.ToString(CultureInfo.InvariantCulture));

        rating_predictor = recommender;

        current_user_id = user_mapping.ToInternalID(current_user_external_id.ToString());
        //rating_predictor.AddUser(current_user_id);

        // add movies that were not in the training set
        //rating_predictor.AddItem( item_mapping.InternalIDs.Count - 1 );

        PredictAllRatings();
    }
Пример #5
0
        public void TestRead()
        {
            var reader = new StringReader(@"5951,50,5
5951,223,5
5951,260,5
5951,293,5
5951,356,4
5951,364,3
5951,457,3
");

            IRatings data = RatingData.Read(reader);

            Assert.AreEqual(7, data.Count);
        }
Пример #6
0
        public void TestReadIgnoreLine()
        {
            var reader = new StringReader(@"# first line
5951,50,5
5951,223,5
5951,260,5
5951,293,5
5951,356,4
5951,364,3
5951,457,3
");

            IRatings data = RatingData.Read(reader, null, null, true);

            Assert.AreEqual(7, data.Count);
        }
Пример #7
0
        public void TestCase()
        {
            string filename = "../../../../tests/example.test";
            var    mapping  = new IdentityMapping();
            var    ratings  = RatingData.Read(filename);

            var recommender = new ExternalRatingPredictor()
            {
                PredictionFile = filename, UserMapping = mapping, ItemMapping = mapping
            };

            recommender.Train();
            for (int i = 0; i < ratings.Count; i++)
            {
                Assert.AreEqual(ratings[i], recommender.Predict(ratings.Users[i], ratings.Items[i]));
            }
        }
Пример #8
0
    void LoadRatings(string name)
    {
        // assumption: ratings, training data and model were reloaded

        ratings.Clear();

        using (var reader = new StreamReader("../../../../saved_data/user-ratings-" + name))
        {
            IRatings user_ratings = RatingData.Read(reader, user_mapping, item_mapping);

            for (int i = 0; i < user_ratings.Count; i++)
            {
                ratings[user_ratings.Items[i]] = user_ratings[i];
                current_user_id = user_ratings.Users[i];                 // TODO check whether user ID is the same for all ratings
            }
        }
    }
        //*********** ItemKNN Recommender - most similar items
        public List <int> getMostSimilarItems(string dataset, int itemid, int recs)
        {
            var mydata = RatingData.Read(dataset);
            //Create the recommender
            var recommenderItemKNN = new MyMediaLite.RatingPrediction.ItemKNN();

            //Give it the dataset
            recommenderItemKNN.Ratings = mydata;
            //Train it
            recommenderItemKNN.Train();
            /////////////

            var item_recs = recommenderItemKNN.GetMostSimilarItems(itemid, (uint)recs);

            // Print similar items
            foreach (var i in item_recs)
            {
                mylist.Add(i);
            }

            return(mylist);
        }
        //*********** GlobalAverage Recommender
        public List <int> getBestItemsGlobalAverage(string dataset, int userid, int recs)
        {
            var mydata = RatingData.Read(dataset);
            //Create the recommender
            var recommenderGlobalAverage = new MyMediaLite.RatingPrediction.GlobalAverage();

            //Give it the dataset
            recommenderGlobalAverage.Ratings = mydata;
            //Train it
            recommenderGlobalAverage.Train();
            /////////////

            // Make the predictions
            var user_recs = recommenderGlobalAverage.Recommend(userid, recs);

            // get the recommendations
            foreach (var i in user_recs)
            {
                mylist.Add(i.Item1);
            }

            return(mylist);
        }
 ///
 public override void Train()
 {
     external_scores = RatingData.Read(PredictionFile, UserMapping, ItemMapping);
     // we dont use it locally but it should be initialized before entering the parallel code
     IList <IList <int> > nonsense = external_scores.ByUser;
 }
Пример #12
0
    protected override void LoadData()
    {
        bool static_data = !online_eval;

        TimeSpan loading_time = Wrap.MeasureTime(delegate() {
            base.LoadData();

            // read training data
            if ((recommender is TimeAwareRatingPredictor || chronological_split != null) && file_format != RatingFileFormat.MOVIELENS_1M)
            {
                training_data = TimedRatingData.Read(training_file, user_mapping, item_mapping);
            }
            else
            {
                if (file_format == RatingFileFormat.DEFAULT)
                {
                    training_data = static_data
                                                ? StaticRatingData.Read(training_file, user_mapping, item_mapping, rating_type)
                                                : RatingData.Read(training_file, user_mapping, item_mapping);
                }
                else if (file_format == RatingFileFormat.IGNORE_FIRST_LINE)
                {
                    training_data = static_data
                                                ? StaticRatingData.Read(training_file, user_mapping, item_mapping, rating_type, TestRatingFileFormat.WITH_RATINGS, true)
                                                : RatingData.Read(training_file, user_mapping, item_mapping, true);
                }
                else if (file_format == RatingFileFormat.MOVIELENS_1M)
                {
                    training_data = MovieLensRatingData.Read(training_file, user_mapping, item_mapping);
                }
                else if (file_format == RatingFileFormat.KDDCUP_2011)
                {
                    training_data = MyMediaLite.IO.KDDCup2011.Ratings.Read(training_file);
                }
            }
            recommender.Ratings = training_data;

            // read test data
            if (test_file != null)
            {
                TestRatingFileFormat test_format = test_no_ratings ? TestRatingFileFormat.WITHOUT_RATINGS : TestRatingFileFormat.WITH_RATINGS;
                if (recommender is TimeAwareRatingPredictor && file_format != RatingFileFormat.MOVIELENS_1M)
                {
                    test_data = TimedRatingData.Read(test_file, user_mapping, item_mapping, test_format);
                }
                else if (file_format == RatingFileFormat.MOVIELENS_1M)
                {
                    test_data = MovieLensRatingData.Read(test_file, user_mapping, item_mapping, test_format);
                }
                else if (file_format == RatingFileFormat.KDDCUP_2011)
                {
                    test_data = MyMediaLite.IO.KDDCup2011.Ratings.Read(test_file);
                }
                else
                {
                    test_data = StaticRatingData.Read(test_file, user_mapping, item_mapping, rating_type, test_format, file_format == RatingFileFormat.IGNORE_FIRST_LINE);
                }

                if (recommender is ITransductiveRatingPredictor)
                {
                    ((ITransductiveRatingPredictor)recommender).AdditionalFeedback = test_data;
                }
            }
        });

        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "loading_time {0:0.##}", loading_time.TotalSeconds));
        Console.Error.WriteLine("memory {0}", Memory.Usage);
    }
Пример #13
0
 // default constructor
 public EvaluationModels(string dataset)
 {
     mydata = RatingData.Read(dataset);
 }
Пример #14
0
        static void Main(string[] args)
        {
            String path_training_data = "user_ratedmovies.dat";
            String path_user_data     = "users.dat";
            String path_atr_data      = "movie_genres.dat";
            String path_pred_data     = "predict.dat";

            var user_mapping = new Mapping();
            var item_mapping = new Mapping();

            //var user_mapping = new EntityMapping();
            //var item_mapping = new EntityMapping();
            //String path_training_data = "1.rec.train";
            //String path_test_data = "1.rec.test";
            //String path_training_data = "u1.base";
            //String path_test_data = "u1.test";
            //var training_data = ItemData.Read(path_training_data);
            //var test_data = ItemData.Read(path_test_data);

            var training_data  = RatingData.Read(path_training_data, user_mapping, item_mapping);
            var relevant_items = item_mapping.InternalIDs;
            var atr_data       = AttributeData.Read(path_atr_data, item_mapping);
            var pred_data      = RatingData.Read(path_pred_data, user_mapping, item_mapping);

            //var user_data = ItemData.Read(path_user_data, user_mapping);
            Console.WriteLine("I'm here");
            //Console.WriteLine(String.Join(";", user_data.AllUsers));
            //var test_data = RatingData.Read(path_test_data);

            //var recommender = new ItemKNN();
            //recommender.Feedback = training_data;
            //recommender.Train();

            //var recommender = new GSVDPlusPlus();
            //var recommender = new MatrixFactorization();
            //recommender.Ratings = training_data;
            // recommender.MaxRating = 5;
            // recommender.MinRating = 1;
            //recommender.ItemAttributes = atr_data;
            //recommender.NumFactors = 5;
            //recommender.NumIter = 10;

            //recommender.Train();


            var recommender = new SVDPlusPlus();

            recommender.Ratings = training_data;
            //recommender.AdditionalFeedback = user_data;
            Console.WriteLine("I'm here2");
            Console.WriteLine(recommender.ToString());
            //recommender.ItemAttributes = atr_data;
            recommender.NumFactors = 10;

            Console.WriteLine("I'm here3");
            Console.WriteLine(recommender.ToString());
            recommender.Train();
            Console.WriteLine("I'm here4");


            //var results = recommender.DoIterativeCrossValidation(recommdender, 10, 1, 1, true);
            var results = recommender.DoCrossValidation(10, false, true);

            ////var results = recommender.Evaluate(test_data, training_data);
            foreach (var key in results.Keys)
            {
                Console.WriteLine("{0}={1}", key, results[key]);
            }
            Console.WriteLine(results);
            //recommender.Predict();
            recommender.WritePredictions(pred_data, "test45.dat", user_mapping, item_mapping, "{0}|{1}|{2}");

            Console.WriteLine(recommender.Predict(1, 1));
            Console.ReadLine();
        }
    public static void Main(string[] args)
    {
        // TODO add random seed
        // TODO report per-user times

        string data_file      = args[0];
        string method         = args[1];
        string options        = args[2];
        int    num_test_users = int.Parse(args[3]);

        // load the data
        var all_data = RatingData.Read(data_file);

        // TODO randomize
        var test_users = new HashSet <int>(Enumerable.Range(0, num_test_users));

        var update_indices = new List <int>();
        var eval_indices   = new List <int>();

        foreach (int user_id in test_users)
        {
            if (all_data.ByUser[user_id].Count > 1)
            {
                var user_indices = all_data.ByUser[user_id];
                for (int i = 0; i < user_indices.Count - 1; i++)
                {
                    update_indices.Add(user_indices[i]);
                }
                for (int i = user_indices.Count - 1; i < user_indices.Count; i++)
                {
                    eval_indices.Add(user_indices[i]);
                }
            }
        }

        var training_indices = new List <int>();

        for (int i = 0; i < all_data.Count; i++)
        {
            if (!test_users.Contains(all_data.Users[i]))
            {
                training_indices.Add(i);
            }
        }
        var training_data = new MyMediaLite.Data.Ratings();

        foreach (int i in training_indices)
        {
            training_data.Add(all_data.Users[i], all_data.Items[i], all_data[i]);
        }

        var update_data = new RatingsProxy(all_data, update_indices);
        var eval_data   = new RatingsProxy(all_data, eval_indices);

        Console.Write(training_data.Statistics());
        Console.Write(update_data.Statistics());
        Console.Write(eval_data.Statistics());

        // prepare recommender
        RatingPredictor recommender = method.CreateRatingPredictor();

        recommender.Configure(options);
        recommender.Ratings = training_data;
        Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "ratings range: [{0}, {1}]", recommender.MinRating, recommender.MaxRating));
        Console.WriteLine("recommender: {0}", recommender);
        recommender.Train();

        // I. complete retraining
        Console.WriteLine(
            "complete training: {0}",
            recommender.EvaluateFoldInCompleteRetraining(update_data, eval_data));

        // II. online updates
        Console.WriteLine(
            "incremental training: {0}",
            ((IncrementalRatingPredictor)recommender).EvaluateFoldInIncrementalTraining(update_data, eval_data));

        // III. fold-in
        Console.WriteLine(
            "fold-in: {0}",
            ((IFoldInRatingPredictor)recommender).EvaluateFoldIn(update_data, eval_data));
    }
Пример #16
0
 ///
 public override void Train()
 {
     external_scores = RatingData.Read(PredictionFile, UserMapping, ItemMapping);
 }