Ejemplo 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 = GetDimensionsCount(ModelContainer.ModelHandler);

            double[] results = new double[resultSize];
            bool     res     = CatBoostModelEvaluator.CalcModelPredictionSingle(
                ModelContainer.ModelHandler,
                floatFeatures, (uint)floatFeatures.Length,
                catFeatures, (uint)catFeatures.Length,
                results, resultSize
                );

            if (res)
            {
                return(results);
            }
            else
            {
                string msg = "";
                throw new CatBoostException(
                          "An error has occurred in the CalcModelPredictionSingle() method in catboostmodel library.\n" +
                          $"Returned error message: {msg}"
                          );
            }
        }
Ejemplo n.º 2
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  = GetDimensionsCount(ModelContainer.ModelHandler);

            IntPtr floatFeaturePtr = PointerTools.AllocateToPtr(floatFeatures);
            IntPtr catFeaturePtr   = PointerTools.AllocateToPtr(catFeatures);

            try
            {
                uint     resultSize = dim * docs;
                double[] results    = new double[resultSize];
                bool     res        = CatBoostModelEvaluator.CalcModelPrediction(
                    ModelContainer.ModelHandler,
                    docs,
                    floatFeaturePtr, (uint)floatFeatures.GetLength(1),
                    catFeaturePtr, (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
                {
                    string msg = "";
                    throw new CatBoostException(
                              "An error has occurred in the CalcModelPredictionSingle() method in catboostmodel library.\n" +
                              $"Returned error message: {msg}"
                              );
                }
            }
            finally
            {
                // TODO Deallocate
            }
        }
 /// <summary>
 /// Model constructor
 /// </summary>
 /// <param name="modelFilePath">Path to the model file</param>
 /// <param name="targetFeature">Name of the target feature</param>
 public CatBoostModel(string modelFilePath, string targetFeature)
 {
     Evaluator     = new CatBoostModelEvaluator(modelFilePath);
     TargetFeature = targetFeature;
 }