Help us understand the problem. What is going on with this article?

# ｋ近傍法の原理とpythonでの実装

More than 1 year has passed since last update.

# pythonでの実装

## データの生成

#### 先ほどの原理をそのままコードにする。

knn.py
```class KNN2d:

def knn2d(self, x, y, k):
num = x.shape[0]
ipsilon_list = []
for j in range(num):
l_list = [] #k番目のデータに対するその他のデータの距離
for i in range(num):
xl = x[i] - x[j]
yl = y[i] - y[j]
l2 = (xl) ** 2 + (yl) ** 2
l = l2 ** 0.5
l_list.append(l)
l_li = np.array(l_list)
l_li = np.sort(l_li)
ipsilon_list.append(l_li[k])
abnormals = np.array(ipsilon_list)

return abnormals/10

def abnormal_decision(self, abnormals, treshold):
result_list = []
num = abnormals.shape[0]
for i in range(num):
abnormal = abnormals[i]
if abnormal > treshold:
result_list.append(i)
return result_list

```

## 判定結果

#### 閾値εを手動で設定しないといけないなど、実用するには改良しないとだけど、イメージはつかめた。

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away