Exemplo n.º 1
0
        public double compare(OneClass one, DataRow test, List <int> core)
        {
            List <Node[]> _X = new List <Node[]>();
            List <double> _Y = new List <double>();

            Problem.sampleSimilar.Clear();
            int attrNum   = core.Count;
            int sampleNum = samples.Count;

            for (int i = 0; i < sampleNum; i++)
            {
                //初始化样本权值
                Problem.sampleSimilar.Add(similars[i]);
                DataRow     sample    = samples[i];//训练样本
                List <Node> sampleVal = new List <Node>();
                for (int j = 0; j < attrNum; j++)
                {
                    sampleVal.Add(new Node(j + 1, Convert.ToDouble(sample[core[j]])));
                }
                _X.Add(sampleVal.ToArray());         //关键属性
                _Y.Add(Convert.ToDouble(sample[0])); //决策属性
            }
            sampleNum = one.samples.Count;           //另一类样本个数
            for (int i = 0; i < sampleNum; i++)
            {
                //初始化样本权值
                Problem.sampleSimilar.Add(one.similars[i]);
                DataRow     sample    = one.samples[i];//训练样本
                List <Node> sampleVal = new List <Node>();
                for (int j = 0; j < attrNum; j++)
                {
                    sampleVal.Add(new Node(j + 1, Convert.ToDouble(sample[core[j]])));
                }
                _X.Add(sampleVal.ToArray());         //关键属性
                _Y.Add(Convert.ToDouble(sample[0])); //决策属性
            }
            //构造测试样本数据
            List <Node> testVal = new List <Node>();

            for (int j = 0; j < attrNum; j++)
            {
                testVal.Add(new Node(j + 1, /*Problem.attrWeight[core[j]-1]**/ Convert.ToDouble(test[core[j]])));
            }
            //构造Svm测试
            Svm    svm    = new Svm(_X.ToArray(), _Y.ToArray());
            double preRes = svm.predict(testVal.ToArray());

            if (preRes != label)
            {
                return(label);
            }
            else
            {
                return(one.label);
            }
        }
Exemplo n.º 2
0
        public int CompareTo(object o)
        {
            OneClass one = (OneClass)o;

            if (distance > one.distance)
            {
                return(1);
            }
            else
            {
                return(-1);
            }
        }
Exemplo n.º 3
0
        public void classify(List <DataRow> trainCollection)
        {
            //将训练样本按照类别分类,标号有序
            int      sampleNum = ids.Count; //训练样本数
            double   oldLabel  = -1;        //临时记录标签
            OneClass one       = null;

            for (int i = 0; i < sampleNum; i++)
            {
                DataRow sample = trainCollection[ids[i]];            //测试样本
                double  label  = double.Parse(sample[0].ToString()); //类标签
                if (label == oldLabel)                               //同类
                {
                    one.addSample(sample, id_u[ids[i]]);
                }
                else //新类
                {
                    oldLabel = label;//更新临时标签
                    one      = new OneClass(label);      //构造类别
                    one.addSample(sample, id_u[ids[i]]); //添加样本及其相似度
                    classes.Add(one);
                }
            }
        }