//*********** GlobalAverage Recommender
        public List <int> getBestItemsGlobalAverage(string dataset, int userid, int recs)
        {
            var mydata = RatingData.Read(dataset);
            //Create the recommender
            var recommenderGlobalAverage = new MyMediaLite.RatingPrediction.GlobalAverage();

            //Give it the dataset
            recommenderGlobalAverage.Ratings = mydata;
            //Train it
            recommenderGlobalAverage.Train();
            /////////////

            // Make the predictions
            var user_recs = recommenderGlobalAverage.Recommend(userid, recs);

            // get the recommendations
            foreach (var i in user_recs)
            {
                mylist.Add(i.Item1);
            }

            return(mylist);
        }
예제 #2
0
    protected override void LoadData()
    {
        bool static_data = !online_eval;

        TimeSpan loading_time = Wrap.MeasureTime(delegate() {
            base.LoadData();

            // read training data
            if ((recommender is TimeAwareRatingPredictor || chronological_split != null) && file_format != RatingFileFormat.MOVIELENS_1M)
            {
                training_data = TimedRatingData.Read(training_file, user_mapping, item_mapping);
            }
            else
            {
                if (file_format == RatingFileFormat.DEFAULT)
                {
                    training_data = static_data
                                                ? StaticRatingData.Read(training_file, user_mapping, item_mapping, rating_type)
                                                : RatingData.Read(training_file, user_mapping, item_mapping);
                }
                else if (file_format == RatingFileFormat.IGNORE_FIRST_LINE)
                {
                    training_data = static_data
                                                ? StaticRatingData.Read(training_file, user_mapping, item_mapping, rating_type, TestRatingFileFormat.WITH_RATINGS, true)
                                                : RatingData.Read(training_file, user_mapping, item_mapping, true);
                }
                else if (file_format == RatingFileFormat.MOVIELENS_1M)
                {
                    training_data = MovieLensRatingData.Read(training_file, user_mapping, item_mapping);
                }
                else if (file_format == RatingFileFormat.KDDCUP_2011)
                {
                    training_data = MyMediaLite.IO.KDDCup2011.Ratings.Read(training_file);
                }
            }
            recommender.Ratings = training_data;

            // read test data
            if (test_file != null)
            {
                TestRatingFileFormat test_format = test_no_ratings ? TestRatingFileFormat.WITHOUT_RATINGS : TestRatingFileFormat.WITH_RATINGS;
                if (recommender is TimeAwareRatingPredictor && file_format != RatingFileFormat.MOVIELENS_1M)
                {
                    test_data = TimedRatingData.Read(test_file, user_mapping, item_mapping, test_format);
                }
                else if (file_format == RatingFileFormat.MOVIELENS_1M)
                {
                    test_data = MovieLensRatingData.Read(test_file, user_mapping, item_mapping, test_format);
                }
                else if (file_format == RatingFileFormat.KDDCUP_2011)
                {
                    test_data = MyMediaLite.IO.KDDCup2011.Ratings.Read(test_file);
                }
                else
                {
                    test_data = StaticRatingData.Read(test_file, user_mapping, item_mapping, rating_type, test_format, file_format == RatingFileFormat.IGNORE_FIRST_LINE);
                }

                if (recommender is ITransductiveRatingPredictor)
                {
                    ((ITransductiveRatingPredictor)recommender).AdditionalFeedback = test_data;
                }
            }
        });

        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "loading_time {0:0.##}", loading_time.TotalSeconds));
        Console.Error.WriteLine("memory {0}", Memory.Usage);
    }
예제 #3
0
 protected BaseRatingAdjustment(IParsedReview review)
 {
     Review = review ?? throw new System.ArgumentNullException(nameof(review));
     calculatedSentiments = new Dictionary <IWordItem, SentimentValue>(SimpleWordItemEquality.Instance);
     Rating = new RatingData();
 }
예제 #4
0
 // default constructor
 public EvaluationModels(string dataset)
 {
     mydata = RatingData.Read(dataset);
 }
