最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)協(xié)同過濾算法的實例代碼

 更新時間:2013年07月03日 11:24:07   作者:  
這篇文章介紹了C#實現(xiàn)協(xié)同過濾算法的實例代碼,有需要的朋友可以參考一下
復(fù)制代碼 代碼如下:

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);
            }
        }
    }
}

相關(guān)文章

  • C# 獲取漢字的拼音首字母

    C# 獲取漢字的拼音首字母

    一種是把所有中文字符集合起來組成一個對照表;另一種是依照漢字在Unicode編碼表中的排序來確定拼音的首字母。碰到多音字時就以常用的為準(第一種方法中可以自行更改,方法為手動把該漢字移動到對應(yīng)的拼音首字母隊列,我們這里介紹第二種
    2015-06-06
  • C#繪制實時曲線圖的方法詳解

    C#繪制實時曲線圖的方法詳解

    這篇文章主要為大家詳細介紹了如何利用C#繪制實時曲線圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 讀取圖片像素的具體實例

    讀取圖片像素的具體實例

    C#讀取圖片像素的具體實例,需要的朋友可以參考一下
    2013-06-06
  • C# wpf簡單顏色板的實現(xiàn)

    C# wpf簡單顏色板的實現(xiàn)

    wpf本身沒有提供顏色板之類的控件,有些業(yè)務(wù)使用場景需要使用顏色板之類的控件,本文就簡單實現(xiàn),感興趣的可以了解一下
    2021-10-10
  • C# 8.0可空引用類型的使用注意記錄

    C# 8.0可空引用類型的使用注意記錄

    這篇文章主要給大家介紹了關(guān)于C# 8.0可空引用類型使用注意的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • C# 16進制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換

    C# 16進制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換

    在串口通訊過程中,經(jīng)常要用到 16進制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換
    2009-05-05
  • 運用示例簡單講解C#取消令牌CancellationTokenSource

    運用示例簡單講解C#取消令牌CancellationTokenSource

    這篇文章運用示例簡單講解C#取消令牌CancellationTokenSource,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C#.Net ArrayList的使用方法

    C#.Net ArrayList的使用方法

    這篇文章主要介紹了C#.Net ArrayList的使用方法,使用動態(tài)數(shù)組的優(yōu)點是可以根據(jù)用戶需要,有效利用存儲空間,需要的朋友可以參考下
    2015-10-10
  • C#中神器類BlockingCollection的實現(xiàn)詳解

    C#中神器類BlockingCollection的實現(xiàn)詳解

    如果你想玩轉(zhuǎn)C#?里面多線程,工廠模式,生產(chǎn)者/消費者,隊列等高級操作,就可以和我一起探索這個強大的線程安全提供阻塞和限制功能的C#神器類BlockingCollection吧
    2023-02-02
  • c#中(&&,||)與(&,|)的區(qū)別詳解

    c#中(&&,||)與(&,|)的區(qū)別詳解

    這篇文章主要介紹了c#中(&&,||)與(&,|)的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12

最新評論

罗江县| 山东省| 新龙县| 屏东县| 南充市| 长治市| 仁寿县| 理塘县| 龙口市| 泾源县| 贡山| 东山县| 清徐县| 桃园市| 莱州市| 磐安县| 泸溪县| 惠州市| 唐山市| 余干县| 新野县| 昭觉县| 柏乡县| 新乡县| 崇仁县| 刚察县| 苍溪县| 永福县| 江西省| 平遥县| 扶风县| 宜宾市| 许昌县| 黄山市| 朝阳区| 莫力| 毕节市| 若尔盖县| 防城港市| 京山县| 芜湖县|