> For the complete documentation index, see [llms.txt](https://delphai.gitbook.io/delphai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://delphai.gitbook.io/delphai/documentacao/images-and-media/agrupamento/k-means.md).

# K-Means

O K-Means é um algoritmo de agrupamento que divide os dados em "K" grupos ou clusters. Ele começa selecionando K pontos, chamados de centróides, que são escolhidos aleatoriamente. Em seguida, cada ponto do conjunto de dados é atribuído ao centróide mais próximo. Após essa etapa, os centróides são atualizados para refletir a posição média de todos os pontos atribuídos a eles. Esse processo é repetido até que os centróides parem de mudar significativamente ou até atingir um número máximo de iterações. É amplamente usado por sua simplicidade, mas pode ter dificuldades com conjuntos de dados mais complexos, como aqueles com formas não esféricas ou tamanhos variados.

#### EXEMPLO DE USO

```
uses
  UKMeans;
  
procedure ExampleClustering;
begin
  ShowArray(KMeans('C:\Delphai\Delphai\Datasets\Iris-Clustering.csv', 3, 500, 42));
end;
```

#### GUIA DE CLASSES E MÉTODOS

* *function KMeans(aData: String; aK, aMaxIterations, aNumInitializations: Integer; aHasHeader : Boolean = True): TArray;* ou
* *function KMeans(aData: TDataSet; aK, aMaxIterations, aNumInitializations: Integer): TArray;* : define os grupos.
  * *aData* : caminho do arquivo CSV ou objeto TDataSet que contém os dados que serão usados para treinamento.
  * *aK (Número de Clusters)* : define a quantidade de agrupamentos desejados no conjunto de dados.
  * *aMaxIterations (Número Máximo de Iterações)* : limita o número de ciclos para ajustar clusters e centróides.
  * *aNumInitializations (Número de Inicializações)* : especifica quantas execuções com inicializações diferentes serão realizadas.
  * *aHasHeader* : indica se tem cabeçalho no arquivo.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://delphai.gitbook.io/delphai/documentacao/images-and-media/agrupamento/k-means.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.
