Home>

Read machine learning starting with Python and use your own data
I did k-neighbors, but when I look at the image, the scatter plot is correct,
The area is strange and it seems that I haven't learned well.

What are the possibilities?

Error message

The k-neighbor method is not working.

Applicable source code

from sklearn.neighbors import KNeighborsClassifier

fig, axes = plt.subplots (1,3, figsize = (10,3))

for n_neighbors, ax in zip ([1,2,20], axes):
clf = KNeighborsClassifier (n_neighbors = n_neighbors)
clf.fit (X, y)
mglearn.plots.plot_2d_separator (clf, X, fill = True, eps = 0.5, ax = ax, alpha = .4)
mglearn.discrete_scatter (X [:, 0], X [:, 1], y, ax = ax)
ax.set_title ("{} neighbor (s)". format (n_neighbors))
ax.set_xlabel ("feature 0")
ax.set_ylabel ("feature 1")
axes [0] .legend (loc = 3)

Supplemental information (FW/tool version etc.)

print (X.shape)
print (y.shape)
print (X [0: 5])
When print (y [0: 5]) is executed, it will be as follows.
(50, 2)
(50,)
[[438 187]
[564 153]
[688 185]
[418 166]
[1068 162]]
[0 0 1 0 1]

  • Answer # 1

    Self-solved. It seems that the two feature ranges are different.
    If you normalized, it worked.

Related articles