k-nearest neighbor user-based collaborative filtering
This recommender supports incremental updates for the BinaryCosine and Cooccurrence similarities.
Inheritance: KNN, IUserSimilarityProvider, IFoldInItemRecommender
Beispiel #1
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();
        }
Beispiel #2
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);
        //}
    }