public Form_SlopeOne()
        {
            InitializeComponent();

            // 默认选择第一个数据集
            this.comboBox1.SelectedIndex = 0;

            // 读取数据,得到训练用户集合以及测试用户集合
            obj_ReadData = new cReadinData(this.comboBox1.SelectedIndex);
            obj_AssStrategy = new cAssStrategy();
            testUsers = cReadinData.getTestUser();
            trainUsers = cReadinData.getBaseUser();

            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView2.RowHeadersVisible = false;

            for (int i = 0; i < count_Num.Length; i++)
            {
                count_Num[i] = 0;
                stat_Info[i] = 0;
            }

                // 根据选择的数据集,填充用户ID的下拉列表
                for (int i = 0; i < testUsers.Length - 1; i++)
                {
                    this.comboBox2.Items.Add(testUsers[i + 1].id);
                }
            // 用户ID默认选择第一个
            this.comboBox2.SelectedIndex = 0;

            obj_SlopeOne = new SlopeOne();

            for (int i = 1; i < trainUsers.Length; i++)
            {
                userRating = new Dictionary<int, float>();

                //count_Num[trainUsers[i].RatingNums/20]++;

                for (int j = 1; j < trainUsers[i].Ratings.Length; j++)
                {
                    if (trainUsers[i].Ratings[j] != 0)
                    {
                        userRating.Add(j, (float)trainUsers[i].Ratings[j]);
                    }
                }

                obj_SlopeOne.AddUserRatings(userRating);
            }

            this.comboBox3.Items.Clear();

            // 填充 连续运行用户数目 下拉列表
            for (int i = 15; i < testUsers.Length; i++)
            {
                comboBox3.Items.Add(i);
            }
            this.comboBox3.SelectedIndex = 0;
        }
        public Form_Apriori()
        {
            InitializeComponent();

            // 读入用户数据
            cReadinData obj_readData = new cReadinData(0);
            testUsers = cReadinData.getTestUser();
            sourceUsers = cReadinData.getBaseUser();

            // 初始化支持的关联规则集合
            supp_AssRules = new AssociationRule[sourceUsers.Length][];
            // 初始化推荐项目集合
            recItems = new RecItemid_Degree[sourceUsers.Length][];

            // 填充comboBox1
            for (int i = 15; i < testUsers.Length; i++)
            {
                this.comboBox1.Items.Add(i);
            }
            this.comboBox1.SelectedIndex = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt_1 = DateTime.Now;
            this.Rec_Items_num = int.Parse(this.textBox13.Text);    // Top-N推荐个数

            this.progressBar1.Maximum = (this.comboBox2.SelectedIndex + 15) * 10 + 5;
            this.progressBar1.Value = 0;
            // 读入数据,生成UI矩阵
            this.textBox3.Text = "开始读入数据";
            this.progressBar1.Value++;

            Application.DoEvents();

            cReadinData obj_ReadData = new cReadinData(comboBox1.SelectedIndex);

            this.textBox3.Text = "读入数据完成   训练数据:" + obj_ReadData.sTrainFileName[comboBox1.SelectedIndex]
                + "     测试数据:" + obj_ReadData.testfileName[comboBox1.SelectedIndex];
            this.progressBar1.Value += 2;

            Application.DoEvents();

            // 读取最近邻居个数
            int number = int.Parse(textBox2.Text);
            this.neigh_num = number;

            // 相似度算法的选择
            if(this.radioButton1.Checked)
            {
                sim_alg = 1;
            }
            else if(this.radioButton2.Checked)
            {
                sim_alg = 2;
            }
            else if(this.radioButton3.Checked)
            {
                sim_alg = 3;
            }

            // 测试用户数目,最少为15
            testUserNum = this.comboBox2.SelectedIndex + 15;

            cUserBased_CF obj_UserBased_CF = new cUserBased_CF(this.neigh_num);
            cUser[] testUsers = cReadinData.getTestUser();

            this.textBox3.Text = "初始化完成 相似度算法:" + sim_alg.ToString() +
                " 最近邻居个数:" + this.neigh_num.ToString() + " 测试用户数:" + testUserNum.ToString();
            this.progressBar1.Value += 2;
            Application.DoEvents();

            double MAE_1, Precison, Recall, F_Measure;
            double total_MAE = 0, total_Precison = 0, total_Recall = 0, total_F_Measure = 0;
            double average_MAE, average_Precison, average_Recall, average_F_Measure;

            for (int i = 1; i <= this.testUserNum; i++)
            {
                this.progressBar1.Value += 5;
                obj_AssStrategy = obj_UserBased_CF.getPredictRating(testUsers[i], this.sim_alg, Rec_Items_num);

                // 取得各项算法评价指标
                MAE_1 = obj_AssStrategy.MAE;
                Precison = obj_AssStrategy.Precison;
                Recall = obj_AssStrategy.Recall;
                F_Measure = obj_AssStrategy.calculateF_Measure();

                // 累计各项指标的和
                total_MAE += MAE_1;
                total_Precison += Precison;
                total_Recall += Recall;
                total_F_Measure += F_Measure;

                this.textBox3.Text = "第 " + i.ToString() + " 个用户计算完成.";

                this.progressBar1.Value += 5;
                Application.DoEvents();
            }
            // 计算各个评价准则的平均值
            average_MAE = total_MAE / this.testUserNum;
            average_Precison = total_Precison / this.testUserNum;
            average_Recall = total_Recall / this.testUserNum;
            average_F_Measure = total_F_Measure / this.testUserNum;

            DateTime dt_2 = DateTime.Now;
            TimeSpan ts = dt_2.Subtract(dt_1);

            this.textBox3.Text = "所有用户计算完成   总耗时:" + ts.TotalMilliseconds + " ms";
            Application.DoEvents();

            this.textBox4.Text = average_MAE.ToString();
            this.textBox5.Text = ts.TotalMilliseconds + " ms";
            this.textBox6.Text = this.sSimAlg[this.sim_alg - 1];
            this.textBox7.Text = this.neigh_num.ToString();
            this.textBox9.Text = average_Precison.ToString();
            this.textBox10.Text = average_Recall.ToString();
            this.textBox11.Text = average_F_Measure.ToString();
            this.textBox12.Text = "20";

            this.dataGridView1.Rows.Add(count_dgv++, sSimAlg[sim_alg - 1],  this.neigh_num, this.Rec_Items_num,average_MAE,
                average_Precison, average_Recall, average_F_Measure, ( ts.TotalMilliseconds / this.testUserNum) + " ms" );
        }
