public KMeans(object threadpool, IDataTable <DataRow> data, string[] properties, int maxcount, int minclustercount, int maxclustercount, double[] mean, double[] v, Protocol.Structure.WaitObject wt, int initialmode, int methodmode, int maxthread) { this.Data = data; this.Properties = properties; this.MaxCount = maxcount; this.DataCount = data.RowCount; this.ParaCount = properties.Length; this.Distences = new double[this.DataCount, this.ParaCount]; this.MaxClusterCount = maxclustercount; this.MinClusterCount = minclustercount; this.WaitObj = wt; this.InitialMode = initialmode; this.MethodMode = methodmode; this.Mean = mean; this.Stdev = v; this.MaxThreadCount = maxthread; this.Distences = new double[DataCount, DataCount]; this.ThreadPool = threadpool as List <Thread>; Parallel.For(0, this.DataCount - 1, new Action <int>((i) => { for (int j = i + 1; j < this.DataCount; j++) { var temp = Assess.GetEdistence(this.Data, i, j, this.Properties, this.ParaCount); this.Distences[i, j] = temp; this.Distences[j, i] = temp; } })); }
private void btnKMeans_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.ActiveDataForm != null && !this.ActiveDataForm.IsBusy) { var targetform = this.ActiveDataForm; var targettable = targetform.GetDataTable(); ConfigForm.KMeansForm configform = new ConfigForm.KMeansForm(targettable.GetColumnsList(false, typeof(string), typeof(DateTime), typeof(bool)), targettable.RowCount); if (configform.ShowDialog() == DialogResult.OK) { try { MdiForm.MdiKMeansResultForm resultform = new MdiForm.MdiKMeansResultForm(targetform); Protocol.Structure.WaitObject wt = new Protocol.Structure.WaitObject(); targetform.DoMethod("K均值聚类", (ThreadPool) => { resultform.Result = Kernel.BusinessLogicOperation.DataMiningOperation.KMeans(ThreadPool, targettable, configform.SelectedColumns, configform.MaxCount, configform.StartClustNum, configform.EndClustNum, configform.Avg, configform.Stdev, wt, configform.InitialMode, configform.MethodMode, configform.MaxThread); }, wt, () => { this.Invoke(new Action(() => { resultform.ShowResult(); resultform.Show(); })); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void btnTableSaveAs_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.ActiveDataForm != null && !this.ActiveDataForm.IsBusy) { var targetform = this.ActiveDataForm; SaveFileDialog configform = new SaveFileDialog(); configform.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); configform.Filter = "Excel 工作簿(*.xlsx)|*.xlsx|Excel 97-2003 工作簿(*.xls)|*.xls"; configform.FilterIndex = 1; configform.RestoreDirectory = true; if (configform.ShowDialog() == DialogResult.OK) { try { var targettable = targetform.GetDataTable(); Protocol.Structure.WaitObject wt = new Protocol.Structure.WaitObject(); targetform.DoMethod("导出Excel", (ThreadPool) => { Kernel.BusinessLogicOperation.DataAccessOperation.ExportTableToExcel(targettable, configform.FileName, wt); }, wt, () => { this.Invoke(new Action(() => { MessageBox.Show("导出成功"); })); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void btnEntropy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.ActiveDataForm != null && !this.ActiveDataForm.IsBusy) { var targetform = this.ActiveDataForm; var targettable = targetform.GetDataTable(); ConfigForm.AttributeSelectionForm configform = new ConfigForm.AttributeSelectionForm(targettable.Name, targettable.GetColumnsList(false, typeof(string), typeof(DateTime), typeof(bool))); if (configform.ShowDialog() == DialogResult.OK) { try { MdiForm.MdiEntropyResultForm resultform = new MdiForm.MdiEntropyResultForm(targetform); Protocol.Structure.WaitObject wt = new Protocol.Structure.WaitObject(); targetform.DoMethod("计算信息熵", (ThreadPool) => { resultform.Result = Kernel.BusinessLogicOperation.DataProcessOperation.Entropy(targettable, configform.SelectedColumns, wt); }, wt, () => { this.Invoke(new Action(() => { resultform.ShowResult(); resultform.Show(); })); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void btnSchmidtSelect_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.ActiveDataForm != null && !this.ActiveDataForm.IsBusy) { var targetform = this.ActiveDataForm; var targettable = targetform.GetDataTable(); ConfigForm.AttributeSelectionForm configform = new ConfigForm.AttributeSelectionForm(targettable.Name, targettable.GetColumnsList(false, typeof(string), typeof(DateTime), typeof(bool))); if (configform.ShowDialog() == DialogResult.OK) { try { MdiForm.MdiSchmidtResultForm resultform = new MdiForm.MdiSchmidtResultForm(targetform); Protocol.Structure.WaitObject wt = new Protocol.Structure.WaitObject(); targetform.DoMethod("施密特正交约减", (ThreadPool) => { resultform.Result = Kernel.BusinessLogicOperation.DataProcessOperation.Schmidt(targettable, configform.SelectedColumns); }, wt, () => { this.Invoke(new Action(() => { resultform.ShowResult(); resultform.Show(); })); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void btnSchmidtAnalys_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.ActiveDataForm != null && !this.ActiveDataForm.IsBusy) { var targetform = this.ActiveDataForm; var targettable = targetform.GetDataTable(); ConfigForm.SchmidtAnalysisConfigForm configform = new ConfigForm.SchmidtAnalysisConfigForm(targettable.Name, targettable.GetColumnsList(false, typeof(string), typeof(DateTime), typeof(bool))); if (configform.ShowDialog() == DialogResult.OK) { try { MdiForm.MdiDataViewForm resultform = new MdiForm.MdiDataViewForm(); Protocol.Structure.WaitObject wt = new Protocol.Structure.WaitObject(); targetform.DoMethod("施密特分析", (ThreadPool) => { var result = Kernel.BusinessLogicOperation.DataProcessOperation.SchmidtAnalysis(targettable, configform.SelectedColumns, configform.AreaSize, configform.AreaSpace); if (result.Item1 != Kernel.BusinessLogicOperation.DataProcessOperation.RESULTCODE.SUCCESS) { string errormsg = "程序发生错误"; switch (result.Item1) { case Kernel.BusinessLogicOperation.DataProcessOperation.RESULTCODE.OUTOFRANGE: { errormsg = "滑动区间超过数据范围"; break; } } MessageBox.Show(errormsg); resultform.AbortMethod(); } else { resultform.DataSource = result.Item2; } }, wt, () => { this.Invoke(new Action(() => { resultform.MdiParent = this; resultform.MdiIndex = this.DataFormIndex++; resultform.Caption = targetform.Caption + "_施密特分析结果"; resultform.Show(); })); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
public void DoMethod(string methodname, Action <object> method, Protocol.Structure.WaitObject waithandle, Action callback = null) { this.State = "正在执行:" + methodname; startWait(waithandle); (methodThread = new Thread((o) => { DateTime start = DateTime.Now; method(this.ThreadPool); this.CostTime = (DateTime.Now - start).TotalSeconds; endWait(); callback(); }) { IsBackground = true }).Start(); }
public static DataSet KMeans(object threadpool, IDataTable <DataRow> data, string[] properties, int maxCount, int minClusterCount, int maxClusterCount, double m, double s, Protocol.Structure.WaitObject wt, int initialmode, int methodmode, int maxthread) { minClusterCount = minClusterCount < 2 ? 2 : minClusterCount; maxClusterCount = maxClusterCount < 2 ? (int)Math.Pow(data.RowCount, 0.5) : maxClusterCount; maxCount = maxCount < 2 ? 20 : maxCount; maxthread = maxthread < 1 ? 1 : maxthread; int paracount = properties.Length; int i, j, k; double[] mean; double[] std; if (!double.IsNaN(m)) { mean = new double[paracount]; for (i = 0; i < paracount; i++) { mean[i] = m; } } else { mean = data.Avg(properties); } if (!double.IsNaN(s)) { std = new double[paracount]; for (i = 0; i < paracount; i++) { std[i] = s; } } else { std = data.Stdev(properties, mean); } var temp = new MPPO.DataMining.KMeans(threadpool, data, properties, maxCount, minClusterCount, maxClusterCount, mean, std, wt, initialmode, methodmode, maxthread); var clusterreport = temp.ParallelStart(); var report = new DataSet(); DataTable overview = new DataTable("OverView"); overview.Columns.Add(new DataColumn("序号", typeof(int))); overview.Columns.Add(new DataColumn("分类数", typeof(int))); overview.Columns.Add(new DataColumn("AvgBWP", typeof(double))); overview.Columns.Add(new DataColumn("耗时", typeof(double))); overview.Columns.Add(new DataColumn("循环次数", typeof(int))); DataTable centers = new DataTable("Centers"); int len = properties.Length; centers.Columns.Add(new DataColumn("序号", typeof(int))); centers.Columns.Add(new DataColumn("类标号", typeof(int))); for (i = 0; i < len; i++) { centers.Columns.Add(new DataColumn(properties[i], typeof(double))); } DataTable results = new DataTable("Results"); results.Columns.Add(new DataColumn("序号", typeof(int))); results.Columns.Add(new DataColumn("行号", typeof(int))); results.Columns.Add(new DataColumn("类标号", typeof(int))); results.Columns.Add(new DataColumn("BWP", typeof(double))); results.Columns.Add(new DataColumn("B", typeof(double))); results.Columns.Add(new DataColumn("W", typeof(double))); report.Tables.Add(overview); report.Tables.Add(centers); report.Tables.Add(results); report.Relations.Add(new DataRelation("OverView_Centers", overview.Columns[0], centers.Columns[0])); int count = clusterreport.HisResult.Count; int datacount = data.RowCount; for (i = 0; i < count; i++) { var tempresult = clusterreport.HisResult[i]; var tempreport = clusterreport.HisReport[i]; overview.Rows.Add(i, tempresult.cCount, tempreport.AvgBWP, tempresult.CostTime, tempresult.LoopCount); for (j = 0; j < tempresult.cCount; j++) { var temprow = centers.NewRow(); temprow[0] = i; temprow[1] = j; for (k = 0; k < len; k++) { temprow[k + 2] = tempresult.Centers[j, k]; } centers.Rows.Add(temprow); } for (j = 0; j < datacount; j++) { results.Rows.Add(i, j, tempresult.ClassNumbers[j], tempreport.BWP[j], tempreport.B[j], tempreport.W[j]); } } return(report); }
public static double[] GetEntropy(MPPO.Protocol.Interface.IDataTable <DataRow> data, string[] sparams, Protocol.Structure.WaitObject wt) { if (data == null || sparams == null) { return(null); } int paraCount = sparams.Length; int dataCount = data.RowCount; if (paraCount == 0 || dataCount == 0) { return(null); } wt.Flags = new int[1]; wt.Max = paraCount * dataCount; double[] result = new double[paraCount]; List <double>[] distinct = new List <double> [paraCount]; List <int>[] distinctCount = new List <int> [paraCount]; for (int i = 0; i < paraCount; i++) { distinct[i] = new List <double>(); distinctCount[i] = new List <int>(); string sparam = sparams[i]; for (int j = 0; j < dataCount; j++) { var temp = data[j]; var value = data[j, sparam].ConvertToDouble(); var index = distinct[i].IndexOf(value); if (index == -1) { distinct[i].Add(value); distinctCount[i].Add(1); } else { distinctCount[i][index]++; } wt.Flags[0]++; } int disLength = distinctCount[i].Count; for (int k = 0; k < disLength; k++) { int c = distinctCount[i][k]; double p = (double)c / dataCount; result[i] += p * Math.Log(p, 2); } result[i] = -result[i]; } return(result); }
public static void ExportTableToExcel(MPPO.Protocol.Interface.IDataTable <DataRow> targettable, string filepath, Protocol.Structure.WaitObject wt) { try { MPPO.DataAccess.BasicExcelOperation.ExportToExcel(targettable, filepath, wt); } catch (Exception ex) { throw ex; } finally { GC.Collect(); } }
public static void ExportToExcel(IDataTable <DataRow> data, string filename, Protocol.Structure.WaitObject wt) { if (data == null) { return; } var excel = new ApplicationClass(); if (excel == null) { throw new Exception("Excel无法启动"); } int rowNum = data.RowCount; string[] columns = data.GetColumnsList(); int columnNum = columns.Length; wt.Flags = new int[1]; wt.Max = rowNum * columnNum; int rowIndex = 1; int columnIndex = 0; var book = excel.Application.Workbooks.Add(true); try { foreach (var column in columns) { columnIndex++; excel.Cells[rowIndex, columnIndex] = column; if (data.GetColumnType(column) == typeof(string)) { excel.get_Range(excel.Cells[rowIndex + 1, columnIndex], excel.Cells[rowNum + 1, columnIndex]).NumberFormatLocal = "@"; } } for (int i = 0; i < rowNum; i++) { rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++) { columnIndex++; excel.Cells[rowIndex, columnIndex] = data[i, columns[j]]; wt.Flags[0]++; } } excel.DisplayAlerts = false; excel.AlertBeforeOverwriting = false; book.SaveCopyAs(filename); } catch (Exception ex) { throw ex; } finally { book.Close(false); book = null; excel.Quit(); excel = null; } }
public static List <Tuple <string, double> > Entropy(IDataTable <DataRow> data, List <string> columns, Protocol.Structure.WaitObject wt) { var result = new List <Tuple <string, double> >(); var columnsarray = columns.ToArray(); var re = MPPO.DataProcess.Entropy.GetEntropy(data, columnsarray, wt); int count = re.Length; for (int i = 0; i < count; i++) { result.Add(new Tuple <string, double>(columns[i], re[i])); } result.Sort(new Comparison <Tuple <string, double> >((a1, a2) => { return(a1.Item2.CompareTo(a2.Item2)); })); return(result); }
private void btnTableSaveAs_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.ActiveDataForm!=null&&!this.ActiveDataForm.IsBusy) { var targetform = this.ActiveDataForm; SaveFileDialog configform = new SaveFileDialog(); configform.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); configform.Filter = "Excel 工作簿(*.xlsx)|*.xlsx|Excel 97-2003 工作簿(*.xls)|*.xls"; configform.FilterIndex = 1; configform.RestoreDirectory = true; if (configform.ShowDialog() == DialogResult.OK) { try { var targettable = targetform.GetDataTable(); Protocol.Structure.WaitObject wt = new Protocol.Structure.WaitObject(); targetform.DoMethod("导出Excel", (ThreadPool) => { Kernel.BusinessLogicOperation.DataAccessOperation.ExportTableToExcel(targettable, configform.FileName, wt); }, wt, () => { this.Invoke(new Action(() => { MessageBox.Show("导出成功"); })); }); } catch(Exception ex) { MessageBox.Show(ex.Message); } } } }
private void btnSchmidtAnalys_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.ActiveDataForm != null && !this.ActiveDataForm.IsBusy) { var targetform = this.ActiveDataForm; var targettable = targetform.GetDataTable(); ConfigForm.SchmidtAnalysisConfigForm configform = new ConfigForm.SchmidtAnalysisConfigForm(targettable.Name, targettable.GetColumnsList(false, typeof(string), typeof(DateTime), typeof(bool))); if (configform.ShowDialog() == DialogResult.OK) { try { MdiForm.MdiDataViewForm resultform = new MdiForm.MdiDataViewForm(); Protocol.Structure.WaitObject wt = new Protocol.Structure.WaitObject(); targetform.DoMethod("施密特分析", (ThreadPool) => { var result = Kernel.BusinessLogicOperation.DataProcessOperation.SchmidtAnalysis(targettable, configform.SelectedColumns, configform.AreaSize, configform.AreaSpace); if(result.Item1 != Kernel.BusinessLogicOperation.DataProcessOperation.RESULTCODE.SUCCESS) { string errormsg = "程序发生错误"; switch(result.Item1) { case Kernel.BusinessLogicOperation.DataProcessOperation.RESULTCODE.OUTOFRANGE: { errormsg = "滑动区间超过数据范围"; break; } } MessageBox.Show(errormsg); resultform.AbortMethod(); } else { resultform.DataSource = result.Item2; } }, wt, () => { this.Invoke(new Action(() => { resultform.MdiParent = this; resultform.MdiIndex = this.DataFormIndex++; resultform.Caption = targetform.Caption + "_施密特分析结果"; resultform.Show(); })); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void btnKMeans_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.ActiveDataForm != null && !this.ActiveDataForm.IsBusy) { var targetform = this.ActiveDataForm; var targettable = targetform.GetDataTable(); ConfigForm.KMeansForm configform = new ConfigForm.KMeansForm(targettable.GetColumnsList(false, typeof(string), typeof(DateTime), typeof(bool)), targettable.RowCount); if (configform.ShowDialog() == DialogResult.OK) { try { MdiForm.MdiKMeansResultForm resultform = new MdiForm.MdiKMeansResultForm(targetform); Protocol.Structure.WaitObject wt = new Protocol.Structure.WaitObject(); targetform.DoMethod("K均值聚类", (ThreadPool) => { resultform.Result = Kernel.BusinessLogicOperation.DataMiningOperation.KMeans(ThreadPool,targettable, configform.SelectedColumns, configform.MaxCount, configform.StartClustNum, configform.EndClustNum, configform.Avg, configform.Stdev, wt, configform.InitialMode, configform.MethodMode, configform.MaxThread); }, wt, () => { this.Invoke(new Action(() => { resultform.ShowResult(); resultform.Show(); })); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }