vb.net machine learning - a candidate for elimination

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox3.Text = ""
        TextBox2.Text = ""

        '属性个数
        Dim xmcount As Integer = 0

        Dim mysr As StreamReader
        Dim strline As String
        '是否正例
        Dim iszl As Boolean

        Dim mystr As String()
        '标题名称
        Dim xmname() As String
        '变型空间
        Dim bxkj As New ArrayList()
        Dim tempsuppose As String()
        '一般假设
        Dim common_suppose As New ArrayList()
        '特殊假设
        Dim specific_suppose As String()
        '读取样本文件
        mysr = New StreamReader("e:\mydata.txt", System.Text.Encoding.Default)
        '第一行是标题
        strline = mysr.ReadLine()
        TextBox1.Clear()
        mystr = strline.Split(New Char() {",", ","})
        '取属性个数
        xmcount = mystr.GetUpperBound(0)
        ReDim xmname(xmcount)
        '初始化假设数组
        ReDim specific_suppose(xmcount)
        ReDim tempsuppose(xmcount)

        For my_i As Integer = 0 To xmcount
            specific_suppose(my_i) = Nothing
        Next
        For my_i As Integer = 0 To xmcount
            tempsuppose(my_i) = "?"
        Next
        common_suppose.Add(tempsuppose)
        '取得标题内容
        xmname = mystr
        Dim i As Integer = 0
        For Each s As String In xmname
            If s.Trim() <> "" Then
                TextBox1.AppendText(s & Space(5))
            End If
        Next s
        '分析样本每一行
        While (Not mysr.EndOfStream)
            strline = mysr.ReadLine()
            If Trim(strline) = "" Then Exit While
            TextBox1.AppendText(vbCrLf)
            mystr = strline.Split(New Char() {",", ","})
            '取样本性质
            If Trim(mystr(xmcount + 1)) = "是" Then
                iszl = True
            Else
                iszl = False
            End If
            TextBox1.AppendText(mystr(0) & Space(5))
            '读取一行中的每个属性,最后一个属性指示了样本是正例还是反例,不能做为分析内容
            For my_i As Integer = 1 To xmcount
                TextBox1.AppendText(mystr(my_i) & Space(5))
                If iszl Then
                    '正例
                    '设置特殊假设
                    If IsNothing(specific_suppose(my_i)) Then
                        '如果特殊假设的该属性未设定,则设定为样本的该属性值
                        specific_suppose(my_i) = mystr(my_i)
                    ElseIf specific_suppose(my_i) <> "?" And specific_suppose(my_i) <> mystr(my_i) Then
                        '如果特殊假设的该属性已设定,且不为当前样本值,则设定为?,可以接受任何值
                        specific_suppose(my_i) = "?"
                    End If
                    '设置一般假设
                    For my_ii As Integer = 0 To common_suppose.Count - 1
                        tempsuppose = TryCast(common_suppose.Item(my_ii), String())
                        If Not IsNothing(tempsuppose) Then
                            If tempsuppose(my_i) <> "?" And tempsuppose(my_i) <> mystr(my_i) Then
                                common_suppose.Item(my_ii) = Nothing
                            End If
                        End If
                    Next
                Else
                    '反例
                    '设置一般假设
                    For my_ii As Integer = 0 To common_suppose.Count - 1
                        tempsuppose = TryCast(common_suppose.Item(my_ii), String())
                        If Not IsNothing(tempsuppose) Then
                            If mystr(my_i) = tempsuppose(my_i) Then
                                common_suppose.Item(my_ii) = Nothing
                            End If
                        End If
                    Next
                    tempsuppose = Nothing
                    ReDim tempsuppose(xmcount)
                    For my_iii As Integer = 1 To xmcount
                        If my_iii <> my_i Then
                            tempsuppose(my_iii) = "?"
                        Else
                            tempsuppose(my_iii) = specific_suppose(my_i)
                        End If
                    Next
                    '如果没有重复元素则增加
                    Dim iscf As Boolean
                    For my_ii As Integer = 0 To common_suppose.Count - 1
                        iscf = True
                        Dim mytempsuppose As String() = TryCast(common_suppose.Item(my_ii), String())
                        If Not IsNothing(mytempsuppose) Then
                            For mmm_i As Integer = 1 To xmcount
                                If Trim(tempsuppose(mmm_i)) <> Trim(mytempsuppose(mmm_i)) Then
                                    iscf = False
                                End If
                            Next
                        End If
                        If iscf And (Not IsNothing(mytempsuppose)) Then
                            Exit For
                        End If
                    Next
                    If Not iscf Then
                        common_suppose.Add(tempsuppose)
                    End If
                End If
            Next my_i
            TextBox1.AppendText(mystr(xmcount + 1) & Space(6))
            If Not iszl Then
                Dim isdel As Boolean
                For my_ii As Integer = 0 To common_suppose.Count - 1
                    isdel = True
                    tempsuppose = TryCast(common_suppose.Item(my_ii), String())
                    If Not IsNothing(tempsuppose) Then
                        For mm_i As Integer = 1 To xmcount
                            If Trim(tempsuppose(mm_i)) <> "?" Then
                                isdel = False
                            End If
                        Next
                    End If
                    If isdel Then
                        common_suppose.Item(my_ii) = Nothing
                    End If
                Next
            End If
        End While
        For my_i As Integer = 1 To xmcount
            TextBox2.Text &= specific_suppose(my_i) & Space(6)
        Next
        For my_iiii As Integer = 0 To common_suppose.Count - 1
            If Not IsNothing(common_suppose.Item(my_iiii)) Then
                tempsuppose = TryCast(common_suppose.Item(my_iiii), String())
                For m_i As Integer = 1 To xmcount
                    TextBox3.Text &= tempsuppose(m_i) & Space(6)
                Next
                TextBox3.Text &= vbCrLf
            End If
        Next
        MsgBox("变型空间在特殊假设和一般假设划分出来")
    End Sub
End Class

e: \ mydata.txt follows:

Name, shape, taste, kind, generally eat, the size of the
apple, rounded, sweetness, fruit, raw, small, a
watermelon, rounded, sweetness, fruit, raw, medium, it is
bitter, elongated , bitterness, vegetables, cooked food, medium-sized, no
bananas, elongated, sweet, fruit, raw, small, no
pumpkin, round, sweet, vegetables, cooked food, medium-sized, is
lychee, round, sweet flavor, fruit, raw, small, is

Candidates for elimination characteristics of samples collected and grouped, variations derived space, the unknown samples may then be classified method, based on its analysis of the sample on the basis of the attribute set summary, the basic principle is:

1, the establishment of two assumptions, one is generally assumed that a special hypothesis through the analysis of each sample, the expansion of specific hypotheses, reduced general assumptions.
2, assuming the property value is set for each sample, and can be set to a fixed value, can be set to an unknown value? That is, what values are acceptable, if set to nothing, to represent any property values do not accept
3, each line of analysis, the samples are extracted each attribute of each line
1) For a positive example:
you want to delete is generally assumed that no positive cases assumptions consistent with the
need to be specific hypotheses deleted and attribute positive patients did not meet the (ie, to "?"), began special Assuming all is NOTHING, that is not to accept any property values
2) for a counterexample
need to remove the general assumptions in line with the counter-example hypothesis, then add the counterexample assumed containing inconsistent with the attribute value, the other attribute value of "?" in
3) the substance is generally assumed that anti-described characteristics of the embodiment, assuming the special features described n Example

Published 473 original articles · won praise 14 · views 60000 +

Guess you like

Origin blog.csdn.net/AI_LX/article/details/105164917