Java实现的朴素贝叶斯分类器

目前的算法只能处理结果只有两种的情况,即true or false. 多分枝或者是数字类型的还无法处理。

用到的一些基础数据结构可以参考上一篇关于ID3的代码。 

 

这里只贴出来实现贝叶斯分类预测的部分:

package classifier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import util.ArffUtil;


/**
 * NBC means Naive Bayes Classifier
 * @author wenjun_yang
 *
 */
public class NBCUtil {
	
	ArffUtil util = new ArffUtil();
	private List<String> attributeList = null;
	private List<String[]> dataList = null;
	private String decAttributeName = null;
	private int decAttributeIndex = -1;
	
	private Map<String, List<String[]>> seperatedDataTable = null;
	public NBCUtil(String decAttributeName, List<String> attributeList, List<String[]> dataList) {
		this.attributeList = attributeList;
		this.dataList = dataList;
		this.decAttributeName = decAttributeName;
		
		this.decAttributeIndex = util.getValueIndex(decAttributeName, this.attributeList);
		this.seperatedDataTable = seperateDataList(dataList);
	}
	
	private Map<String, List<String[]>> seperateDataList(List<String[]> dataList) {
		Map<String, List<String[]>> map = new HashMap<String, List<String[]>>();
		
		for(String[] arr : dataList) {
			if(decAttributeIndex >= 0 && decAttributeIndex < arr.length) {
				String currentKey = arr[decAttributeIndex]; 
				if(map.containsKey(currentKey)) {
					List<String[]> tempList = map.get(currentKey);
					tempList.add(arr);
					map.put(currentKey, tempList);
				} else {
					List<String[]> tempList = new ArrayList<String[]>();
					tempList.add(arr);
					map.put(currentKey , tempList);
				}
			}
		}
		
		return map;
	}
	
	public Boolean predict(Map<String, String> predictData, String targetDecAttributeValue) {
		if(predictData.containsKey(decAttributeName)) predictData.remove(decAttributeName);
		
		List<String[]> positiveDataTable = new ArrayList<String[]>();
		if(seperatedDataTable.containsKey(targetDecAttributeValue)) {
			positiveDataTable = seperatedDataTable.get(targetDecAttributeValue);
		}
		
		double resultP = 1.;
		
		// Step 1: 逐个属性的比率进行计算
		// 即: 计算 P(Attr=Value|Y=true) / P(Attr=Value|Y=false) 的值
		for(String attrName : predictData.keySet()) {
			String attrValue = predictData.get(attrName);
			int attrIndex = util.getValueIndex(attrName, attributeList);
			int attrPositiveCount = 0;
			int attrNegativeCount = 0;
			
			for(String[] arr : dataList) {
				if(arr[attrIndex].equals(attrValue)) {
					if(arr[decAttributeIndex].equals(targetDecAttributeValue)) {
						attrPositiveCount++;
					} else {
						attrNegativeCount++;
					}
				}
			}
			double temp =  (attrPositiveCount / (double)positiveDataTable.size() ) /
							(attrNegativeCount / (double)(dataList.size() - positiveDataTable.size()));
			resultP *= temp;
		}
		// 最后计算 P(Y=true) / P(Y=false)
		resultP *= positiveDataTable.size() / (double)(dataList.size() - positiveDataTable.size());
		System.out.println(resultP);
		if(resultP > 1) {
			return true;
		} else {
			return false;
		}
	}
}

 

 

完整的项目也上传了,可以直接使用。

数据源来自weka

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用IntelliJ IDEA运行朴素贝叶斯分类器时,你可以按照以下步骤进行操作: 1. 导入朴素贝叶斯分类器的相关代码和依赖库:在IntelliJ IDEA中创建一个新的Java项目,将朴素贝叶斯分类器的源代码导入到项目中。确保你已经添加了适当的依赖库,例如Weka库,以便使用其中的朴素贝叶斯分类器模块。 2. 创建一个Java类:在IntelliJ IDEA中创建一个新的Java类,用于编写朴素贝叶斯分类器的运行代码。 3. 初始化和加载数据:在Java类中,你需要初始化和加载用于训练和测试朴素贝叶斯分类器的数据。这可能涉及到从文件中读取数据、进行数据预处理和特征工程等步骤。 4. 配置朴素贝叶斯分类器参数:使用Weka库提供的API,你可以设置和配置朴素贝叶斯分类器的参数,例如平滑参数、特征选择等。可以根据具体需求进行调整。 5. 构建和训练朴素贝叶斯分类器:通过实例化一个朴素贝叶斯分类器对象,并使用训练数据对其进行训练。 6. 进行分类预测:使用训练好的朴素贝叶斯分类器对测试数据进行分类预测,并获取预测结果。 7. 输出结果:根据需要,你可以将分类结果进行输出显示或保存到文件中。 8. 运行代码:在IntelliJ IDEA中运行你编写的Java类,观察朴素贝叶斯分类器的运行结果。 通过以上步骤,你可以在IntelliJ IDEA中成功运行朴素贝叶斯分类器。请注意,具体实现细节可能因你所使用的朴素贝叶斯分类器库和数据集而有所不同,你需要根据实际情况进行相应的调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值