计算关联与背离
图书馆又来咨询您关于用《Twilight》(《暮光之城》)带动《Harry Potter》(《哈利·波特》)推广的建议。他们担心两者可能存在背离关系,这会对推广产生负面影响。他们请您验证并非如此。
您立刻想到 Zhang 指标,它能够连续刻画关联与背离:关联为正,背离为负。与之前的练习一样,已为您导入了 DataFrame books,以及别名为 np 的 numpy。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)