Zhang का metric परिभाषित करना
अक्सर, जब हमें कोई काम कई बार करना होता है, तो हम हर instance को अलग-अलग कोड करने के बजाय एक फंक्शन लिखते हैं। इस अभ्यास में, हम Zhang के metric के लिए एक फंक्शन परिभाषित करेंगे जो antecedent और consequent लेता है और metric को आउटपुट करता है। अगले अध्याय में समस्याएँ अधिक जटिल होंगी, तब किसी metric की गणना का आसान तरीका होना चीजों को काफी सरल बना देगा.
ध्यान दें कि numpy को np के रूप में और pandas को pd के रूप में इम्पोर्ट किया गया है। साथ ही, support गणनाओं के संदर्भ में Zhang के 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))]}$$
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Market Basket Analysis
अभ्यास निर्देश
- antecedent और consequent के support मानों को अलग-अलग परिभाषित करें.
- {antecedent, consequent} का support परिभाषित करें.
- numerator और denominator के लिए अभिव्यक्तियाँ पूरी करें.
- Zhang के 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