예제 #1
0
        public void ComputeDeviations8sec(Dictionary <int, UserPreferance> users)
        {
            int itemA; // x
            int itemB; // y

            // for each person in the data
            foreach (KeyValuePair <int, UserPreferance> user in users)
            {
                Dictionary <int, double> ratings = user.Value.UserRatings;
                // 2 for loop to loop through every ratings to get x and y
                foreach (KeyValuePair <int, double> rating in ratings)
                {
                    itemA = rating.Key;
                    // itemA doesnt exist yet in x
                    if (!data.ContainsKey(itemA))
                    {
                        data.Add(itemA, new Dictionary <int, DeviationData>());
                    }

                    foreach (KeyValuePair <int, double> rating2 in ratings)
                    {
                        itemB = rating2.Key;

                        if (itemA != itemB)
                        {
                            if (!data[itemA].ContainsKey(itemB))
                            {
                                data[itemA].Add(itemB, new DeviationData(0.0, 0));
                            }
                            data[itemA][itemB].nrOfPeople++;
                            data[itemA][itemB].deviationValue += rating.Value - rating2.Value;
                        }
                    }
                }
            }
            // for each row
            foreach (KeyValuePair <int, Dictionary <int, DeviationData> > pair in data)
            {
                //for each column
                foreach (KeyValuePair <int, DeviationData> cell in pair.Value)
                {
                    DeviationData data = cell.Value;
                    data.deviationValue /= data.nrOfPeople;
                }
            }
        }
예제 #2
0
        public double SlopeOneRecommendations(int target, Dictionary <int, double> ratings)
        {
            double numerator   = 0.0;
            int    denominator = 0;

            // for each item that user alrdy rated
            foreach (KeyValuePair <int, double> rating in ratings)
            {
                // extract info about the dev between i(item rated by user) and the j
                if (data[target].ContainsKey(rating.Key))
                {
                    DeviationData deviationCell = data[target][rating.Key];
                    numerator   += (rating.Value + deviationCell.deviationValue) * deviationCell.nrOfPeople;
                    denominator += deviationCell.nrOfPeople;
                }
            }
            return(numerator / denominator);
        }
예제 #3
0
        public void Update(Dictionary <int, double> ratings, int article, double articleRating)
        {
            // update all articles connected to the ratings of the current user
            foreach (KeyValuePair <int, double> rating in ratings)
            {
                // x
                if (data.ContainsKey(article))
                {
                    // y
                    if (data[article].ContainsKey(rating.Key))
                    {
                        DeviationData cell1 = data[article][rating.Key];
                        cell1.deviationValue = (cell1.deviationValue * cell1.nrOfPeople) + articleRating - rating.Value;
                        cell1.nrOfPeople++;
                        cell1.deviationValue /= cell1.nrOfPeople;

                        DeviationData cell2 = data[rating.Key][article];
                        cell2.deviationValue = -1 * cell1.deviationValue;
                        cell2.nrOfPeople     = cell1.nrOfPeople;
                    }
                }
            }
            ratings.Add(article, articleRating);
        }