連関と乖離を計算する
図書館から、Twilight を使って Harry Potter を宣伝するというあなたの提案について、再び連絡がありました。2つの本が乖離していると、プロモーションに悪影響が出るのではと心配しています。そこで、それが当てはまらないことを確認してほしいと依頼されました。
あなたがすぐに思い浮かべたのは Zhang の指標です。これは連関と乖離を連続的に測定し、連関は正、乖離は負になります。これまでの演習と同様に、DataFrame books はすでにインポートされており、numpy はエイリアス np で利用できます。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 で学ぶマーケットバスケット分析
演習の手順
- {Twilight} のサポートと {Potter} のサポートを計算します。
- {Twilight, Potter} のサポートを計算します。
- 分母の式を完成させます。
- {Twilight} \(\rightarrow\) {Potter} の Zhang の指標を計算します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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)