.net随笔-vb.net Accord.Net机器学习之SVM分类

线性核分类

 Imports Accord.Controls
    Imports Accord.IO
    Imports Accord.Math
    Imports Accord.Statistics.Distributions.Univariate
    Imports Accord.MachineLearning.VectorMachines.Learning
    Imports Accord.Statistics
    Imports System
    
    Public Class Form1
    
        'SVM线性分类
        '使用Accord.IO里的的excel读取类
        Dim table As DataTable = New ExcelReader("E:\pro\books\AI_.net\src\AI_learn\learnai-1\data\examples.xls").GetWorksheet("Classification - Yin Yang")
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            NaiveBayes()
        End Sub
        Private Sub NaiveBayes()
            '读取样本
            ' 转换datatable为输入输出数组
            Dim sample As Double()() = table.ToJagged(Of Double)("X", "Y")
            Dim outs As Integer() = table.Columns("G").ToArray(Of Integer)()
    
    
    
    
            Dim trainer As New LinearCoordinateDescent()
    
            Dim svm = trainer.Learn(sample, outs)
    
            Dim predictResult As Boolean() = svm.Decide(sample)
    
    
    
            Dim zeroOneAnswers As Integer() = predictResult.ToZeroOne()
    
    
    
    
    
            ScatterplotBox.Show("实际结果", sample, outs)
            ScatterplotBox.Show("SVM预测结果", sample, zeroOneAnswers).Hold()
    
    
    
    
    
    
        End Sub
    End Class

效果如下
.net随笔-vb.net Accord.Net机器学习之SVM分类_第1张图片
非线性高斯核分类

Imports Accord.Controls
Imports Accord.IO
Imports Accord.Math
Imports Accord.Statistics.Distributions.Univariate
Imports Accord.MachineLearning.VectorMachines.Learning
Imports Accord.Statistics
Imports Accord.Statistics.Kernels
Imports System

Public Class Form1

    'SVM非线性核
    '使用Accord.IO里的的excel读取类
    Dim table As DataTable = New ExcelReader("E:\pro\books\AI_.net\src\AI_learn\data\examples.xls").GetWorksheet("Classification - Yin Yang")
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        NaiveBayes()
    End Sub
    Private Sub NaiveBayes()
        '读取样本
        ' 转换datatable为输入输出数组
        Dim sample As Double()() = table.ToJagged(Of Double)("X", "Y")
        Dim outs As Integer() = table.Columns("G").ToArray(Of Integer)()




        Dim trainer As New SequentialMinimalOptimization(Of Gaussian)
        trainer.UseComplexityHeuristic = True
        trainer.UseKernelEstimation = True

        Dim svm = trainer.Learn(sample, outs)

        Dim predictResult As Boolean() = svm.Decide(sample)



        Dim zeroOneAnswers As Integer() = predictResult.ToZeroOne()





        ScatterplotBox.Show("实际结果", sample, outs)
        ScatterplotBox.Show("SVM预测结果", sample, zeroOneAnswers).Hold()






    End Sub
End Class

.net随笔-vb.net Accord.Net机器学习之SVM分类_第2张图片

你可能感兴趣的:(AI)