# K-Means Clustering (ML\_CG\_2)

K-Means Clustering is a machine learning algorithm used for grouping similar data points into K clusters. It randomly selects K cluster centers and assigns each data point to the nearest center based on a distance metric. It then updates the cluster centers based on the mean of the assigned data points, and repeats the assignment and update steps until convergence.

The algorithm can converge to a local minimum, so it is often run multiple times with different initial cluster centers to improve the chances of finding the optimal solution.

## Sample Request

Find the optimal number of clusters&#x20;

```javascript
{
    "project_id": 12,
    "parent_id": 2,
    "block_id": 3,
    "function_code": "ML_CG_2_F",
    "args": {
        "n_clusters": 11,
        "init": "k-means++",
        "random_state": 42
    }
}
```

<mark style="color:green;">`POST`</mark> `https://autogon.ai/api/v1/engine/start`

#### Request Body

| Name                                             | Type   | Description                            |
| ------------------------------------------------ | ------ | -------------------------------------- |
| project\_id<mark style="color:red;">\*</mark>    | int    | The `id` of the current project        |
| parent\_id<mark style="color:red;">\*</mark>     | int    | The `id` of the previous block         |
| block\_id<mark style="color:red;">\*</mark>      | int    | The `id` of the current block          |
| function\_code<mark style="color:red;">\*</mark> | string | The function code of the current block |
| n\_clusters<mark style="color:red;">\*</mark>    | int    | numbers of iterations to make          |
| init<mark style="color:red;">\*</mark>           | string | initialization method to use           |
| random\_state                                    | String | control algorithm randomness           |

{% tabs %}
{% tab title="Python" %}

```
// Some code
```

{% endtab %}

{% tab title="Node" %}

```javascript
const project_id = 1
const parent_id = 7
const block_id = 8


kmeansClusteringFindClusters   = await client.kmeans_clustering_find_clusters(project_id, parent_id, block_id, {
    n_clusters: 11,
    init: "k-means++",
    random_state: 42
});
```

{% endtab %}
{% endtabs %}

## Sample Request

Build a K-Means Clustering model named, *"ClusterModel"*

```javascript
{
    "project_id": 1,
    "parent_id": 8,
    "block_id": 9,
    "function_code": "ML_CG_2",
    "args": {
        "model_name": "ClusterModel",
        "n_clusters": 11,
        "init": "k-means++",
        "random_state": 42
    }
}
```

## Building a K-Means Clustering model

## K-Means Clustering

<mark style="color:green;">`POST`</mark> `https://autogon.ai/api/v1/engine/start`

#### Request Body

| Name                                             | Type   | Description                                  |
| ------------------------------------------------ | ------ | -------------------------------------------- |
| project\_id<mark style="color:red;">\*</mark>    | int    | The `id` of the current project              |
| block\_id<mark style="color:red;">\*</mark>      | int    | The `id` of the current block                |
| function\_code<mark style="color:red;">\*</mark> | string | The function code for current block          |
| parent\_id<mark style="color:red;">\*</mark>     | int    | The `id` of the previous block               |
| args<mark style="color:red;">\*</mark>           | object | Block arguments                              |
| model\_name<mark style="color:red;">\*</mark>    | String | Name of the model to be used for prediction. |

{% tabs %}
{% tab title="200 Statemanagement object" %}

```javascript
{
    "status": "true",
    "message": {
        "id": 8,
        "project": 1,
        "block_id": 8,
        "parent_id": 7,
        "dataset_url": "",
        "x_value_url": "",
        "y_value_url": "",
        "x_train_url": "",
        "y_train_url": "",
        "x_test_url": "",
        "y_test_url": "",
        "output": "{\"ClusterModel\": {\"function_code\": \"ML_R_3\", \"model_url\": ""}}"
    }
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Python" %}

```
// Some code
```

{% endtab %}

{% tab title="Node" %}

```javascript
const project_id = 1
const parent_id = 7
const block_id = 8

kmeansClustering = await client.kmeans_clustering(project_id, parent_id, block_id, {
    model_name: "ClassicModel",
    n_clusters: 5,
    affinity: "euclidean",
    linkage: "ward"
});
```

{% endtab %}
{% endtabs %}

## Sample Request

Make predictions with the pre-built model passing an optional test data.

```javascript
{
    "project_id": 1,
    "parent_id": 9,
    "block_id": 10,
    "function_code": "ML_CG_2_P",
    "args": {
        "model_name": "ClusterModel",
        "test_data": ""
    }
}
```

## Predicting with K-Means Clustering

## K-Means Clustering Predict

<mark style="color:green;">`POST`</mark> `https://autogon.ai/api/v1/engine/start`

#### Request Body

| Name                                             | Type   | Description                                                            |
| ------------------------------------------------ | ------ | ---------------------------------------------------------------------- |
| model\_name<mark style="color:red;">\*</mark>    | String | Name of previously trained model to be used for prediction             |
| test\_data                                       | String | Input data for prediction. Defaults to `x_train_url` in StateManagment |
| project\_id<mark style="color:red;">\*</mark>    | int    | ID of the current project                                              |
| block\_id<mark style="color:red;">\*</mark>      | int    | ID of the current block                                                |
| parent\_id<mark style="color:red;">\*</mark>     | int    | ID of the previous block                                               |
| function\_code<mark style="color:red;">\*</mark> | String | Function code for the current block                                    |

{% tabs %}
{% tab title="200: OK Statemanagement object" %}

```javascript
{
    "status": "true",
    "message": {
        "id": 9,
        "project": 1,
        "block_id": 9,
        "parent_id": 8,
        "dataset_url": "",
        "x_value_url": "",
        "y_value_url": "",
        "x_train_url": "",
        "y_train_url": "",
        "x_test_url": "",
        "y_test_url": "",
        "output": "{\"y_pred_url\": ""}"
    }
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Python" %}

```
// Some code
```

{% endtab %}

{% tab title="Node" %}

```javascript
const project_id = 1
const parent_id = 7
const block_id = 8

kmeansClusteringPredict = await client.kmeans_clustering_predict(project_id, parent_id, block_id, {
    model_name: "SimpleModel",
    test_data: ""
});
```

{% endtab %}
{% endtabs %}
