Пример #1
0
    private static void getSubset(ITimedRatings all_data, ref ITimedRatings sub_all_data)
    {
        List <int>      sub_list_item = new List <int>();
        List <DateTime> sub_list_time = new List <DateTime>();
        List <int>      sub_list_user = new List <int>();

        System.Random gen = new System.Random();

        IList <int> all_users = all_data.AllUsers;
        IList <int> users     = new List <int> ();

        for (int i = 0; i < all_users.Count; i++)
        {
            int rnd = gen.Next(100);
            if (rnd <= 2)
            {
                users.Add(all_users[i]);
            }
        }

        for (int i = 0; i < all_data.Users.Count; i++)
        {
            if (users.Contains(all_data.Users [i]))
            {
                sub_list_item.Add(all_data.Items [i]);
                sub_list_time.Add(all_data.Times [i]);
                sub_list_user.Add(all_data.Users [i]);
            }
        }
        create_data(sub_list_user, sub_list_item, sub_list_time, ref sub_all_data);
        Console.Write(sub_all_data.Statistics());
        Console.WriteLine("finished creating subset:");
        Console.WriteLine(DateTime.Now);
    }
Пример #2
0
    private static void startBPRMF(ITimedRatings all_data)
    {
        removeUserThreshold(ref all_data);
        Console.WriteLine("Start iteration Test BPRMF");
        //for (int i = 0; i < 5; i++) {
        ITimedRatings validation_data = new TimedRatings();    // 10%
        ITimedRatings test_data       = new TimedRatings();    // 20%
        ITimedRatings training_data   = new TimedRatings();    // 70%

        readAndSplitData(all_data, ref test_data, ref training_data, ref validation_data);
        IPosOnlyFeedback training_data_pos = new PosOnlyFeedback <SparseBooleanMatrix> ();        // 80%

        for (int index = 0; index < training_data.Users.Count; index++)
        {
            training_data_pos.Add(training_data.Users [index], training_data.Items [index]);
        }


        MyMediaLite.ItemRecommendation.BPRMF recommender = new MyMediaLite.ItemRecommendation.BPRMF();
        recommender.Feedback = training_data_pos;
        DateTime start_time = DateTime.Now;

        recommender.Train();

        Console.Write("Total Training time needed:");
        Console.WriteLine(((TimeSpan)(DateTime.Now - start_time)).TotalMilliseconds);
        Console.WriteLine("Final results in this iteration:");
        var results = MyMediaLite.Eval.ItemsWeatherItemRecommender.EvaluateTime(recommender, validation_data, training_data, "VALIDATION ", false);

        results = MyMediaLite.Eval.ItemsWeatherItemRecommender.EvaluateTime(recommender, test_data, training_data, "TEST ", false);
        //}
    }
Пример #3
0
    public static void removeUserThreshold(ref ITimedRatings all_data)
    {
        IList <int> items_to_delete = new List <int> ();
        Dictionary <int, IList <DateTime> > itemsTimes = all_data.getTimesItemDict();

        foreach (int item in all_data.AllItems)
        {
            if (itemsTimes [item].Count < 20)
            {
                items_to_delete.Add(item);
            }
        }
        foreach (int item in items_to_delete)
        {
            all_data.RemoveItem(item);
        }

        IList <int> user_to_delete = new List <int>();
        Dictionary <int, IList <int> > userItems = all_data.getItemsUserDict();

        foreach (int user in all_data.AllUsers)
        {
            if (userItems[user].Count < 20)
            {
                user_to_delete.Add(user);
            }
        }
        foreach (int user in user_to_delete)
        {
            all_data.RemoveUser(user);
        }

        Console.Write(all_data.Statistics());
        Console.Write("Finished removing thresholds");
    }
