public static void CreateModel() { var context = new SiteKickContext(); var totalUniqueTechnology = (ulong)context.Technology.Select(x => x.Id).Count(); Console.WriteLine("Creating model..."); // load data from database Console.WriteLine("Loading data..."); IDataView trainingDataView = mlContext.Data.LoadFromTextFile( GetFullPath(CommonConstant.TRAINING_FILE_NAME), columns: new[] { new TextLoader.Column("Label", DataKind.Single, 0), new TextLoader.Column(nameof(TechEntry.TechnologyId), DataKind.UInt32, keyCount: new KeyCount(CommonConstant.KEYCOUNT), source: new [] { new TextLoader.Range(0) }), new TextLoader.Column(nameof(TechEntry.CombinedTechnologyId), DataKind.UInt32, keyCount: new KeyCount(CommonConstant.KEYCOUNT), source: new [] { new TextLoader.Range(1) }), }, separatorChar: ',', hasHeader: true); var partitions = SplitData(trainingDataView, 0.2); var pipeline = BuildTrainingPipeline(); var model = TrainModel(pipeline, partitions); SaveModel(model, trainingDataView.Schema, GetFullPath(CommonConstant.MODEL_FILE_NAME)); EvaluateModel(model, partitions); Console.WriteLine("Model created!"); }
public static void CreateTrainingData() { var entryList = new List <TechEntry>(); var context = new SiteKickContext(); var site = context.Site.Include(s => s.SiteTech).ThenInclude(st => st.Technology).ToList(); site.ForEach(s => { var techList = s.SiteTech.Select(st => st.Technology).OrderBy(x => x.TechnologyGroupId).ToList(); var groupList = techList.Select(x => x.TechnologyGroupId).Distinct().ToList(); groupList.ForEach(g => { var allTechInGroup = techList.Where(x => x.TechnologyGroupId == g).Select(x => x.Id).ToList(); allTechInGroup.ForEach(t => { var techInGroupNotCurrent = allTechInGroup.Where(y => y != t).ToList(); techInGroupNotCurrent.ForEach(z => { entryList.Add(new TechEntry { TechnologyId = (uint)t, CombinedTechnologyId = (uint)z }); }); }); }); }); CreateCSVFromList(entryList); }
/// <summary> /// Find the top 5 combined technology for the technology id /// </summary> public List <TechnologySuggestion> Predict(uint techId, int maxResult = 5) { _predictionEngine = _mlContext.Model.CreatePredictionEngine <TechEntry, TechnologySuggestion>(Model); Console.WriteLine($"Calculating the top 5 technology for technology {techId} ..."); var context = new SiteKickContext(); var lastTechId = context.Technology.Select(x => x.Id).ToList().LastOrDefault(); var topResult = (from m in Enumerable.Range(1, lastTechId) let predictions = _predictionEngine.Predict(new TechEntry { TechnologyId = techId, CombinedTechnologyId = (uint)m }) orderby predictions.Score descending select(SuggestTechnologyId: m, Score: predictions.Score)).Select(x => new TechnologySuggestion() { TechnologyId = (uint)techId, TechnologyName = context.Technology.Find((int)techId).Name, SuggestTechnologyId = (uint)x.SuggestTechnologyId, SuggestTechnologyName = context.Technology.Find(x.SuggestTechnologyId).Name, Score = x.Score }).Where(t => t.SuggestTechnologyId != techId).Take(maxResult).ToList(); return(topResult); }