예제 #5
0
        static void Main(string[] args)
        {
            String path_training_data = "user_ratedmovies.dat";
            String path_user_data     = "users.dat";
            String path_atr_data      = "movie_genres.dat";
            String path_pred_data     = "predict.dat";

            var user_mapping = new Mapping();
            var item_mapping = new Mapping();

            //var user_mapping = new EntityMapping();
            //var item_mapping = new EntityMapping();
            //String path_training_data = "1.rec.train";
            //String path_test_data = "1.rec.test";
            //String path_training_data = "u1.base";
            //String path_test_data = "u1.test";
            //var training_data = ItemData.Read(path_training_data);
            //var test_data = ItemData.Read(path_test_data);

            var training_data  = RatingData.Read(path_training_data, user_mapping, item_mapping);
            var relevant_items = item_mapping.InternalIDs;
            var atr_data       = AttributeData.Read(path_atr_data, item_mapping);
            var pred_data      = RatingData.Read(path_pred_data, user_mapping, item_mapping);

            //var user_data = ItemData.Read(path_user_data, user_mapping);
            Console.WriteLine("I'm here");
            //Console.WriteLine(String.Join(";", user_data.AllUsers));
            //var test_data = RatingData.Read(path_test_data);

            //var recommender = new ItemKNN();
            //recommender.Feedback = training_data;
            //recommender.Train();

            //var recommender = new GSVDPlusPlus();
            //var recommender = new MatrixFactorization();
            //recommender.Ratings = training_data;
            // recommender.MaxRating = 5;
            // recommender.MinRating = 1;
            //recommender.ItemAttributes = atr_data;
            //recommender.NumFactors = 5;
            //recommender.NumIter = 10;

            //recommender.Train();


            var recommender = new SVDPlusPlus();

            recommender.Ratings = training_data;
            //recommender.AdditionalFeedback = user_data;
            Console.WriteLine("I'm here2");
            Console.WriteLine(recommender.ToString());
            //recommender.ItemAttributes = atr_data;
            recommender.NumFactors = 10;

            Console.WriteLine("I'm here3");
            Console.WriteLine(recommender.ToString());
            recommender.Train();
            Console.WriteLine("I'm here4");


            //var results = recommender.DoIterativeCrossValidation(recommdender, 10, 1, 1, true);
            var results = recommender.DoCrossValidation(10, false, true);

            ////var results = recommender.Evaluate(test_data, training_data);
            foreach (var key in results.Keys)
            {
                Console.WriteLine("{0}={1}", key, results[key]);
            }
            Console.WriteLine(results);
            //recommender.Predict();
            recommender.WritePredictions(pred_data, "test45.dat", user_mapping, item_mapping, "{0}|{1}|{2}");

            Console.WriteLine(recommender.Predict(1, 1));
            Console.ReadLine();
        }
