网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文
当前位置:书香门第 > 计算机 > C语言

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

栏目: C语言 / 发布于: / 人气:1.81W

还在找C#实现协同过滤算法的代码吗?下面小编为大家整理了C#实现协同过滤算法的实例代码,希望能帮到大家!

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

复制代码 代码如下:

using System;

using ric;

using ;

using ;

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

{

private string GetKey(int Item1Id, int Item2Id)

{

return (Item1Id < Item2Id) ? Item1Id + "/" + Item2Id : Item2Id + "/" + Item1Id ;

}

public bool Contains(int Item1Id, int Item2Id)

{

return ains(GetKey(Item1Id, Item2Id));

}

public Rating this[int Item1Id, int Item2Id]

{

get {

return this[ey(Item1Id, Item2Id)];

}

set { this[ey(Item1Id, Item2Id)] = value; }

}

}

public class SlopeOne

{

public RatingDifferenceCollection _DiffMarix = new RatingDifferenceCollection(); // The dictionary to keep the diff matrix

public HashSet_Items = new HashSet(); // Tracking how many items totally

public void AddUserRatings(IDictionaryuserRatings)

{

foreach (var item1 in userRatings)

{

int item1Id = ;

float item1Rating = e;

_();

foreach (var item2 in userRatings)

{

if ( <= item1Id) continue; // Eliminate redundancy

int item2Id = ;

float item2Rating = e;

Rating ratingDiff;

if (_ains(item1Id, item2Id))

{

ratingDiff = _DiffMarix[item1Id, item2Id];

}

else

{

ratingDiff = new Rating();

_DiffMarix[item1Id, item2Id] = ratingDiff;

}

e += item1Rating - item2Rating;

+= 1;

}

}

}

// Input ratings of all users

public void AddUerRatings(IListRatings)

{

foreach(var userRatings in Ratings)

{

AddUserRatings(userRatings);

}

}

public IDictionaryPredict(IDictionaryuserRatings)

{

DictionaryPredictions = new Dictionary();

foreach (var itemId in this._Items)

{

if (ains(itemId)) continue; // User has rated this item, just skip it

Rating itemRating = new Rating();

foreach (var userRating in userRatings)

{

if ( == itemId) continue;

int inputItemId = ;

if (_ains(itemId, inputItemId))

{

Rating diff = _DiffMarix[itemId, inputItemId];

e += * (e + ageValue * ((itemId < inputItemId) ? 1 : -1));

+= ;

}

}

(itemId, ageValue);

}

return Predictions;

}

public static void Test()

{

SlopeOne test = new SlopeOne();

DictionaryuserRating = new Dictionary();

(1, 5);

(2, 4);

(3, 4);

serRatings(userRating);

userRating = new Dictionary();

(1, 4);

(2, 5);

(3, 3);

(4, 5);

serRatings(userRating);

userRating = new Dictionary();

(1, 4);

(2, 4);

(4, 5);

serRatings(userRating);

userRating = new Dictionary();

(1, 5);

(3, 4);

IDictionaryPredictions = ict(userRating);

foreach (var rating in Predictions)

{

eLine("Item " + + " Rating: " + e);

}

}

}

}