แสดงสีหลักของภาพ
เราได้โหลดภาพต่อไปนี้โดยใช้ฟังก์ชัน imread() ของคลาส image ใน matplotlib

ในการแสดงสีหลักของภาพ ให้แปลงสีของจุดศูนย์กลางคลัสเตอร์กลับเป็นค่าดิบ แล้วแปลงให้อยู่ในช่วง 0-1 โดยใช้สูตรต่อไปนี้:
converted_pixel = standardized_pixel * pixel_std / 255
ค่า RGB ถูกเก็บไว้ใน DataFrame ชื่อ batman_df ส่วนค่า RGB ที่ผ่านการ scale แล้วถูกเก็บไว้ในคอลัมน์ scaled_red, scaled_blue และ scaled_green จุดศูนย์กลางคลัสเตอร์ถูกเก็บไว้ในตัวแปร cluster_centers ซึ่งได้จากการรันฟังก์ชัน kmeans() ด้วย 3 คลัสเตอร์
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์กลุ่มข้อมูลใน Python
คำแนะนำการฝึกหัด
- ดึงค่าส่วนเบี่ยงเบนมาตรฐานของแต่ละสีจาก DataFrame แล้วเก็บไว้ในตัวแปร
r_std,g_std,b_std - สำหรับจุดศูนย์กลางคลัสเตอร์แต่ละจุด ให้แปลงค่า RGB ที่ผ่านการ standardized แล้วให้เป็นค่าที่อยู่ในช่วง 0-1
- แสดงสีของจุดศูนย์กลางคลัสเตอร์
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Get standard deviations of each color
____, ____, ____ = batman_df[['red', 'green', 'blue']].___()
for cluster_center in cluster_centers:
scaled_r, scaled_g, scaled_b = cluster_center
# Convert each standardized value to scaled value
colors.append((
scaled_r * ____ / ____,
scaled_g * ____ / ____,
scaled_b * ____ / ____
))
# Display colors of cluster centers
plt.____(____)
plt.show()