Hình 7: Các giai đoạn áp dụng khuôn mẫu của thuật toán Apriori DT

Một phần của tài liệu MÔ PHỎNG THUẬT TOÁN APRIORI TÌM TẬP PHỔ BIẾN & LUẬT KẾT HỢP (Trang 31)

liệu phù hợp, nâng cao hiệu quả truy xuất tìm kiếm độ hỗ trợ trên các tập mẫu thường xuyên.

Thuật toán lai tạp Apriori-DT được đề xuất với mục đích nâng cao hiệu năng khai thác luật kết hợp trên các tập dữ liệu có cấu trúc dạng bảng quyết định. Những lai tạp trong thuật toán là: áp dụng hhuôn mẫu luật nhằm loại bỏ những luật không cần thiết, chuyển đổi cấu trúc dữ liệu phục vụ tính toán độ hỗ trợ dựa trên truy vấn, lưu trữ danh sách các tập mẫu thường xuyên kết hợp với cấu trúc dữ liệu từ điển nhằm tối ưu hoá thao tác tìm kiếm.

Chương IV> Cài đặt thuật toán tìm luật kết hợp:

1/ Giới thiệu chương trình:

Chương trình Mô phỏng thuật toán Apriori tìm tập phổ biến và luật kết hợp

được viết bằng ngôn ngữ C# của bộ Visual Studio 2010 (download) trên nền tảng .NET Framework 3.5 SP1 (download) nhằm giúp cho việc tìm ra các tập luật kết hợp từ các tập phổ biến tương ứng. Từ đó chúng ta có thể ứng dụng phương pháp này để tìm ra các tập luật kết hợp trong kho dữ liệu siêu thị hay tìm ra cách chuẩn đoán bệnh trong kho dữ liệu bệnh viện,...

Dữ liệu đầu vào gồm có bảng danh sách các giao tác, độ hỗ trợ và độ tin cậy. Để giúp cho dữ liệu đầu vào được linh động hơn, ngoài việc nhập trực tiếp chúng ta có thể chọn cách nhập dữ liệu bằng cách mở file từ một số file do người nghiên cứu đã tạo sẵn. Sau khi mở file chúng ta có thể thêm hoặc bớt các giao tác cũng như thêm bớt các mặt hàng sao cho phù hợp với nhu cầu sử dụng.

2/ Một số đoạn mã chính để xây dựng chương trình:

2.1/ Mở tập tin: Mở file text để nhập thông số đầu vào.

//Mở tập tin

private void MoTapTin() {

openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1.Title = "Mở tập tin";

openFileDialog1.FileName = "";

if (openFileDialog1.ShowDialog() == DialogResult.OK) {

DocTapTin(openFileDialog1.FileName); }

}

//Đọc tập tin

private void DocTapTin(string fileName) {

int i; string line; string[] myArray; string[] itemSet;

TextBox txtItem = new TextBox(); btnLamLai.PerformClick();

FileStream fileStream = new FileStream(fileName, FileMode.Open,

FileAccess.Read);

StreamReader streamReader = new StreamReader(fileStream); //Nhập độ ủng hộ line = streamReader.ReadLine().Trim(); myArray = line.Split('='); txtDoHoTro.Text = myArray[1]; //Nhập độ tin cậy line = streamReader.ReadLine().Trim(); myArray = line.Split('='); txtDoTinCay.Text = myArray[1]; //Tập các mặt hàng line = streamReader.ReadLine().Trim(); myArray = line.Split('='); itemSet = myArray[1].Split(','); for (i = 0; i < itemSet.Length; i++) { txtItem.Text = itemSet[i]; if (function.ValidateInput(txtItem, lvMatHang)) { lvMatHang.Items.Add(txtItem.Text); } } //Nhập các giao tác i = 1;

{

gvThongTin.Rows.Add("T" + i.ToString(), line); i++;

}

streamReader.Close(); fileStream.Close(); }

2.2/ Lưu tập tin: Lưu các thông số đầu vào thành file text

//Lưu tập tin (adsbygoogle = window.adsbygoogle || []).push({});

