Exemple #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);
        }