Пример #4
0
//	private static void readAndSplitDataRandomly(ITimedRatings all_data, ref ITimedRatings validation_data, ref ITimedRatings test_data, ref ITimedRatings training_data){
//
//
//		Console.WriteLine (all_data.Statistics ());
//
//		List<int> validation_list_item = new List<int>();
//		List<int> test_list_item = new List<int>();
//		List<int> training_list_item = new List<int>();
//
//		List<DateTime> validation_list_time = new List<DateTime>();
//		List<DateTime> test_list_time = new List<DateTime>();
//		List<DateTime> training_list_time = new List<DateTime>();
//
//		List<int> validation_list_user = new List<int>();
//		List<int> test_list_user = new List<int>();
//		List<int> training_list_user = new List<int>();
//
//		System.Random gen = new System.Random();
//		for (int i = 0; i < all_data.Users.Count; i++) {
//			int rnd = gen.Next(100);
//			if(rnd <= 10){
//				validation_list_item.Add(all_data.Items[i]);
//				validation_list_time.Add(all_data.Times[i]);
//				validation_list_user.Add(all_data.Users[i]);
//			}else if(rnd <=30){
//				test_list_item.Add(all_data.Items[i]);
//				test_list_time.Add(all_data.Times[i]);
//				test_list_user.Add(all_data.Users[i]);
//			}else{
//				training_list_item.Add(all_data.Items[i]);
//				training_list_time.Add(all_data.Times[i]);
//				training_list_user.Add(all_data.Users[i]);
//			}
//		}
//		create_data(validation_list_user,  validation_list_item, validation_list_time, ref validation_data);
//		create_data(test_list_user,  test_list_item, test_list_time, ref test_data);
//		create_data(training_list_user,  training_list_item, training_list_time, ref training_data);
//
//
//
//		Console.Write(validation_data.Statistics ());
//		Console.Write(test_data.Statistics ());
//		Console.Write(training_data.Statistics());
//		Console.WriteLine ("finished creating datasets:");
//		Console.WriteLine (DateTime.Now);
//	}
//
    private static void readAndSplitData(ITimedRatings all_data, ref ITimedRatings test_data, ref ITimedRatings training_data, ref ITimedRatings validation_data)
    {
        Dictionary <int, IList <DateTime> > user_times = all_data.getTimesUserDict();
        Dictionary <int, IList <int> >      user_items = all_data.getItemsUserDict();

        foreach (int user_id in all_data.AllUsers)
        {
            List <DateTime> timesOfUser    = (List <DateTime>)user_times [user_id];
            List <int>      itemsOfUser    = (List <int>)user_items[user_id];
            int             amountCheckIns = timesOfUser.Count;
            //int validation = (int)(amountCheckIns * 0.1);
            int test     = (int)(amountCheckIns * 0.2);
            int training = (int)(amountCheckIns * 0.7);

            List <int> training_list_item   = itemsOfUser.GetRange(0, training);
            List <int> test_list_item       = itemsOfUser.GetRange(training, amountCheckIns - (training));
            List <int> validation_list_item = itemsOfUser.GetRange(training + test, amountCheckIns - (training + test));

            List <DateTime> training_list_time   = timesOfUser.GetRange(0, training);
            List <DateTime> test_list_time       = timesOfUser.GetRange(training, amountCheckIns - (training));
            List <DateTime> validation_list_time = timesOfUser.GetRange(training + test, amountCheckIns - (training + test));

            create_data(user_id, validation_list_item, validation_list_time, ref validation_data);
            create_data(user_id, test_list_item, test_list_time, ref test_data);
            create_data(user_id, training_list_item, training_list_time, ref training_data);
        }

        Console.Write(validation_data.Statistics());
        Console.Write(test_data.Statistics());
        Console.Write(training_data.Statistics());
        Console.WriteLine("finished creating datasets:");
        Console.WriteLine(DateTime.Now);
    }
Пример #5
0
    private static void startIterationTestNew(ITimedRatings all_data, bool weather_aware, double beta, double mu, int city, int iterations, string feature = "")
    {
        removeUserThreshold(ref all_data);
        Console.WriteLine("Start iteration test");
        ITimedRatings validation_data = new TimedRatings();    // 10%
        ITimedRatings test_data       = new TimedRatings();    // 30%
        ITimedRatings training_data   = new TimedRatings();    // 70%

        readAndSplitData(all_data, ref test_data, ref training_data, ref validation_data);

        int K              = 100;
        int rangeSize      = 10;
        int user_count     = all_data.AllUsers.Count;
        int location_count = all_data.AllItems.Count();

        double[,] U1 = new double[user_count, K];
        double[,] U2 = new double[user_count, K];
        double[,] L1 = new double[location_count, K];
        double[,] L2 = new double[location_count, K];
        double[,] L3 = new double[location_count, K];
        double[,] F  = new double[rangeSize, K];
        Dictionary <int, int> idMapperCategories = new Dictionary <int, int> ();
        Dictionary <int, int> idMapperLocations  = new Dictionary <int, int> ();
        Dictionary <int, int> idMapperUser       = new Dictionary <int, int> ();

        initMatrixNormal(all_data.AllUsers, ref U1, ref idMapperUser, K);
        initMatrixNormal(all_data.AllUsers, ref U2, ref idMapperUser, K);
        initMatrixNormal(all_data.AllItems, ref L1, ref idMapperLocations, K);
        initMatrixNormal(all_data.AllItems, ref L2, ref idMapperLocations, K);
        initMatrixNormal(all_data.AllItems, ref L3, ref idMapperLocations, K);
        initMatrixNormal(rangeSize, ref F, K);
        WeatherContextAwareItemRecommender recommender = new WeatherContextAwareItemRecommender(U1, U2, L1, L2, L3, F, idMapperLocations, idMapperUser, city, feature);

        recommender.connection_string = connection;
        recommender.Ratings           = training_data;
        recommender.Validation        = validation_data;
        recommender.Test          = test_data;
        recommender.weather_aware = weather_aware;
        recommender.rangeSize     = rangeSize;
        recommender.max_iter      = iterations;
        recommender.evaluation_at = 20;
        recommender.beta          = beta;
        DateTime start_time = DateTime.Now;

        recommender.Train();
        Console.Write("Total Training time needed:");
        Console.WriteLine(((TimeSpan)(DateTime.Now - start_time)).TotalMilliseconds);
        Console.WriteLine("Final results in this iteration:");

        //}
    }
