示例#1
0
        /// <summary>
        /// 获取差别矩阵
        /// </summary>
        public DiffMatrix getDiffMatrix(List <double> valLength)
        {
            DiffMatrix matrix    = new DiffMatrix();      //差别矩阵
            int        sampleNum = trainCollection.Count; //相似样本个数

            //组合训练样本
            for (int i = 0; i < sampleNum - 1; i++)
            {
                DataRow first = trainCollection[i];
                for (int j = i + 1; j < sampleNum; j++)
                {
                    DataRow second = trainCollection[j];
                    //计算差别
                    if (!first[0].ToString().Equals(second[0].ToString()))//决策属性不同
                    {
                        //产生差别矩阵元素
                        DiffMatrixElem elem   = new DiffMatrixElem(i, j);
                        int            keyNum = keyCollection.Count;
                        for (int k = 1; k < keyNum; k++)//第0号属性是决策属性,不是关键属性
                        {
                            //关键属性
                            DataColumn keyAttr = keyCollection[k];
                            if (keyAttr.DataType.ToString().Equals("System.Single") ||
                                keyAttr.DataType.ToString().Equals("System.Double") ||
                                keyAttr.DataType.ToString().Equals("System.Int32"))
                            {
                                //提取浮点数据
                                double a = Convert.ToDouble(first[k]);
                                double x = Convert.ToDouble(second[k]);
                                //计算差别程度
                                //double diff = Math.Abs(Math.Abs(x - a) / a);
                                double diff = 0;
                                if (valLength[k - 1] != 0)
                                {
                                    diff = Math.Abs(x - a) / valLength[k - 1];
                                }
                                if (diff > 0)                  //有区别,没有约简,置为0
                                {
                                    elem.addDiffAttr(k, diff); //添加差别属性
                                }
                            }
                            else
                            {
                                //文本二元化
                                if (!first[k].ToString().Equals(second[k].ToString()))
                                {
                                    elem.addDiffAttr(k, 1);//添加差别属性
                                }
                            }
                        }
                        //添加差别元素
                        matrix.addElem(elem);
                    }
                }
            }
            //matrix.write();
            return(matrix);
        }
示例#2
0
        /// <summary>
        /// 主过程
        /// </summary>
        public void run()
        {
            List <DataRow> trainCollection = new List <DataRow>(); //训练样本
            List <DataRow> testCollection  = new List <DataRow>(); //测试样本
            List <double>  valLength       = new List <double>();  //值空间
            List <double>  max             = new List <double> (); //最大值
            //提取测试样本和训练样本
            DataColumnCollection attrCollection = classify(testCollection, trainCollection, valLength, max);

            //构造筛选对象
            filter = new Filter(trainCollection, attrCollection);
            int     testNum = testCollection.Count; //训练样本个数
            DataRow testSample;                     //测试样本
            double  sucNum = 0;                     //测试成功的样本数

            for (int i = 0; i < testNum; i++)       //遍历测试样本
            {
                //根据相似样本集和关键属性构造约简对象
                reduct = new Reduct(trainCollection, attrCollection);
                List <double> dependance = reduct.calDependance();          //求出属性的依赖度
                DiffMatrix    matrix     = reduct.getDiffMatrix(valLength); //获得差别矩阵
                //获得专家参数
                List <double> expertPara = new List <double>();             //专家参数集合
                int           attrNum    = attrCollection.Count;
                for (int k = 0; k < attrNum - 1; k++)
                {
                    expertPara.Add(1);
                }
                //为关键属性加权
                List <double> weights = matrix.weightedKeyAttr(dependance, expertPara);
                //按照属性对属性排序
                List <int> sortedIds = matrix.sortWeight(weights); //按照权值大小排序,记录关键属性顺序
                //约简生成core集
                List <int> core = matrix.reduce(sortedIds);        //根据权值顺序约简差别矩阵,获得核心集合
                if (core.Count == 0)
                {
                    continue;
                }
                //测试样本
                testSample = testCollection[i];
                //求解针对测试样本相似度满足阈值的训练样本及其相似度
                Dictionary <int, double> id_u = filter.getIdSimilarity(testSample, valLength, weights, max, core);
                if (id_u.Count == 0)
                {
                    continue;
                }
                //调用多类SVM进行决策
                Strategy strategy = new Strategy(id_u, weights, core);
                strategy.classify(trainCollection);
                if (strategy.predict(testSample))
                {
                    sucNum++;
                }
            }
            Console.WriteLine("准确度=" + (int)(sucNum / testNum * 10000) / 100.0 + "%");
        }