เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

ความสัมพันธ์ของข้อมูลในธรรมชาติ

กำหนดให้มีอาร์เรย์ grains ที่เก็บข้อมูลความกว้างและความยาวของตัวอย่างเมล็ดพืช คาดว่าความกว้างและความยาวน่าจะมีความสัมพันธ์กัน ให้สร้าง scatter plot ของความกว้างเทียบกับความยาว และวัดค่า Pearson correlation เพื่อยืนยันสมมติฐานนี้

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Unsupervised Learning ใน Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Import:
    • matplotlib.pyplot โดยตั้งชื่อแทนว่า plt
    • pearsonr จาก 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)
แก้ไขและรันโค้ด