数据分析与挖掘(二):实现关联规则Aproiri算法应用、使用算法实现关联规则度量统计
一、实验目的及要求
安装函数包、调用函数实现关联规则Aproiri算法应用
使用算法实现关联规则度量统计,显示结果
二、实验设备(环境)及要求
Jupyter notebook、百度 AI studio
三、实验内容
1.调用函数实现对数据集数据的分析。
数据集:
[[’apple’,’banana’]
[’milk’,’bread’,’banana’,’ham’]
[’milk’,’bread’,’apple’,’banana’]
[’milk’,’apple’,’banana’,’ham’]
[’bread’,’banana’]]
(1) 实现基本步骤
安装函数包代码:
pip install apyori
调用函数代码段:
data_file = "goods.txt"# 接收要导入的文件
Data = np.loadtxt(data_file, delimiter=",")
min_supp = 0.5
min_conf = 0.5
min_lift = 0.0
res=apriori(transactions=Data,min_support=min_supp,min_confidence=min_conf,min_lift=min_lift)
显示结果代码段:
for rule in res:
print(str(rule))
print("data")
print(Data)
(2) 结果查看与分析
2.从文本获取数据,统计输出:
多少人买了牛奶
多少人买了面包
多少人既买了牛奶又买了面包
(1) 实施基本步骤
获取数据代码:
import numpy as np
from apyori import apriori
data_file = "goods.txt"# 接收要导入的文件
Data = np.loadtxt(data_file, delimiter=",")
统计代码:
num_milk = 0
num_bread = 0
num_milk_bread = 0
for sample in Data: # 取出数据中的每一行
if sample[0] == 1: # 检测sample[0]的值是否为1,即顾客是否购买牛奶
num_milk += 1
if sample[1] == 1: # 检测sample[1]的值是否为1,即顾客是否购买面包
num_bread += 1
if sample[0] == 1 and sample[1] == 1:
num_milk_bread += 1
print("{0} people bought milk".format(num_milk))
print("{0} people bought bread".format(num_bread))
print("{0} people bought both milk and bread".format(num_milk_bread))
(2) 结果查看与分析:
3.从文本获取数据,计算每条规则的支持度和置信度,置信度为float浮点型,输出保留3位小数。
(1) 基本步骤
导入数据:
import numpy as np
from apyori import apriori
data_file = "goods.txt"# 接收要导入的文件
Data = np.loadtxt(data_file, delimiter=",")
计算置信度:
from collections import defaultdict
features = ["milk", "bread", "apple", "banana", "ham"] # 存放商品名称
valid_rules = defaultdict(int) # 存放所有的规则应验的情况
invalid_rules = defaultdict(int) # 存放规则无效
num_occurances = defaultdict(int) # 存放条件相同的规则数量
for sample in Data: # 第一层循环:购买了X商品的作为前提条件
for premise in range(4):
if sample[premise] == 0:
continue # 没买当前商品,忽略以下内容,进入下一次循环
num_occurances[premise] += 1 # 买了X商品,又买了当前商品
for conclusion in range(premise, 5):
if premise == conclusion:
continue
if sample[conclusion] == 1:
valid_rules[(premise, conclusion)] += 1
else:
invalid_rules[(premise, conclusion)] += 1
support = valid_rules
confidence = defaultdict(int)
for premise, conclusion in valid_rules.keys():
confidence[premise, conclusion] = valid_rules[premise, conclusion]/num_occurances[premise]
def print_rule(premise, conclusion, support, confidence, features):
print("Rule: If a person buys " +
features[premise]+" they will also buy "+features[conclusion])
print("- Confidence: {0:.3f}".format(confidence[premise, conclusion]))
print("- Support: {0}".format(support[premise, conclusion]))
premise = int(input()) # 获取条件
conclusion = int(input()) # 获取结论
print_rule(premise, conclusion, support, confidence, features)
(2)结果查看与分析