private void LuuTapTin() {

if (!txtDoHoTro.Text.Equals("") && !txtDoTinCay.Text.Equals("") && gvThongTin.Rows.Count > 1)

{

saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)| *.*";

saveFileDialog1.Title = "Lưu tập tin";

if (saveFileDialog1.ShowDialog() == DialogResult.OK) {

GhiTapTin(saveFileDialog1.FileName);

MessageBox.Show("Bạn đã lưu tập tin thành công.", "Thông báo",

MessageBoxButtons.OK, MessageBoxIcon.Information); }

} else

MessageBox.Show("Chưa có thông tin để lưu.", "Lỗi lưu tập tin",

MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }

//Ghi tập tin

private void GhiTapTin(string fileName) {

string[] myArray;

string ItemSet = string.Empty;

FileStream fileStream = new FileStream(fileName, FileMode.Create,

FileAccess.Write);

StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.WriteLine("MinSupp=" + txtDoHoTro.Text); streamWriter.WriteLine("MinConf=" + txtDoTinCay.Text); ItemsetCollection db = new ItemsetCollection();

for (int i = 0; i < gvThongTin.Rows.Count - 1; i++) {

myArray = gvThongTin.Rows[i].Cells[1].Value.ToString().Split(','); Itemset itemSet = new Itemset();

for (int j = 0; j < myArray.Length; j++) {

itemSet.Add(myArray[j]); }

db.Add(itemSet); }

foreach (string item in uniqueItems) {

ItemSet += item + ","; }

streamWriter.WriteLine("ItemSet=" + ItemSet.Trim(',')); for (int i = 0; i < gvThongTin.Rows.Count - 1; i++) { streamWriter.WriteLine(gvThongTin.Rows[i].Cells[1].Value.ToString()); } streamWriter.Close(); fileStream.Close(); } 2.3/ Tìm luật kết hợp: //Tìm luật

private void btnTimLuat_Click(object sender, EventArgs e) {

TimLuatKetHop(); }

//Tìm luật kết hợp (adsbygoogle = window.adsbygoogle || []).push({});

