0
Tải bản đầy đủ (.pdf) (60 trang)

Xây dựng khung ứng dụng (Framework)

Một phần của tài liệu KHAI PHÁ DỮ LIỆU PHÁT HIỆN LUẬT KẾT HỢP VÀ ỨNG DỤNG ĐỐI VỚI KHO DỮ LIỆU CỦA NGÂN HÀNG (Trang 46 -46 )

Xuất phát từ cơ sở lý thuyết đã trình bày ở trên, khung ứng dụng của chúng ta sẽ gồm có 3 phần:

(1)Tập hợp, làm sạch và chuẩn hoá dữ liệu theo các tiêu chí quan tâm. (2)Xây dựng Data-cube.

(3)Khai phá luật kết hợp từ Data-cube.

3.2.1. Tập hợp, làm sạch và chuẩn hoá dữ liệu

Trong giai đoạn này dữ liệu từ CSDL ban đầu sẽ được tập hợp rồi làm mịn, rời rạc hoá và chuẩn hoá lại theo các tiêu chí mà người sử dụng quan tâm.

Ứng với giai đoạn này sẽ có các bước:

1.Tập hợp dữ liệu thô

Tại bước này, người sử dụng phải thực hiện tập hợp dữ liệu từ nhiều nguồn để đưa vào một bảng dữ liệu thô duy nhất.

45

Hình 3.1: Minh hoạ dữ liệu thô.

2.Xây dựng các tiêu chí để lọc bớt và rời rạc hoá dữ liệu

Giả sử dữ liệu thô thống kê giao dịch của khách hàng trong năm có rất nhiều trường:

CustomerID, CustomerName, Gender, Age, AverageBalance, TransactionCount, Address, AccountType, .... Nhưng ta chỉ quan tâm tới các trường như Gender, Age, AverageBalance, Address, AccountType, ... thì dữ liệu khi thực hiện chuẩn hoá sẽ được gộp nhóm (Group by) theo các trường này mà thôi.

Tương tự như vậy ta thực hiện rời rạc và hữu hạn hoá miền giá trị của các trường, ví dụ: ta chia miền giá trị của trường Age thành 6 miền: VeryYoung (Dưới 20), Young (20-30), Normal (30-40), MiddleAged (40-50), Old (50-60), VeryOld (Trên 60).

Hình 3.2: Minh hoạ các tiêu chí lọc và rời rạc hoá dữ liệu.

3.Thực hiện chuẩn hoá dữ liệu dựa trên các tiêu chí lọc và tiêu chí rời rạc hoá Dưới đây là đoạn mã thực hiện chuẩn hoá dữ liệu:

Bảng 3.1: Đoạn mã thực hiện chuẩn hoá dữ liệu.

privatevoid tsbtnStandardizeDataBuild_Click(object sender, EventArgs e) { try { // Temp: _dtAttributes.AcceptChanges(); _dtAttributesDetail.AcceptChanges(); #region // Build Sql: string strSqlFields = "";

for (int i = 0; i < _dtAttributes.Rows.Count; i++) {

DataRow drAttribute = _dtAttributes.Rows[i];

if (Convert.ToInt32(drAttribute["SELECTED"]) == 0) continue;

string strFilterAttributeDetail = string.Format("(ATTRIBUTE = '{0}')", drAttribute["ATTRIBUTE"]); DataRow[] arrdrAttributeDetail = _dtAttributesDetail.Select(strFilterAttributeDetail);

string strSqlCase = "";

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

if (string.Equals(arrdrAttributeDetail[j]["VALUEFROM"], arrdrAttributeDetail[j]["VALUETO"])) {

47 when t.{0} = '{1}' then '{2}' " , arrdrAttributeDetail[j]["ATTRIBUTE"] , arrdrAttributeDetail[j]["VALUEFROM"] , arrdrAttributeDetail[j]["VALUELABEL"] ); } else {

strSqlCase += string.Format(@"

when t.{0} >= '{1}' and t.{0} < '{2}' then '{3}' " , arrdrAttributeDetail[j]["ATTRIBUTE"] , arrdrAttributeDetail[j]["VALUEFROM"] , arrdrAttributeDetail[j]["VALUETO"] , arrdrAttributeDetail[j]["VALUELABEL"] ); } }

strSqlFields += string.Format(@" , (

case

when 0 = 1 then null {0} else '{1}' end ) {2} " , strSqlCase , drAttribute["ATTRIBUTEDEFAULTVALUE"] , drAttribute["ATTRIBUTE"] ); } // for i

string strSqlStandardize = string.Format(@" select 1 MYCOUNT {0} into DataStardardFor{1} from {1} t ; " , strSqlFields , _strTableName ); #endregion

// Clear old data and create new Standard data:

coreClearOldData(string.Format("DataStardardFor{0}", _strTableName)); _db.ExecQuery(strSqlStandardize);

// Return good:

MessageBox.Show("Build standard data successfully.", "Simple OLAP Mining"); }

catch (Exception exc) {

CProcessException.Process(exc); }

}

Hình 3.3: Dữ liệu sau khi chuẩn hoá (Xem dạng Grid).

3.2.2. Xây dựng Data-cube

Trong giai đoạn này chúng ta thực hiện xây dựng Data-cube từ dữ liệu đã được chuẩn hoá ở trên, cụ thể OLAP-engine sẽ thực hiện tính toán các toán tử nhóm (Aggregation operator) đồng thời lưu trữ kết quả tính toán được vào Data-cube.

Dưới đây là đoạn mã thực hiện xây dựng Data-cube:

Bảng 3.2: Đoạn mã thực hiện xây dựng Data-cube.

privatevoid tsbtnDataCubeBuild_Click(object sender, EventArgs e) {

try

{

// Temp:

#region // Build Sql:

string strSqlGetStardardSchema = string.Format(@" select t.* from {0} t where (0=1) ; ", "DataStardardFor" + _strTableName );

DataTable dtStardardSchema = _db.ExecQuery(strSqlGetStardardSchema).Tables[0]; string strFields = "";

string strGroupBy = "";

for (int i = 0; i < dtStardardSchema.Columns.Count; i++) {

DataColumn dc = dtStardardSchema.Columns[i];

49 , IsNull(t.{0}, '*') {0} ", dc.ColumnName ); strGroupBy += ", " + dc.ColumnName; }

strGroupBy = strGroupBy.Substring(", ".Length); string strSqlBuildDataCube = string.Format(@" select Sum(t.MYCOUNT) MYCOUNT {0} into DataCubeFor{1} from DataStardardFor{1} t group by {2} with cube order by {2} ; " , strFields , _strTableName , strGroupBy ); #endregion

// Clear old data and create new Data cube:

coreClearOldData(string.Format("DataCubeFor{0}", _strTableName)); _db.ExecQuery(strSqlBuildDataCube);

// Return good:

MessageBox.Show("Build data cube successfully.", "Simple OLAP Mining"); }

catch (Exception exc) {

CProcessException.Process(exc); }

}

51

Một phần của tài liệu KHAI PHÁ DỮ LIỆU PHÁT HIỆN LUẬT KẾT HỢP VÀ ỨNG DỤNG ĐỐI VỚI KHO DỮ LIỆU CỦA NGÂN HÀNG (Trang 46 -46 )

×