예제 #6
0
        private string Processing(NameValueCollection par)
        {
            string ret = string.Empty;

            string[] allKeys = par.AllKeys;

            if (allKeys.Contains("addr") &&
                allKeys.Contains("userid") &&
                allKeys.Contains("callback") &&
                allKeys.Contains("cssreg"))
            {
                ret = Global.widgetNotFoundData;
                string addr   = HttpUtility.UrlDecode(par["addr"]).Trim();
                string cssreg = HttpUtility.UrlDecode(par["cssreg"]).Trim();

                if (!string.IsNullOrWhiteSpace(addr) && (1 < addr.Split(' ').Count()))
                {
                    GeoMatching gm = new GeoMatching();

                    if (!addr.StartsWith("омск", StringComparison.InvariantCultureIgnoreCase))
                    {
                        addr = addr.Insert(0, "Омск ");
                    }

                    Coordinates cc = gm.GetCoordinates(addr);

                    if (null != cc)
                    {
                        NumberFormatInfo provider = new NumberFormatInfo();
                        provider.NumberDecimalSeparator = ".";

                        GeoCoordinate source = new GeoCoordinate(Convert.ToDouble(cc.Lng, provider), Convert.ToDouble(cc.Lat, provider));

                        Dictionary <int, Tuple <double, Guid> > schoolDistance = new Dictionary <int, Tuple <double, Guid> >();
                        foreach (KeyValuePair <Guid, SchoolData> item in Global.schoolDict)
                        {
                            double dist = item.Value.pointOnMap.GetDistanceTo(source);

                            Tuple <double, Guid> dataExist;
                            if (schoolDistance.TryGetValue(item.Value.school, out dataExist))
                            {
                                if (dist < dataExist.Item1)
                                {
                                    schoolDistance[item.Value.school] = Tuple.Create <double, Guid>(dist, item.Key);
                                }
                            }
                            else
                            {
                                schoolDistance.Add(item.Value.school, Tuple.Create <double, Guid>(dist, item.Key));
                            }
                        }

                        schoolDistance = schoolDistance.OrderBy(x => x.Value.Item1).Take(5).ToDictionary(x => x.Key, x => x.Value);


                        string tableBody = string.Empty;

                        foreach (KeyValuePair <int, Tuple <double, Guid> > item in schoolDistance)
                        {
                            SchoolData sd = Global.schoolDict[item.Value.Item2];
                            RatingData rd = null;
                            Global.ratingDict.TryGetValue(item.Key, out rd);
                            GenerateWidgetRow(ref tableBody, Global.patternWidjetRow, sd, item.Value.Item1, rd);
                        }

                        if ("0" == cssreg)
                        {
                            ret = string.Empty;
                        }
                        else
                        {
                            ret = Global.patternWidjetStyle;
                        }

                        ret += Global.patternWidjetBody.Replace(Global.rowStart, string.Empty)
                               .Replace(Global.rowEnd, string.Empty)
                               .Replace(Global.patternWidjetRow, tableBody);
                    }
                }

                ret = ret.Replace(Environment.NewLine, string.Empty);

                string callBackName = par["callback"];
                ret = string.Concat(callBackName, ".Callback('", ret, "');");
            }

            return(ret);
        }
예제 #7
0
        private void GenerateWidgetRow(ref string ret, string pattern, SchoolData sd, double?distance, RatingData rd)
        {
            string row = pattern;

            row = row.Replace("{school_num}", sd.school.ToString());

            string adr = string.Concat("ул. ", sd.street, " д.", sd.buildNum);

            row = row.Replace("{address}", adr);

            string distStr = string.Empty;

            if ((distance.HasValue) && (40 < distance.Value))
            {
                distStr = string.Concat("Расстояние:<br/>", DistanceFormat.InKiloMeters(distance.Value));
            }
            row = row.Replace("{distance}", distStr);


            if (null != rd)
            {
                row = row.Replace("{common_raiting}", rd.egCommonRating);
                row = row.Replace("{common_level}", rd.egCommomLevel);
                row = row.Replace("{common_raiting_level}", rd.egCommonState);

                string ratingStyle = string.Empty;
                switch (rd.egCommonState)
                {
                case "Средний": { ratingStyle = "schoollist_item_data_middle_level"; break; }

                case "Ниже среднего": { ratingStyle = "schoollist_item_data_low_middle_level"; break; }

                case "Выше среднего": { ratingStyle = "schoollist_item_data_high_middle_level"; break; }

                case "Самый высокий": { ratingStyle = "schoollist_item_data_high_level"; break; }

                case "Самый низкий": { ratingStyle = "schoollist_item_data_low_level"; break; }
                }
                row = row.Replace("{common_raiting_level_style}", ratingStyle);

                Global.EnableBlock(ref row, "RAITING DATA");
                Global.DisableBlock(ref row, "NORAITING DATA");
            }
            else
            {
                Global.EnableBlock(ref row, "NORAITING DATA");
                Global.DisableBlock(ref row, "RAITING DATA");
            }

            ret = ret + row;
        }
