Unweighted k-nearest neighbor item-based collaborative filtering using cosine similarity
This recommender does NOT support incremental updates.
Inheritance: KNN
Ejemplo n.º 1
0
        ///
        protected override void InitModel()
        {
            base.InitModel();

            if (K > 0)
            {
                itemKNN             = new ItemKNN();
                itemKNN.K           = K;
                itemKNN.Correlation = MyMediaLite.Correlation.BinaryCorrelationType.Cosine;
                itemKNN.Feedback    = this.Feedback;
            }
        }
Ejemplo n.º 2
0
		///
		protected override void InitModel()
		{
			base.InitModel();

			if (K > 0)
			{
				itemKNN = new ItemKNN();
				itemKNN.K = K;
				itemKNN.Correlation = MyMediaLite.Correlation.BinaryCorrelationType.Cosine;
				itemKNN.Feedback = this.Feedback;
			}
		}
Ejemplo n.º 3
0
        private void btnProcess_ItemClick(object sender, ItemClickEventArgs e)
        {
            // Validating
            if (editUserID.EditValue.ToString().IsEmpty())
            {
                XtraMessageBox.Show(this, "Vui lòng nhập ID User", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var uid   = int.Parse(editUserID.EditValue.ToString());
            var count = int.Parse(editCount.EditValue.ToString());

            if (relas.All(r => r.UserID != uid))
            {
                XtraMessageBox.Show(this, "User ID không hợp lệ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ItemRecommender recommender;

            if (cbType.EditValue.ToString().Equals("User Based"))
            {
                recommender = new UserKNN();
            }
            else if (cbType.EditValue.ToString().Equals("Item Based"))
            {
                recommender = new ItemKNN();
            }
            else
            {
                XtraMessageBox.Show(this, "Invalid value " + cbType.EditValue, "Lỗi", MessageBoxButtons.OK,
                                    MessageBoxIcon.Asterisk);
                return;
            }
            var mat = new PosOnlyFeedback <SparseBooleanMatrix>();

            foreach (var rela in relas)
            {
                mat.Add(rela.UserID, rela.SongID);
            }
            recommender.Feedback = mat;
            recommender.Train();
            var result = recommender.Recommend(uid, count);

            gridResult.DataSource =
                result.Select(i => new KetQua {
                BaiHat = songs.Find(song => song.ID == i.Item1).Name, DiemSo = i.Item2
            })
                .ToList();
            gridResult.RefreshDataSource();
        }
Ejemplo n.º 4
0
        ///
        public override void LoadModel(string filename)
        {
            if (K > 0)
            {
                itemKNN = new ItemKNN();
                itemKNN.LoadModel(filename + "-knn");
            }

            using (StreamReader reader = Model.GetReader(filename, this.GetType()))
            {
                var item_weights = (Matrix <float>)reader.ReadMatrix(new Matrix <float>(0, 0));

                this.MaxItemID    = item_weights.NumberOfRows - 1;
                this.item_weights = item_weights;
            }
        }
Ejemplo n.º 5
0
    private static void startItemKNN(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 ItemKNN");

        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.ItemKNN recommender = new MyMediaLite.ItemRecommendation.ItemKNN();
        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);
        //}
    }
Ejemplo n.º 6
0
		///
		public override void LoadModel(string filename)
		{
			if (K > 0)
			{
				itemKNN = new ItemKNN();
				itemKNN.LoadModel(filename + "-knn");
			}

			using ( StreamReader reader = Model.GetReader(filename, this.GetType()) )
			{
				var item_weights = (Matrix<float>) reader.ReadMatrix(new Matrix<float>(0, 0));

				this.MaxItemID = item_weights.NumberOfRows - 1;
				this.item_weights = item_weights;
			}
		}