コード例 #1
0
        public SparseVector <double> Trim(SparseVector <double> vec, int size, double cutPerc)
        {
            SparseVector <double> trimmed = vec;

            if (vec.Count > size)
            {
                ArrayList <KeyDat <double, int> > tmp = new ArrayList <KeyDat <double, int> >(vec.Count);
                foreach (IdxDat <double> item in vec)
                {
                    tmp.Add(new KeyDat <double, int>(item.Dat, item.Idx));
                }
                tmp.Sort(DescSort <KeyDat <double, int> > .Instance);
                tmp.RemoveRange(size, tmp.Count - size);
                ArrayList <IdxDat <double> > tmp2 = new ArrayList <IdxDat <double> >();
                foreach (KeyDat <double, int> item in tmp)
                {
                    tmp2.Add(new IdxDat <double>(item.Dat, item.Key));
                }
                tmp2.Sort();
                trimmed = new SparseVector <double>(tmp2);
            }
            ModelUtils.CutLowWeights(ref trimmed, cutPerc);
            ModelUtils.TryNrmVecL2(trimmed);
            return(trimmed);
        }