zoukankan      html  css  js  c++  java
  • 统计学习方法——第四章朴素贝叶斯及c++实现

    1、名词解释

    贝叶斯定理,自己看书,没啥说的,翻译成人话就是,条件A下的bi出现的概率等于A和bi一起出现的概率除以A出现的概率。

    记忆方式就是变后验概率为先验概率,或者说,将条件与结果转换。

    先验概率:某件事情发生概率

    后验概率:某件事情发生后,由于某个原因引起的概率大小。

    2、朴素贝叶斯代码

    #include <cstdio>
    #include <Windows.h>
    #include "LBayesClassifier.h"
    
    const int NUM = 14;  
    const int Dim = 4;  
    
    int main()
    {
        
        int dataList[NUM*Dim] =
        { 20, 3, 0, 0,
        20, 3, 0, 1,
        30, 3, 0, 0,
        40, 2, 0, 0,
        40, 1, 1, 0,
        40, 1, 1, 1,
        30, 1, 1, 1,
        20, 2, 0, 0,
        20, 1, 1, 0,
        40, 2, 1, 0,
        20, 2, 1, 1,
        30, 2, 0, 1,
        30, 3, 1, 0,
        40, 2, 0, 1 };
        LBayesMatrix sampleMatrix(NUM, Dim, dataList);
    
    
        int classList[NUM] = { 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0 };
        LBayesMatrix classVector(NUM, 1, classList);
    
    
        LBayesProblem problem(sampleMatrix, classVector, BAYES_FEATURE_CONTINUS);
    
    
        LBayesClassifier classifier;
        classifier.TrainModel(problem);
    
    
        LBayesMatrix newSample(1, Dim);
        newSample[0][0] = 20;
        newSample[0][1] = 2;
        newSample[0][2] = 0;
        newSample[0][3] = 0;
        int predictValue;
        classifier.Predict(newSample, &predictValue);
    
        printf("%d
    ", predictValue);
        system("pause");
        return 0;
    }
    View Code

     3、这一张后面的题

    以第一道题为例,第一题第二问差不多,第二题就是上面加个k,下面加个所有k之和,总的来说他们想加之后为1的。没啥说的,加班撸代码了。

  • 相关阅读:
    [转]char、varchar、nchar、nvarchar的区别
    【转】Asp.net 2.0中页的生存周期(Lifecycle)和动态控件 [.Net]
    git免登录sshkey
    ios8,xcode6 周边
    iOS 推送证书
    Lazarus中TreeView导出XML以及XML导入TreeView
    flac文件提取专辑封面手记
    Lazarus解决含中文文件名或路径的使用问题
    使用PowerShell管理Windows8应用
    thbgm拆包【in progress】
  • 原文地址:https://www.cnblogs.com/baochen/p/9332317.html
Copyright © 2011-2022 走看看