朴素贝叶斯算法(NB)

算法分析:贝叶斯分类器的分类原理是通过某对象的先验概率,利用贝叶斯公式计算出其后验概率,即该对象属于某一类的概率,选择具有最大后验概率的类作为该对象所属的类。目前研究较多的贝叶斯分类器主要有四种,分别是:Naive Bayes、TAN、BAN和GBN。这次使用NB算法来实现。

       实现步骤:

    1、找到一个已知分类的待分类项集合,这个集合叫做训练样本集。

      2、统计得到在各类别下各个特征属性的条件概率估计。即

      3、如果各个特征属性是条件独立的,则根据贝叶斯定理有如下推导:

      

      因为分母对于所有类别为常数,因为我们只要将分子最大化皆可。

      

 

【优化提升】

   计算各个划分的条件概率P(a|y)是朴素贝叶斯分类的关键性步骤,当特征属性为离散值时,只要很方便的统计训练样本中各个划分在每个类别中出现的频率即可用来估计P(a|y),这次给的数据由于属性是连续的非离散的,所以,采用了正态分布的原则来获取每一个属性下的值占每个类别的概率,取代了原来计数的方式。

       第二点是可能是tests数据集里面,有的属性可能在训练集里面本身就没有,这样会导致最终得到的概率结果为0,所以我这里添加了一个很小的参数来代替原来为0 的概率分项。

       得到的概率可能很小,为了防止数据向下溢出,使用了log函数。


 

【实验结果】

           此方法如果只使用上面的操作来处理的话,一般会得到0.62的正确系数。

 

【实验思考】

        NB算法的前提是提供的数据属性之间没有相关性,因为最终分类的标准是根据各个属性的值在各个划分上的概率的积来判断的,很明显本次给出的数据集的属性有最大最小和平均这种有很大相关度的属性,所以后续的提升空间是在相关属性这方面来处理,但是我之前手动去除了最大最小的属性,得到的结果才是0.61,所以后续有待继续研究,不过之前有同学分享说用随机去除固定个属性,最后可以得到0.64+,这方面有待于后续来继续探讨了。


package com.sysu.jerry;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;

import javax.management.AttributeList;

public class TestBayes {
	
	FileInputStream file;
	
	public ArrayList<String> readAttr(){
		//获取所有属性的列表
		ArrayList<String> AttrList = new ArrayList<String>();
		try{
			file = new FileInputStream("train_.txt");
			InputStreamReader isr = new InputStreamReader(file);
			BufferedReader bfr = new BufferedReader(isr);
			String s = "";
			String sSplit[] = new String[100];
			while((s = bfr.readLine()) != null)
			{
				sSplit = s.toString().trim().split("	");
				for(int j=0; j < sSplit.length - 1; j++)
				{
					AttrList.add(sSplit[j]);
					//System.out.println(sSplit[j]);
				}
				break;
			}
			file.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		
		return AttrList;
		
	}
	
	public ArrayList<ArrayList<String>> readData()
	{
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		try{
			file = new FileInputStream("train_.txt");
			InputStreamReader isr = new InputStreamReader(file);
			BufferedReader bfr = new BufferedReader(isr);
			
			String s = bfr.readLine();
			String sSplit[] = new String[100];
			while((s = bfr.readLine()) != null)
			{
				sSplit = s.toString().trim().split("	");
				ArrayList<String> data = new ArrayList<String>();
				for(int j = 0;j < sSplit.length; j++)
				{
					data.add(sSplit[j]);
				}
				datas.add(data);
			}
			file.close();
			
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		return datas;
	}
	
	
	public ArrayList<ArrayList<String>> readTest()
	{
		ArrayList<ArrayList<String>> tests = new ArrayList<ArrayList<String>>();
		try{
			file = new FileInputStream("test_.txt");
			InputStreamReader isr = new InputStreamReader(file);
			BufferedReader bfr = new BufferedReader(isr);
			
			String s = bfr.readLine();
			String sSplit[] = new String[100];
			while((s = bfr.readLine()) != null)
			{
				sSplit = s.toString().trim().split("	");
				ArrayList<String> test = new ArrayList<String>();
				for(int j = 0;j < sSplit.length; j++)
				{
					test.add(sSplit[j]);
				}
				tests.add(test);
			}
			file.close();
			
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		return tests;
	}
	
	
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值