예제 #8
0
        private void GenerateWidgetData(ref string wigetPattern, int schoolNum, IEnumerable <SchoolData> schoolList, RatingData rd)
        {
            wigetPattern = wigetPattern.Replace("{school_num}", schoolNum.ToString());

            string addrList = string.Empty;

            foreach (SchoolData sd in schoolList)
            {
                addrList += string.Concat("ул. ", sd.street, " д.", sd.buildNum, "<br/>");
            }
            if (string.IsNullOrWhiteSpace(addrList))
            {
                addrList = "Нет данных";
            }
            wigetPattern = wigetPattern.Replace("{address}", addrList);


            if (null != rd)
            {
                wigetPattern = wigetPattern.Replace("{common_raiting}", rd.egCommonRating);
                wigetPattern = wigetPattern.Replace("{common_level}", rd.egCommomLevel);
                wigetPattern = wigetPattern.Replace("{common_raiting_level}", rd.egCommonState);

                string ratingStyle = string.Empty;
                switch (rd.egCommonState)
                {
                case "Средний": { ratingStyle = "schooldata_item_data_middle_level"; break; }

                case "Ниже среднего": { ratingStyle = "schooldata_item_data_low_middle_level"; break; }

                case "Выше среднего": { ratingStyle = "schooldata_item_data_high_middle_level"; break; }

                case "Самый высокий": { ratingStyle = "schooldata_item_data_high_level"; break; }

                case "Самый низкий": { ratingStyle = "schooldata_item_data_low_level"; break; }
                }
                wigetPattern = wigetPattern.Replace("{common_raiting_level_style}", ratingStyle);

                Global.EnableBlock(ref wigetPattern, "RAITING DATA");
                Global.DisableBlock(ref wigetPattern, "NORAITING DATA");
            }
            else
            {
                Global.EnableBlock(ref wigetPattern, "NORAITING DATA");
                Global.DisableBlock(ref wigetPattern, "RAITING DATA");
            }
        }
        public ProductTypes.Rating Convert(RatingData r)
        {
            if (r == null) return new ProductTypes.Rating
            (
                0,
                0
            );

            return new ProductTypes.Rating
            (
                r.score,
                (int)r.count
            );
        }
    public static void Main(string[] args)
    {
        // TODO add random seed
        // TODO report per-user times

        string data_file      = args[0];
        string method         = args[1];
        string options        = args[2];
        int    num_test_users = int.Parse(args[3]);

        // load the data
        var all_data = RatingData.Read(data_file);

        // TODO randomize
        var test_users = new HashSet <int>(Enumerable.Range(0, num_test_users));

        var update_indices = new List <int>();
        var eval_indices   = new List <int>();

        foreach (int user_id in test_users)
        {
            if (all_data.ByUser[user_id].Count > 1)
            {
                var user_indices = all_data.ByUser[user_id];
                for (int i = 0; i < user_indices.Count - 1; i++)
                {
                    update_indices.Add(user_indices[i]);
                }
                for (int i = user_indices.Count - 1; i < user_indices.Count; i++)
                {
                    eval_indices.Add(user_indices[i]);
                }
            }
        }

        var training_indices = new List <int>();

        for (int i = 0; i < all_data.Count; i++)
        {
            if (!test_users.Contains(all_data.Users[i]))
            {
                training_indices.Add(i);
            }
        }
        var training_data = new MyMediaLite.Data.Ratings();

        foreach (int i in training_indices)
        {
            training_data.Add(all_data.Users[i], all_data.Items[i], all_data[i]);
        }

        var update_data = new RatingsProxy(all_data, update_indices);
        var eval_data   = new RatingsProxy(all_data, eval_indices);

        Console.Write(training_data.Statistics());
        Console.Write(update_data.Statistics());
        Console.Write(eval_data.Statistics());

        // prepare recommender
        RatingPredictor recommender = method.CreateRatingPredictor();

        recommender.Configure(options);
        recommender.Ratings = training_data;
        Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "ratings range: [{0}, {1}]", recommender.MinRating, recommender.MaxRating));
        Console.WriteLine("recommender: {0}", recommender);
        recommender.Train();

        // I. complete retraining
        Console.WriteLine(
            "complete training: {0}",
            recommender.EvaluateFoldInCompleteRetraining(update_data, eval_data));

        // II. online updates
        Console.WriteLine(
            "incremental training: {0}",
            ((IncrementalRatingPredictor)recommender).EvaluateFoldInIncrementalTraining(update_data, eval_data));

        // III. fold-in
        Console.WriteLine(
            "fold-in: {0}",
            ((IFoldInRatingPredictor)recommender).EvaluateFoldIn(update_data, eval_data));
    }
예제 #11
0
 ///
 public override void Train()
 {
     external_scores = RatingData.Read(PredictionFile, UserMapping, ItemMapping);
 }
