def association_rules(df, minSup=0.08, minConf=0.4, Y=None):
    '关联规则分析\n    df： DataFrame，bool 类型。是一个类似购物篮数据   \n\n    '
    try:
        df = df.astype(bool)
    except:
        print('df 必须为 bool 类型')
        return (None, None, None)
    columns = np.array(df.columns)
    gen = associate.frequent_itemsets(np.array(df), minSup)
    itemsets = dict(gen)
    rules = associate.association_rules(itemsets, minConf)
    rules = pd.DataFrame(list(rules))
    if (len(rules) == 0):
        return (None, None, None)
    rules.columns = ['antecedent', 'consequent', 'sup', 'conf']
    rules['sup'] = (rules['sup'] / len(df))
    rules['antecedent'] = rules['antecedent'].map((lambda x: [columns[i] for i in list(x)]))
    rules['consequent'] = rules['consequent'].map((lambda x: [columns[i] for i in list(x)]))
    rules['rule'] = ((rules['antecedent'].map((lambda x: ','.join([('%s' % i) for i in x]))) + '-->') + rules['consequent'].map((lambda x: ','.join([('%s' % i) for i in x]))))
    result = ';\n'.join(['{}:  支持度={:.1f}%, 置信度={:.1f}%'.format(rules.loc[(ii, 'rule')], (100 * rules.loc[(ii, 'sup')]), (100 * rules.loc[(ii, 'conf')])) for ii in rules.index[:4]])
    return (result, rules, itemsets)
关联规则分析
df： DataFrame，bool 类型。是一个类似购物篮数据
def association_rules(df, minSup=0.08, minConf=0.4, Y=None):
    '关联规则分析\n    df： DataFrame，bool 类型。是一个类似购物篮数据   \n\n    '
    try:
        df = df.astype(bool)
    except:
        print('df 必须为 bool 类型')
        return (None, None, None)
    columns = np.array(df.columns)
    gen = associate.frequent_itemsets(np.array(df), minSup)
    itemsets = dict(gen)
    rules = associate.association_rules(itemsets, minConf)
    rules = pd.DataFrame(list(rules))
    if (len(rules) == 0):
        return (None, None, None)
    rules.columns = ['antecedent', 'consequent', 'sup', 'conf']
    rules['sup'] = (rules['sup'] / len(df))
    rules['antecedent'] = rules['antecedent'].map((lambda x: [columns[i] for i in list(x)]))
    rules['consequent'] = rules['consequent'].map((lambda x: [columns[i] for i in list(x)]))
    rules['rule'] = ((rules['antecedent'].map((lambda x: ','.join([('%s' % i) for i in x]))) + '-->') + rules['consequent'].map((lambda x: ','.join([('%s' % i) for i in x]))))
    result = ';\n'.join(['{}:  支持度={:.1f}%, 置信度={:.1f}%'.format(rules.loc[(ii, 'rule')], (100 * rules.loc[(ii, 'sup')]), (100 * rules.loc[(ii, 'conf')])) for ii in rules.index[:4]])
    return (result, rules, itemsets)