定义 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