Package ai.catboost

Class CatBoostModel

java.lang.Object
ai.catboost.CatBoostModel
All Implemented Interfaces:
AutoCloseable

public class CatBoostModel extends Object implements AutoCloseable
CatBoost model, supports basic model application.
  • Field Details

    • handle

      private long handle
    • predictionDimension

      private int predictionDimension
    • treeCount

      private int treeCount
    • usedNumericFeatureCount

      private int usedNumericFeatureCount
    • usedCategoricFeatureCount

      private int usedCategoricFeatureCount
    • usedTextFeatureCount

      private int usedTextFeatureCount
    • usedEmbeddingFeatureCount

      private int usedEmbeddingFeatureCount
    • featureNames

      private String[] featureNames
    • metadata

      private Map<String,String> metadata
    • features

      private List<CatBoostModel.Feature> features
    • implLibrary

      private static CatBoostJNI implLibrary
  • Constructor Details

  • Method Details

    • loadModel

      @Nonnull public static CatBoostModel loadModel(@Nonnull String modelPath) throws CatBoostError
      Load CatBoost model from file modelPath.
      Parameters:
      modelPath - Path to the model.
      Returns:
      CatBoost model.
      Throws:
      CatBoostError - When failed to load model.
    • loadModel

      @Nonnull public static CatBoostModel loadModel(@Nonnull String modelPath, @Nonnull String modelFormat) throws CatBoostError
      Load CatBoost model from file modelPath.
      Parameters:
      modelPath - Path to the model.
      modelFormat - Model file format (bin or json)
      Returns:
      CatBoost model.
      Throws:
      CatBoostError - When failed to load model.
    • loadModel

      @Nonnull public static CatBoostModel loadModel(@Nonnull byte[] serializedModel) throws CatBoostError
      Load CatBoost model serialized in an array.
      Parameters:
      serializedModel - Byte array containing model.
      Returns:
      CatBoost model.
      Throws:
      CatBoostError - When failed to load model.
    • loadModel

      @Nonnull public static CatBoostModel loadModel(@Nonnull byte[] serializedModel, @Nonnull String modelFormat) throws CatBoostError
      Load CatBoost model serialized in an array.
      Parameters:
      serializedModel - Byte array containing model.
      modelFormat - Model file format (bin or json)
      Returns:
      CatBoost model.
      Throws:
      CatBoostError - When failed to load model.
    • loadModel

      @Nonnull public static CatBoostModel loadModel(InputStream in) throws CatBoostError, IOException
      Load CatBoost model from stream.
      Parameters:
      in - Input stream containing model.
      Returns:
      CatBoost model.
      Throws:
      CatBoostError - When failed to load model.
      IOException - When failed to read model from file.
    • loadModel

      @Nonnull public static CatBoostModel loadModel(InputStream in, @Nonnull String modelFormat) throws CatBoostError, IOException
      Load CatBoost model from stream.
      Parameters:
      in - Input stream containing model.
      modelFormat - Model file format (bin or json)
      Returns:
      CatBoost model.
      Throws:
      CatBoostError - When failed to load model.
      IOException - When failed to read model from file.
    • hashCategoricalFeature

      public static int hashCategoricalFeature(@Nonnull String catFeature) throws CatBoostError
      Hash categorical feature.
      Parameters:
      catFeature - String representation of categorical feature.
      Returns:
      Hash for categorical feature.
      Throws:
      CatBoostError - In case of error within native library.
    • hashCategoricalFeatures

      public static void hashCategoricalFeatures(@Nonnull String[] catFeatures, @Nonnull int[] hashes) throws CatBoostError
      Hash array of categorical features. May be cheaper to call this function once instead of calling hashCategoricalFeature(String) for each categorical feature in array.
      Parameters:
      catFeatures - Array of categorical features.
      hashes - Array of hashes of categorical features.
      Throws:
      CatBoostError - In case of error within native library.
    • hashCategoricalFeatures

      @Nonnull public static int[] hashCategoricalFeatures(@Nonnull String[] catFeatures) throws CatBoostError
      Hash array of categorical features.
      Parameters:
      catFeatures - Array of categorical features.
      Returns:
      Array of hashes of categorical features.
      Throws:
      CatBoostError - In case of error within native library.
    • getSupportedEvaluatorTypes

      public CatBoostModel.FormulaEvaluatorType[] getSupportedEvaluatorTypes() throws CatBoostError
      Throws:
      CatBoostError
    • setEvaluatorType

      public void setEvaluatorType(CatBoostModel.FormulaEvaluatorType evaluatorType) throws CatBoostError
      Throws:
      CatBoostError
    • getEvaluatorType

      Throws:
      CatBoostError
      IllegalArgumentException
    • getPredictionDimension

      public int getPredictionDimension()
      Returns:
      Dimension of model prediction.
    • getTreeCount

      public int getTreeCount()
      Returns:
      Number of trees in model.
    • getUsedNumericFeatureCount

      public int getUsedNumericFeatureCount()
      Returns:
      Number of numeric features used by the model.
    • getUsedCategoricFeatureCount

      public int getUsedCategoricFeatureCount()
      Returns:
      Number of categorical features used by the model.
    • getUsedTextFeatureCount

      public int getUsedTextFeatureCount()
      Returns:
      Number of text features used by the model.
    • getUsedEmbeddingFeatureCount

      public int getUsedEmbeddingFeatureCount()
      Returns:
      Number of embedding features used by the model.
    • getFeatureNames

      public String[] getFeatureNames()
      Returns:
      Names of features used by the model.
    • getMetadata

      public Map<String,String> getMetadata()
      Returns:
      A map of metadata
    • getFeatures

      public List<CatBoostModel.Feature> getFeatures()
      Returns:
      A list of feature metadata, sorted by flat index.
    • predict

      public void predict(@Nullable float[] numericFeatures, @Nullable String[] catFeatures, @Nonnull CatBoostPredictions prediction) throws CatBoostError
      Apply model to object defined by features.
      Parameters:
      numericFeatures - Numeric features.
      catFeatures - Categoric features.
      prediction - Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      public void predict(@Nullable float[] numericFeatures, @Nullable String[] catFeatures, @Nullable String[] textFeatures, @Nullable float[][] embeddingFeatures, @Nonnull CatBoostPredictions prediction) throws CatBoostError
      Apply model to object defined by features.
      Parameters:
      numericFeatures - Numeric features.
      catFeatures - Categoric features.
      textFeatures - Text features.
      embeddingFeatures - Embedding features.
      prediction - Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      @Nonnull public CatBoostPredictions predict(@Nullable float[] numericFeatures, @Nullable String[] catFeatures) throws CatBoostError
      Same as predict(float[], String[], CatBoostPredictions), but returns prediction instead of taking it as the last parameter.
      Parameters:
      numericFeatures - Numeric features.
      catFeatures - Categoric features.
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      @Nonnull public CatBoostPredictions predict(@Nullable float[] numericFeatures, @Nullable String[] catFeatures, @Nullable String[] textFeatures, @Nullable float[][] embeddingFeatures) throws CatBoostError
      Same as predict(float[], String[], String[], float[][], CatBoostPredictions), but returns prediction instead of taking it as the last parameter.
      Parameters:
      numericFeatures - Numeric features.
      catFeatures - Categoric features.
      textFeatures - Text features.
      embeddingFeatures - Embedding features.
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      public void predict(@Nullable float[] numericFeatures, @Nullable int[] catFeatureHashes, @Nonnull CatBoostPredictions prediction) throws CatBoostError
      Same as predict(float[], String[], CatBoostPredictions), but accept categoric features as hashes computed by hashCategoricalFeature(String).
      Parameters:
      numericFeatures - Numeric features.
      catFeatureHashes - Categoric feature hashes.
      prediction - Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      public void predict(@Nullable float[] numericFeatures, @Nullable int[] catFeatureHashes, @Nullable String[] textFeatures, @Nullable float[][] embeddingFeatures, @Nonnull CatBoostPredictions prediction) throws CatBoostError
      Parameters:
      numericFeatures - Numeric features.
      catFeatureHashes - Categoric feature hashes.
      textFeatures - Text features.
      embeddingFeatures - Embedding features.
      prediction - Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      @Nonnull public CatBoostPredictions predict(@Nullable float[] numericFeatures, @Nullable int[] catFeatureHashes) throws CatBoostError
      Same as predict(float[], int[], CatBoostPredictions), but returns prediction instead of taking it as third parameter.
      Parameters:
      numericFeatures - Numeric features.
      catFeatureHashes - Categoric feature hashes.
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      @Nonnull public CatBoostPredictions predict(@Nullable float[] numericFeatures, @Nullable int[] catFeatureHashes, @Nullable String[] textFeatures, @Nullable float[][] embeddingFeatures) throws CatBoostError
      Same as predict(float[], int[], String[], float[][], CatBoostPredictions), but returns prediction instead of taking it as third parameter.
      Parameters:
      numericFeatures - Numeric features.
      catFeatureHashes - Categoric feature hashes.
      textFeatures - Text features.
      embeddingFeatures - Embedding features.
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      public void predict(@Nullable float[][] numericFeatures, @Nullable String[][] catFeatures, @Nonnull CatBoostPredictions prediction) throws CatBoostError
      Apply model to a batch of objects.
      Parameters:
      numericFeatures - Numeric features matrix.
      catFeatures - Categoric features matrix.
      prediction - Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      public void predict(@Nullable float[][] numericFeatures, @Nullable String[][] catFeatures, @Nullable String[][] textFeatures, @Nullable float[][][] embeddingFeatures, @Nonnull CatBoostPredictions prediction) throws CatBoostError
      Apply model to a batch of objects.
      Parameters:
      numericFeatures - Numeric features matrix.
      catFeatures - Categoric features matrix.
      textFeatures - Text features matrix.
      embeddingFeatures - Embedding features matrix.
      prediction - Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      @Nonnull public CatBoostPredictions predict(@Nullable float[][] numericFeatures, @Nullable String[][] catFeatures) throws CatBoostError
      Same as predict(float[][], String[][], CatBoostPredictions), but returns prediction instead of taking it as the last parameter.
      Parameters:
      numericFeatures - Numeric features.
      catFeatures - Categoric features.
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predictTransposed

      @Nonnull public CatBoostPredictions predictTransposed(@Nonnull float[][] numericFeatures) throws CatBoostError
      Apply model to a batch of objects, but features of objects used in column format.
      Parameters:
      numericFeatures - numeric features in column format (row of matrix is one feature for all objects).
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      @Nonnull public CatBoostPredictions predict(@Nullable float[][] numericFeatures, @Nullable String[][] catFeatures, @Nullable String[][] textFeatures, @Nullable float[][][] embeddingFeatures) throws CatBoostError
      Same as predict(float[][], String[][], String[][], float[][][], CatBoostPredictions), but returns prediction instead of taking it as the last parameter.
      Parameters:
      numericFeatures - Numeric features.
      catFeatures - Categoric features.
      textFeatures - Text features.
      embeddingFeatures - Embedding features.
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      public void predict(@Nullable float[][] numericFeatures, @Nullable int[][] catFeatureHashes, @Nonnull CatBoostPredictions prediction) throws CatBoostError
      Same as predict(float[][], String[][], CatBoostPredictions), but accept categoric features as hashes computed by hashCategoricalFeature(String).
      Parameters:
      numericFeatures - Numeric features.
      catFeatureHashes - Categoric feature hashes.
      prediction - Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      public void predict(@Nullable float[][] numericFeatures, @Nullable int[][] catFeatureHashes, @Nullable String[][] textFeatures, @Nullable float[][][] embeddingFeatures, @Nonnull CatBoostPredictions prediction) throws CatBoostError
      Parameters:
      numericFeatures - Numeric features.
      catFeatureHashes - Categoric feature hashes.
      textFeatures - Text features.
      embeddingFeatures - Embedding features.
      prediction - Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      @Nonnull public CatBoostPredictions predict(@Nullable float[][] numericFeatures, @Nullable int[][] catFeatureHashes) throws CatBoostError
      Same as predict(float[][], String[][], CatBoostPredictions), but returns predictions instead of taking it as the last parameter.
      Parameters:
      numericFeatures - Numeric features.
      catFeatureHashes - Categoric feature hashes.
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • predict

      @Nonnull public CatBoostPredictions predict(@Nullable float[][] numericFeatures, @Nullable int[][] catFeatureHashes, @Nullable String[][] textFeatures, @Nullable float[][][] embeddingFeatures) throws CatBoostError
      Same as predict(float[][], String[][], String[][], float[][][], CatBoostPredictions), but returns predictions instead of taking it as the last parameter.
      Parameters:
      numericFeatures - Numeric features.
      catFeatureHashes - Categoric feature hashes.
      textFeatures - Text features.
      embeddingFeatures - Embedding features.
      Returns:
      Model predictions.
      Throws:
      CatBoostError - In case of error within native library.
    • finalize

      protected void finalize() throws Throwable
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • dispose

      private void dispose() throws CatBoostError
      Throws:
      CatBoostError
    • close

      public void close() throws CatBoostError
      Specified by:
      close in interface AutoCloseable
      Throws:
      CatBoostError