Exemple #1
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 ");
        }
Exemple #2
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...");
        }
Exemple #3
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] + "  ");
            //    }
            //}
        }