Пример #6
0
		/// <summary>Create a TimedRatingsProxy object</summary>
		/// <param name="ratings">a ratings data structure</param>
		/// <param name="indices">an index list pointing to entries in the ratings</param>
		public TimedRatingsProxy(ITimedRatings ratings, IList<int> indices)
		{
			Users  = new ListProxy<int>(ratings.Users, indices);
			Items  = new ListProxy<int>(ratings.Items, indices);
			Values = new ListProxy<float>(ratings, indices);
			Times  = new ListProxy<DateTime>(ratings.Times, indices);

			MaxUserID = ratings.MaxUserID;
			MaxItemID = ratings.MaxItemID;
			Scale = ratings.Scale;

			EarliestTime = Count > 0 ? Times.Min() : DateTime.MaxValue;
			LatestTime   = Count > 0 ? Times.Max() : DateTime.MinValue;
		}
        /// <summary>Create a TimedRatingsProxy object</summary>
        /// <param name="ratings">a ratings data structure</param>
        /// <param name="indices">an index list pointing to entries in the ratings</param>
        public TimedRatingsProxy(ITimedRatings ratings, IList <int> indices)
        {
            Users  = new ListProxy <int>(ratings.Users, indices);
            Items  = new ListProxy <int>(ratings.Items, indices);
            Values = new ListProxy <float>(ratings, indices);
            Times  = new ListProxy <DateTime>(ratings.Times, indices);

            MaxUserID = ratings.MaxUserID;
            MaxItemID = ratings.MaxItemID;
            Scale     = ratings.Scale;

            EarliestTime = Count > 0 ? Times.Min() : DateTime.MaxValue;
            LatestTime   = Count > 0 ? Times.Max() : DateTime.MinValue;
        }
Пример #8
0
    private static void startIterationTest(ITimedRatings all_data, bool weather_aware, double beta, double mu, int city, int iterations)
    {
        removeUserThreshold(ref all_data);
        Console.WriteLine("Start iteration test");
        ITimedRatings validation_data = new TimedRatings();    // 10%
        ITimedRatings test_data       = new TimedRatings();    // 30%
        ITimedRatings training_data   = new TimedRatings();    // 70%

        readAndSplitData(all_data, ref test_data, ref training_data, ref validation_data);

        int K              = 100;
        int rangeSize      = 10;
        int user_count     = all_data.AllUsers.Count;
        int location_count = all_data.AllItems.Count();

        double[,] U1 = new double[user_count, K];
        double[,] U2 = new double[user_count, K];
        double[,] U3 = new double[user_count, K];
        double[,] L1 = new double[location_count, K];

        Dictionary <int, int> idMapperLocations = new Dictionary <int, int> ();
        Dictionary <int, int> idMapperUser      = new Dictionary <int, int> ();

        initMatrixNormal(all_data.AllUsers, ref U1, ref idMapperUser, K);
        initMatrixNormal(all_data.AllUsers, ref U2, ref idMapperUser, K);
        initMatrixNormal(all_data.AllUsers, ref U3, ref idMapperUser, K);
        initMatrixNormal(all_data.AllItems, ref L1, ref idMapperLocations, K);

        WeatherItemRecommender recommender = new WeatherItemRecommender(U1, U2, U3, L1, idMapperLocations, idMapperUser);

        recommender.connection_string = connection;
        recommender.Ratings           = training_data;
        recommender.Validation        = validation_data;
        recommender.Test          = test_data;
        recommender.weather_aware = weather_aware;
        recommender.max_iter      = iterations;
        recommender.evaluation_at = 20;
        recommender.beta          = beta;
        DateTime start_time = DateTime.Now;

        recommender.Train();
        Console.Write("Total Training time needed:");
        Console.WriteLine(((TimeSpan)(DateTime.Now - start_time)).TotalMilliseconds);
        Console.WriteLine("Final results in this iteration:");
        var results = MyMediaLite.Eval.ItemsWeatherItemRecommender.EvaluateTime(recommender, validation_data, training_data, "VALIDATION ", false);

        results = MyMediaLite.Eval.ItemsWeatherItemRecommender.EvaluateTime(recommender, test_data, training_data, "TEST ", false);
    }
