public simi[,] OrderedSimilarityMatrix = new simi[itemTotal, itemTotal]; //排序后的相似性矩阵 /// <summary> /// 将数据集分成训练集 和 测试集 ----------------Success /// </summary> /// <param name="i"></param> /// <param name="j"></param> public void SplitDataSet(int i, int j) { Array.Clear(TestSet, 0, 3952 * 6040); Array.Clear(TrainSet, 0, 3952 * 6040); Thread.Sleep(1000); string sqlStr = "select * from Ratings"; var dataReader = SqlSeverProvider.ExecuteReader(sqlStr); while (dataReader.Read()) { var userID = dataReader.GetInt32(0); var itemID = dataReader.GetInt32(1); var rate = dataReader.GetInt32(2); Random rand = new Random(DateTime.Now.Millisecond); try { if (rand.Next() % (i - 1) == j) { TestSet[itemID - 1, userID] = 1; } else { TrainSet[itemID - 1, userID - 1] = 1; } } catch { } } }
//public int[] GetRecomend(int userId,List<int> ?itemIds) //{ // List<int> items = new List<int>(); // items.Sort(); // return null; //} public int[] GetRecommend(int userId, params int[] itemIds) { Dictionary <int, double> recommendedList = new Dictionary <int, double>(); //根据用户id 获取最佳推荐的10个,添加到recommendedList string sqlStr = string.Format("select top(10) itemID from UserInterest where userID={0} and interest !=0 order by interest desc", userId); SqlSeverProvider.ExecuteReader(sqlStr); //根据itemid 获取相似度较高的物品,添加到recommendedList // select top(20)* from SimilarityMatrix where itemOne=176 order by similarity desc // select top(10) itemID,COUNT(itemID) as total from Ratings group by itemID order by total desc 高访问率 //根据相似度高低对 recommendedList进行排序 var result = from it in recommendedList orderby it.Value select it; foreach (var item in result.Take(10)) { Console.WriteLine(item.Key); } return(null); }