ความสัมพันธ์ของข้อมูลในธรรมชาติ
กำหนดให้มีอาร์เรย์ grains ที่เก็บข้อมูลความกว้างและความยาวของตัวอย่างเมล็ดพืช คาดว่าความกว้างและความยาวน่าจะมีความสัมพันธ์กัน ให้สร้าง scatter plot ของความกว้างเทียบกับความยาว และวัดค่า Pearson correlation เพื่อยืนยันสมมติฐานนี้
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Unsupervised Learning ใน Python
คำแนะนำการฝึกหัด
- Import:
matplotlib.pyplotโดยตั้งชื่อแทนว่าpltpearsonrจากscipy.stats
- กำหนดให้คอลัมน์
0ของgrainsเป็นwidthและคอลัมน์1เป็นlength - สร้าง scatter plot โดยให้
widthอยู่บนแกน x และlengthอยู่บนแกน y - ใช้ฟังก์ชัน
pearsonr()เพื่อคำนวณค่า Pearson correlation ของwidthและlength
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Perform the necessary imports
____
____
# Assign the 0th column of grains: width
width = ____
# Assign the 1st column of grains: length
length = ____
# Scatter plot width vs length
plt.scatter(____, ____)
plt.axis('equal')
plt.show()
# Calculate the Pearson correlation
correlation, pvalue = ____
# Display the correlation
print(correlation)