Пример #9
0
    /// <summary>
    /// Gets string for subselecting all id's used from database
    /// </summary>
    /// <returns>The all identifiers string for database.</returns>
    public static string getAllIdsStringForDatabase(ITimedRatings ratings)
    {
        string all_ids = "(";
        bool   first   = true;

        foreach (int id in ratings.AllItems)
        {
            if (first)
            {
                all_ids += id.ToString();
                first    = false;
            }
            else
            {
                all_ids += "," + id.ToString();
            }
        }
        all_ids += ")";
        return(all_ids);
    }
Пример #10
0
    private static void startUserKNN(string data)
    {
        MyMediaLite.Data.Mapping user_mapping = new MyMediaLite.Data.Mapping();
        MyMediaLite.Data.Mapping item_mapping = new MyMediaLite.Data.Mapping();
        ITimedRatings            all_data     = readDataMapped(data, ref user_mapping, ref item_mapping);

        removeUserThreshold(ref all_data);
        Console.WriteLine("Start iteration Test UserKNN");
        //for (int i = 0; i < 5; i++) {
        ITimedRatings validation_data = new TimedRatings();    // 10%
        ITimedRatings test_data       = new TimedRatings();    // 20%
        ITimedRatings training_data   = new TimedRatings();    // 70%

        readAndSplitData(all_data, ref test_data, ref training_data, ref validation_data);
        IPosOnlyFeedback training_data_pos = new PosOnlyFeedback <SparseBooleanMatrix> ();        // 80%

        for (int index = 0; index < training_data.Users.Count; index++)
        {
            training_data_pos.Add(training_data.Users [index], training_data.Items [index]);
        }


        MyMediaLite.ItemRecommendation.UserKNN recommender = new MyMediaLite.ItemRecommendation.UserKNN();
        recommender.K           = 80;
        recommender.Q           = 1;
        recommender.Weighted    = false;
        recommender.Alpha       = 0.5f;
        recommender.Correlation = MyMediaLite.Correlation.BinaryCorrelationType.Jaccard;
        recommender.Feedback    = training_data_pos;
        DateTime start_time = DateTime.Now;

        recommender.Train();

        Console.Write("Total Training time needed:");
        Console.WriteLine(((TimeSpan)(DateTime.Now - start_time)).TotalMilliseconds);
        Console.WriteLine("Final results in this iteration:");
        var results = MyMediaLite.Eval.ItemsWeatherItemRecommender.EvaluateTime(recommender, validation_data, training_data, "VALIDATION ", false);

        results = MyMediaLite.Eval.ItemsWeatherItemRecommender.EvaluateTime(recommender, test_data, training_data, "TEST ", false);
        //}
    }
Пример #11
0
    public static Dictionary <int, int> getCategories(ITimedRatings ratings)
    {
        TestMediaLite.DBConnect conn = new TestMediaLite.DBConnect(connection);
        string all_ids = getAllIdsStringForDatabase(ratings);
        Dictionary <int, int> venueCategoryMapper = new Dictionary <int, int>();

        //select suca.id, v.id_int from VENUE v inner join CATEGORY ca on(v.category_id = ca.id) inner join CATEGORY suca on(ca.parent_category = suca.foursquare_id)
        List <string>[] res       = conn.Select(" select ca.id, v.id_int from VENUE v inner join CATEGORY ca on(v.category_id = ca.id) where v.id_int in " + all_ids, 2);
        List <string>   ca_ids    = res[0];
        List <string>   venue_ids = res[1];
        int             i         = 0;

        foreach (string venue_id in venue_ids)
        {
            int v_id  = int.Parse(venue_id);
            int ca_id = int.Parse(ca_ids[i]);
            venueCategoryMapper.Add(v_id, ca_id);
            i++;
        }
        return(venueCategoryMapper);
    }
