1. Trang chủ
  2. » Luận Văn - Báo Cáo

Nghiên cứu một số thuật toán khai phá luật kết hợp và thử nghiệm ứng dụng vào khai phá cơ sở dữ liệu bảo hiểm y tế tại bảo hiểm xã hội bắc giang

90 0 0

Đ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

Nội dung

1 ĐẠI HỌC THÁI NGUYÊN TRƢỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG  NGUYỄN NGỌC HẢI NGHIÊN CỨU MỘT SỐ THUẬT TOÁN KHAI PHÁ LUẬT KẾT HỢP VÀ THỬ NGHIỆM ỨNG DỤNG VÀO KHAI PHÁ CƠ SỞ DỮ LIỆU BẢO HIỂM Y TẾ TẠI BẢO HIỂM XÃ HỘI BẮC GIANG LUẬN VĂN THẠC SĨ KHOA HỌC MÁY TÍNH THÁI NGUN - 2013 Số hóa Trung tâm Học liệu Tai ngay!!! Ban co the xoa dong chu nay!!! 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  NGUYỄN NGỌC HẢI NGHIÊN CỨU MỘT SỐ THUẬT TOÁN KHAI PHÁ LUẬT KẾT HỢP VÀ THỬ NGHIỆM ỨNG DỤNG VÀO KHAI PHÁ CƠ SỞ DỮ LIỆU BẢO HIỂM Y TẾ TẠI BẢO HIỂM XÃ HỘI BẮC GIANG Chuyên 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 PHÙNG VĂN ỔN THÁI NGUYÊN - 2013 Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ MỤC LỤC Trang MỤC LỤC LỜI CẢM ƠN DANH MỤC CÁC KÝ HIỆU, CÁC CHỮ VIẾT TẮT MỞ ĐẦU Chƣơng TỔNG QUAN VỀ KHAI PHÁ DỮ LIỆU 1.1 Tổ chức khai thác sở liệu truyền thống 1.2 Tổng quan kỹ thuật phát tri thức khai phá liệu 1.3 Các nhiệm vụ khai phá liệu phát tri thức 11 1.4 Phân lớp liệu 18 1.4.1 Các loại liệu đƣợc khai phá 21 1.4.1.1 Cơ sở liệu quan hệ 21 1.4.1.3 Cơ sở liệu giao tác 21 1.4.1.4 Cơ sở liệu không gian .21 1.4.1.5 Cơ sở liệu có yếu tố thời gian 22 1.4.1.6 Cơ sở liệu đa phƣơng tiện .22 1.4.2 Những vấn đề quan tâm khai phá liệu .22 Chƣơng MỘT SỐ THUẬT TOÁN KHAI PHÁ DỮ LIỆU 25 2.1 Luật kết hợp 25 2.2 Các đặc trƣng luật kết hợp .35 2.2.1 Khơng gian tìm kiếm luật 35 2.2.2 Độ hỗ trợ luật 38 2.3 Một số thuật toán khai thác luật kết hợp 38 2.3.1 Giải thuật BFS (Breadth First Search) 39 2.3.2 Giải thuật DFS (Depth First Search) .52 2.3.3 Giải thuật DHP (Direct Hashing and Pruning) .52 2.3.4 Giải thuật PHP (Perfect Hashing and Pruning) .55 2.3.5 Phát sinh luật từ tập phổ biến 58 2.4 Đánh giá, nhận xét 62 Chƣơng ÁP DỤNG KHAI PHÁ TRÊN CƠ SỞ DỮ LIỆU BẢO HIỂM Y TẾ CỦA BẢO HIỂM XÃ HỘI TỈNH BẮC GIANG 63 3.1 CSDL bảo hiểm xã hội, bảo hiểm y tế yêu cầu toán 63 3.2 Lựa chọn công cụ khai phá 64 3.3 Thiết kế ứng dụng .64 3.4 Phân tích cài đặt thuật toán .64 3.5 Các kết đạt đƣợc 69 * Đánh giá, nhật xét 71 KẾT LUẬN VÀ HƢỚNG PHÁT TRIỂN 73 TÀI LIỆU THAM KHẢO 75 PHỤ LỤC 77 Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ LỜI CẢM ƠN Tác giả xin chân thành cảm ơn thầy giáo, cô giáo Trƣờng Đại học Công nghệ thông tin Truyền thông Thái Nguyên thầy Viện Công nghệ thông tin - Đại học quốc gia Hà Nội, tận tâm giảng dạy kiến thức hai năm học qua với cố gắng thân Đặc biệt xin bày tỏ biết ơn sâu sắc đến thầy giáo Tiến sĩ Phùng Văn Ổn, PGS TS Ngô Quốc Tạo ngƣời tận tình giảng dạy hƣớng dẫn tơi thực luận văn Tác giả xin chân thành cảm ơn lãnh đạo Bảo hiểm xã hội tỉnh Bắc Giang, bạn đồng nghiệp, bạn lớp cao học CK10B tạo điều kiện, giúp đỡ suốt thời gian qua Rất mong nhận đƣợc góp ý thầy, cô, bạn bè, đồng nghiệp để luận văn phát triển hồn thiện LỜI CAM ĐOAN Tơi xin cam đoan cơng trình nghiên cứu riêng Các số liệu, kết nêu luận văn chƣa đƣợc công bố cơng trình khác Thái Ngun, tháng 09 năm 2013 TÁC GIẢ Nguyễn Ngọc Hải Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ DANH MỤC CÁC KÝ HIỆU, CÁC CHỮ VIẾT TẮT Từ viết tắt Ck Conf CSDL DW Item Itemset K- itemset KDD Lk Minconf Minsup OLAP MOLAP ROLAP pre(k, s) Record Supp TID SQL SQO DBSCAN DENCLUE ADO DFS BFS DHP PHP I/O BHXH BHYT KCB Tiếng Anh Ck Confidence Database Data Warehouse Item Itemset K- itemset Knowledge Discovery and Data Mining Lk Minimum Confidence Minimum Support On Line Analytical Processing Multidimensional OLAP Relational OLAP pre(k, s) Record Support Transaction Indentification Structured Query Language Semantic Query Optimization Density Based Spatial Clustering of Application with Noise DENsity Based CLUstEring Activate X Data Object Depth First Search Breadth First Search Direct Hashing and Pruning Perfect Hashing and Pruning Input/Output Số hóa Trung tâm Học liệu Tiếng Việt Tập K – itemset ứng cử Độ tin cậy Cơ sở liệu Kho liệu Khoản mục Tập khoản mục Tập gồm K mục Kỹ thuật phát tri thức khai phá liệu Tập K - itemset phổ biến Độ tin cậy tối thiểu Độ hỗ trợ tối thiểu Phân tích trực tuyến Phân tích đa chiều trực tuyến Phân tích quan hệ trực tuyến Tiếp đầu dãy có độ dài k s Bản ghi Độ hỗ trợ Định danh giao tác Ngơn ngữ truy vấn có cấu trúc Tối ƣu truy vấn ngữ nghĩa Thuật toán phân lớp dựa vào vị trí địa phƣơng Thuật tốn phân lớp (tổng quát) Đối tƣợng liệu Active X Tìm kiếm theo chiều sâu Tìm kiếm theo chiều rộng Bảng băm trực tiếp cắt tỉa Bảng băm lý tƣởng cắt tỉa Vào/ra Bảo hiểm xã hội Bảo hiểm y tế Khám chữa bệnh http://www.lrc-tnu.edu.vn/ DANH MỤC CÁC BẢNG Trang Bảng 1.1 So sánh nhiệm vụ phát tri thức 16 Bảng 2.1 Ví dụ sở liệu dạng giao dịch D 27 Bảng 2.2 Các tập phổ biến sở liệu bảng 2.1 với độ hỗ trợ tối thiểu 50% 28 Bảng Kết minh họa chạy thuật toán Apriori 70 DANH MỤC CÁC HÌNH VẼ, ĐỒ THỊ Trang Hình 1.1 Quy trình phát tri thức Hình 2.1 Dàn cho tập I = {1,2,3,4} 36 Hình 2.2 Cây cho tập I = {1, 2, 3, 4} 37 Hình 2.3 Hệ thống hóa giải thuật 39 Hình 2.4 Ví dụ thuật tốn Apriori 45 Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ MỞ ĐẦU Ngày nay, thông tin đƣợc coi tài sản quan trọng tổ chức, doanh nghiệp cá nhân Cá nhân tổ chức thu thập hiểu đƣợc thông tin, hành động kịp thời dựa thơng tin có đƣợc kết tốt lĩnh vực sản xuất, kinh doanh, quản lý Chính lý đó, việc tạo thông tin, tổ chức lƣu trữ khai thác thông tin ngày trở nên quan trọng gia tăng không ngừng Sự tăng trƣởng vƣợt bậc sở liệu (CSDL) hoạt động nhƣ: sản xuất kinh doanh, thƣơng mại, quản lý làm nảy sinh thúc đẩy phát triển kỹ thuật thu thập, lƣu trữ, phân tích khai phá liệu… không phƣơng pháp thông thƣờng nhƣ: thống kê mà địi hỏi cách xử lý thơng minh hơn, hiệu Từ nhà quản lý có đƣợc thơng tin hữu ích để tác động lại q trình sản xuất, kinh doanh mình… tri thức Các kỹ thuật cho phép ta khai thác đƣợc tri thức hữu dụng từ CSDL (lớn) đƣợc gọi kỹ thuật khai phá liệu (DM – Data Mining) Khai phá luật kết hợp nội dung quan trọng khai phá liệu Luận văn tìm hiểu luật kết hợp ứng dụng thử nghiệm khai phá sở liệu Bảo hiểm y tế nhằm hỗ trợ cho công tác quản lý, sử dụng quỹ BHYT tỉnh Bắc Giang Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ Chƣơng TỔNG QUAN VỀ KHAI PHÁ DỮ LIỆU 1.1 Tổ chức khai thác sở liệu truyền thống Việc dùng phƣơng tiện tin học để tổ chức khai thác sở liệu (CSDL) đƣợc phát triển từ năm 60 kỉ trƣớc Từ nay, nhiều CSDL đƣợc tổ chức, phát triển khai thác quy mô lĩnh vực hoạt động ngƣời xã hội Cho đến nay, số lƣợng CSDL trở nên khổng lồ bao gồm CSDL cực lớn cỡ gigabytes chí terabytes lƣu trữ liệu kinh doanh ví dụ nhƣ liệu thơng tin khách hàng, liệu bán hàng, liệu tài khoản, Nhiều hệ quản trị CSDL mạnh với công cụ phong phú thuận tiện giúp ngƣời khai thác có hiệu nguồn tài ngun liệu Mơ hình CSDL quan hệ ngơn ngữ (SQL) có vai trò quan trọng việc tổ chức khai thác CSDL Tuy nhiên bên cạnh chức khai thác liệu có tính chất tác nghiệp, thành cơng cơng việc khơng cịn suất hệ thống thơng tin mà tính linh hoạt sẵn sàng đáp ứng yêu cầu thực tế, CSDL cần đem lại “tri thức” liệu Lúc này, mơ hình CSDL truyền thống ngơn ngữ SQL cho thấy khơng có khả thực cơng việc Để lấy thơng tin có tính “tri thức” khối liệu khổng lồ này, ngƣời ta tìm kỹ thuật có khả hợp liệu từ hệ thống giao dịch khác nhau, chuyển đổi thành tập hợp CSDL ổn định, có chất lƣợng đƣợc sử dụng cho riêng vài mục đích Các kỹ thuật gọi chung kỹ thuật tạo kho liệu (data warehousing) mơi trƣờng liệu có đƣợc gọi kho liệu (data warehouse) Đồng thời, Công nghệ khai phá liệu (data mining) đời đáp ứng đòi hỏi khoa học nhƣ hoạt động thực tiễn Đây ứng dụng để khai phá kho liệu nhằm phát tri thức (Knowledge Discovery) phục vụ công tác quản lý, kinh doanh,… 1.2 Tổng quan kỹ thuật phát tri thức khai phá liệu Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ Chúng ta xem tri thức nhƣ thơng tin tích hợp, bao gồm kiện mối quan hệ chúng Các mối quan hệ đƣợc hiểu ra, đƣợc phát hiện, đƣợc học Nói cách khác, tri thức đƣợc coi liệu có độ trừu tƣợng tổ chức cao Phát tri thức sở liệu qui trình nhận biết mẫu mơ hình liệu với tính năng: hợp thức, mới, khả ích, hiểu đƣợc Còn khai phá liệu bƣớc qui trình phát tri thức gồm có thuật toán khai thác liệu chuyên dùng dƣới số qui định hiệu tính tốn chấp nhận đƣợc để tìm mẫu mơ hình liệu Nói cách khác, mục đích phát tri thức khai phá liệu tìm mẫu và/hoặc mơ hình tồn sở liệu nhƣng bị che khuất hàng núi liệu Định nghĩa: Phát tri thức khai phá liệu (KDD: Knowledge Discovery and Data Mining) q trình khơng tầm thƣờng nhận mẫu có giá trị, mới, hữu ích tiềm hiểu đƣợc liệu [7] Cịn nhà thống kê xem Khai phá liệu nhƣ qui trình phân tích đƣợc thiết kế để thăm dò lƣợng cực lớn liệu nhằm phát mẫu thích hợp và/hoặc mối quan hệ mang tính hệ thống biến sau hợp thức hố kết tìm đƣợc cách áp dụng mẫu phát đƣợc cho tập liệu Qui trình bao gồm ba giai đoạn bản: thăm dị, xây dựng mơ hình định nghĩa mẫu, hợp thức/kiểm chứng 1.2.1 Qui trình khai phá liệu phát tri thức Qui trình phát tri thức đƣợc mơ tả tóm tắt Hình 1: Hình 1.1 Quy trình phát tri thức Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 10 Bƣớc thứ nhất: Hình thành, xác định định nghĩa tốn Là tìm hiểu lĩnh vực ứng dụng từ hình thành tốn, xác định nhiệm vụ cần phải hồn thành Bƣớc định cho việc rút đƣợc tri thức hữu ích cho phép chọn phƣơng pháp khai phá liệu thích hợp với mục đích ứng dụng chất liệu Bƣớc thứ hai: Thu thập tiền xử lý liệu Là thu thập xử lý thơ, cịn đƣợc gọi tiền xử lý liệu nhằm loại bỏ nhiễu, xử lý việc thiếu liệu, biến đổi liệu rút gọn liệu cần thiết, bƣớc thƣờng chiếm nhiều thời gian toàn qui trình phát tri thức Bƣớc thứ ba: Khai phá liệu, rút tri thức Là trích mẫu và/hoặc mơ hình ẩn dƣới liệu Giai đoạn quan trọng, bao gồm công đoạn nhƣ: chức năng, nhiệm vụ mục đích khai phá liệu, dùng phƣơng pháp khai phá nào? Bƣớc thứ tƣ: Sử dụng tri thức phát đƣợc Là hiểu tri thức tìm đƣợc, đặc biệt làm sáng tỏ mô tả dự đốn Các bƣớc lặp lặp lại số lần, kết thu đƣợc đƣợc lấy trung bình tất lần thực Tóm lại: KDD q trình chiết xuất tri thức từ kho liệu mà khai phá liệu công đoạn quan trọng 1.2.2 Các lĩnh vực liên quan đến khai phá liệu phát tri thức Khai phá liệu phát tri thức liên quan đến nhiều ngành, nhiều lĩnh vực: thống kê, trí tuệ nhân tạo, sở liệu, thuật tốn học, tính tốn song song tốc độ cao, thu thập tri thức cho hệ chuyên gia, quan sát liệu Đặc biệt Phát tri thức khai phá liệu gần gũi với lĩnh vực thống kê, sử dụng phƣơng pháp thống kê để mơ hình liệu phát mẫu, luật Kho liệu (Data Warehousing) cơng cụ phân tích trực tuyến (OLAP) liên quan chặt chẽ với Phát tri thức khai phá liệu Khai phá liệu có nhiều ứng dụng thực tế Một số ứng dụng điển hình nhƣ: Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 76 [11] Yao H., Hamilton H J (2006), “Mining Itemsets Utilities from Transaction Databases”, Data and Knowledge Engeneering, Vol 59, issue 3, pp 603-626 Xác nhận Giáo viên hƣớng dẫn Thái Nguyên, ngày 15 tháng năm 2013 Học viên TS Phùng Văn Ổn Nguyễn Ngọc Hải Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 77 PHỤ LỤC 3.3 Thiết kế ứng dụng 3.3.1 Chƣơng trình có chức sau: Hình Chạy thuật tốn Apriori 3.3.3 Mã nguồn chƣơng trình 3.3.3.1 frmApriori ‘ Khai báo sử dụng thư viện Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Linq Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 78 Imports System.Text Imports System.Threading.Tasks Imports System.Windows.Forms Imports Apriori_Sample.codeding.Apriori.DataStructures Imports System.Data.OleDb Imports System.Data.SqlClient Public Class frmApriori #Region "Fields" Private _items As Itemset Private _db As ItemsetCollection #End Region Public Sub New() InitializeComponent() SetSampleData() End Sub #Region "Internal-Methods" Private Sub SetSampleData() 'sample items _items = New Itemset() _items.Add("Beer") _items.Add("Diaper") _items.Add("Baby Powder") _items.Add("Bread") _items.Add("Umbrella") _items.Add("Milk") _items.Add("Detergent") _items.Add("Coca-Cola") ItemsTextBox.Text = String.Join(",", _items.ToArray()) 'sample database _db = New ItemsetCollection() _db.Add(New Itemset() From { _ _items(0), _ _items(1), _ _items(2), _ _items(3), _ _items(4) _ Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 79 }) _db.Add(New Itemset() From { _ _items(1), _ _items(2) _ }) _db.Add(New Itemset() From { _ _items(0), _ _items(1), _ _items(5) _ }) _db.Add(New Itemset() From { _ _items(1), _ _items(0), _ _items(6) _ }) _db.Add(New Itemset() From { _ _items(0), _ _items(5), _ _items(7) _ }) DatabaseTextBox.Text = _db.ToString() TransactionsTextBox.Text = _db.Count.ToString() SupportThresholdTextBox.Text = "40.00" ConfidenceThresholdTextBox.Text = "70.00" End Sub Private Sub AddResultLine(text As String) ResultTextBox.Text += text & vbCr & vbLf End Sub #End Region #Region "Control-Events" Private Sub GenerateDbButton_Click(sender As Object, e As EventArgs) Handles GenerateDbButton.Click 'get items specified by user Dim items As New Itemset() items.AddRange(ItemsTextBox.Text.Split(","c)) Dim transactionCount As Integer = Integer.TryParse(TransactionsTextBox.Text, transactionCount) Dim rnd As New Random() 'create random transactions Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 80 _db = New ItemsetCollection() Dim transactionIndex As Integer = While transactionIndex < transactionCount Dim itemCount As Integer = rnd.[Next](items.Count) Dim transaction As New Itemset() For itemIndex As Integer = To itemCount - Dim randomItemIndex As Integer = rnd.[Next](itemCount) If Not transaction.Contains(items(randomItemIndex)) Then transaction.Add(items(randomItemIndex)) End If Next If transaction.Count > Then _db.Add(transaction) transactionIndex += End If End While DatabaseTextBox.Text = _db.ToString() End Sub Private Sub SetSampleDataKCB(top As String, bg As String, en As String, dv As String) 'sample items loadItem() 'sample database _db = New ItemsetCollection() Dim ds As New DataSet() Dim sqlstr1 As String = _ " select top " & top & " do_tuoi,gioi_tinh,rtrim(ma_icd)as ma_icd, " & _ " case when tong_tien between 1001000 and 2000000 then '1001000-2000000' when tong_tien between 2001000 and 3000000 then '2001000-3000000' when tong_tien>3000000 then '3001000-Max' else '0-1000000' end as tong_tien" & _ " from tpkcb where ngay_tt between '" & bg & "' and '" & en & "' and ma_icd in('I10','J02','K29','G45','L20')" ds = SelectSqlSrvRows(ds, cn, sqlstr1) For Each row As DataRow In ds.Tables(0).Rows _db.Add(New Itemset() From {row(0).ToString, row(1).ToString, row(2).ToString, row(3).ToString}) Next DatabaseTextBox.Text = _db.ToString() Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 81 TransactionsTextBox.Text = _db.Count.ToString() 'sample support, confidence SupportThresholdTextBox.Text = "40.00" ConfidenceThresholdTextBox.Text = "70.00" End Sub Public Function SelectOleDbSrvRows(dataSet As DataSet, connection As String, query As String) As DataSet Dim conn As New OleDbConnection(connection) Dim adapter As New OleDbDataAdapter() adapter.SelectCommand = New OleDbCommand(query, conn) adapter.Fill(dataSet) Return dataSet End Function Public Function SelectSqlSrvRows(dataSet As DataSet, connection As String, query As String) As DataSet Dim conn As New SqlConnection(connection) Dim adapter As New SqlDataAdapter() adapter.SelectCommand = New SqlCommand(query, conn) adapter.Fill(dataSet) Return dataSet End Function Private Sub DoAprioriButton_Click(sender As Object, e As EventArgs) Handles DoAprioriButton.Click ResultTextBox.Text = String.Empty 'get items Dim uniqueItems As Itemset = _db.GetUniqueItems() AddResultLine("Items: " & uniqueItems.ToString()) AddResultLine(String.Empty) 'do apriori Dim supportThreshold As Double = Double.Parse(SupportThresholdTextBox.Text) Dim L As ItemsetCollection = AprioriMining.DoApriori(_db, supportThreshold) AddResultLine(L.Count.ToString + " Large Itemsets (by Apriori)") For Each itemset As Itemset In L AddResultLine(itemset.ToString()) Next AddResultLine(String.Empty) 'do mining Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 82 Dim confidenceThreshold As Double = Double.Parse(ConfidenceThresholdTextBox.Text) Dim allRules As List(Of AssociationRule) = AprioriMining.Mine(_db, L, confidenceThreshold) AddResultLine(allRules.Count.ToString + " Association Rules") For Each rule As AssociationRule In allRules AddResultLine(rule.ToString()) Next End Sub Private Sub LoadData_button_Click(sender As Object, e As EventArgs) Handles LoadData_button.Click Using frm As New frmInput If frm.ShowDialog(Me.FindForm()) = Windows.Forms.DialogResult.OK Then SetSampleDataKCB(frm.txtTop.Text, frm.txtBgDate.Text, frm.txtEndate.Text, frm.cbbDonvi.Text) End If End Using End Sub Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click SetSampleData() End Sub Private Sub loadItem() Dim sqlstr As String = "select item from tblItem" Dim ds As New DataSet() 'ds = SelectOleDbSrvRows(ds, cn, sqlstr) ds = SelectSqlSrvRows(ds, cn, sqlstr) _items = New Itemset() For Each row As DataRow In ds.Tables(0).Rows _items.Add(row("item").ToString.Trim) Next ItemsTextBox.Text = String.Join(",", _items.ToArray()) End Sub #End Region End Class 3.3.3.2 Class AprioriMining Imports System.Collections.Generic Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 83 Imports System.Linq Imports System.Text Namespace codeding.Apriori.DataStructures Public Class AprioriMining Public Shared Function DoApriori(db As ItemsetCollection, supportThreshold As Double) As ItemsetCollection Dim I As Itemset = db.GetUniqueItems() Dim L As New ItemsetCollection() 'resultant large itemsets Dim Li As New ItemsetCollection() 'large itemset in each iteration Dim Ci As New ItemsetCollection() 'pruned itemset in each iteration 'first iteration (1-item itemsets) For Each item As String In I Ci.Add(New Itemset() From { _ item _ }) Next 'next iterations Dim k As Integer = While Ci.Count 'set Li from Ci (pruning) Li.Clear() For Each itemset As Itemset In Ci itemset.Support = db.FindSupport(itemset) If itemset.Support >= supportThreshold Then Li.Add(itemset) L.Add(itemset) End If Next 'set Ci for next iteration (find supersets of Li) Ci.Clear() Ci.AddRange(Bit.FindSubsets(Li.GetUniqueItems(), k)) 'get k-item subsets k += End While Return (L) End Function Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 84 Public Shared Function Mine(db As ItemsetCollection, L As ItemsetCollection, confidenceThreshold As Double) As List(Of AssociationRule) Dim allRules As New List(Of AssociationRule)() For Each itemset As Itemset In L Dim subsets As ItemsetCollection = Bit.FindSubsets(itemset, 0) 'get all subsets For Each subset As Itemset In subsets Dim confidence As Double = (db.FindSupport(itemset) / db.FindSupport(subset)) * 100.0 If confidence >= confidenceThreshold Then Dim rule As New AssociationRule() rule.X.AddRange(subset) rule.Y.AddRange(itemset.Remove(subset)) rule.Support = db.FindSupport(itemset) rule.Confidence = confidence If rule.X.Count > AndAlso rule.Y.Count > Then allRules.Add(rule) End If End If Next Next Return (allRules) End Function End Class End Namespace 3.3.3.3 Class AssociationRule Imports System.Collections.Generic Imports System.Linq Imports System.Text Namespace codeding.Apriori.DataStructures Public Class AssociationRule #Region "Properties" Public Property X() As Itemset Get Return m_X End Get Set(value As Itemset) m_X = Value End Set Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 85 End Property Private m_X As Itemset Public Property Y() As Itemset Get Return m_Y End Get Set(value As Itemset) m_Y = Value End Set End Property Private m_Y As Itemset Public Property Support() As Double Get Return m_Support End Get Set(value As Double) m_Support = Value End Set End Property Private m_Support As Double Public Property Confidence() As Double Get Return m_Confidence End Get Set(value As Double) m_Confidence = Value End Set End Property Private m_Confidence As Double #End Region #Region "Constructors" Public Sub New() X = New Itemset() Y = New Itemset() Support = 0.0 Confidence = 0.0 End Sub #End Region #Region "Methods" Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 86 Public Overrides Function ToString() As String Return (Convert.ToString(X) & " => " & Convert.ToString(Y) & " (support: " & Math.Round(Support, 2) & "%, confidence: " & Math.Round(Confidence, 2) & "%)") End Function #End Region End Class End Namespace 3.3.3.4 Class Bit 'Bit.cs Imports System.Collections.Generic Imports System.Linq Imports System.Text Namespace codeding.Apriori.DataStructures Public Class Bit Public Shared Function FindSubsets(itemset As Itemset, n As Integer) As ItemsetCollection Dim subsets As New ItemsetCollection() Dim subsetCount As Integer = CInt(Math.Truncate(Math.Pow(2, itemset.Count))) For i As Integer = To subsetCount - If n = OrElse GetOnCount(i, itemset.Count) = n Then Dim binary As String = DecimalToBinary(i, itemset.Count) Dim subset As New Itemset() For charIndex As Integer = To binary.Length - If binary(charIndex) = "1" Then subset.Add(itemset(charIndex)) End If Next subsets.Add(subset) End If Next Return (subsets) End Function Public Shared Function GetBit(value As Integer, position As Integer) As Integer Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 87 Dim bit As Integer = value And CInt(Math.Truncate(Math.Pow(2, position))) Return (If(bit > 0, 1, 0)) End Function Public Shared Function DecimalToBinary(value As Integer, length As Integer) As String Dim binary As String = String.Empty For position As Integer = To length - binary = GetBit(value, position) & binary Next Return (binary) End Function Public Shared Function GetOnCount(value As Integer, length As Integer) As Integer Dim binary As String = DecimalToBinary(value, length) Return (From c In binary.ToCharArray() Where c = "1").Count() End Function End Class End Namespace 3.3.3.5 Class Itemset 'Itemset.vb Imports System.Collections.Generic Imports System.Linq Imports System.Text Namespace codeding.Apriori.DataStructures Public Class Itemset Inherits List(Of String) #Region "Properties" Public Property Support() As Double Get Return m_Support End Get Set(value As Double) m_Support = Value End Set End Property Private m_Support As Double #End Region Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 88 #Region "Methods" Public Overloads Function Contains(itemset As Itemset) As Boolean Return (Me.Intersect(itemset).Count() = itemset.Count) End Function Public Overloads Function Remove(itemset As Itemset) As Itemset Dim removed As New Itemset() removed.AddRange(From item In Me Where Not itemset.Contains(item) Select item) Return (removed) End Function Public Overrides Function ToString() As String Return ("{" & String.Join(", ", Me.ToArray()) & "}" & (If(Me.Support > 0, " (support: " & Math.Round(Me.Support, 2) & "%)", String.Empty))) End Function #End Region End Class End Namespace 3.3.3.6 Class ItemsetCollection 'ItemsetCollection.vb Option Explicit On Option Strict On Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Namespace codeding.Apriori.DataStructures Public Class ItemsetCollection Inherits List(Of Itemset) #Region "Methods" Public Function GetUniqueItems() As Itemset Dim unique As New Itemset() For Each itemset As Itemset In Me unique.AddRange(From item In itemset Where Not unique.Contains(item) Select item) Next Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 89 Return (unique) End Function Public Function FindSupport(item As String) As Double Dim matchCount As Integer = (From itemset In Me Where itemset.Contains(item) Select itemset).Count() Dim support As Double = (CDbl(matchCount) / CDbl(Me.Count)) * 100.0 Return (support) End Function Public Function FindSupport(itemset As Itemset) As Double Dim matchCount As Integer = (From i In Me Where i.Contains(itemset) Select i).Count() Dim support As Double = (CDbl(matchCount) / CDbl(Me.Count)) * 100.0 Return (support) End Function Public Overloads Overrides Function ToString() As String Return (String.Join(vbCr & vbLf, (From itemset In Me Select (itemset.ToString())).ToArray())) End Function #End Region End Class End Namespace 3.3.3.7 Module SysModule Imports System.Data.SqlClient Imports Microsoft.VisualBasic Module SysModule Public cn As String Function getNo(ByVal strSQL As String, ByVal cn As String) As Integer Dim CnSQL As New SqlConnection(cn) Dim myDs As New DataSet Dim Tong As Integer Try CnSQL.Open() Dim sqlAdapter As New SqlDataAdapter(strSQL, CnSQL) Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/ 90 sqlAdapter.Fill(myDs, strSQL) Tong = myDs.Tables(0).Rows.Count Catch Vb As Exception Finally If (CnSQL.State = ConnectionState.Open) Then CnSQL.Close() End If End Try Return Tong End Function Public Function fcConvertString(ByVal strValue As Date) As String If strValue.ToString = "" Then Return "" Else Return IIf(Day(strValue).ToString.Length = 1, "0" & Day(strValue), Day(strValue)) & "/" & IIf(Month(strValue).ToString.Length = 1, "0" & Month(strValue), Month(strValue)) & "/" & Year(strValue) End If End Function End Module Số hóa Trung tâm Học liệu http://www.lrc-tnu.edu.vn/

Ngày đăng: 18/10/2023, 15:41

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN