⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 采用类平均法实现聚类分析的算法
💻 VB
📖 第 1 页 / 共 2 页
字号:
Imports System
Imports System.Data
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D


Public Class jlfx2
    Inherits System.Windows.Forms.Form

    Private myds As System.Data.DataSet = New DataSet("聚类分析")
    Private yushuju As New DataTable("原数据")
    '原数据表 
    Private biaozhun As New DataTable("标准化数据")
    '标准化数据表 
    Private juli As New DataTable("距离数据")
    '距离表 
    Private julimatrix As Double(,)
    '距离阵 
    Private Gramatrix As Double(,)
    '为画图存储数据 
    Private zuobiao As Single(,)
    Private newClassCount As Int32 = 0
    Private zhibiao As String()
    Private julimethod As Integer = 0
    '计算距离的方法 
    Private sta_method As Integer = 0
    '标准化的方法 
    'private int coordinate_x=0,coordinate_y=0; 

    Private tabControl1 As System.Windows.Forms.TabControl
    Private tabPage1 As System.Windows.Forms.TabPage


    Private tabPage2 As System.Windows.Forms.TabPage
    Private dataGrid1 As System.Windows.Forms.DataGrid
    Private saveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Private openFileDialog1 As System.Windows.Forms.OpenFileDialog
    Private tabPage3 As System.Windows.Forms.TabPage
    Private richTextBox1 As System.Windows.Forms.RichTextBox
    Private label1 As System.Windows.Forms.Label
    Private label3 As System.Windows.Forms.Label
    Private label4 As System.Windows.Forms.Label
    Private label5 As System.Windows.Forms.Label
    Private label6 As System.Windows.Forms.Label
    Private button11 As System.Windows.Forms.Button
    Private textBox2 As System.Windows.Forms.TextBox
    Private textBox3 As System.Windows.Forms.TextBox
    Private textBox4 As System.Windows.Forms.TextBox
    Private textBox5 As System.Windows.Forms.TextBox
    Private components As System.ComponentModel.Container = Nothing

    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button9 As System.Windows.Forms.Button
    Friend WithEvents Button8 As System.Windows.Forms.Button
    Friend WithEvents Button7 As System.Windows.Forms.Button
    Friend WithEvents Button10 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Panel1 As System.Windows.Forms.Panel

    Public Sub New()
        InitializeComponent()
    End Sub

    ' Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    'If disposing Then
    'If components IsNot Nothing Then
    'components.Dispose()
    ' End If
    'End If
    '  MyBase.Dispose(disposing)
    'End Sub

    Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.saveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Me.myds.WriteXml(Me.saveFileDialog1.FileName)
        End If
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Me.InitVarValuble()
            Try
                Me.myds.Tables.Clear()

                Me.myds.ReadXml(Me.openFileDialog1.FileName)
                For i As Integer = 0 To Me.myds.Tables(0).Columns.Count - 1
                    Me.myds.Tables(0).Columns(1).AllowDBNull = False
                Next
                Me.yushuju = Me.myds.Tables(0)
                If Me.myds.Tables.Count > 1 Then
                    Me.biaozhun = Me.myds.Tables(1)
                End If
                If Me.myds.Tables.Count > 2 Then
                    Me.juli = Me.myds.Tables(2)
                End If
                Me.dataGrid1.DataSource = Me.myds.Tables(0)
                Me.dataGrid1.Refresh()
            Catch err As Exception
                MessageBox.Show(err.ToString())
            End Try
        End If
    End Sub
    Private Sub biaozhu_bzc()
        '标准差标准化数据 
        Dim ColumnsCount As Int32 = Me.yushuju.Columns.Count - 1
        Dim RowsCount As Int32 = Me.yushuju.Rows.Count

        Dim i As Int32, j As Int32
        Dim temp As Double() = New Double(ColumnsCount - 1) {}
        Dim s As Double() = New Double(ColumnsCount - 1) {}
        For j = 0 To ColumnsCount - 1
            '求每一列的平均值 
            temp(j) = 0
            For i = 0 To RowsCount - 1
                temp(j) += Convert.ToDouble(Me.biaozhun.Rows(i)(j + 1))
            Next
            temp(j) /= RowsCount
        Next
        For j = 0 To ColumnsCount - 1
            '求每一列的标准差 
            s(j) = 0
            For i = 0 To RowsCount - 1
                s(j) += Math.Pow(Convert.ToDouble(Me.biaozhun.Rows(i)(j + 1)) - temp(j), 2)
            Next
            s(j) = Math.Sqrt(s(j) / (RowsCount - 1))
        Next
        For j = 0 To ColumnsCount - 1
            For i = 0 To RowsCount - 1
                Me.biaozhun.Rows(i)(j + 1) = (Convert.ToDouble(Me.biaozhun.Rows(i)(j + 1)) - temp(j)) / s(j)
            Next
        Next
        '求每一个数据的标准数据 
    End Sub

    Private Sub button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
        '标准化数据 
        If myds.Tables.Count > 1 Then
            If (MessageBox.Show("要重新计算吗?", "数据已经标准化", System.Windows.Forms.MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.No) Then
                Return
            Else
                Try
                    Me.myds.Tables.Clear()
                    Me.myds.Tables.Add(Me.yushuju)
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try

            End If
        End If

        Try
            Me.biaozhun = Me.myds.Tables(0).Copy()
            Me.biaozhun.TableName = "标准化数据"
            Me.sta_method = 1
            Select Case Me.sta_method
                Case 1
                    Me.biaozhu_bzc()
                    '标准差 
                    Exit Select
            End Select
            Me.myds.Tables.Add(Me.biaozhun)

            Me.dataGrid1.DataSource = Me.myds.Tables(1)
            Me.dataGrid1.Refresh()
        Catch err As Exception
            MessageBox.Show(err.ToString())
        End Try
    End Sub

    Private Sub button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click
        If myds.Tables.Count > 2 Then
            If (MessageBox.Show("要重新计算吗?", "距离已经计算", System.Windows.Forms.MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.No) Then
                Return
            Else
                Me.myds.Tables.Clear()
                Me.myds.Tables.Add(Me.yushuju)
                Me.myds.Tables.Add(Me.biaozhun)
            End If
        End If
        If myds.Tables.Count < 2 Then
            MessageBox.Show("没有进行标准计算!")
            Return
        End If
        '殴式距离算法 
        Try
            Dim work As DataColumn
            Dim temptable As DataTable = Me.biaozhun
            Dim i As Int32, j As Int32
            Dim k As Integer
            Dim RowCount As Int32 = Me.myds.Tables(0).Rows.Count
            Dim ColCount As Integer = Me.myds.Tables(0).Columns.Count
            Dim tempSumCol As Double
            Me.juli = Nothing
            Me.juli = New DataTable("距离数据")
            For i = 0 To temptable.Rows.Count - 1
                work = Me.juli.Columns.Add(temptable.Rows(i)(0).ToString(), GetType(Double))
                work.AllowDBNull = False
            Next
            For i = 0 To RowCount - 1
                Dim temprow As DataRow = Me.juli.NewRow()
                For j = 0 To RowCount - 1
                    tempSumCol = 0
                    For k = 1 To ColCount - 1
                        tempSumCol += Math.Pow((Convert.ToDouble(temptable.Rows(i)(k)) - Convert.ToDouble(temptable.Rows(j)(k))), 2)
                    Next
                    tempSumCol = Math.Sqrt(tempSumCol)
                    temprow(j) = tempSumCol
                Next
                Me.juli.Rows.Add(temprow)
            Next
            Me.juli.TableName = "距离数据"
            Me.myds.Tables.Add(Me.juli)

            Me.dataGrid1.DataSource = Me.myds.Tables(2)
            Me.dataGrid1.Refresh()
        Catch err As Exception
            MessageBox.Show(err.ToString())
        End Try
    End Sub

    Private Sub button7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button7.Click
        If Me.myds.Tables.Count < 1 Then
            MessageBox.Show("没有数据!")
            Return
        Else
            Me.dataGrid1.DataSource = Me.myds.Tables(0)
            Me.dataGrid1.Refresh()
        End If
    End Sub

    Private Sub button8_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button8.Click
        If Me.myds.Tables.Count < 2 Then
            MessageBox.Show("没有进行标准化计算!")
            Return
        Else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -