Пример #1
0
        public double Calculate(SimilarityList data)
        {
            var    length = 0;
            double sumX   = 0;
            double sumY   = 0;
            double sumX2  = 0;
            double sumY2  = 0;

            // ReSharper disable once InconsistentNaming
            double sumXY = 0;

            foreach (var value in data.Data)
            {
                sumX += value.Item1;
                sumY += value.Item2;

                sumX2 += Math.Pow(value.Item1, 2);
                sumY2 += Math.Pow(value.Item2, 2);

                sumXY += value.Item1 * value.Item2;

                length++;
            }

            var numerator   = sumXY - sumX * sumY / length;
            var denominator = Math.Sqrt(sumX2 - Math.Pow(sumX, 2) / length) *
                              Math.Sqrt(sumY2 - Math.Pow(sumY, 2) / length);

            return(numerator / denominator);
        }
Пример #2
0
        public double Calculate(SimilarityList data)
        {
            double multiSum = 0;
            double sumX2    = 0;
            double sumY2    = 0;

            foreach (var value in data.ZerodData)
            {
                multiSum += value.Item1 * value.Item2;

                sumX2 += Math.Pow(value.Item1, 2);
                sumY2 += Math.Pow(value.Item2, 2);
            }

            return(multiSum / (Math.Sqrt(sumX2) * Math.Sqrt(sumY2)));
        }
Пример #3
0
        /// <summary>
        /// Calculate similarity and return a list of tuples with the user and how similar it is.
        /// </summary>
        public IEnumerable <Tuple <User, float> > ListSimilarities(Similarity similarity, float threshold = 1)
        {
            var users = new List <Tuple <User, float> >();

            foreach (var user in Users)
            {
                var data   = new SimilarityList(user.Ratings, User.Ratings, Articles);
                var result = (float)similarity.Calculate(data);

                if (result < threshold)
                {
                    continue;
                }

                users.Add(new Tuple <User, float>(user, result));
            }

            // Sort
            users.Sort((x, y) => y.Item2.CompareTo(x.Item2));

            return(users);
        }
Пример #4
0
 /// <summary>
 /// Converts two lists together based on the articles.
 /// </summary>
 public SimilarityWrapper(SimilarityList data)
 {
     Data = data;
 }
Пример #5
0
 public double Distance(SimilarityList data)
 {
     return(Math.Sqrt((from value in data.Data select Math.Pow(value.Item1 - value.Item2, 2)).Sum()));
 }
Пример #6
0
 public double Calculate(SimilarityList data)
 {
     return(1 / (1 + Distance(data)));
 }
Пример #7
0
 public double Distance(SimilarityList data)
 {
     return((from value in data.Data select Math.Abs(value.Item1 - value.Item2)).Sum());
 }