예제 #4
0
        /// <summary>
        /// 生成频繁-1项集
        /// </summary>
        public void genFreq_one_itemsets()
        {
            cReadinData obj_readData = new cReadinData(0);

            sourceUsers = cReadinData.getBaseUser();       // 读入训练数据
            testUsers   = cReadinData.getTestUser();       // 读入测试数据
            float  itemSupport;
            int    count         = 0;
            double total_support = 0;

            discretize = new bool[sourceUsers.Length][];
            bool[] testUser_discretize = new bool[testUsers[1].Ratings.Length];

            ArrayList temp_one_items = new ArrayList();
            Freq_Item obj_freq_item;

            Freq_Item[] obj_freq_items;

            // 得到训练用户喜好矩阵
            for (int i = 1; i < sourceUsers.Length; i++)
            {
                discretize[i] = new bool[sourceUsers[i].Ratings.Length];
                discretize[i] = sourceUsers[i].discretizeRating();
            }

            // 得到测试用户喜好矩阵
            for (int i = 1; i < testUsers.Length; i++)
            {
                testUser_discretize = testUsers[i].discretizeRating();
            }

            int count_zero = 0;

            for (int itemid = 1; itemid < sourceUsers[1].Ratings.Length; itemid++)
            {
                // for循环扫描数据集,得到用户中喜欢id为itemid的项目的人数
                for (int userid = 1; userid < sourceUsers.Length; userid++)
                {
                    // 如果id为userid的用户喜欢id为itemid的项目
                    if (discretize[userid][itemid] == true)
                    {
                        count++;
                    }
                }
                // 得到该项目的支持度值
                itemSupport = (float)((float)count / (float)sourceUsers.Length);

                obj_freq_item = new Freq_Item(itemid, count, itemSupport);
                temp_one_items.Add(obj_freq_item);

                // 计算支持度计数为零的项目数量
                if (itemSupport == 0)
                {
                    count_zero++;
                }

                // 支持度计数累加为和
                total_support += itemSupport;
                count          = 0;
            }

            // 支持度阈值(平均值)
            float min_support_one = (float)((float)total_support / (float)(temp_one_items.Count));

            Freq_Item.min_support = min_support_one;

//             obj_freq_items = new Freq_Item[temp_one_items.Count];
//
//             int count_one_item = 0;
//             foreach (Freq_Item obj in temp_one_items)
//             {
//                 obj_freq_items[count_one_item++] = obj;
//             }
            // 按照支持度排序
            //   Array.Sort(obj_freq_items);

            // 生成频繁-1项集
            foreach (Freq_Item obj in temp_one_items)
            {
                if (obj.support > min_support_one)
                {
                    freq_one_items.Add(obj);
                }
            }
            // 最小支持度计数
            Freq_Item last_one = (Freq_Item)freq_one_items[freq_one_items.Count - 1];

            Freq_Item.min_support_count = last_one.support_count;
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 清除用户ID下拉列表
            this.comboBox2.Items.Clear();

            // 读取数据,得到训练用户集合以及测试用户集合
            obj_ReadData = new cReadinData(this.comboBox1.SelectedIndex);
            testUsers = cReadinData.getTestUser();
            trainUsers = cReadinData.getBaseUser();

            // 根据选择的数据集,填充用户ID的下拉列表
            for (int i = 0; i < testUsers.Length-1; i++)
            {
                this.comboBox2.Items.Add( testUsers[i+1].id );
            }

            // 用户ID默认选择第一个
            this.comboBox2.SelectedIndex = 0;

            this.comboBox3.Items.Clear();

            // 填充 连续运行用户数目 下拉列表
            for (int i = 15; i < testUsers.Length; i++)
            {
                comboBox3.Items.Add(i);
            }
        }
        /// <summary>
        /// 生成频繁-1项集
        /// </summary>
        public void genFreq_one_itemsets()
        {
            cReadinData obj_readData = new cReadinData(0);
            sourceUsers = cReadinData.getBaseUser();       // 读入训练数据
            testUsers = cReadinData.getTestUser();         // 读入测试数据
            float itemSupport;
            int count = 0;
            double total_support = 0;

            discretize = new bool[sourceUsers.Length][];
            bool[] testUser_discretize = new bool[testUsers[1].Ratings.Length];

            ArrayList temp_one_items = new ArrayList();
            Freq_Item obj_freq_item;
            Freq_Item[] obj_freq_items;

            // 得到训练用户喜好矩阵
            for (int i = 1; i < sourceUsers.Length; i++)
            {
                discretize[i] = new bool[sourceUsers[i].Ratings.Length];
                discretize[i] = sourceUsers[i].discretizeRating();
            }

            // 得到测试用户喜好矩阵
            for (int i = 1; i < testUsers.Length; i++)
            {
                testUser_discretize = testUsers[i].discretizeRating();
            }

            int count_zero = 0;

            for (int itemid = 1; itemid < sourceUsers[1].Ratings.Length; itemid++)
            {
                // for循环扫描数据集,得到用户中喜欢id为itemid的项目的人数
                for (int userid = 1; userid < sourceUsers.Length; userid++)
                {
                    // 如果id为userid的用户喜欢id为itemid的项目
                    if (discretize[userid][itemid] == true)
                        count++;
                }
                // 得到该项目的支持度值
                itemSupport = (float)((float)count / (float)sourceUsers.Length);

                obj_freq_item = new Freq_Item(itemid, count, itemSupport);
                temp_one_items.Add(obj_freq_item);

                // 计算支持度计数为零的项目数量
                if (itemSupport == 0)
                {
                    count_zero++;
                }

                // 支持度计数累加为和
                total_support += itemSupport;
                count = 0;
            }

            // 支持度阈值(平均值)
            float min_support_one = (float)((float)total_support / (float)(temp_one_items.Count));
            Freq_Item.min_support = min_support_one;

            //             obj_freq_items = new Freq_Item[temp_one_items.Count];
            //
            //             int count_one_item = 0;
            //             foreach (Freq_Item obj in temp_one_items)
            //             {
            //                 obj_freq_items[count_one_item++] = obj;
            //             }
            // 按照支持度排序
             //   Array.Sort(obj_freq_items);

            // 生成频繁-1项集
            foreach (Freq_Item obj in temp_one_items)
            {
                if (obj.support > min_support_one)
                {
                    freq_one_items.Add(obj);
                }
            }
            // 最小支持度计数
            Freq_Item last_one = (Freq_Item)freq_one_items[freq_one_items.Count - 1];
            Freq_Item.min_support_count = last_one.support_count;
        }