Пример #12
0
//
    private static void readAndSplitDataRandomly(ITimedRatings all_data, ref ITimedRatings test_data, ref ITimedRatings training_data)
    {
        Dictionary <int, IList <DateTime> > user_times = all_data.getTimesUserDict();
        Dictionary <int, IList <int> >      user_items = all_data.getItemsUserDict();

        List <int> test_list_item     = new List <int>();
        List <int> training_list_item = new List <int>();


        List <DateTime> test_list_time     = new List <DateTime>();
        List <DateTime> training_list_time = new List <DateTime>();

        List <int> test_list_user     = new List <int>();
        List <int> training_list_user = new List <int>();

        System.Random gen = new System.Random();
        for (int i = 0; i < all_data.Users.Count; i++)
        {
            int rnd = gen.Next(100);
            if (rnd <= 30)
            {
                test_list_item.Add(all_data.Items[i]);
                test_list_time.Add(all_data.Times[i]);
                test_list_user.Add(all_data.Users[i]);
            }
            else
            {
                training_list_item.Add(all_data.Items[i]);
                training_list_time.Add(all_data.Times[i]);
                training_list_user.Add(all_data.Users[i]);
            }
        }
        create_data(test_list_user, test_list_item, test_list_time, ref test_data);
        create_data(training_list_user, training_list_item, training_list_time, ref training_data);

        Console.Write(test_data.Statistics());
        Console.Write(training_data.Statistics());
        Console.WriteLine("finished creating datasets:");
        Console.WriteLine(DateTime.Now);
    }
Пример #13
0
//		/// <summary>
//		/// Gets string for subselecting all id's used from database
//		/// </summary>
//		/// <returns>The all identifiers string for database.</returns>
//		static private string getAllIdsStringForDatabase(IList<int> allItems){
//
//			string all_ids = "(";
//			bool first = true;
//			foreach (int id in allItems) {
//				if (first) {
//					all_ids += id.ToString ();
//					first = false;
//				} else
//					all_ids += "," + id.ToString ();
//			}
//			all_ids += ")";
//			return all_ids;
//		}
//
////		static public void getWeatherVectorLocation(IList<int> items, string connection_string, ref Dictionary<int,IList<double>> venueWeatherVectors){
////			DBConnect conn = new DBConnect (connection_string);
////			List<string>[] res;
////			res = conn.Select ("select * " +
////			" from weather_avgs_per_venue where id_int in "+getAllIdsStringForDatabase(items), 9);
////			List<string> all_ids = res [0];
////			List<string> temperature = res [1];
////			List<string> precip_intensity = res [2];
////			List<string> wind_speed = res [3];
////			List<string> humidity = res [4];
////			List<string> cloud_cover = res [5];
////			List<string> pressure = res [6];
////			List<string> visibility = res [7];
////			List<string> moonphase = res [8];
////			int i = 0;
////			foreach(string id in all_ids){
////				venueWeatherVectors.Add(int.Parse (id),new List<double> { double.Parse(temperature [i]), double.Parse(precip_intensity [i]), double.Parse(wind_speed [i]), double.Parse(humidity [i]),
////					double.Parse(cloud_cover [i])});
////				i++;
////			}
////		}


        /// <summary>Evaluation for rankings of items</summary>
        /// <remarks>
        /// User-item combinations that appear in both sets are ignored for the test set, and thus in the evaluation,
        /// except the boolean argument repeated_events is set.
        ///
        /// The evaluation measures are listed in the Measures property.
        /// Additionally, 'num_users' and 'num_items' report the number of users that were used to compute the results
        /// and the number of items that were taken into account.
        ///
        /// Literature:
        /// <list type="bullet">
        ///   <item><description>
        ///   C. Manning, P. Raghavan, H. Schütze: Introduction to Information Retrieval, Cambridge University Press, 2008
        ///   </description></item>
        /// </list>
        ///
        /// On multi-core/multi-processor systems, the routine tries to use as many cores as possible,
        /// which should to an almost linear speed-up.
        /// </remarks>
        /// <param name="recommender">item recommender</param>
        /// <param name="test">test cases</param>
        /// <param name="training">training data</param>
        /// <param name="n">length of the item list to evaluate -- if set to -1 (default), use the complete list, otherwise compute evaluation measures on the top n items</param>
        /// <returns>a dictionary containing the evaluation results (default is false)</returns>
