예제 #1
0
        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 { }
            }
        }
예제 #2
0
        /// <summary>
        /// 计算所有用户的兴趣程度矩阵
        /// </summary>
        public void GetUserInterestMatrix()
        {
            Array.Clear(UserInterestMatrix, 0, 3952 * 6040);


            for (int i = 0; i < userTotal; i++)
            {
                DataTable Tb_UserInterest = new DataTable();
                Tb_UserInterest.Columns.Add("userID", System.Type.GetType("System.Int32"));
                Tb_UserInterest.Columns.Add("itemID", System.Type.GetType("System.Int32"));
                Tb_UserInterest.Columns.Add("interest", System.Type.GetType("System.Double"));
                string tableName = "UserInterest";
                for (int j = 0; j < itemTotal; j++)
                {
                    if (TrainSet[j, i] == 0)
                    {
                        UserInterestMatrix[i, j] = CalculateUserInterest(i, j, 10);//----------K默认是10
                        DataRow row = Tb_UserInterest.NewRow();
                        row[0] = i + 1;
                        row[1] = j + 1;
                        row[2] = UserInterestMatrix[i, j];
                        Tb_UserInterest.Rows.Add(row);
                    }
                }

                SqlSeverProvider.ExecuteSqlBulkCopy(Tb_UserInterest, tableName);
            }

            Console.WriteLine("Tb_UserInterest OK ");
        }
예제 #3
0
파일: Program.cs 프로젝트: YihuiFu/ItemBase
        public static void Insert(int userId, int itemId, int rate)
        {
            string sqlStr = "insert into tb_TestRatings values (@userID,@itemID,@rating)";

            SqlParameter[] parms =
            {
                new SqlParameter("@userID", userId),
                new SqlParameter("@itemID", itemId),
                new SqlParameter("@rating", rate)
            };
            var result = SqlSeverProvider.ExecuteNonQuery(sqlStr, parms);
        }
예제 #4
0
        /// <summary>
        /// 推荐物品
        /// </summary>
        public void GetRecommend()
        {
            int mostInterestNum; //当前最感兴趣物品

            for (int i = 0; i < userTotal; i++)
            {
                int [] orderFlag = new int[itemTotal];
                for (int j = 0; j < recommendNum; j++)
                {
                    mostInterestNum = 0;
                    while (orderFlag[mostInterestNum] != 0)
                    {
                        mostInterestNum++;
                    }
                    for (int k = 0; k < itemTotal; k++)
                    {
                        if (UserInterestMatrix[i, mostInterestNum] < UserInterestMatrix[j, k] && orderFlag[k] == 0)
                        {
                            mostInterestNum = k;
                        }
                    }
                    orderFlag[mostInterestNum] = 1;
                    RecommendSet[i, j]         = mostInterestNum;
                }
            }
            Console.WriteLine("推荐完成...");

            for (int i = 0; i < 6040; i++)
            {
                DataTable Tb_Recommended = new DataTable();
                Tb_Recommended.Columns.Add("userID", System.Type.GetType("System.Int32"));
                Tb_Recommended.Columns.Add("itemID", System.Type.GetType("System.Int32"));
                string tableName = "Recommended";
                for (int j = 0; j < 10; j++)
                {
                    var item = (int)RecommendSet[i, j];
                    //string str = string.Format("insert into Recommended values({0},{1})",i+1,item);
                    //SqlSeverProvider.ExecuteNonQuery(str);
                    DataRow row = Tb_Recommended.NewRow();
                    row[0] = i + 1;
                    row[1] = item;
                    Tb_Recommended.Rows.Add(row);
                }
                SqlSeverProvider.ExecuteSqlBulkCopy(Tb_Recommended, tableName);
            }
            Console.WriteLine("推荐完成22222...");
        }
예제 #5
0
        /// <summary>
        /// 计算所有物品之间的相似性,得到相似性矩阵 --------------Success
        /// </summary>
        ///

        //DataTable Tb_SimilarityMatrix = new DataTable();

        //table.Columns.Add("userID", System.Type.GetType("System.Int32"));
        //table.Columns.Add("itemID", System.Type.GetType("System.Int32"));
        //table.Columns.Add("rating", System.Type.GetType("System.Int32"));
        //string tableName = "tb_TestRatings";

        //     //sqlbulkCopy
        //     //DataRow row = table.NewRow();
        //     //row[0] = userId;
        //     //row[1] = itemId;
        //     //row[2] = rating;
        //     //table.Rows.Add(row);

        //// SqlSeverProvider.ExecuteSqlBulkCopy(table, tableName);

        public void GetSimilarityMatrix()
        {
            Array.Clear(SimilarityMatrix, 0, 3952 * 3952);


            for (int i = 0; i < itemTotal; i++)
            {
                DataTable Tb_SimilarityMatrix = new DataTable();
                Tb_SimilarityMatrix.Columns.Add("itemOne", System.Type.GetType("System.Int32"));
                Tb_SimilarityMatrix.Columns.Add("itemTwo", System.Type.GetType("System.Int32"));
                Tb_SimilarityMatrix.Columns.Add("similarity", System.Type.GetType("System.Double"));
                string tableName = "SimilarityMatrix";
                for (int j = 0; j < itemTotal; j++)
                {
                    SimilarityMatrix[i, j] = CalculateSimilarityForA_B(i, j);
                    // double similar = CalculateSimilarityForA_B(i, j);
                    DataRow row = Tb_SimilarityMatrix.NewRow();
                    row[0] = i + 1;
                    row[1] = j + 1;
                    row[2] = SimilarityMatrix[i, j];
                    Tb_SimilarityMatrix.Rows.Add(row);

                    //double similar= CalculateSimilarityForA_B(i, j);
                    //string sqlStr = string.Format("insert into SimilarityMatrix values({0},{1},{2})",i+1,j+1,similar);
                    //SqlSeverProvider.ExecuteNonQuery(sqlStr);
                }
                SqlSeverProvider.ExecuteSqlBulkCopy(Tb_SimilarityMatrix, tableName);
            }

            Console.WriteLine("Tb_SimilarityMatrix OK ");

            // 输出相似性矩阵
            //for (int i = 0; i < 3; i++)
            //{
            //    Console.WriteLine("item " + i + ":");
            //    for (int j = 0; j < 100; j++)
            //    {
            //        Console.Write(SimilarityMatrix[i, j] + "  ");
            //    }
            //}
        }
예제 #6
0
        //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);
        }