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的。没啥说的,加班撸代码了。

  • 相关阅读:
    ServletWebServerApplicationContext -带有*WebxxxApplication的容器
    SpringMvc-DispatchServlet初始化
    SimpleDateFormat和java8日期格式化
    01导入配置类和用户自定义添加db。ImportBeanDefinitionRegistrar和DeferredImportSelector
    @ConfigurationProperties和@PropertySource
    mybatis语句的存储
    leetCode3: 无重复字符的最长子串
    八锁问题
    数据库杂记
    背包问题
  • 原文地址:https://www.cnblogs.com/baochen/p/9332317.html
Copyright © 2011-2022 走看看