//		static public ItemRecommendationEvaluationResults Evaluate(
//			this IRecommender recommender,
//			ITimedRatings test,
//			ITimedRatings training,
//			string connection_string = "",
//			int n = -1,double alpha = 0.1)
//		{
//
//			var result = new ItemRecommendationEvaluationResults();
//			var candidates = test.AllItems.Intersect(training.AllItems).ToList();
//			int num_users = 0;
//			ThreadPool.SetMinThreads(test.AllUsers.Count, test.AllUsers.Count);
//			Dictionary<int,IList<int>> user_items = test.getItemsUserDict ();
//			ParallelOptions po = new ParallelOptions{
//				MaxDegreeOfParallelism = Environment.ProcessorCount
//			};
//
//			//foreach(int user_id in test.AllUsers){
//			Parallel.ForEach (test.AllUsers, po, user_id => {
//				try {
//					n = user_items [user_id].Count;
//					IList<Tuple<int,float>> prediction;
//					prediction = recommender.Recommend (user_id, candidate_items: candidates, n: n);
//					var prediction_list = (from t in prediction select t.Item1).ToArray ();
//					int num_candidates_for_this_user = candidates.Count ();
//					int num_dropped_items = num_candidates_for_this_user - prediction.Count;
//					var correct_items = user_items [user_id].Intersect (candidates).ToList ();
//					if (correct_items.Count () == 0)
//						return;
//
//					double auc = AUC.Compute (prediction_list, correct_items, num_dropped_items);
//					double map = PrecisionAndRecall.AP (prediction_list, correct_items);
//					double ndcg = NDCG.Compute (prediction_list, correct_items);
//					double rr = ReciprocalRank.Compute (prediction_list, correct_items);
//					var positions = new int[] { 5, 10 };
//					var prec = PrecisionAndRecall.PrecisionAt (prediction_list, correct_items, positions);
//					var recall = PrecisionAndRecall.RecallAt (prediction_list, correct_items, positions);
//
//					// thread-safe incrementing
//					lock (result) {
//						num_users++;
//						result ["AUC"] += (float)auc;
//						result ["MAP"] += (float)map;
//						result ["NDCG"] += (float)ndcg;
//						result ["MRR"] += (float)rr;
//						result ["prec@5"] += (float)prec [5];
//						result ["prec@10"] += (float)prec [10];
//						result ["recall@5"] += (float)recall [5];
//						result ["recall@10"] += (float)recall [10];
//					}
//
//					if (num_users % 1000 == 0)
//						Console.Error.Write (".");
//					if (num_users % 60000 == 0)
//						Console.Error.WriteLine ();
//				} catch (Exception e) {
//					Console.Error.WriteLine ("===> ERROR: " + e.Message + e.StackTrace);
//					throw;
//				}
//			});
//
//			foreach (string measure in Measures)
//				result[measure] /= num_users;
//			result["num_users"] = num_users;
//			result["num_lists"] = num_users;
//			result["num_items"] = candidates.Count();
//
//			return result;
//		}


        static public double EvaluateTime(
            this IRecommender recommender,
            ITimedRatings test,
            ITimedRatings training,
            string dataset,
            bool time_aware,
            int n = -1, double alpha = 0.1)
        {
            Dictionary <int, ItemRecommendationEvaluationResults> userRecommendationResults = new Dictionary <int, ItemRecommendationEvaluationResults> ();

            foreach (int user in test.AllUsers)
            {
                userRecommendationResults.Add(user, new ItemRecommendationEvaluationResults());
            }

            var             candidates = test.AllItems.Intersect(training.AllItems).ToList();
            ParallelOptions po         = new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            };
            bool init = true;
            Dictionary <int, IList <int> > trainingUserItems = training.getItemsUserDict();

            Parallel.For(0, test.Users.Count - 1, po, index => {
                try{
                    DateTime time = test.Times[index];

                    int user = test.Users[index];
                    int item = test.Items[index];
                    if (trainingUserItems[user].Contains(item))
                    {
                        return;
                    }
                    IList <int> correct_items = new List <int>();
                    correct_items.Add(item);
                    correct_items = correct_items.Intersect(candidates).ToList();
                    if (correct_items.Count() == 0)
                    {
                        return;
                    }
                    IList <Tuple <int, float> > prediction;
                    if (time_aware)
                    {
                        prediction = ((ITimeAwareRatingPredictor)recommender).RecommendTime(user, time, candidate_items: candidates, n: 20);
                    }
                    else
                    {
                        prediction = recommender.Recommend(user, candidate_items: candidates, n: 20);
                    }
                    var prediction_list = (from t in prediction select t.Item1).ToArray();

                    double auc    = AUC.Compute(prediction_list, correct_items, 0);
                    double map    = PrecisionAndRecall.AP(prediction_list, correct_items);
                    double ndcg   = NDCG.Compute(prediction_list, correct_items);
                    double rr     = ReciprocalRank.Compute(prediction_list, correct_items);
                    var positions = new int[] { 5, 10 };
                    var prec      = PrecisionAndRecall.PrecisionAt(prediction_list, correct_items, positions);
                    var recall    = PrecisionAndRecall.RecallAt(prediction_list, correct_items, positions);

                    lock (userRecommendationResults){
                        ItemRecommendationEvaluationResults res = userRecommendationResults[user];
                        res["AUC"]       += (float)auc;
                        res["MAP"]       += (float)map;
                        res["NDCG"]      += (float)ndcg;
                        res["MRR"]       += (float)rr;
                        res["prec@5"]    += (float)prec [5];
                        res["prec@10"]   += (float)prec [10];
                        res["recall@5"]  += (float)recall [5];
                        res["recall@10"] += (float)recall [10];
                        if (!init)
                        {
                            res["AUC"]       /= 2;
                            res["MAP"]       /= 2;
                            res["NDCG"]      /= 2;
                            res["MRR"]       /= 2;
                            res["prec@5"]    /= 2;
                            res["prec@10"]   /= 2;
                            res["recall@5"]  /= 2;
                            res["recall@10"] /= 2;
                        }
                        init = false;
                        userRecommendationResults[user] = res;
                    }
                } catch (Exception e) {
                    Console.Error.WriteLine("===> ERROR: " + e.Message + e.StackTrace);
                    throw;
                }
            });
            ItemRecommendationEvaluationResults avg_res = new ItemRecommendationEvaluationResults();
            int num_users = 0;

            Console.WriteLine("Detailed user results:");
            foreach (int user in userRecommendationResults.Keys)
            {
                Console.Write("User: "******"{0}={1}", key, userRecommendationResults [user] [key]);
                }
                num_users++;
            }
            foreach (string measure in Measures)
            {
                avg_res[measure] /= num_users;
            }
            Console.WriteLine(dataset + " Avg results:");
            foreach (var key in avg_res.Keys)
            {
                Console.WriteLine("{0}={1}", key, avg_res[key]);
            }
            return(avg_res["prec@5"]);
        }
