ตรวจสอบผลการจัดกลุ่ม
ตอนนี้มาตรวจสอบผลการจัดกลุ่มที่ทำไว้ในแบบฝึกหัดก่อนหน้ากัน
โค้ดจากแบบฝึกหัดก่อนหน้าได้รันไปแล้ว ดังนั้น new_points จึงเป็น array ของจุดข้อมูล และ labels คือ array ของป้ายกำกับคลัสเตอร์ของแต่ละจุด
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Unsupervised Learning ใน Python
คำแนะนำการฝึกหัด
- นำเข้า
matplotlib.pyplotโดยใช้ชื่อแทนว่าplt - กำหนดให้
xsเท่ากับคอลัมน์0ของnew_pointsและysเท่ากับคอลัมน์1ของnew_points - สร้าง scatter plot จาก
xsและysโดยระบุ keyword argumentc=labelsเพื่อกำหนดสีของจุดตามป้ายกำกับคลัสเตอร์ และระบุalpha=0.5ด้วย - คำนวณพิกัดของจุด centroid โดยใช้ attribute
.cluster_centers_ของmodel - กำหนดให้
centroids_xเท่ากับคอลัมน์0ของcentroidsและcentroids_yเท่ากับคอลัมน์1ของcentroids - สร้าง scatter plot จาก
centroids_xและcentroids_yโดยใช้'D'(รูปทรงเพชร) เป็น marker ผ่านพารามิเตอร์markerและกำหนดขนาด marker เป็น50โดยใช้s=50
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import pyplot
____
# Assign the columns of new_points: xs and ys
xs = ____
ys = ____
# Make a scatter plot of xs and ys, using labels to define the colors
____
# Assign the cluster centers: centroids
centroids = ____
# Assign the columns of centroids: centroids_x, centroids_y
centroids_x = centroids[:,0]
centroids_y = centroids[:,1]
# Make a scatter plot of centroids_x and centroids_y
____
plt.show()