Boolean Indexing และการสร้างกราฟด้วย Matplotlib
มาดูกันว่า Boolean indexing ช่วยให้สำรวจข้อมูลในเชิงภาพได้อย่างไรด้วยโค้ดเพียงไม่กี่บรรทัด ในแบบฝึกหัดนี้ จะได้ฝึกทักษะหลายอย่างที่เรียนมา ไม่ว่าจะเป็นการแปลงข้อมูลจาก dictionary ให้เป็น DataFrame ของ pandas การกรองข้อมูลด้วย Boolean และการใช้ matplotlib แสดงผลข้อมูลเพื่อค้นหาความสัมพันธ์ในชุดข้อมูลการชนของสัตว์ป่า
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Python สำหรับผู้ใช้ MATLAB
คำแนะนำการฝึกหัด
- แปลง dictionary
strikesให้เป็น DataFrame - สร้าง Boolean filter สำหรับ
'Turbofan'ในคอลัมน์'Engine' - สร้าง Boolean filter สำหรับ
'Turboprop'ในคอลัมน์'Engine' - พล็อต scatter plot สองกราฟโดยใช้
turbofanและturbopropเพื่อกรองชุดข้อมูลstrikes
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create a dictionary and then a DataFrame from the dictionary
strikes = {'Date': date,'Speed': speed,'Height':height,'Engine':engine}
strikes = pd.____(strikes)
# Filter strikes by engine type
turbofan = strikes['Engine']=='____'
turboprop = strikes['____']=='____'
# Create scatter plot of speed and height for each engine type
plt.scatter(strikes[____]['Speed'],strikes[____]['Height'],label='Turbofan')
plt.scatter(strikes[____]['Speed'],strikes[____]['Height'],label='Turboprop')
plt.legend()
plt.xlabel('Strike speed (knots)')
plt.ylabel('Strike height (feet)')
plt.show()