# Кластеризация

## Кластеризация областями

Области каждого из трёх (например) кластеров заданы математическими уравнениями equation1, equation2, equation3.

```python
def clustersA(df):
    clusters = [[], [], []]
    anomaly = []
    for x, y in df:
        if equation1:
            clusters[0].append([x, y])
        elif equation2:
            clusters[1].append([x, y])
        elif equation3:
            clusters[2].append([x, y])
        else:
            anomaly.append([x, y])
    return clusters, anomaly
```

## Алгоритм dbscan

#### Вход алгоритма

df — список объектов;

r — радиус объединения объектов в один кластер;

dist — функция расстояния между объектами;

min\_samples — минимальное количество объектов в одном кластере.

#### Выход алгоритма

clusters — список кластеров, каждый кластер — список объектов;

anomaly — список объектов, не вошедших ни в один кластер.

```python
def dbscan(df, r, dist, min_samples=1):
    clusters = []
    while len(df) > 0:
        clusters.append([df.pop()])
        for pc in clusters[-1]:
            for pd in df.copy():
                if dist(pd, pc) < r:
                    clusters[-1].append(pd)
                    df.remove(pd)
    anomaly = []
    for cluster in clusters.copy():
        if len(cluster) < min_samples:
            anomaly = anomaly + cluster
            clusters.remove(cluster)
    print(f"Кластеров\t{len(clusters)}")
    print(f"Аномалий\t{len(anomaly)}")
    return clusters, anomaly
```

## Диаграмма рассеяния

```python
from turtle import *
from random import randrange


def draw(clusters, s, w, h):
    screensize(2 * s * w, 2 * h * s)
    tracer(0)
    up()
    for cluster in clusters:
        color(f"#{hex(randrange(16**6))[2:].zfill(6)}")
        for x,y in  cluster:
            goto(x * s, y * s)
            dot()
    update()
    exitonclick()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://andrewmaths.gitbook.io/conspects/inf/klasterizaciya.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