private void TimLuatKetHop() {

if (!txtDoHoTro.Text.Equals("") && !txtDoTinCay.Text.Equals("") && gvThongTin.Rows.Count > 1) { rtxKetQua.Clear(); int doPhoBien = 30; int doTinCay = 100; int count = 0; string[] myArray;

ItemsetCollection db = new ItemsetCollection(); for (int i = 0; i < gvThongTin.Rows.Count - 1; i++) {

myArray =

gvThongTin.Rows[i].Cells[1].Value.ToString().Split(','); Itemset itemSet = new Itemset();

for (int j = 0; j < myArray.Length; j++) {

itemSet.Add(myArray[j]); }

db.Add(itemSet); }

TimeSpan time = DateTime.Now.TimeOfDay; //Tập các loại mặt hàng

Itemset uniqueItems = db.GetUniqueItems(); rtxKetQua.SelectionColor = Color.Red;

rtxKetQua.SelectedText = "Có " + uniqueItems.Count + " loại mặt hàng:\n";

//Sinh tập phổ biến

ItemsetCollection L = AprioriMining.DoApriori(db, doPhoBien); rtxKetQua.SelectionColor = Color.Red;

rtxKetQua.SelectedText = "\nCó " + L.Count + " tập phổ biến:\n"; foreach (Itemset i in L) { count++; rtxKetQua.AppendText("(L" + count + "): " + i + "\n"); } //Sinh luật kết hợp

List<AssociationRule> allRules = AprioriMining.Mine(db, L, doTinCay);

rtxKetQua.SelectionColor = Color.Red;

rtxKetQua.SelectedText = "\nCó " + allRules.Count + " luật kết hợp được sinh ra:\n";

count = 0;

foreach (AssociationRule rule in allRules) {

count++;

rtxKetQua.AppendText("(R" + count + "): " + rule + "\n"); }

time = DateTime.Now.TimeOfDay - time;

grpKetQua.Text = "Thời gian thực hiện: " + time.TotalSeconds + " giây";

} else

MessageBox.Show("Vui lòng nhập các thông tin cần thiết.", "Thiếu thông tin tìm luật", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

2.4/ Thuật toán Apriori tìm tập phổ biến:

//Thuật toán Apriori trả về tất cả các tập phổ biến

public static ItemsetCollection DoApriori(ItemsetCollection db, double

supportThreshold) { (adsbygoogle = window.adsbygoogle || []).push({});

Itemset I = db.GetUniqueItems(); //Tập các mặt hàng

ItemsetCollection L = new ItemsetCollection(); //Tất cả các tập phổ biến ItemsetCollection Li = new ItemsetCollection(); //Tập phổ biến lớn trong mỗi lần lặp

ItemsetCollection Ci = new ItemsetCollection(); //Tập các ứng viên trong mỗi lần lặp

//Tập các ứng viên đầu tiên có 1 phần tử foreach (string item in I)

{

Ci.Add(new Itemset() { item }); } //Tìm tập phổ biến int k = 2; while (Ci.Count != 0) { Li.Clear();

if (itemset.Support >= supportThreshold) { Li.Add(itemset); // Tập phổ biến có k - 1 phần tử L.Add(itemset); } } Ci.Clear(); Ci.AddRange(Bit.FindSubsets(Li.GetUniqueItems(), k)); //Lấy tập ứng viên có k phần tử k += 1; }

return (L); }

2.5/ Thuật toán phát sinh luật kết hợp: //Thuật toán phát sinh luật kết hợp public static List<AssociationRule> Mine(ItemsetCollection db, ItemsetCollection L, double confidenceThreshold) {

List<AssociationRule> allRules = new List<AssociationRule>(); foreach (Itemset itemset in L) {

ItemsetCollection subsets = Bit.FindSubsets(itemset, 0); foreach (Itemset subset in subsets) {

double confidence = (db.FindSupport(itemset) / db.FindSupport(subset)) * 100.0; if (confidence >= confidenceThreshold) {

AssociationRule rule = new AssociationRule(); rule.X.AddRange(subset); rule.Y.AddRange(itemset.Remove(subset)); rule.Support = db.FindSupport(itemset); rule.Confidence = confidence; if (rule.X.Count > 0 && rule.Y.Count > 0) { allRules.Add(rule); } } } } return (allRules); }

2.6/ Xuất kết quả: Xuất kết quả thành file text sau khi tìm luật.

private void XuatKetQua() {

if (!rtxKetQua.Text.Equals("")) {

saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)| *.*"; saveFileDialog1.Title = "Xuất kết quả"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) {

rtxKetQua.SaveFile(saveFileDialog1.FileName,

RichTextBoxStreamType.UnicodePlainText);

MessageBox.Show("Bạn đã xuất kết quả thành công.", "Thông báo",

MessageBoxButtons.OK, MessageBoxIcon.Information); }

} else

MessageBox.Show("Không có kết quả cần xuất.", "Lỗi xuất kết quả",

MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }

3/ Hướng dẫn sử dụng chương trình:

Bước 1: Nhập dữ liệu đầu vào. Có 2 cách nhập:

a. Cách 1: Nhập dữ liệu từ file. Từ trình đơn chọn Tập tin/Mở tập tin

hoặc nhấn nút Mở tập tin trên thanh công cụ. Chọn tập tin cần mở rồi nhấn nút Open.

Hình 9: Mở tập tin.

b. Cách 2: Nhập dữ liệu bằng tay. Có 3 bước: (adsbygoogle = window.adsbygoogle || []).push({});

i. Thêm mặt hàng: Nhập tên mặt hàng cần thêm vào mục Mặt hàng và nhấn nút Thêm.

ii. Thêm giao tác: Chọn các mặt hàng ứng với một giao tác và nhấn nút Thêm giao tác.

Hình 11: Thêm giao tác.

iii. Nhập độ hỗ trợ và độ tin cậy: Độ hỗ trợ và độ tin cậy là những số nguyên dương từ 1 đến 100.

Hình 12: Độ hỗ trợ và độ tin cậy.

Bước 2: Nhấn nút Tìm luật để chương trình tự động thực hiện tính toán và hiển

thị kết quả vào mục Kết quả thực hiện. Đồng thời hiển thị được thời gian chạy của thuật toán tìm luật.

Hình 13: Kết quả thực hiện.

Một phần của tài liệu MÔ PHỎNG THUẬT TOÁN APRIORI TÌM TẬP PHỔ BIẾN & LUẬT KẾT HỢP (Trang 31)