Nghiên cứu thuật toán charm trong khai phá tập mục thường xuyên đóng

71 34 1
Nghiên cứu thuật toán charm trong khai phá tập mục thường xuyên đóng

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

ĐẠI HỌC THÁI NGUYÊN TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG PHAN VĂN TUYÊN NGHIÊN CỨU THUẬT TOÁN CHARM TRONG KHAI PHÁ TẬP MỤC THƯỜNG XUN ĐĨNG LUẬN VĂN THẠC SĨ KHOA HỌC MÁY TÍNH Thái Nguyên - 2011 Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn ĐẠI HỌC THÁI NGUYÊN TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG PHAN VĂN TUYÊN NGHIÊN CỨU THUẬT TOÁN CHARM TRONG KHAI PHÁ TẬP MỤC THƯỜNG XUN ĐĨNG CHUN NGÀNH: KHOA HỌC MÁY TÍNH MÃ SỐ: 60 48 01 LUẬN VĂN THẠC SĨ KHOA HỌC MÁY TÍNH NGƯỜI HƯỚNG DẪN KHOA HỌC TS NGUYỄN HUY ĐỨC Thái Nguyên - 2011 Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn LỜI CẢM ƠN Để hồn thành luận văn tơi nhận giúp đỡ tận tình thầy Trường Đại học Công nghệ Thông tin Truyền thông – Đại học Thái Nguyên, thầy cô Viện công nghệ thông tin – Viện Khoa học Công nghệ Việt Nam, anh chị lớp Cao học K8 - khóa 2009-2011 Đặc biệt TS Nguyễn Huy Đức, người thầy trực tiếp hướng dẫn tơi q trình nghiên cứu thực luận văn Nhân dịp xin bày tỏ lời cảm ơn tới tất thầy cô giáo Viện Công nghệ thông tin – Viện Khoa học Công nghệ Việt Nam, thầy cô Trường đại học Công nghệ Thông tin Truyền thông – Đại học Thái Nguyên giảng dạy tạo điều kiện thuận lợi giúp đỡ trình học tập, nghiên cứu Tơi xin trân trọng cảm ơn TS Nguyễn Huy Đức – Khoa Thông tin - Máy tính, Trường Cao đẳng Sư phạm Trung ương, người thầy trực tiếp hướng dẫn, đưa ý tưởng, định hướng, đóng góp ý kiến chun mơn tận tình giúp đỡ tơi suốt q trình nghiên cứu thực luận văn Tôi xin cảm ơn bạn bè đồng nghiệp gia đình giúp đỡ, đóng góp ý kiến động viên tơi suốt qua trình học, q trình nghiên cứu hồn thành luận văn Tác giả Phan Văn Tuyên Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn LỜI CAM ĐOAN Tơi xin cam đoan tồn nội dung Luận văn hoàn toàn theo nội dung đề cương nội dung mà giáo viên hướng dẫn giao cho Nội dung luận văn, phần trích lục tài liệu hồn tồn xác Nếu có sai sót tơi hồn tồn chịu trách nhiệm Tác giả luận văn Phan Văn Tuyên Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn I MỤC LỤC Trang Lời cảm ơn Lời cam đoan MỤC LỤC I DANH MỤC CÁC KÝ HIỆU VÀ CÁC CHỮ VIẾT TẮT III DANH MỤC CÁC BẢNG IV DANH MỤC HÌNH VẼ V MỞ ĐẦU CHƢƠNG 1: TỔNG QUAN VỀ KHAI PHÁ DỮ LIỆU 1.1 KHÁM PHÁ TRI THỨC VÀ KHAI PHÁ DỮ LIỆU 1.2 KIẾN TRÚC CỦA HỆ THỐNG KHAI PHÁ DỮ LIỆU 1.3 QUÁ TRÌNH KHAI PHÁ DỮ LIỆU 1.4 CÁC KỸ THUẬT KHAI PHÁ DỮ LIỆU 1.4.1.Phân lớp liệu 1.4.2.Phân cụm liệu 1.4.3.Khai phá luật kết hợp 1.4.4.Hồi quy 1.4.5.Giải thuật di truyền 1.4.6.Mạng nơron 1.4.7.Cây định 1.5 MỘT SỐ ỨNG DỤNG CỦA KHAI PHÁ DỮ LIỆU 10 1.6 KHAI PHÁ TẬP MỤC THƢỜNG XUYÊN 11 1.6.1 Cơ sở liệu giao tác 11 1.6.2 Tập mục thƣờng xuyên 13 1.6.3 Các cách tiếp cận khai phá tập mục thƣờng xuyên 14 1.6.4 Một số thuật tốn điển hình tìm tập mục thƣờng xuyên 16 1.6.4.1 Thuật toán Apriori 16 1.6.4.2 Thuật toán FP-Growth 20 1.7 KẾT LUẬN CHƢƠNG 28 CHƢƠNG 2: KHAI PHÁ TẬP MỤC THƢỜNG XUYÊN ĐÓNG 29 2.1 CƠ SỞ TOÁN HỌC 29 Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn II 2.1.1 Ánh xạ đóng 29 2.1.2 Tập đóng 30 2.1.3 Kết nối Galois 30 2.1.4 Bao đóng tập mục liệu 31 2.2 TẬP MỤC THƢỜNG XUYÊN ĐÓNG 32 2.2.1 Định nghĩa 32 2.2.2 Các tính chất tập mục thƣờng xuyên đóng 32 2.3 THUẬT TOÁN CHARM 32 2.3.1 Giới thiệu thuật toán CHARM 32 2.3.2 Cây tìm kiếm lớp tƣơng đƣơng 33 2.3.3 Các tính chất cặp tập mục - tập định danh: 34 2.3.4 Thiết kế thuật toán 35 2.3.5 Ví dụ minh họa 37 2.3.6 Đánh giá thuật toán 39 2.4 KẾT LUẬN CHƢƠNG 39 CHƢƠNG 3: CÀI ĐẶT THỰC NGHIỆM 41 3.1 XÂY DỰNG CHƢƠNG TRÌNH 41 3.2 GIAO DIỆN CỦA CHƢƠNG TRÌNH 43 3.3 KẾT QUẢ THỰC NGHIỆM 44 3.4 NHẬN XÉT 47 KẾT LUẬN 48 TÀI LIỆU THAM KHẢO 49 PHỤ LỤC 51 Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn III DANH MỤC CÁC KÝ HIỆU VÀ CÁC CHỮ VIẾT TẮT Diễn giải Ký hiệu Ck Tập k tập mục ứng viên BFS Breadth First Search CSDL Cơ sở liệu CHARM Closed Asociation RuleMning DB Cơ sở liệu giao tác DFS Depth First Search FP -growth Frequent -Pattern Growth FP -tree Frequent pattern tree IT-tree Itemset-Tidset tree I Tập mục liệu k-itemset Tập mục gồm k mục KPDL Khai phá liệu Minsup Ngƣỡng hỗ trợ tối thiểu Lk Tập k-tập mục thƣờng xuyên Supp Độ hỗ trợ (support) TID Định danh giao tác T Giao tác (transaction) Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn IV DANH MỤC CÁC BẢNG Bảng 1.1: Biểu diễn ngang sở liệu giao tác .11 Bảng 1.2: Biểu diễn dọc sở liệu giao tác 12 Bảng 1.3: Ma trận giao tác sở liệu cho bảng 1.1 12 Bảng 1.4: Cơ sở liệu giao tác minh họa thực thuật toán Apriori .19 Bảng 1.5: CSDL giao tác minh hoạ cho thuật toán FP- growth 22 Bảng 2.1: a) CSDL giao tác biểu diễn ngang 31 Bảng 2.1: b) CSDL giao tác biểu diễn dọc .31 Bảng 3.1: Đặc điểm tệp liệu thử nghiệm .41 Bảng 3.2: Kết thực nghiệm tệp liệu Input1.txt 46 Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn V DANH MỤC HÌNH VẼ Hình 1.1: Qúa trình phát tri thức Hình 1.2: Kiến trúc hệ thống khai phá liệu .5 Hình 1.3 Quá trình KPDL Hình 1.4: Phân loại các thuật tốn khai phá tập mục thƣờng xuyên .15 Hình 1.5: Cây FP-tree đƣợc xây dựng dần thêm giao tác t1, t2, t3 23 Hình 1.6: Cây FP-tree CSDL DB bảng 1.5 .23 Hình 1.7 : FP-tree phụ thuộc m 26 Hình 1.8 : Các FP-tree phụ thuộc am, cm cam .27 Hình 2.1: Kết nối Galois 30 Hình 2.2 Cây IT-tree tìm tập thƣờng xun đóng thoả ngƣỡng minsup =50% 38 Hình 3.1: CSDL giao tác mã hóa chuẩn bị cho khai phá 42 Hình 3.2: Giao diện chƣơng trình thực nghiệm sau khởi động 43 Hình 3.3: Kết tìm tập mục thƣờng xuyên với ngƣỡng minsup = 10% 44 Hình 3.4: Kết tìm tập mục thƣờng xun đóng với ngƣỡng minsup = 10% .45 Hình 3.5: So sánh thời gian thực khai phá tệp Input1.txt 46 Hình 3.6: So sánh số tập mục kết khai phá tệp Input1.txt 47 Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn MỞ ĐẦU Chúng ta "thời đại thơng tin", thời đại đƣợc định hình ngành khoa học công nghệ kỹ thuật trẻ phát triển nhƣ vũ bão, ảnh hƣởng vô sâu sắc mãi đến sống chúng ta, ngành cơng nghiệp "cơng nghệ thơng tin" Trong kinh doanh có nhiều thơng tin ngƣời làm chủ thị trƣờng, nghiên cứu nhiều thơng tin hội thành cơng ngƣời lớn Vì việc thu thập thơng tin có vai trị đặc biệt quan trọng cơng việc sống Khai phá liệu khám phá tri thức (Data Mining and Knowledge Discovery) lĩnh vực quan trọng ngành công nghệ thông tin Đây hƣớng nghiên cứu tập trung đƣợc hùng hậu nhà khoa học giới tham gia Hội nghị quốc tế khai phá liệu khám phá tri thức đƣợc tổ chức hàng năm, luân phiên nhiều nƣớc giới, hội thảo có hàng trăm nhà khoa học hàng đầu tham gia Tại Việt Nam, khai phá liệu đƣợc nhóm nghiên cứu Viện Cơng nghệ Thơng tin thuộc Viện Khoa học Công nghệ Việt Nam, nhóm nghiên cứu số trƣờng đại học nhƣ Đại học Quốc gia Hà Nội, Đại học Bách Khoa Hà Nội, Đại học Quốc gia thành phố Hồ Chí Minh thực có nhiều kết đƣợc công bố Một nội dung khai phá liệu toán khai phá luật kết hợp Khai phá luật kết hợp gồm hai bƣớc: Bƣớc một, tìm tất tập mục thờng xuyên Bƣớc hai, dựa vào tập mục thƣờng xuyên tìm luật kết hợp Bƣớc thứ địi hỏi tính tốn lớn, bƣớc thứ hai địi hỏi tính tốn hơn, song gặp phải vấn đề là: sinh nhiều luật, vƣợt khỏi kiểm soát ngƣời khai phá ngƣời dùng, có nhiều luật khơng cần thiết Để giải vấn đề đó, bƣớc thứ nhất, khơng cần thiết phải khai phá tất tập mục thƣờng xuyên mà cần Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 48 KẾT LUẬN Những kết đạt đƣợc: Luận văn nghiên cứu toán quan trọng khai phá liệu tốn tìm tập mục thƣờng xun đóng Từ tốn đƣợc giới thiệu có nhiều thuật tốn nhiều nhóm nghiên cứu giới đề xuất Vì cần phải lựa chọn nghiên cứu thuật toán hiệu cách sâu sắc để góp phần hệ thống lại kiến thức lĩnh vực khám phá tri thức khai phá liệu Mục tiêu luận văn muốn giúp cho ngƣời nghiên cứu tiếp cận nhanh kiến thức cập nhật giới lĩnh vực Luận văn khái quát vấn đề khai phá liệu tập mục thƣờng xuyên, trình bày khái niệm cách tiếp cận để khai phá tập mục thƣờng xuyên Đồng thời trình bày chi tiết hai thuật tốn điển hình tìm tập thƣờng xun: Thuật tốn Apriori FP-growth Khái niệm ánh xạ đóng, tập đóng bao đóng tập mục liệu đƣợc trình bày chi tiết để làm sở cho khai phá tập mục thƣờng xun đóng Luận văn trình bày rõ cấu trúc tìm kiếm IT-tree cấu trúc liệu sử dụng thuật toán Charm bƣớc thực thuật toán Đề tài đƣợc làm sáng tỏ qua kết thực nghiệm cài đặt hai thuật toán: Apriori Charm, so sánh thực nghiệm cho thấy số lƣợng tập mục thƣờng xuyên đóng nhỏ nhiều so với số tập thƣờng xuyên, thời gian tìm tập mục thƣờng xun đóng so với thời gian tìm tập thƣờng xuyên Hƣớng phát triển đề tài Đề tài luận văn tiếp tục nghiên cứu tiếp số vấn đề sau: Khai phá luật kết hợp dựa tập mục thƣờng xuyên đóng, khai phá luật hạt giống dựa tập mục thƣờng xun đóng Các thuật tốn cần phải nghiên cứu để bổ xung thêm lựa chọn cho phép thiết lập mục đích sử dụng Vì ngƣời sử dụng khơng quan tâm hết toàn kết khai phá đƣợc mà kết thỏa điều kiện ràng buộc Cho nên thuật tốn hoạt động tốt bám sát mục đích ngƣời dùng thay khai thác hết tất thơng tin sau chọn lọc kết phù hợp Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 49 TÀI LIỆU THAM KHẢO Tiếng Việt [1] [2] [3] [4] [5] [6] Hoàng Kiếm, Lƣơng Hán Cơ (2004), "Các vấn đề liên quan tốn tìm tập mục thƣờng xuyên đóng", Hội Thảo Quốc Gian lần Thứ VII: Một số vấn đề chọn lọc Công Nghệ Thông Tin Truyền Thông, Đà Nẵng Vũ Đức Thi, Nguyễn Huy Đức (2008), “Thuật toán hiệu khai phá tập mục lợi ích cao cấu trúc liệu cây”, Tạp chí Tin học Điều khiển học, 24(3), tr 204-216 Nguyễn Huy Đức (2009), “Khai phá tập mục cổ phần cao lợi ích cao sở liệu”, Luận án Tiến sĩ Tốn học, Viện Cơng nghệ Thông tin – Viện KH CN Việt Nam Nguyễn Hữu Trọng (2008), "Phát triển số thuật toán khai phá luật kết hợp sở liệu gia tăng", Luận án Tiến sĩ Tốn học, Viện Cơng nghệ Thông tin – Viện KH CN Việt Nam Hồ Thuần, Hồ Cẩm Hà (2005), Các hệ sở liệu: Lý thuyết tập, tập 2, NXB Giáo dục, Hoàng Kiếm, Đỗ Phúc (2001), "Phát triển thuật tốn tìm tập mục thƣờng xun sở liệu", Hội thảo Quốc gia lần thứ IV: Một số vấn đề chọn lọc Công Nghệ Thông Tin, Hải Phòng TiÕng Anh [7] [8] R Agrawal, T Imielinski, and A Swami (1993), "Mining association rules between sets of items in large databases", In FroG 1993 ACMSIGMOD Int Con! Management of Data, pages 207-216, Washington, D.C Pasquier, N., Bastide, T., Taouil, R., Lakhal, L.: Discovering Frequent Closed Itemsets for Association Rules In: Proceedings of the 7th International Conference on Database Theory, Jerusalem, Israel, pp 398–416 (1999) Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 50 [9] Han J and Kamber M., Data Mining: Concepts and Techniques, University of Illinois, Morgan Kaufmann Publishers, 2002 [10] B Goethals, MJ Zaki (2003), "FIMI'03: Workshop on Frequent Itemset Mining Implementations", ICDM'03 [11] J Han, J Pei, Y Yin (2000), "Mining frequent patterns without candidate generation", In Proc 2000 ACM-SIGMOD Int Conf Management of Data (SIGMOD' 00), pages 1-12, Dallas [12] Y Bastide, Stumme, N L Lakhal, Non-Redundant Frequent Pasquier, (2000) Association Itemssets", R "Mining Rules In Taouil, 1st G Minimal using Closed International Conference on Computational Logic, [13] M.J Zaki ,C Hsiao (2002), "CHARM: An Efficient Algorithm, for Closed Itemset Mining", In SDM '02 [14] M J Zaki, Algorithms for C.J Hsiao, (2005) "Efficient Mining Closed Itemsets and Their Lattice Structure", IEEE Transactions on Knowledge and Data Engineering [15] M J Zaki, (2004) "Mining Association Rules", Knowledge Discovery, Academic Publishers Data 9, Non-Redundant Mining 223–248, Manufactured and Kluwer in The Netherlands, [16] Bodon Ferenc (2006), “A Survey on Frequent Itemset Mining”, Technical Report, Budapest University of Technology and Economic, Hungary Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 51 PHỤ LỤC 1/ Tệp MaintestCharm.java package tests; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import frequentpatterns.charm.AlgoCharm; import frequentpatterns.charm.Context; import frequentpatterns.charm.Itemsets; /** * Class to test the Charm algorithm */ public class MainTestCharm { public static void main(String arg[]){ // Loading the binary context Context context = new Context(); try { context.loadFile(arg[0]); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } context.printContext(); // Applying the Charm algorithm AlgoCharm algo = new AlgoCharm(context, 100000); Itemsets frequents = algo.runAlgorithm(Double.parseDouble(arg[2]), true); try { frequents.saveToFile(arg[1], context.size()); } catch (IOException e) { e.printStackTrace(); } // print the frequent closed itemsets found algo.getClosedItemsets().printItemsets(context.size()); //System.out.println("total Time: "+(endTime startTime)+"ms"); } public static String fileToPath(String filename) throws UnsupportedEncodingException{ URL url = MainTestCharm.class.getResource(filename); return java.net.URLDecoder.decode(url.getPath(),"UTF-8"); } } Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 52 2/ Tệp AlgoCharm.java package charm; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import TriangularMatrix; /* ** This is an implementation of the CHARM algorithm that was proposed by MOHAMED ZAKI */ public class AlgoCharm { protected Itemsets frequentItemsets = new Itemsets("FREQUENT CLOSED ITEMSETS"); protected Context context; // initial context private long startTimestamp; // for stats private long endTimestamp; // for stats private int minsupRelative; private TriangularMatrix matrix; private boolean useTriangularMatrixOptimization; // for optimization with a hashTable private HashTable hash; public AlgoCharm(Context context, int hashTableSize) { this.context = context; this.hash = new HashTable(hashTableSize); } public Itemsets runAlgorithm(double minsuppAbsolute, boolean useTriangularMatrixOptimization) { this.minsupRelative = (int) Math.ceil(minsuppAbsolute * context.size()); this.useTriangularMatrixOptimization = useTriangularMatrixOptimization; return run(); } public Itemsets runAlgorithmWithRelativeMinsup(boolean useTriangularMatrixOptimization, int minsupRelative) { this.minsupRelative = minsupRelative; this.useTriangularMatrixOptimization = useTriangularMatrixOptimization; return run(); } private Itemsets run() { startTimestamp = System.currentTimeMillis(); Set allTIDS = new HashSet(); // (1) First database pass : calculate tidsets of each item int maxItemId = 0; Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 53 final Map mapItemCount = new HashMap(); for(int i=0; i< context.size(); i++){ allTIDS.add(i); // context.getObjects().get(i).transactionId for(Integer item : context.getObjects().get(i).getItems()){ Set set = mapItemCount.get(item); if(set == null){ set = new HashSet(); mapItemCount.put(item, set); if(item > maxItemId){ maxItemId = item; } } set.add(i); // add tid // context.getObjects().get(i).transactionId } } if(useTriangularMatrixOptimization){ // (1.b) create the triangular matrix for counting the support of itemsets of size // for optimization purposes matrix = new TriangularMatrix(maxItemId+1); // for each transaction, take each itemset of size 2, // and update the triangular matrix for(Itemset itemset : context.getObjects()){ Object[] array = itemset.getItems().toArray(); for(int i=0; i< itemset.size(); i++){ Integer itemI = (Integer) array[i]; for(int j=i+1; j< itemset.size(); j++){ Integer itemJ = (Integer) array[j]; // update the matrix matrix.incrementCount(itemI, itemJ); } } } } // (2) create ITSearchTree with root node ITSearchTree tree = new ITSearchTree(); ITNode root = new ITNode(new Itemset()); root.setTidset(allTIDS); tree.setRoot(root); // (3) create childs of the root node for(Entry entry : mapItemCount.entrySet()){ // we only add nodes for items that are frequents if(entry.getValue().size() >= minsupRelative){ // create the new node Itemset itemset = new Itemset(); Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 54 itemset.addItem(entry.getKey()); ITNode newNode = new ITNode(itemset); newNode.setTidset(entry.getValue()); newNode.setParent(root); // add the new node as child of the root node root.getChildNodes().add(newNode); } } // save root node save(root); // for optimization sortChildren(root); while(root.getChildNodes().size() > 0){ ITNode child = root.getChildNodes().get(0); extend(child); save(child); delete(child); } endTimestamp = System.currentTimeMillis(); return frequentItemsets; // Return all frequent itemsets found! } private void extend(ITNode currNode) { // loop over the brothers int i=0; while(i < currNode.getParent().getChildNodes().size()){ ITNode brother = currNode.getParent().getChildNodes().get(i); if(brother != currNode){ // Property if(currNode.getTidset().equals(brother.getTidset())){ replaceInSubtree(currNode, brother.getItemset()); delete(brother); } // Property else if(brother.getTidset().containsAll(currNode.getTidset())){ replaceInSubtree(currNode, brother.getItemset()); i++; } // Property else if(currNode.getTidset().containsAll(brother.getTidset())){ ITNode candidate = getCandidate(currNode,brother); delete(brother); if(candidate != null){ currNode.getChildNodes().add(candidate); candidate.setParent(currNode); } } // Property Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 55 else if(!currNode.getTidset().equals(brother.getTidset())){ ITNode candidate = getCandidate(currNode,brother); if(candidate != null){ currNode.getChildNodes().add(candidate); candidate.setParent(currNode); } i++; }else{ i++; } }else{ i++; } } sortChildren(currNode); while(currNode.getChildNodes().size() > 0){ ITNode child = currNode.getChildNodes().get(0); extend(child); save(child); delete(child); } } private void replaceInSubtree(ITNode currNode, Itemset itemset) { // make the union Itemset union = new Itemset(); union.getItems().addAll(currNode.getItemset().getItems()); union.getItems().addAll(itemset.getItems()); // replace for this node currNode.setItemset(union); // replace for the childs of this node currNode.replaceInChildren(union); } private ITNode getCandidate(ITNode currNode, ITNode brother) { if(useTriangularMatrixOptimization && currNode.getItemset().size() ==1){ int support = matrix.getSupportForItems((Integer)currNode.getItemset().getItems().toArr ay()[0],(Integer)brother.getItemset().getItems().toArray()[0]); if(support < minsupRelative){ return null; } } // create list of common tids Set commonTids = new HashSet(); for(Integer tid : currNode.getTidset()){ if(brother.getTidset().contains(tid)){ commonTids.add(tid); } } Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 56 // (2) check if the two itemsets have enough common tids // if not, we don't need to generate a rule for them if(commonTids.size() >= minsupRelative){ Itemset union = currNode.getItemset().union(brother.getItemset()); ITNode node = new ITNode(union); node.setTidset(commonTids); return node; } return null; } private void delete(ITNode child) { child.getParent().getChildNodes().remove(child); } private void save(ITNode node) { Itemset itemset = node.getItemset(); itemset.setTransactioncount(node.getTidset()); if(!hash.containsSupersetOf(itemset)){ frequentItemsets.addItemset(itemset, itemset.size()); hash.put(itemset); } } private void sortChildren(ITNode node) { // sort children of the node according to the support Collections.sort(node.getChildNodes(), new Comparator(){ //Returns a negative integer, zero, or a positive integer as // the first argument is less than, equal to, or greater than the second public int compare(ITNode o1, ITNode o2) { return o1.getTidset().size() - o2.getTidset().size(); } }); } public void printStats() { System.out.println("==== CHARM - STATS ========="); long temps = endTimestamp - startTimestamp; System.out.println(" Transactions count from database : " + context.size()); System.out.println(" Frequent itemsets count : " + frequentItemsets.getItemsetsCount()); frequentItemsets.printItemsets(context.size()); System.out.println(" Total time ~ " + temps + " ms"); System.out.println("================================="); } public Itemsets getClosedItemsets() { return frequentItemsets; } public HashTable getHashTable() { return hash; } } Số hóa Trung tâm Học liệu – ĐHTN http://www.lrc-tnu.edu.vn 57 3/ Tệp Itemsets.java package charm; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * This class represents a set of itemsets */ public class Itemsets { private final List levels = new ArrayList(); // itemset classé par taille private int itemsetsCount=0; private final String name; public Itemsets(String name){ this.name = name; levels.add(new ArrayList()); // We create an empty level by default } public void printItemsets(int nbObject){ System.out.println(" - " + name + " -"); int patternCount=0; int levelCount=0; for(List level : levels){ System.out.println(" L" + levelCount + " "); for(Itemset itemset : level){ System.out.print(" pattern " + patternCount + ": "); itemset.print(); System.out.print("support : " + itemset.getSupportRelatifFormatted(nbObject)); System.out.print(" (" + itemset.getAbsoluteSupport() + "/" + nbObject + " transactions)"); patternCount++; System.out.println(""); } levelCount++; } System.out.println(" "); } public void addItemset(Itemset itemset, int k){ while(levels.size()

Ngày đăng: 25/02/2021, 09:06

Tài liệu cùng người dùng

Tài liệu liên quan