[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for ClickHouse®](../index.md) > [Step-by-step guides](index.md) > Storing and processing data > Managing machine learning models

# Managing machine learning models in Managed Service for ClickHouse®

Managed Service for ClickHouse® enables data analysis using [CatBoost](https://catboost.ai/) ML models without any additional tools.

To apply a model, add it to your cluster and call it in an SQL query using the built-in `catboostEvaluate()` function. This query will return model predictions for each row of the input data.

Read more about the `catboostEvaluate()` function in [this ClickHouse® guide](https://clickhouse.com/docs/enen/sql-reference/functions/other-functions#catboostevaluatepath_to_model-feature_1-feature_2--feature_n).

## Before adding a model {#prereq}

Managed Service for ClickHouse® only works with models uploaded to Yandex Object Storage and accessible for reading:


1. To attach a [service account](../../iam/concepts/users/service-accounts.md) to a cluster, [assign](../../iam/operations/roles/grant.md) the [iam.serviceAccounts.user](../../iam/security/index.md#iam-serviceAccounts-user) role or higher to your Yandex Cloud account.
1. [Upload](../../storage/operations/objects/upload.md) the trained model file to Yandex Object Storage.
1. [Attach the service account to the cluster](s3-access.md#connect-service-account). You will use this [service account](../../iam/concepts/users/service-accounts.md) to configure access to the model file.
1. [Assign](s3-access.md#configure-acl) the `storage.viewer` role to the service account.
1. In the bucket ACL, [add](../../storage/operations/buckets/edit-acl.md) the `READ` permission to the service account.
1. [Get a link](s3-access.md#get-link-to-object) to the model file.


## Getting a list of models in a cluster {#list}

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
    1. Navigate to **Managed Service for&nbsp;ClickHouse**.
    1. Click the name of your cluster and select the **Machine learning** tab in the left-hand panel.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To get a list of models in a cluster, run this command:

    ```bash
    yc managed-clickhouse ml-model list --cluster-name=<cluster_name>
    ```

    You can get the cluster name with the [list of clusters in the folder](cluster-list.md#list-clusters).

- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [MlModel.List](../api-ref/MlModel/list.md) method, e.g., via the following [cURL](https://curl.se/) request:

        ```bash
        curl \
            --request GET \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters/<cluster_ID>/mlModels'
        ```

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

    1. Check the [server response](../api-ref/MlModel/list.md#yandex.cloud.mdb.clickhouse.v1.ListMlModelsResponse) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
    1. Call the [MlModelService.List](../api-ref/grpc/MlModel/list.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/ml_model_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                    "cluster_id": "<cluster_ID>"
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.clickhouse.v1.MlModelService.List
        ```

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

    1. View the [server response](../api-ref/grpc/MlModel/list.md#yandex.cloud.mdb.clickhouse.v1.ListMlModelsResponse) to make sure your request was successful.

{% endlist %}

## Getting detailed information about a model {#get}

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
    1. Navigate to **Managed Service for&nbsp;ClickHouse**.
    1. Click the name of your cluster and select the **Machine learning** tab in the left-hand panel.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To get model details, run this command:

    ```bash
    yc managed-clickhouse ml-model get <model_name> \
      --cluster-name=<cluster_name>
    ```

    You can get the model name with the [list of cluster models](#list), and the cluster name, with the [list of clusters in the folder](cluster-list.md#list-clusters).

- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [MlModel.Get](../api-ref/MlModel/get.md) method, e.g., via the following [cURL](https://curl.se/) request:

        ```bash
        curl \
            --request GET \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters/<cluster_ID>/mlModels/<model_name>'
        ```

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the model name, with the [list of models](#list) in the cluster.

    1. View the [server response](../api-ref/MlModel/get.md#yandex.cloud.mdb.clickhouse.v1.MlModel) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
    1. Call the [MlModelService.Get](../api-ref/grpc/MlModel/get.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/ml_model_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                    "cluster_id": "<cluster_ID>",
                    "ml_model_name": "<model_name>"
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.clickhouse.v1.MlModelService.Get
        ```

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the model name, with the [list of models](#list) in the cluster.

    1. View the [server response](../api-ref/grpc/MlModel/get.md#yandex.cloud.mdb.clickhouse.v1.MlModel) to make sure your request was successful.

{% endlist %}

## Creating a model {#add}

{% note info %}

The only supported model type is CatBoost: `ML_MODEL_TYPE_CATBOOST`.

{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

    1. Select the cluster:

        1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
        1. Navigate to **Managed Service for&nbsp;ClickHouse**.
        1. Click the name of your cluster and select the **Machine learning** tab in the left-hand panel.
        1. Click **Create model**.

    1. Configure the model:

        * **Type**: `ML_MODEL_TYPE_CATBOOST`.
        * **Name**: Model name. It is one of the arguments of the `catboostEvaluate()` function, which is used to call the model in ClickHouse®.
        * **URL**: Model address in Yandex Object Storage.

    1. Click **Create** and wait for the model to be created.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To create a model, run this command:

    ```bash
    yc managed-clickhouse ml-model create <model_name> \
      --cluster-name=<cluster_name> \
      --type=ML_MODEL_TYPE_CATBOOST \
      --uri=<link_to_model_file_in_Object_Storage>
    ```

    You can get the cluster name from the [list of clusters in your folder](cluster-list.md#list-clusters).


- Terraform {#tf}

    1. Open the current Terraform configuration file with the infrastructure plan.

        For more on how to create such a file, see the [Creating a cluster](cluster-create.md) section.

    1. Add the `ml_model` section describing the ML model being added to the Managed Service for ClickHouse® cluster description:

        ```hcl
        resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
          ...
          ml_model {
            name = "<model_name>"
            type = "ML_MODEL_TYPE_CATBOOST"
            uri  = "<link_to_model_file_in_Object_Storage>"
          }
        }
        ```

    1. Make sure the settings are correct.

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Confirm resource changes.

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

    For more information, see [this Terraform provider guide](../../terraform/resources/mdb_clickhouse_cluster.md).

    {% note warning "Timeouts" %}
    
    The Terraform provider sets the following timeouts for Managed Service for ClickHouse® cluster operations:
    
    * Creating a cluster, including by restoring from a backup: 60 minutes.
    * Updating a cluster: 90 minutes.
    * Deleting a cluster: 30 minutes.
    
    Operations exceeding the timeout are aborted.
    
    {% cut "How to change these limits" %}
    
    Add the `timeouts` section to your cluster description, such as the following:
    
    ```hcl
    resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
      ...
      timeouts = {
        create = "1h30m" # 1 hour 30 minutes
        update = "2h"    # 2 hours
        delete = "30m"   # 30 minutes
      }
    }
    ```
    
    {% endcut %}
    
    {% endnote %}


- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [MlModel.Create](../api-ref/MlModel/create.md) method, e.g., via the following [cURL](https://curl.se/) request:

        ```bash
        curl \
            --request POST \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --header "Content-Type: application/json" \
            --url 'https://{{ api-host-mdb }/managed-clickhouse/v1/clusters/<cluster_ID>/mlModels' \
            --data '{
                      "mlModelName": "<model_name>",
                      "type": "ML_MODEL_TYPE_CATBOOST",
                      "uri": "<file_link>"
                    }'
        ```

        Where:

        * `mlModelName`: Model name.
        * `type`: Model type, which is always `ML_MODEL_TYPE_CATBOOST`.
        * `uri`: Link to the model file in Object Storage.

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

    1. Check the [server response](../api-ref/MlModel/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
    1. Call the [MlModelService.Create](../api-ref/grpc/MlModel/create.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/ml_model_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                    "cluster_id": "<cluster_ID>",
                    "ml_model_name": "<model_name>",
                    "type": "ML_MODEL_TYPE_CATBOOST",
                    "uri": "<file_link>"
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.clickhouse.v1.MlModelService.Create
        ```

        Where:

        * `ml_model_name`: Model name.
        * `type`: Model type, which is always `ML_MODEL_TYPE_CATBOOST`.
        * `uri`: Link to the model file in Object Storage.

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

    1. View the [server response](../api-ref/grpc/MlModel/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

## Applying a model {#apply}

To apply a model to data stored in a ClickHouse® cluster:

1. [Connect to the cluster](connect/clients.md).
1. Run the following SQL query:

   ```sql
   SELECT 
       catboostEvaluate('<path_to_model_file>', 
                     <column_1_name>,
                     <column_2_name>,
                     ...
                     <column_N_name>)
   FROM <table_name>
   ```

Specify the following as the `catboostEvaluate()` function arguments:

   * Path to the model file in `/var/lib/clickhouse/models/<model_name>.bin` format.
   * Names of columns containing the input data.

The query will return a column with model predictions for each row of the source table.

## Updating a model {#update}

Managed Service for ClickHouse® does not track changes in a model file located in a Yandex Object Storage bucket.

To update the contents of a model that is already added to the cluster:


1. [Upload](../../storage/operations/objects/upload.md) the current model file to Yandex Object Storage.
1. [Get a link](s3-access.md#get-link-to-object) to this file.
1. Update the settings of the model added to Managed Service for ClickHouse® by providing a new link to the model file.


{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
    1. Navigate to **Managed Service for&nbsp;ClickHouse**.
    1. Click the name of your cluster and select the **Machine learning** tab in the left-hand panel.
    1. Find your model, click ![image](../../_assets/console-icons/ellipsis-vertical.svg), and select **Edit**.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To update the link to a model file in a Yandex Object Storage bucket, run this command:

    ```bash
    yc managed-clickhouse ml-model update <model_name> \
      --cluster-name=<cluster_name> \
      --uri=<new_link_to_file_in_Object_Storage>
    ```

    You can get the model name with the [list of cluster models](#list), and the cluster name, with the [list of clusters in the folder](cluster-list.md#list-clusters).


- Terraform {#tf}

    1. Open the current Terraform configuration file with the infrastructure plan.

        For more on how to create such a file, see the [Creating a cluster](cluster-create.md) section.

    1. In the Managed Service for ClickHouse® cluster description, change the `uri` parameter value under `ml_model`:

        ```hcl
        resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
        ...
          ml_model {
            name = "<model_name>"
            type = "ML_MODEL_TYPE_CATBOOST"
            uri  = "<new_link_to_model_file_in_Object_Storage>"
          }
        }
        ```

    1. Make sure the settings are correct.

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Confirm resource changes.

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

    For more information, see [this Terraform provider guide](../../terraform/resources/mdb_clickhouse_cluster.md).

    {% note warning "Timeouts" %}
    
    The Terraform provider sets the following timeouts for Managed Service for ClickHouse® cluster operations:
    
    * Creating a cluster, including by restoring from a backup: 60 minutes.
    * Updating a cluster: 90 minutes.
    * Deleting a cluster: 30 minutes.
    
    Operations exceeding the timeout are aborted.
    
    {% cut "How to change these limits" %}
    
    Add the `timeouts` section to your cluster description, such as the following:
    
    ```hcl
    resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
      ...
      timeouts = {
        create = "1h30m" # 1 hour 30 minutes
        update = "2h"    # 2 hours
        delete = "30m"   # 30 minutes
      }
    }
    ```
    
    {% endcut %}
    
    {% endnote %}


- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [MlModel.Update](../api-ref/MlModel/update.md) method, e.g., via the following [cURL](https://curl.se/) request:

        {% note warning %}
        
        The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
        
        {% endnote %}

        ```bash
        curl \
            --request PATCH \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --header "Content-Type: application/json" \
            --url 'https://{{ api-host-mdb }/managed-clickhouse/v1/clusters/<cluster_ID>/mlModels/<model_name>' \
            --data '{
                      "updateMask": "uri",
                      "uri": "<file_link>"
                    }'
        ```

        Where:

        * `updateMask`: Comma-separated string of settings to update.

            Here, we only specified a single parameter, `uri`.

        * `uri`: Link to the new schema file in Object Storage.

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

    1. Check the [server response](../api-ref/MlModel/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
    1. Call the [MlModelService.Update](../api-ref/grpc/MlModel/update.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        {% note warning %}
        
        The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `update_mask` parameter as an array of `paths[]` strings.
        
        {% cut "Format for listing settings" %}
        
        ```yaml
        "update_mask": {
            "paths": [
                "<setting_1>",
                "<setting_2>",
                ...
                "<setting_N>"
            ]
        }
        ```
        
        {% endcut %}
        
        {% endnote %}

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/ml_model_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                    "cluster_id": "<cluster_ID>",
                    "ml_model_name": "<schema_name>",
                    "update_mask": {
                      "paths": ["uri"]
                    },
                    "uri": "<file_link>"
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.clickhouse.v1.MlModelService.Create
        ```

        Where:

        * `ml_model_name`: Model name.
        * `update_mask`: List of settings to update as an array of strings (`paths[]`).

            Here, we only specified a single parameter, `uri`.

        * `uri`: Link to the new schema file in Object Storage.

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

    1. View the [server response](../api-ref/grpc/MlModel/update.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

## Removing a model {#disable}

{% note info %}


After removing a model, the related object remains in the Yandex Object Storage bucket. If you no longer need this model object, you can [delete](../../storage/operations/objects/delete.md) it.


{% endnote %}

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
    1. Navigate to **Managed Service for&nbsp;ClickHouse**.
    1. Click the name of your cluster and select the **Machine learning** tab in the left-hand panel.
    1. Find your model, click ![image](../../_assets/console-icons/ellipsis-vertical.svg), and select **Delete**.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To remove a model, run this command:

    ```bash
    yc managed-clickhouse ml-model delete <model_name> \
      --cluster-name=<cluster_name>
    ```

    You can get the model name with the [list of cluster models](#list), and the cluster name, with the [list of clusters in the folder](cluster-list.md#list-clusters).


- Terraform {#tf}

    1. Open the current Terraform configuration file with the infrastructure plan.

        For more on how to create such a file, see the [Creating a cluster](cluster-create.md) section.

    1. Delete the section describing `ml_model` in question from the Managed Service for ClickHouse® cluster description.

    1. Make sure the settings are correct.

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Confirm resource changes.

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

    For more information, see [this Terraform provider guide](../../terraform/resources/mdb_clickhouse_cluster.md).

    {% note warning "Timeouts" %}
    
    The Terraform provider sets the following timeouts for Managed Service for ClickHouse® cluster operations:
    
    * Creating a cluster, including by restoring from a backup: 60 minutes.
    * Updating a cluster: 90 minutes.
    * Deleting a cluster: 30 minutes.
    
    Operations exceeding the timeout are aborted.
    
    {% cut "How to change these limits" %}
    
    Add the `timeouts` section to your cluster description, such as the following:
    
    ```hcl
    resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
      ...
      timeouts = {
        create = "1h30m" # 1 hour 30 minutes
        update = "2h"    # 2 hours
        delete = "30m"   # 30 minutes
      }
    }
    ```
    
    {% endcut %}
    
    {% endnote %}


- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [MlModel.Delete](../api-ref/MlModel/delete.md) method, e.g., via the following [cURL](https://curl.se/) request:

        ```bash
        curl \
            --request DELETE \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters/<cluster_ID>/mlModels/<model_name>'
        ```

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the model name, with the [list of models](#list) in the cluster.

    1. View the [server response](../api-ref/MlModel/delete.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
    1. Call the [MlModelService.Delete](../api-ref/grpc/MlModel/delete.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/ml_model_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                    "cluster_id": "<cluster_ID>",
                    "ml_model_name": "<schema_name>"
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.clickhouse.v1.MlModelService.Delete
        ```

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

    1. View the [server response](../api-ref/grpc/MlModel/delete.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}

## Example {#example-ml-model}

If you do not have an appropriate dataset and model to process it, you can test machine learning in Managed Service for ClickHouse® with this example. We prepared a data file and trained a model to analyze it. You can upload data to ClickHouse® and see model predictions for different table rows.

{% note info %}

In this example, we will be using public data from the [Amazon Employee Access Challenge](https://www.kaggle.com/c/amazon-employee-access-challenge). Our model is trained to predict values in the `ACTION` column. [GitHub](https://github.com/ClickHouse/clickhouse-presentations/blob/master/tutorials/catboost_with_clickhouse_ru.md) examples use the same data and model.

{% endnote %}

To upload data to ClickHouse® and test the model:

1. In the [management console](https://console.yandex.cloud), add the test model:

    * **Type**: `ML_MODEL_TYPE_CATBOOST`.
    * **Name**: `ml_test`.
    * **URL**: `https://storage.yandexcloud.net/managed-clickhouse/catboost_model.bin`.


1. [Download the data file](https://storage.yandexcloud.net/doc-files/managed-clickhouse/train.csv) to analyze.


1. [Connect to the cluster](connect/clients.md).

1. Create a test table:

    ```sql
    CREATE TABLE
                ml_test_table (date Date MATERIALIZED today(), 
                              ACTION UInt8, 
                              RESOURCE UInt32, 
                              MGR_ID UInt32, 
                              ROLE_ROLLUP_1 UInt32, 
                              ROLE_ROLLUP_2 UInt32, 
                              ROLE_DEPTNAME UInt32, 
                              ROLE_TITLE UInt32, 
                              ROLE_FAMILY_DESC UInt32, 
                              ROLE_FAMILY UInt32, 
                              ROLE_CODE UInt32) 
                ENGINE = MergeTree() 
    PARTITION BY date 
    ORDER BY date;
    ```

1. Upload the data to the table:

    ```sql
    INSERT INTO ml_test_table FROM INFILE '<file_path>/train.csv' FORMAT CSVWithNames;
    ```

1. Test the model:

    * Get the predicted values of the `ACTION` column for the first ten rows in the table:

        ```sql
        SELECT
            catboostEvaluate('/var/lib/clickhouse/models/ml_test.bin',
                            RESOURCE,
                            MGR_ID,
                            ROLE_ROLLUP_1,
                            ROLE_ROLLUP_2,
                            ROLE_DEPTNAME,
                            ROLE_TITLE,
                            ROLE_FAMILY_DESC,
                            ROLE_FAMILY,
                            ROLE_CODE) > 0 AS prediction,
            ACTION AS target
        FROM ml_test_table
        LIMIT 10;
        ```

    * Get the predicted probability for the first ten rows in the table:

        ```sql
        SELECT
            catboostEvaluate('/var/lib/clickhouse/models/ml_test.bin',
                            RESOURCE,
                            MGR_ID,
                            ROLE_ROLLUP_1,
                            ROLE_ROLLUP_2,
                            ROLE_DEPTNAME,
                            ROLE_TITLE,
                            ROLE_FAMILY_DESC,
                            ROLE_FAMILY,
                            ROLE_CODE) AS prediction,
            1. / (1 + exp(-prediction)) AS probability,
            ACTION AS target
        FROM ml_test_table
        LIMIT 10;
        ```

_ClickHouse® is a registered trademark of [ClickHouse, Inc](https://clickhouse.com)._