Esempio 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;
                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);
        }