예제 #1
0
 private static double DoComputeSimilarity(User u1, User u2)
 {
     IEnumerable<Movie> common = u1.Ratings.Keys.Intersect(u2.Ratings.Keys);
     int count = common.Count();
     if (count < 2) return 0;
     double sum1 = 0;
     double sum2 = 0;
     double sumProd = 0;
     double sumSquare1 = 0;
     double sumSquare2 = 0;
     foreach (var m in common)
     {
         int r1 = u1.Ratings[m];
         int r2 = u2.Ratings[m];
         sum1 += r1;
         sum2 += r2;
         sumProd += r1 * r2;
         sumSquare1 += (r1 * r1);
         sumSquare2 += (r2 * r2);
     }
     double numerator = sumProd - (sum1 * sum2) / count;
     double denominator = Math.Sqrt((sumSquare1 - (sum1 * sum1) / count) * (sumSquare2 - (sum2 * sum2) / count));
     if (denominator < Double.Epsilon) return 0;
     double s = numerator / denominator;
     if (s < -1) return -1;
     if (s > 1) return 1;
     return s;
 }
예제 #2
0
 public double SimilarityPearson(User u1, User u2)
 {
     if (u1 == u2) return 1.0;
     double result;
     if (_similarities.TryGetValue(new KeyPair(u1, u2), out result))
     {
         return result;
     }
     result = DoComputeSimilarity(u1, u2);
     _similarities.Add(new KeyPair(u1, u2), result);
     return result;
 }
예제 #3
0
파일: User.cs 프로젝트: jmathon/Algo-2014-1
 public static void ReadRatings( User[] users, Movie[] movies, string path )
 {
     using( TextReader r = File.OpenText( path ) )
     {
         string line;
         while( (line = r.ReadLine()) != null )
         {
             string[] cells = line.Split( CellSeparator, StringSplitOptions.None );
             int idUser = int.Parse( cells[0] );
             int idMovie = int.Parse( cells[1] );
             if( idMovie >= 0 && idMovie < movies.Length
                 && idUser >= 0 && idUser < users.Length )
             {
                 users[idUser].Ratings.Add( movies[idMovie], int.Parse( cells[2] ) );
             }
         }
     }
 }
예제 #4
0
 public static void ReadUsers( string path, out Dictionary<int,User> first, out Dictionary<int,List<User>> duplicates )
 {
     first = new Dictionary<int, User>();
     duplicates = new Dictionary<int, List<User>>();
     using( TextReader r = File.OpenText( path ) )
     {
         string line;
         while( (line = r.ReadLine()) != null )
         {
             User exists, u = new User( line );
             if( first.TryGetValue( u.UserID, out exists ) )
             {
                 List<User> list;
                 if( !duplicates.TryGetValue( u.UserID, out list ) )
                 {
                     list = new List<User>();
                     duplicates.Add( u.UserID, list );
                 }
                 list.Add( u );
             }
             else first.Add( u.UserID, u );
         }
     }
 }
예제 #5
0
 public KeyPair(User u1, User u2)
 {
     U1 = u1;
     U2 = u2;
 }
예제 #6
0
 public BestFUser( User u, double s )
 {
     FUser = u;
     Similarity = s;
 }