Exemplo n.º 1
0
        public Prediction <LblT> Predict(SparseVector <double> example)
        {
            Utils.ThrowException(mModelId == -1 ? new InvalidOperationException() : null);
            Utils.ThrowException(example == null ? new ArgumentNullException("example") : null);
            Prediction <LblT> result = new Prediction <LblT>();

            int[]   idx = new int[example.Count];
            float[] val = new float[example.Count];
            for (int i = 0; i < example.Count; i++)
            {
                idx[i] = example.InnerIdx[i] + 1;    // *** indices are 1-based in SvmLightLib
                val[i] = (float)example.InnerDat[i]; // *** loss of precision (double -> float)
            }
            int vecId = SvmLightLib.NewFeatureVector(idx.Length, idx, val, 0);

            SvmLightLib.MulticlassClassify(mModelId, 1, new int[] { vecId });
            int n = SvmLightLib.GetFeatureVectorClassifScoreCount(vecId);

            for (int i = 0; i < n; i++)
            {
                double score = SvmLightLib.GetFeatureVectorClassifScore(vecId, i);
                LblT   lbl   = mIdxToLbl[i];
                result.Inner.Add(new KeyDat <double, LblT>(score, lbl));
            }
            result.Inner.Sort(DescSort <KeyDat <double, LblT> > .Instance);
            result.Trim();
            SvmLightLib.DeleteFeatureVector(vecId); // delete feature vector
            return(result);
        }
Exemplo n.º 2
0
        public Prediction <LblT> Predict(SparseVector <double> example)
        {
            Utils.ThrowException(mModelId == -1 ? new InvalidOperationException() : null);
            Utils.ThrowException(example == null ? new ArgumentNullException("example") : null);
            Prediction <LblT> result = new Prediction <LblT>();

            int[]   idx = new int[example.Count];
            float[] val = new float[example.Count];
            for (int i = 0; i < example.Count; i++)
            {
                idx[i] = example.InnerIdx[i] + 1;
                val[i] = (float)example.InnerDat[i]; // *** cast to float
            }
            int vecId = SvmLightLib.NewFeatureVector(idx.Length, idx, val, 0);

            SvmLightLib.Classify(mModelId, 1, new int[] { vecId });
            double score    = SvmLightLib.GetFeatureVectorClassifScore(vecId, 0);
            LblT   lbl      = mIdxToLbl[score > 0 ? 0 : 1];
            LblT   otherLbl = mIdxToLbl[score > 0 ? 1 : 0];

            result.Inner.Add(new KeyDat <double, LblT>(Math.Abs(score), lbl));
            result.Inner.Add(new KeyDat <double, LblT>(-Math.Abs(score), otherLbl));
            SvmLightLib.DeleteFeatureVector(vecId); // delete feature vector
            return(result);
        }