Exemplo n.º 1
0
        /// <summary>
        /// Evaluates the model on the single input.
        /// <seealso cref="EvaluateSingle(float[], string[])"/>
        /// </summary>
        /// <param name="floatFeatures">Array of float features</param>
        /// <param name="catFeatures">Array of categorical features</param>
        /// <returns>Array storing model prediction for the input</returns>
        public double[] EvaluateSingle(float[] floatFeatures, string[] catFeatures)
        {
            uint resultSize = CatboostNativeInterface.GetDimensionsCount(ModelContainer.ModelHandler);

            double[] results = new double[resultSize];
            bool     res     = true;

            unsafe
            {
                fixed(float *floatFeaturesPtr = floatFeatures)
                {
                    res = CatboostNativeInterface.CalcModelPredictionSingle(
                        ModelContainer.ModelHandler,
                        floatFeatures, (uint)floatFeatures.Length,
                        catFeatures, (uint)catFeatures.Length,
                        results, resultSize
                        );
                }
            }

            if (res)
            {
                return(results);
            }
            else
            {
                var msg = CatboostNativeInterface.GetErrorStringConst(ModelContainer.ModelHandler);
                throw new CatBoostException(
                          "An error has occurred in the CalcModelPredictionSingle() method in catboostmodel library.\n" +
                          $"Returned error message: {msg}"
                          );
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Container constructor.
 /// </summary>
 /// <param name="path">Path to the model file</param>
 public CatBoostModelContainer(string path)
 {
     ModelPath    = path;
     ModelHandler = CatboostNativeInterface.ModelCalcerCreate();
     if (!CatboostNativeInterface.LoadFullModelFromFile(ModelHandler, ModelPath))
     {
         var msg = CatboostNativeInterface.GetErrorStringConst(ModelHandler);
         throw new CatBoostException(
                   "An error has occurred in the LoadFullModelFromFile() method in catboostmodel library.\n" +
                   $"Returned error message: {msg}"
                   );
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Evaluates the model on the input batch.
        /// <seealso cref="EvaluateSingle(float[], string[])"/>
        /// </summary>
        /// <param name="floatFeatures">
        /// 2D array of float features.
        /// Should have the same <c>.GetLength(0)</c> as <paramref name="catFeatures"/>
        /// </param>
        /// <param name="catFeatures">
        /// 2D array of categorical features encoded as strings.
        /// Should have the same <c>.GetLength(0)</c> as <paramref name="floatFeatures"/>
        /// </param>
        /// <returns>2D array with model predictions for all samples in the batch</returns>
        public double[,] EvaluateBatch(float[,] floatFeatures, string[,] catFeatures)
        {
            if (floatFeatures.GetLength(0) != catFeatures.GetLength(0))
            {
                if (floatFeatures.GetLength(0) > 0 && catFeatures.GetLength(0) > 0)
                {
                    throw new CatBoostException("Inconsistent EvaluateBatch arguments:" +
                                                $"got {floatFeatures.GetLength(0)} samples for float features " +
                                                $"but {catFeatures.GetLength(0)} samples for cat features");
                }
            }
            uint docs = (uint)Math.Max(floatFeatures.GetLength(0), catFeatures.GetLength(0));
            uint dim  = CatboostNativeInterface.GetDimensionsCount(ModelContainer.ModelHandler);


            uint resultSize = dim * docs;

            double[] results = new double[resultSize];
            bool     res     = false;

            unsafe
            {
                fixed(float *floatFeaturesPtr = floatFeatures)
                {
                    using (var catFeatureHolder = new StringPointerHolder(catFeatures)) {
                        float *[] floatFeaturesBeginPtrs = new float *[docs];
                        for (int i = 0; i < floatFeatures.GetLength(0); ++i)
                        {
                            floatFeaturesBeginPtrs[i] = floatFeaturesPtr + i * floatFeatures.GetLength(1);
                        }

                        fixed(float **fff = floatFeaturesBeginPtrs)
                        {
                            res = CatboostNativeInterface.CalcModelPrediction(
                                ModelContainer.ModelHandler,
                                docs,
                                fff, (uint)floatFeatures.GetLength(1),
                                catFeatureHolder.MainPointer, (uint)catFeatures.GetLength(1),
                                results, resultSize
                                );
                        }
                    }
                }
            }
            if (res)
            {
                double[,] resultMatrix = new double[docs, dim];
                for (int doc = 0; doc < docs; ++doc)
                {
                    for (int d = 0; d < dim; ++d)
                    {
                        resultMatrix[doc, d] = results[dim * doc + d];
                    }
                }
                return(resultMatrix);
            }
            else
            {
                var msg = CatboostNativeInterface.GetErrorStringConst(ModelContainer.ModelHandler);
                throw new CatBoostException(
                          "An error has occurred in the CalcModelPredictionSingle() method in catboostmodel library.\n" +
                          $"Returned error message: {msg}"
                          );
            }
        }