การคำนวณ association และ dissociation
ห้องสมุดกลับมาหาคุณอีกครั้งเกี่ยวกับคำแนะนำที่ให้โปรโมต Harry Potter ควบคู่กับ Twilight พวกเขากังวลว่าหนังสือทั้งสองเล่มอาจมีความสัมพันธ์แบบตรงข้าม (dissociation) ซึ่งอาจส่งผลเสียต่อแคมเปญโปรโมชัน จึงขอให้คุณตรวจสอบว่าเป็นเช่นนั้นหรือไม่
คุณนึกถึง Zhang's metric ซึ่งวัดความสัมพันธ์ได้อย่างต่อเนื่อง ค่าที่เป็นบวกหมายถึง association ส่วนค่าที่เป็นลบหมายถึง dissociation เช่นเดียวกับแบบฝึกหัดก่อนหน้า DataFrame books ถูกนำเข้ามาให้แล้ว พร้อมกับ numpy ในชื่อ alias np โดย Zhang's metric คำนวณได้ดังนี้:
$$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 ของ {Twilight} และ support ของ {Potter}
- คำนวณ support ของ {Twilight, Potter}
- เติมนิพจน์สำหรับตัวหารให้สมบูรณ์
- คำนวณ Zhang's metric สำหรับ {Twilight} \(\rightarrow\) {Potter}
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Compute the support of Twilight and Harry Potter
supportT = books['Twilight'].____
supportP = books['Potter'].____
# Compute the support of both books
supportTP = ____.mean()
# Complete the expressions for the numerator and denominator
numerator = supportTP - supportT*supportP
denominator = ___(supportTP*(1-supportT), supportT*(supportP-supportTP))
# Compute and print Zhang's metric
zhang = ____ / ____
print(zhang)