亲和性分析---根据样本个体(物体)之间的相似度,确定他们关系的亲疏,探索各变量间的关系。

一、应用场景:

1.向网站用户提供多样化的服务或投放定向广告

2.为了向用户推荐电影或商品,而卖给他们一些与之相关的小玩意

3.根据基因寻找有亲缘关系的人

测量方法:统计两件商品一起出售的频率,或者统计顾客购买了商品X后再买商品Y的比率,或者计算个体之间的相似度。

简单的规则排序:记一条规则为“如果一个人买了X,那么他很有可能购买Y。”找出规则后,还需要判 断其优劣,我们挑好的规则用。 规则的优劣有多种衡量方法,常用的是支持度(support)和置信度(confidence)。

支持度:支持度衡量的是给定规则应验的比例。

置信度:置信度衡量的则是规则准确率如何,即符合给定条

件(即规则的“如果”语句所表示的前提条件)的所有规则里,跟当前规则结论一致的比例有多

大。计算方法为首先统计当前规则的出现次数,再用它来除以条件(“如果”语句)相同的规则

数量。

二、以顾客购买面包、牛奶、奶酪、苹果和香蕉5种水果的交易记录为例。

1.加载数据集--affinity_dataset.txt

import numpy as np
dataset_filname = "affinity_dataset"
x = np.loadtxt(dataset_filname)
print(x[:5])

上述代码的运行结果为前5次交易中,顾客都买了什么。




容器 亲和性 具有亲和性_数据挖掘


affinity_dataset.txt内容如下:复制一下内容到txt新文件中即可。

0 0 1 1 1
1 1 0 1 0
1 0 1 1 0
0 0 1 1 1
0 1 0 0 1
0 1 0 0 0
1 0 0 0 1
1 0 0 0 1
0 0 0 1 1
0 0 1 1 1
1 1 0 0 1
0 1 0 0 0
0 0 0 0 1
0 0 1 0 1
0 1 0 0 1
0 0 1 1 1
1 0 0 0 1
0 0 1 1 1
1 1 0 0 0
0 1 0 0 0
0 0 1 0 0
0 1 0 0 1
0 1 0 0 0
0 1 0 0 1
0 0 1 1 1
0 0 1 1 0
0 0 1 0 1
0 0 0 0 1
0 1 0 0 0
0 1 0 1 0
1 1 1 0 1
1 1 0 0 1
0 0 1 1 1
0 0 1 0 1
0 0 1 1 1
0 0 1 1 0
0 1 1 0 1
0 0 1 1 0
0 1 0 0 1
0 0 0 0 1
0 0 1 0 1
1 1 0 1 1
1 0 0 0 1
0 0 1 1 1
0 1 0 0 0
0 1 0 1 1
0 1 0 0 0
0 1 0 0 0
0 0 1 1 0
0 0 1 1 1
0 1 0 1 0
0 1 1 0 0
0 0 1 1 0
0 0 1 1 1
1 0 0 0 0
0 1 0 1 0
1 0 0 0 1
0 1 0 0 0
0 0 0 0 1
0 0 1 1 1
0 1 1 1 1
1 1 0 0 0
0 0 1 0 1
1 0 0 0 1
1 1 0 0 0
0 1 1 0 0
0 0 0 0 1
0 1 0 0 0
0 0 1 1 1
0 1 0 0 1
1 0 0 0 1
1 0 0 0 1
0 1 0 0 1
0 0 1 1 1
1 0 1 0 1
1 1 0 0 1
0 1 0 0 1
1 1 1 0 1
0 0 1 1 1
1 0 0 0 0
0 0 1 1 1
1 1 0 1 0
0 0 1 0 0
0 0 1 0 1
0 1 0 0 0
1 1 0 0 0
0 0 0 1 0
0 0 0 1 1
0 1 0 0 0
0 1 0 0 0
1 1 0 0 1
0 0 1 0 0
0 1 0 0 1
1 1 0 1 0
1 0 0 0 1
0 1 0 0 0
0 0 1 1 0
0 1 1 0 0
0 0 1 1 0
0 0 0 0 1

2.如何根据数据集X知道顾客买了苹果?

num_apple_purchases = 0
for sample in x:
if sample[3] == 1:
        num_apple_purchases += 1