예제 #12
0
        protected void Application_Start(object sender, EventArgs e)
        {
            // Не стартуем пока ПОЛНОСТЬЮ не подготовим все данные !!!

            // wfFacade = new Facade();

            string line;
            JavaScriptSerializer js = new JavaScriptSerializer();

            StreamReader file = new StreamReader(Server.MapPath("~\\data\\SchoolData.txt"));

            using (file)
            {
                while ((line = file.ReadLine()) != null)
                {
                    SchoolData sc = js.Deserialize <SchoolData>(line);
                    sc.pointOnMap = new GeoCoordinate(sc.lattitude, sc.longtitude);
                    schoolDict.Add(Guid.NewGuid(), sc);
                }
            }



            file = new StreamReader(Server.MapPath("~\\data\\RatingData.txt"));
            using (file)
            {
                while ((line = file.ReadLine()) != null)
                {
                    RatingData rd = js.Deserialize <RatingData>(line);

                    ratingDict.Add(rd.school, rd);
                }
            }


            patternWidjetSchoolList = File.ReadAllText(Server.MapPath("~\\widjets\\schoollistrow.html"));
            patternWidjetStyle      = getWidgetStyle(patternWidjetSchoolList);
            patternWidjetRow        = getWidgetRow(patternWidjetSchoolList);
            patternWidjetBody       = getWidgetBody(patternWidjetSchoolList);

            widgetNotFoundData         = File.ReadAllText(Server.MapPath("~\\widjets\\NoFoundData.html"));
            widgetNotFoundAddressStyle = getWidgetStyle(widgetNotFoundData);
            widgetNotFoundAddressBody  = getWidgetBody(widgetNotFoundData);

            patternWidjetSchoolData      = File.ReadAllText(Server.MapPath("~\\widjets\\schooldata.html"));
            patternWidjetSchoolDataStyle = getWidgetStyle(patternWidjetSchoolData);
            patternWidjetSchoolDataBody  = getWidgetBody(patternWidjetSchoolData);


            patternWidjetSchoolMap = File.ReadAllText(Server.MapPath("~\\widjets\\schoolmap.html"));


            patternOlympicWidjet      = File.ReadAllText(Server.MapPath("~\\widjets\\olympic.html"));
            patternOlympicWidjetStyle = getWidgetStyle(patternOlympicWidjet);
            patternOlympicWidjetBody  = getWidgetBody(patternOlympicWidjet);

            patternOlympicWidjetFarmes = File.ReadAllText(Server.MapPath("~\\widjets\\olympicFrames.html"));


            patternWFWidjet      = File.ReadAllText(Server.MapPath("~\\widjets\\WF.html"));
            patternWFWidjetStyle = getWidgetStyle(patternWFWidjet);
            patternWFWidjetBody  = getWidgetBody(patternWFWidjet);
        }
예제 #13
0
        static void Main(string[] args)
        {
            string folder = args.Length != 1 ? @"..\..\Resources\" : args[0];

            Console.WriteLine($"{GetTimeStamp()} Files must be in folder {folder}. Use the first argument to specify another folder.");
            Console.Write($"{GetTimeStamp()} Enter # of neighbors [50-10000]: ");
            int neighbors;

            if (!int.TryParse(Console.ReadLine(), out neighbors))
            {
                Console.Write("Unable to parse input.");
            }
            else
            {
                neighbors = neighbors < 50 ? 50 : (neighbors > 10000 ? 10000 : neighbors);
            }
            Console.WriteLine($"{GetTimeStamp()} neighbors set to: {neighbors}");

            Console.WriteLine($"{GetTimeStamp()} Loading data...");
            const string moviesFile          = "movie_titles.txt";
            const string trainingRatingsFile = "TrainingRatings.txt";
            const string testRatingsFile     = "TestingRatings.txt";

            // ReSharper disable once UnusedVariable
            MovieData movies = ParseMoviesData(File.ReadAllLines(Path.Combine(folder, moviesFile)));

            RatingData trainingRatings = ParseRatingData(File.ReadAllLines(Path.Combine(folder, trainingRatingsFile)));
            RatingData testRatings     = ParseRatingData(File.ReadAllLines(Path.Combine(folder, testRatingsFile)));

            Console.WriteLine($"{GetTimeStamp()} Done.");

            DateTime now     = DateTime.Now;
            long     index   = 0;
            var      results = new List <Tuple <double /*predicted*/, double /*actual*/> >();

            //Parallel.ForEach(testRatings.GetUsers().TakeWhile(x => Interlocked.Read(ref index) < 5000), userId =>
            Parallel.ForEach(testRatings.GetUsers(), userId =>
            {
                if (Interlocked.Add(ref index, 1) % 100 == 0)
                {
                    PrintProgress(index, now, testRatings.GetUsersCount());
                }

                var ratings = testRatings.GetUserRatings(userId);
                foreach (var movieRating in ratings)
                {
                    double actual    = movieRating.Value;
                    double predicted = trainingRatings.PredictMovieRating(userId, movieRating.Key, neighbors);
                    lock (results)
                        results.Add(new Tuple <double, double>(predicted, actual));
                }
            });

            PrintProgress(index, now, testRatings.GetUsersCount());

            double mae  = CalculateMAE(results);
            double mape = CalculateMAPE(results);
            double rmsd = CalculateRMSD(results);

            Console.WriteLine();
            Console.WriteLine($"{GetTimeStamp()} MAE={mae} (Mean Absolute Error)");
            Console.WriteLine($"{GetTimeStamp()} MAPE={mape} (Mean Absolute Percentage Error)");
            Console.WriteLine($"{GetTimeStamp()} RMSD={rmsd} (Root-mean-square deviation)");
        }
