关联规则算法实现
一、实验目的
1. 了解编写数据挖掘算法的一般过程;
2. 理解关联规则生成算法;
3. 掌握生成频繁项集的 Apriori 算法;
4. 掌握由频繁项集生成关联规则的方法。
二、实验环境
windows 操作系统,一种程序设计语言开发环境。
三、实验内容及步骤
1、基于模拟数据集,实现 Apriori 算法以获得频繁项集。
2、基于上一步得到的频繁项集,编写算法得到关联规则。
3.实验最后生成的 Apriori 算法的演示程序如下图所示:
输入最小的支持度阈值为 1 生成频繁项集
输入最小可信度的值生成关联规则值:
四、实验中的问题和心得
在实验过程中,遇到了不少的问题,比如说模拟数据集的输入(本人通过一个二维数组
将其解决)。通过此次实验,我也从中掌握了不和知识,比如加深了对 Apriori 算法的理解。
通过此次算法,我也设想过如何编写数据挖掘的一般过程,对其也有了更深层次的认识。
五、回答问题
1. 请设置不同的最小支持度阈值,观察得到的频繁项集的数目,说说频繁项集与最小
支持度阈值之间的关系。
答:输入最小支持度阈值为 2 时,其生成的频繁项集如下:
最小支持度阈值表示数据项集在统计意义上的最低主要性,小于最低支持度的数据项将
会被丢弃,将影响频繁项集的结果。
2. 请设置不同的最小可信度阈值,观察得到的关联规则的数目,说说关联规则与最小
可信度阈值之间的关系。
答:输入的最小可信度的值为 1,生成的关联规则如下:
最小可信度阈值表示规则的最低可靠性,小于该设定的可靠性值的规则将会被丢弃。
3. 详细介绍各算法的流程图和所用到的数据结构,并附带源代码(源代码中应有必要
的注释信息)。
答:(1)算法用到的数据结构:哈希表和二维数组。
(2)算法的伪代码描述如下:
输入:交易数据库 D;最小支持度阈值 min_sup。
输出:D 中的频繁项集 L。
方法:
(1) L1=find_frequent_1_itemset(D);找频繁项集 1-项集;
(2) for ( k=2; Lk-1X <; min_sup)
{
apriori_gen(Lk-1,min_sup) 连接和剪枝。用于在
for each t|
D 扫描数据库,确定每个候选项集的支持频度
第 k-1 次遍历中生成的 Lk-1 生成 Ck
{
Ct=subset(Ck ,t)获得 t 所包含的候选项集
for
each
cCt
c.count++;
} }
(3) Lk={ c Ck | c.count > min_sup }由 Ck 生成 Lk
(4) return
L=L1 ∪ L2 …. ∪ Lk
procedure apriori_gen(Lk-1,min_sup)
{
for each l1 Lk-1
for each l2 Lk-1
{ if(l1[1]=l2[1] ∧… ∧ l1[k-2]=l2[k-2] ∧
l1[k-1] < l2[k-1] )
c=l1 l2; 将两个项集连接在一起
if not has_infrequent_itemset(c,Lk-1)
Ck=Ck ∪ { c } ;
}
reutrn
Ck
}
procedure has_infrequent_itemset (c,Lk-1)
{
for each(k-1) subset
s
of c
if s |
Lk-1
return true ;
else
return false ;
}
(3)算法源码:
/**
* 编写者: oklzh
* Apriori算法
* 编写日期: 2007-11-04
*/
package Apriori;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import Apriori.Apriori.Item;
public class AprioriGUI extends JFrame {
private JTextArea textArea; //模拟数据集
private static JTextArea textArea_relating; //关联规则集
private static JTextArea textArea_frequency;
private static JTextField support;
private static JTextField limintCon;
private String frequency;
private String relating;
// 最小支持度
// 最小可信度
//频繁项目集
Hashtable ht1 = new Hashtable(); // 用于L1
Hashtable ht2 = new Hashtable(); // 用于L2
private static Hashtable ht3 = new Hashtable(); // 用于L3
//模拟数据集
String info[][]={
{"I1","I2","I5"},
{"I2","I4"},
{"I2","I3"},
{"I1","I2","I4"},
{"I1","I3"},
{"I2","I3"},
{"I1","I3"},
{"I1","I2","I3","I5"},
{"I1","I2","I3"}};
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
AprioriGUI frame = new AprioriGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public AprioriGUI() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 536, 408);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setText("请输入最小支持度阈值");
label.setBounds(10, 10, 162, 23);
getContentPane().add(label);
limintCon = new JTextField();
limintCon.setBounds(169, 39, 90, 21);
getContentPane().add(limintCon);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
});
button.setText("生成频繁集");
button.setBounds(285, 10, 131, 23);
getContentPane().add(button);
final JLabel label_1 = new JLabel();
label_1.setText("请输入最小可信度的值");
String str = support.getText();
String str1 = "频繁项集生成过程(其中最小支持度阈值是" + str
textArea_frequency.setText(str1 + createL3());
} catch (RuntimeException e1) {
JOptionPane.showMessageDialog(null, "请输入最小支持度阈
+ ")" + "\n";
值如: 1 ");
}
}
label_1.setBounds(10, 39, 162, 15);
getContentPane().add(label_1);
support = new JTextField();
support.setBounds(169, 11, 90, 21);
getContentPane().add(support);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
String str = limintCon.getText();
String str2 = "关联规则生成如下(其中最小可信度是:" + str +
textArea_relating.setText(str2 + createTransRule());
} catch (RuntimeException e1) {
JOptionPane.showMessageDialog(null, "请输入最小可信度值
")" + "\n";
如: 0.5 ");
}
}
});
button_1.setText("生成关联规则");
button_1.setBounds(285, 35, 131, 23);
getContentPane().add(button_1);
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBounds(10, 75, 508, 289);
getContentPane().add(panel);
final JTabbedPane tabbedPane = new JTabbedPane();
panel.add(tabbedPane, BorderLayout.CENTER);
final JPanel panel_3 = new JPanel();
panel_3.setLayout(new BorderLayout());
tabbedPane.addTab("模拟数据集", null, panel_3, null);
textArea = new JTextArea();
textArea.setText("交易号 项集合" + "\n" +
"T100
"T200
"T300
"T400
"T500
I1,I2,I5 " + "\n" +
I2,I4 " + "\n" +
I2,I3 " + "\n" +
I1,I2,I4 " + "\n" +
I1,I3 " + "\n" +