# kNN

\#### k-Nearest Neighbours (kNN)

\*\*k-nearest neighbors algorithm (k-NN)\*\* 可能是最簡單的機器學習演算法，但是在許多場合還是很好用。

* 不預設資料的分佈型態。
* 如果很難定義概念，但是知道資料何時出現時。
* 利用資料的「同類相聚」特徵，classifying unlabeled examples by assigning them the class of similar labeled examples.

\`\`\`

\# Import required library

library(knn)

\# Fitting model

fit <-knn(y\_train \~ ., data = x, k = 4)

summary(fit)

\# Predict Output

predicted <- predict(fit, x\_test)

\`\`\`


---

# 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://lab-of-ontologies-language-proce.gitbook.io/ladsbook/partiji-chu-zhi-shi/machine_learning/knn.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.
