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

การนิยาม Zhang's metric

โดยทั่วไป เมื่อต้องการทำงานซ้ำหลายครั้ง เราจะเขียนเป็นฟังก์ชัน แทนที่จะเขียนโค้ดแยกสำหรับแต่ละกรณี ในแบบฝึกหัดนี้ เราจะนิยามฟังก์ชันสำหรับ Zhang's metric ที่รับ antecedent และ consequent เป็น input แล้วคืนค่า metric ออกมา เมื่อโจทย์มีความซับซ้อนมากขึ้นในบทถัดไป การมีฟังก์ชันสำเร็จรูปสำหรับคำนวณ metric จะช่วยให้สิ่งต่าง ๆ ง่ายขึ้นมาก

หมายเหตุ: numpy ถูก import เป็น np และ pandas ถูก import เป็น pd นอกจากนี้ สูตรของ Zhang's metric ในรูปของการคำนวณ support มีดังนี้:

$$Zhang(A \rightarrow B) = $$ $$\frac{Support(A \& B) - Support(A) Support(B)}{ max[Support(AB) (1-Support(A)), Support(A)(Support(B)-Support(AB))]}$$

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

Market Basket Analysis ด้วย Python

ดูคอร์ส

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

  • กำหนดค่า support ของ antecedent และ consequent แยกกัน
  • กำหนดค่า support ของ {antecedent, consequent}
  • เติมนิพจน์สำหรับ numerator และ denominator ให้ครบ
  • เติมนิพจน์สำหรับ Zhang's metric ให้ครบ

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Define a function to compute Zhang's metric
def zhang(antecedent, consequent):
	# Compute the support of each book
	supportA = antecedent.____
	supportC = consequent.____

	# Compute the support of both books
	supportAC = np.____(antecedent, consequent).____

	# Complete the expressions for the numerator and denominator
	numerator = supportAC - supportA*supportC
	denominator = ___(supportAC*(1-supportA), supportA*(supportC-supportAC))

	# Return Zhang's metric
	return numerator / denominator
แก้ไขและรันโค้ด