Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การพล็อต Decision Boundaries ของ k-NN ใน Matplotlib

ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างรอบๆ subplots

  • กำหนดตัวแปร n_neighbors จำนวนเพื่อนบ้าน

  • โหลดชุดข้อมูล Iris (Classification)

  • สร้างข้อมูล x และ y (ใช้ 2 ฟีเจอร์แรก)

  • กำหนดรายการสีสำหรับพื้นที่และจุดข้อมูล

  • สร้าง Classifier k-Nearest Neighbors

  • คำนวณขอบเขต xmin, xmax, ymin, ymax

  • สร้าง Meshgrid สำหรับพื้นที่ตัดสินใจ

  • ทำนายคลาสสำหรับทุกจุดใน Meshgrid

  • สร้าง Contourf Plot สำหรับ Decision Boundaries

  • สร้าง Scatter Plot ของข้อมูลจริง

  • ตั้งค่า Label, Title, Scale ของแกน

  • แสดงรูปภาพด้วยเมธอด show()

ตัวอย่างโค้ด

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

n_neighbors = 15
iris = datasets.load_iris()
X = iris.data[:, :2]  # ใช้ 2 ฟีเจอร์แรก
y = iris.target

h = .02
cmap_light = ListedColormap(['orange', 'cyan', 'cornflowerblue'])
cmap_bold = ['darkorange', 'c', 'darkblue']

clf = neighbors.KNeighborsClassifier(n_neighbors, weights='uniform')
clf.fit(X, y)

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.figure()
plt.contourf(xx, yy, Z, cmap=cmap_light)
sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=iris.target_names[y],
                palette=cmap_bold, alpha=1.0, edgecolor="black")
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("3-Class classification (k = %i, 'uniform' = '%s')" % (n_neighbors, 'uniform'))
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
plt.show()

ผลลัพธ์

กราฟแสดง Decision Boundaries ของ k-NN (k=15) บนชุดข้อมูล Iris 2 ฟีเจอร์ พื้นที่สีแสดงคลาสที่ทำนายได้ จุดข้อมูลจริงทับอยู่บนพื้นที่

แก้ไข: โค้ดต้นฉบับมี plt.Show() (S ใหญ่) ควรเป็น plt.show() (s เล็ก)