解釈度が高い最近傍法で分析してみた

#最近傍法

- ある入力xが与えられた時、その値に最も近いデータを予測値とする手法。

- 最も単純で基本的な**分類**アルゴリズム

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/d82b4353-2369-4812-678a-d5af6600e886.png)

#k最近傍法

- 最近傍法の**改良アルゴリズム**

- ある入力xが与えられた時、それに最も近いk個のデータの平均を予測値とする。

- 「最も近い」という距離は、ユークリッド距離によって測る 

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/570e1f00-260f-2fb9-546b-e596f0a0ce47.png)


#データを入手
```py
import mglearn
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
#matplotlib inline

X,y=mglearn.datasets.make_forge()
```

#データの内容を理解
```py
X
```

<img width="150" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/40de05e8-a52e-c328-9564-d88292e0313b.png" class="img" alt="データ">

```py
X.shape
```
<img width="300" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/4c759459-2817-0641-81a0-529ed5d80cc2.png" class="img" alt="データ可視化">



```py
#データの可視化
mglearn.discrete_scatter(X[:,0],X[:,1],y)
plt.show()
```
<img width="300" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/28875955-fc0e-dd8a-3376-129d2c4a01e9.png" class="img" alt="データ可視化">

#モデル構築
```py
#k最近傍法を使う
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
```

```py
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0)
```

```py
X_train.shape
```

<img width="200" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/c233efa3-be38-68f9-8992-e1ffde3e1b73.png" class="img" alt="データ">

```py
X_test.shape
```
<img width="150" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/cbd23bf1-1c39-14c5-761c-0af861d0c760.png" class="img" alt="データ">

```py
clf=KNeighborsClassifier(n_neighbors=3) #モデルの生成
clf.fit(X_train,y_train) #モデルの学習
```

```py
clf.predict(X_test) #予測結果
```

<img width="300" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/7ca739bb-237e-2aac-5dc7-3c42f62a1b44.png" class="img" alt="データ">




```py
y_test #実際の結果(一つだけ違うことが分かる)
```
<img width="300" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/b7b32e0d-ce53-bcad-4fa4-8b7e054e3063.png" class="img" alt="データ">


```py
#正解率
clf.score(X_test,y_test)
```
<img width="300" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/38479530-b0cc-1090-1032-b9ba1e9c0399.png" class="img" alt="データ">

```py
for n_neighbors in range(1,16):
    clf=KNeighborsClassifier(n_neighbors=n_neighbors).fit(X_train,y_train)
    print("Test set accuracy : n_neighbors={}, {:.2f}" .format(n_neighbors,clf.score(X_test,y_test)))
```
<img width="300" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/bbe4c832-5010-9076-5713-5185394d5dac.png" class="img" alt="データ">


```py
#グリッドサーチでハイパーパラメータを最適化
fig, axes = plt.subplots(1,5,figsize=(15,3)) #figはウィンドウ,axesはブラウザみたいなもの

for n_neighbors, ax in zip([1,3,5,10,15], axes):
    clf=KNeighborsClassifier(n_neighbors=n_neighbors).fit(X_train,y_train)
    mglearn.plots.plot_2d_separator(clf,X,fill=True,ax=ax,alpha=0.5) #fillは領域に色を付けるか否か 
    mglearn.discrete_scatter(X[:,0],X[:,1],y,ax=ax)
    ax.set_title("{} neighbors".format(n_neighbors))
    
plt.show()
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/449715/c5daea30-694d-a74d-2371-5638f9a67bef.png)