Zhang の指標を定義する
一般に、同じ作業を何度も行う場合は、個別に毎回コードを書くのではなく、関数を作成します。この演習では、前件と後件を受け取り、Zhang の指標値を返す関数を定義します。次の章で問題がより複雑になるにつれて、指標を手軽に計算できる手段があると大いに役立ちます。
なお、numpy は np、pandas は pd としてインポートされています。さらに、サポートを用いて表した Zhang の指標の式は次のとおりです。
$$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 で学ぶマーケットバスケット分析
演習の手順
- 前件と後件のサポート値を個別に定義します。
- {antecedent, consequent} のサポートを定義します。
- 分子と分母の式を完成させます。
- Zhang の指標の式を完成させます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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