This article introduces the example code for Implementing the Collaborative Filtering Algorithm in C #. For more information, seeCopy codeThe Code is as follows:
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 );
}
}
}
}