Пример #14
0
//	public static double startMuTuning(ITimedRatings all_data){
//
//		ITimedRatings validation_data = new TimedRatings (); // 10%
//		ITimedRatings test_data = new TimedRatings (); // 20%
//		ITimedRatings training_data = new TimedRatings (); // 70%
//		ITimedRatings sub_all_data = new TimedRatings();
//
//		getSubset (all_data, ref sub_all_data);
//		removeUserThreshold (ref sub_all_data);
//		readAndSplitData (sub_all_data, ref validation_data, ref test_data, ref training_data);
//		Dictionary<int,int> venueCategoryMapper = getCategories(sub_all_data);
//
//		int K = 100;
//		int user_count = sub_all_data.AllUsers.Count;
//		int location_count = sub_all_data.AllItems.Count ();
//		Console.WriteLine (location_count);
//		int category_count = venueCategoryMapper.Values.ToList ().Distinct ().Count();
//
//		double[,] U1 = new double[user_count,K];
//		double[,] U2 = new double[user_count,K];
//		double[,] U3 = new double[user_count,K];
//		double[,] U4 = new double[user_count,K];
//		double[,] L1 = new double[location_count,K];
//		double[,] CA1 = new double[category_count,K];
//		Dictionary<int,int> idMapperCategories = new Dictionary<int,int>();
//		Dictionary<int,int> idMapperLocations =new Dictionary<int,int>();
//		Dictionary<int,int> idMapperUser = new Dictionary<int,int>();
//
//
//		initMatrixNormal (sub_all_data.AllUsers, ref U3, ref idMapperUser, K);
//		initMatrixNormal (sub_all_data.AllUsers, ref U4, ref idMapperUser, K);
//		initMatrixNormal (sub_all_data.AllUsers, ref U1, ref idMapperUser, K);
//		initMatrixNormal (sub_all_data.AllUsers, ref U2, ref idMapperUser, K);
//		initMatrixNormal(sub_all_data.AllItems, ref L1,ref idMapperLocations, K);
//		initMatrixNormal(venueCategoryMapper.Values.ToList ().Distinct ().ToList(), ref CA1,ref idMapperCategories ,K);
//		Console.WriteLine (idMapperCategories.Keys.ToList ().Count);
//		for (int i = 0; i < idMapperCategories.Keys.ToList ().Count; i++) {
//			Console.Write (idMapperCategories.Keys.ToList () [i]);
//			Console.Write (", ");
//			Console.Write (idMapperCategories.Values.ToList () [i]);
//		}
//		List<MyMediaLite.Eval.ItemRecommendationEvaluationResults> result_list = new List<MyMediaLite.Eval.ItemRecommendationEvaluationResults> ();
//
//		double mu = 0.1;
//		double best_mu = 0;
//		double best_mu_value = 0;
//		while (mu <= 1+0.01) { // +0.01 because of double rounding error
//			Console.WriteLine ("Start with mu = " + mu.ToString ());
//			WeatherItemRecommender recommender = new WeatherItemRecommender (U1, U2, U3, U4, CA1, L1,idMapperLocations, idMapperCategories, idMapperUser);
//			recommender.connection_string = connection;
//			recommender.Ratings = training_data;
//			recommender.Validation = validation_data;
//			recommender.weather_category_aware = true;
//			recommender.weather_aware = false;
//			recommender.max_iter = 250;
//			recommender.evaluation_at = 251;
//			recommender.beta = 0f;
//			recommender.mu = mu;
//			DateTime start_time = DateTime.Now;
//			recommender.Train ();
//			Console.Write ("Total Training time needed:");
//			Console.WriteLine (((TimeSpan)(DateTime.Now - start_time)).TotalMilliseconds);
//			Console.WriteLine ("Final results in this iteration:");
//			MyMediaLite.Eval.ItemRecommendationEvaluationResults results = MyMediaLite.Eval.ItemsWeatherItemRecommender.EvaluateTime (recommender, test_data, training_data);
//			foreach (var key in results.Keys)
//				Console.WriteLine ("{0}={1}", key, results [key]);
//			if (results ["prec@5"] > best_mu_value) {
//				best_mu = mu;
//				best_mu_value = results ["prec@5"];
//			}
//			Console.WriteLine ("Finished mu = " + mu.ToString ());
//			mu +=	0.1;
//
//		}
//		return best_mu;
//	}


    public static void totalTest(string data_file, int mode, int city, int iterations, string feature)
    {
        ITimedRatings validation_data = new TimedRatings();    // 10%
        ITimedRatings test_data       = new TimedRatings();    // 20%
        ITimedRatings training_data   = new TimedRatings();    // 70%
        ITimedRatings all_data        = readData(data_file);


        if (mode == 1)
        {
//			Console.WriteLine ("Start beta tuning");
//			double beta = 0.1;
//			for (int i = 0; i < 5; i++) {
//				Console.WriteLine ("Start beta total iteration " + i.ToString ());
//				beta = startBetaTuning (all_data);
//				Console.WriteLine ("Finished beta total iteration " + i.ToString ());
//				Console.WriteLine ("Best beta = " + beta.ToString ());
//			}
//			Console.WriteLine ("End beta tuning");
        }
        if (mode == 2)
        {
            double beta = 0.2;
            Console.WriteLine("Start geo base-line algo");
            startIterationTestNew(all_data, false, beta, 0f, city, iterations);
            Console.WriteLine("End geo base-line algo");
        }
        if (mode == 3)
        {
            double beta = 0.2;
            Console.WriteLine("Start geo weather aware algo");
            startIterationTest(all_data, true, beta, 0f, city, iterations);
            Console.WriteLine("End geo base-line algo");
        }
        if (mode == 4)
        {
            double beta = 0.2;
            Console.WriteLine("Start weather context aware algo");
            startIterationTestNew(all_data, true, beta, 0f, city, iterations, feature);
            Console.WriteLine("End weather context aware algo");
        }
        if (mode == 5)
        {
            Console.WriteLine("Start most popular algo");
            startMostPopular(all_data);
            Console.WriteLine("End most popular algo");
        }
        if (mode == 6)
        {
            Console.WriteLine("Start ItemKNN algo");
            startItemKNN(data_file);
            Console.WriteLine("End ItemKNN algo");
        }
        if (mode == 7)
        {
            Console.WriteLine("Start UserKNN algo");
            startUserKNN(data_file);
            Console.WriteLine("End UserKNN algo");
        }
        if (mode == 8)
        {
            Console.WriteLine("Start WRMF algo");
            startWRMF(all_data);
            Console.WriteLine("End WRMF algo");
        }
        if (mode == 9)
        {
            Console.WriteLine("Start BPRMF algo");
            startBPRMF(all_data);
            Console.WriteLine("End BPRMF algo");
        }
    }
Пример #15
0
 private static void create_data(List <int> user_list, List <int> item_list, List <DateTime> time_list, ref ITimedRatings data)
 {
     for (int i = 0; i < item_list.Count; i++)
     {
         data.Add(user_list[i], item_list [i], 0, time_list [i]);
     }
 }