preloader

Nearest Neighbors - Práctica de clasificación

Índice de contenido
python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets, neighbors
from sklearn.model_selection import train_test_split
python
iris = datasets.load_iris(as_frame = True)
iris.data

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2
...............
1456.73.05.22.3
1466.32.55.01.9
1476.53.05.22.0
1486.23.45.42.3
1495.93.05.11.8

150 rows × 4 columns

python
iris.target
0      0
1      0
2      0
3      0
4      0
      ..
145    2
146    2
147    2
148    2
149    2
Name: target, Length: 150, dtype: int32
python
df = pd.concat([iris.data, iris.target],
              axis = 1)
df

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)target
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20
..................
1456.73.05.22.32
1466.32.55.01.92
1476.53.05.22.02
1486.23.45.42.32
1495.93.05.11.82

150 rows × 5 columns

python
df.target.unique()
array([0, 1, 2])
python
sum(df.target == 2)
50
python
df = df.loc[df.target.isin([0, 1])]
df

sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)target
05.13.51.40.20
14.93.01.40.20
24.73.21.30.20
34.63.11.50.20
45.03.61.40.20
..................
955.73.04.21.21
965.72.94.21.31
976.22.94.31.31
985.12.53.01.11
995.72.84.11.31

100 rows × 5 columns

python
df = df[["sepal length (cm)", "sepal width (cm)", "target"]]
df

sepal length (cm)sepal width (cm)target
05.13.50
14.93.00
24.73.20
34.63.10
45.03.60
............
955.73.01
965.72.91
976.22.91
985.12.51
995.72.81

100 rows × 3 columns

python
df.columns
Index(['sepal length (cm)', 'sepal width (cm)', 'target'], dtype='object')
python
X = df.loc[:, df.columns != "target"]
X

sepal length (cm)sepal width (cm)
05.13.5
14.93.0
24.73.2
34.63.1
45.03.6
.........
955.73.0
965.72.9
976.22.9
985.12.5
995.72.8

100 rows × 2 columns

python
y = df.target
y
0     0
1     0
2     0
3     0
4     0
     ..
95    1
96    1
97    1
98    1
99    1
Name: target, Length: 100, dtype: int32
python
class_0 = df[df["target"] == 0]
class_0

sepal length (cm)sepal width (cm)target
05.13.50
14.93.00
24.73.20
34.63.10
45.03.60
55.43.90
64.63.40
75.03.40
84.42.90
94.93.10
105.43.70
114.83.40
124.83.00
134.33.00
145.84.00
155.74.40
165.43.90
175.13.50
185.73.80
195.13.80
205.43.40
215.13.70
224.63.60
235.13.30
244.83.40
255.03.00
265.03.40
275.23.50
285.23.40
294.73.20
304.83.10
315.43.40
325.24.10
335.54.20
344.93.10
355.03.20
365.53.50
374.93.60
384.43.00
395.13.40
405.03.50
414.52.30
424.43.20
435.03.50
445.13.80
454.83.00
465.13.80
474.63.20
485.33.70
495.03.30
python
class_1 = df[df["target"] == 1]
class_1

sepal length (cm)sepal width (cm)target
507.03.21
516.43.21
526.93.11
535.52.31
546.52.81
555.72.81
566.33.31
574.92.41
586.62.91
595.22.71
605.02.01
615.93.01
626.02.21
636.12.91
645.62.91
656.73.11
665.63.01
675.82.71
686.22.21
695.62.51
705.93.21
716.12.81
726.32.51
736.12.81
746.42.91
756.63.01
766.82.81
776.73.01
786.02.91
795.72.61
805.52.41
815.52.41
825.82.71
836.02.71
845.43.01
856.03.41
866.73.11
876.32.31
885.63.01
895.52.51
905.52.61
916.13.01
925.82.61
935.02.31
945.62.71
955.73.01
965.72.91
976.22.91
985.12.51
995.72.81
python
plt.scatter(class_0["sepal length (cm)"], class_0["sepal width (cm)"], color = "r", label = "class_0")
plt.scatter(class_1["sepal length (cm)"], class_1["sepal width (cm)"], color = "g", label = "class_1")
plt.xlabel("sepal length (cm)")
plt.ylabel("sepal width (cm)")
plt.legend()
<matplotlib.legend.Legend at 0xbc19f10>

png

python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42)
python
knn = neighbors.KNeighborsClassifier(n_neighbors= 5)
python
knn.fit(X_train, y_train)
KNeighborsClassifier()
python
y_predict = knn.predict(X_test)
python
np.vstack([y_test, y_predict]).T
array([[1, 1],
       [1, 1],
       [1, 1],
       [0, 0],
       [0, 0],
       [0, 0],
       [0, 0],
       [1, 1],
       [0, 0],
       [0, 0],
       [0, 0],
       [0, 0],
       [1, 1],
       [0, 0],
       [1, 1],
       [0, 0],
       [1, 1],
       [1, 1],
       [0, 0],
       [0, 0],
       [1, 1],
       [1, 1],
       [0, 0],
       [0, 0],
       [1, 1],
       [0, 0],
       [0, 0],
       [1, 1],
       [0, 0],
       [1, 1]])
python
knn.score(X_test, y_test)
1.0
python
score_list = []
for i in range(1, 15):
    knn_test = neighbors.KNeighborsClassifier(n_neighbors= i)
    knn_test.fit(X_train, y_train)
    score_list.append(knn_test.score(X_test, y_test))
score_list
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
python
plt.plot(range(1, 15), score_list)
plt.xlabel("Nº de vecinos")
plt.ylabel("Accuracy")
plt.show()

png

python
plt.scatter(class_0["sepal length (cm)"], class_0["sepal width (cm)"], color = "r", label = "class_0")
plt.scatter(class_1["sepal length (cm)"], class_1["sepal width (cm)"], color = "g", label = "class_1")
plt.scatter(4.8, 3.25, color = "b")
plt.scatter(6.5, 2.25, color = "b")
plt.scatter(5.6, 3.25, color = "b")

plt.xlabel("sepal length (cm)")
plt.ylabel("sepal width (cm)")
plt.legend()
<matplotlib.legend.Legend at 0xd00bfd0>

png

python
X_test.values
array([[6. , 2.7],
       [5.5, 2.3],
       [5.9, 3.2],
       [4.8, 3. ],
       [5.1, 3.8],
       [5.1, 3.4],
       [4.6, 3.6],
       [5.5, 2.4],
       [5.4, 3.7],
       [5.1, 3.5],
       [5.7, 3.8],
       [4.8, 3.1],
       [6.1, 2.8],
       [5.5, 4.2],
       [5.5, 2.6],
       [5. , 3.6],
       [6.8, 2.8],
       [6.7, 3. ],
       [4.8, 3. ],
       [5.4, 3.4],
       [5.7, 2.8],
       [5.6, 3. ],
       [5. , 3.4],
       [4.4, 3.2],
       [5.6, 2.5],
       [5.7, 4.4],
       [5. , 3.5],
       [5.7, 2.9],
       [4.9, 3.1],
       [6.3, 2.5]])
python
new_points = [[4.8, 3.25],
             [6.5, 2.25],
             [5.6, 3.25]]
python
knn.predict(new_points)
array([0, 1, 1])
comments powered by Disqus