C#实现协同过滤算法的实例代码

 更新时间:2013年07月03日 11:24:07   作者:  
这篇文章介绍了C#实现协同过滤算法的实例代码,有需要的朋友可以参考一下
复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SlopeOne
{
    public class Rating
    {
        public float Value { get; set; }
        public int Freq { get; set; }
        public float AverageValue
        {
            get { return Value / Freq; }
        }
    }
    public class RatingDifferenceCollection : Dictionary<string, Rating>
    {
        private string GetKey(int Item1Id, int Item2Id)
        {
            return (Item1Id < Item2Id) ? Item1Id + "/" + Item2Id : Item2Id + "/" + Item1Id ;
        }
        public bool Contains(int Item1Id, int Item2Id)
        {
            return this.Keys.Contains<string>(GetKey(Item1Id, Item2Id));
        }
        public Rating this[int Item1Id, int Item2Id]
        {
            get {
                    return this[this.GetKey(Item1Id, Item2Id)];
            }
            set { this[this.GetKey(Item1Id, Item2Id)] = value; }
        }
    }
     public class SlopeOne
    {       
        public RatingDifferenceCollection _DiffMarix = new RatingDifferenceCollection();  // The dictionary to keep the diff matrix
        public HashSet<int> _Items = new HashSet<int>();  // Tracking how many items totally
        public void AddUserRatings(IDictionary<int, float> userRatings)
        {
            foreach (var item1 in userRatings)
            {
                int item1Id = item1.Key;
                float item1Rating = item1.Value;
                _Items.Add(item1.Key);
                foreach (var item2 in userRatings)
                {
                    if (item2.Key <= item1Id) continue; // Eliminate redundancy
                    int item2Id = item2.Key;
                    float item2Rating = item2.Value;
                    Rating ratingDiff;
                    if (_DiffMarix.Contains(item1Id, item2Id))
                    {
                        ratingDiff = _DiffMarix[item1Id, item2Id];
                    }
                    else
                    {
                        ratingDiff = new Rating();
                        _DiffMarix[item1Id, item2Id] = ratingDiff;
                    }
                    ratingDiff.Value += item1Rating - item2Rating;
                    ratingDiff.Freq += 1;
                }
            }
        }
        // Input ratings of all users
        public void AddUerRatings(IList<IDictionary<int, float>> Ratings)
        {
            foreach(var userRatings in Ratings)
            {
                AddUserRatings(userRatings);
            }
        }
        public IDictionary<int, float> Predict(IDictionary<int, float> userRatings)
        {
            Dictionary<int, float> Predictions = new Dictionary<int, float>();
            foreach (var itemId in this._Items)
            {
                if (userRatings.Keys.Contains(itemId))    continue; // User has rated this item, just skip it
                Rating itemRating = new Rating();
                foreach (var userRating in userRatings)
                {
                    if (userRating.Key == itemId) continue;
                    int inputItemId = userRating.Key;
                    if (_DiffMarix.Contains(itemId, inputItemId))
                    {
                        Rating diff = _DiffMarix[itemId, inputItemId];
                        itemRating.Value += diff.Freq * (userRating.Value + diff.AverageValue * ((itemId < inputItemId) ? 1 : -1));
                        itemRating.Freq += diff.Freq;
                    }
                }
                Predictions.Add(itemId, itemRating.AverageValue);               
            }
            return Predictions;
        }
        public static void Test()
        {
            SlopeOne test = new SlopeOne();
            Dictionary<int, float> userRating = new Dictionary<int, float>();
            userRating.Add(1, 5);
            userRating.Add(2, 4);
            userRating.Add(3, 4);
            test.AddUserRatings(userRating);
            userRating = new Dictionary<int, float>();
            userRating.Add(1, 4);
            userRating.Add(2, 5);
            userRating.Add(3, 3);
            userRating.Add(4, 5);
            test.AddUserRatings(userRating);
            userRating = new Dictionary<int, float>();
            userRating.Add(1, 4);
            userRating.Add(2, 4);
            userRating.Add(4, 5);
            test.AddUserRatings(userRating);
            userRating = new Dictionary<int, float>();
            userRating.Add(1, 5);
            userRating.Add(3, 4);
            IDictionary<int, float> Predictions = test.Predict(userRating);
            foreach (var rating in Predictions)
            {
                Console.WriteLine("Item " + rating.Key + " Rating: " + rating.Value);
            }
        }
    }
}

相关文章

  • C# 获取汉字的拼音首字母

    C# 获取汉字的拼音首字母

    一种是把所有中文字符集合起来组成一个对照表;另一种是依照汉字在Unicode编码表中的排序来确定拼音的首字母。碰到多音字时就以常用的为准(第一种方法中可以自行更改,方法为手动把该汉字移动到对应的拼音首字母队列,我们这里介绍第二种
    2015-06-06
  • C#绘制实时曲线图的方法详解

    C#绘制实时曲线图的方法详解

    这篇文章主要为大家详细介绍了如何利用C#绘制实时曲线图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • 读取图片像素的具体实例

    读取图片像素的具体实例

    C#读取图片像素的具体实例,需要的朋友可以参考一下
    2013-06-06
  • C# wpf简单颜色板的实现

    C# wpf简单颜色板的实现

    wpf本身没有提供颜色板之类的控件,有些业务使用场景需要使用颜色板之类的控件,本文就简单实现,感兴趣的可以了解一下
    2021-10-10
  • C# 8.0可空引用类型的使用注意记录

    C# 8.0可空引用类型的使用注意记录

    这篇文章主要给大家介绍了关于C# 8.0可空引用类型使用注意的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • C# 16进制与字符串、字节数组之间的转换

    C# 16进制与字符串、字节数组之间的转换

    在串口通讯过程中,经常要用到 16进制与字符串、字节数组之间的转换
    2009-05-05
  • 运用示例简单讲解C#取消令牌CancellationTokenSource

    运用示例简单讲解C#取消令牌CancellationTokenSource

    这篇文章运用示例简单讲解C#取消令牌CancellationTokenSource,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • C#.Net ArrayList的使用方法

    C#.Net ArrayList的使用方法

    这篇文章主要介绍了C#.Net ArrayList的使用方法,使用动态数组的优点是可以根据用户需要,有效利用存储空间,需要的朋友可以参考下
    2015-10-10
  • C#中神器类BlockingCollection的实现详解

    C#中神器类BlockingCollection的实现详解

    如果你想玩转C# 里面多线程,工厂模式,生产者/消费者,队列等高级操作,就可以和我一起探索这个强大的线程安全提供阻塞和限制功能的C#神器类BlockingCollection吧
    2023-02-02
  • c#中(&&,||)与(&,|)的区别详解

    c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12

最新评论