예제 #14
0
        private async Task <List <string> > GetEventsRating(int superConferenceId)
        {
            var output          = new List <RatingData>();
            var superConference = await _context.SuperConferences
                                  .Include(c => c.Conferences)
                                  .FirstOrDefaultAsync(m => m.Id == superConferenceId);

            var conferences = superConference.Conferences.ToList();
            var eEvents     = await _context.Events
                              .Where(e => e.Conference.SuperConferenceId == superConferenceId)
                              .ToListAsync();

            foreach (var eEvent in eEvents)
            {
                var tuple = new RatingData()
                {
                    Name = eEvent.Name, Data = new List <NameRating>()
                };
                var eventConferences = conferences.Where(c => c.Id == eEvent.ConferenceId);
                foreach (var conference in eventConferences)
                {
                    var userConference = from ea in _context.EventRatings.Where(eu => eu.EventId == eEvent.Id)
                                         join ce in conference.Events on ea.EventId equals ce.Id
                                         where ea.EventId == ce.Id select new
                    {
                        ea.UserId,
                        ea.Rating,
                        ce.ConferenceId
                    };

                    var conferenceRating = userConference.GroupBy(uc => uc.ConferenceId).ToList().Select(group => new IdRating()
                    {
                        Id     = @group.Key,
                        Rating = @group.Sum(i => i.Rating) / Math.Max(@group.Count(), 1)
                    });
                    var rating = conferenceRating.FirstOrDefault(ca => ca.Id == conference.Id);
                    if (output.Any(o => o.Name == eEvent.Name))
                    {
                        output.First(o => o.Name == eEvent.Name).Data.Add(rating == null
                            ? new NameRating()
                        {
                            Name = conference.Edition, Rating = 0
                        }
                            : new NameRating()
                        {
                            Name = conference.Edition, Rating = rating.Rating
                        });
                    }
                    else
                    {
                        tuple.Data.Add(rating == null
                            ? new NameRating()
                        {
                            Name = conference.Edition, Rating = 0
                        }
                            : new NameRating()
                        {
                            Name = conference.Edition, Rating = rating.Rating
                        });
                    }
                }

                output.Add(tuple);
            }

            return(output.ConvertAll(JsonConvert.SerializeObject));
        }
예제 #15
0
 public RatingData CalculateRawRating()
 {
     return(RatingData.Accumulate(allSentences.Select(item => item.CalculateRating())));
 }
 ///
 public override void Train()
 {
     external_scores = RatingData.Read(PredictionFile, UserMapping, ItemMapping);
     // we dont use it locally but it should be initialized before entering the parallel code
     IList <IList <int> > nonsense = external_scores.ByUser;
 }
 public ProductTypes.Rating Merge(ProductTypes.Rating src, RatingData update)
 {
     return update == null ? src : Convert(update);
 }