print("{} people bought Apples".format(num_apple_purchases))


容器 亲和性 具有亲和性_数据分析_02


3.计算"如果顾客买了苹果,也会购买香蕉"这条规则的支持度和置信度。

rule_valid = 0
rule_invalid = 0
for sample in x:
    if sample[3] == 1:
        if sample[4] == 1:
            rule_valid += 1
        else:
            rule_invalid += 1
surport = rule_valid
confidence = rule_valid / num_apple_purchases
print("如果顾客购买了苹果,他们也会购买香蕉这条规则的支持度和置信度:", surport, confidence)


容器 亲和性 具有亲和性_容器 亲和性_03


4.计算所有规则的支持度和置信度。

from collections import defaultdict
n_sample, n_feature = x.shape
valid_rules = defaultdict(int)
invalid_rules = defaultdict(int)
num_occurences = defaultdict(int)
for sample in x:
    for premise in range(n_feature):
        if sample[premise] == 0:  # 检查个体是否满足条件
            continue
        num_occurences[premise] += 1
        for conclusion in range(n_feature):
            if premise == conclusion:
                continue
            if sample[conclusion] == 1:
                valid_rules[(premise, conclusion)] += 1  # 规则应验
            else:
                invalid_rules[(premise, conclusion)] += 1  # 违反规则

# 所有规则的支持度字典=规则出现次数
surport = valid_rules
# 所有规则的置信度字典
confidence = defaultdict(float)
# 计算某一规则的置信度,并将其存在字典confidence中
for premise, conclusion in valid_rules.keys():
    confidence[(premise, conclusion)] = valid_rules[(premise, conclusion)] / num_occurences[premise]

5.打印所有规则,将规则用水果文字表达“如果顾客购买了A,他们也会购买B”

features = ["bread", "milk", "cheese", "apples", "bananas"]
for premise, conclusion in confidence:
    premise_name = features[premise]
    conclusion_name = features[conclusion]
    print("规则:如果一个人买了{0},那么他会买{1}".format(premise_name, conclusion_name))
    print('-支持度{0}'.format(surport[(premise, conclusion)]))
    print('-置信度{0:.2f}\n'.format(confidence[(premise, conclusion)]))


容器 亲和性 具有亲和性_数据分析_04


6.实现调用函数输出规则。

# 根据输入数据打印规则的置信度和支持度
def print_rule(premise, conclusion, surport, confidence, features):
    premise_name = features[premise]
    conclusion_name = features[conclusion]
    print("规则:如果一个人买了{0},那么他也会买{1}".format(premise_name, conclusion_name))
    print('-支持度{0}'.format(surport[(premise, conclusion)]))
    print('-置信度{0:.2f}\n'.format(confidence[(premise, conclusion)]))

# 调用函数并输出规则
print_rule(3, 4, surport, confidence, features)


容器 亲和性 具有亲和性_数据挖掘_05


7.规则排序。分别根据支持度和置信度找出排名前5的规则。

(1)按支持度排序,输出排名前5

# 按支持排序
# pprint()模块打印出来的数据结构更加完整,每行为一个数据结构,更加方便阅读打印输出结果
from pprint import pprint  # 导入pprit模块
pprint(list(surport.items()))  # 完整打印出list(surport.items())

# 对规则支持度进行排序,并输出排名前5的规则
sorted_surport = sorted(surport.items(), key=itemgetter(1), reverse=True)
for index in range(5):
    print("规则 #{0}".format(index + 1))
    (premise, conclusion) = sorted_surport[index][0]
    print_rule(premise, conclusion, surport, confidence, features)


容器 亲和性 具有亲和性_容器 亲和性_06


(2)按置信度排序,输出排名前5

pprint(list(confidence.items()))
sorted_confidence = sorted(confidence.items(), key=itemgetter(1), reverse=True)
for index in range(5):
    print("规则 #{0}".format(index + 1))
    (premise, conclusion) = sorted_confidence[index][0]
    print_rule(premise, conclusion, surport, confidence, features)


容器 亲和性 具有亲和性_容器 亲和性_07


8.结论

从排序结果来看,“顾客买苹果,也会买奶酪”和“顾客买奶酪,也会买香蕉”,这两条规则的支持度和置信度都很高。超市经理可以根据这些规则来调整商品摆放位置。