コード例 #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>
 /// <param name="e">添加的元素</param>
 public void addElem(DiffMatrixElem e)
 {
     diff.Add(e);
 }