当前位置:   article > 正文

数据分析与挖掘(二):实现关联规则Aproiri算法应用、使用算法实现关联规则度量统计

数据分析与挖掘(二):实现关联规则Aproiri算法应用、使用算法实现关联规则度量统计

数据分析与挖掘(二):实现关联规则Aproiri算法应用、使用算法实现关联规则度量统计

一、实验目的及要求

安装函数包、调用函数实现关联规则Aproiri算法应用

使用算法实现关联规则度量统计,显示结果

二、实验设备(环境)及要求

Jupyter notebook、百度 AI studio

三、实验内容

1.调用函数实现对数据集数据的分析。

数据集:

[[’apple’,’banana’]

[’milk’,’bread’,’banana’,’ham’]

[’milk’,’bread’,’apple’,’banana’]

[’milk’,’apple’,’banana’,’ham’]

[’bread’,’banana’]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

(1) 实现基本步骤

安装函数包代码:

pip install apyori
  • 1

调用函数代码段:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

显示结果代码段:

for rule in res:

print(str(rule))

print("data")

print(Data)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(2) 结果查看与分析

在这里插入图片描述

2.从文本获取数据,统计输出:

多少人买了牛奶

多少人买了面包

多少人既买了牛奶又买了面包

(1) 实施基本步骤

获取数据代码:

import numpy as np

from apyori import apriori

data_file = "goods.txt"# 接收要导入的文件

Data = np.loadtxt(data_file, delimiter=",")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

统计代码:

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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

(2) 结果查看与分析:

在这里插入图片描述

3.从文本获取数据,计算每条规则的支持度和置信度,置信度为float浮点型,输出保留3位小数。

(1) 基本步骤

导入数据:

import numpy as np

from apyori import apriori

data_file = "goods.txt"# 接收要导入的文件

Data = np.loadtxt(data_file, delimiter=",")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

计算置信度:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

(2)结果查看与分析
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/206226
推荐阅读
相关标签
  

闽ICP备14008679号