数据分类——weka的朴素贝叶斯分类器

数据分类就是给定一组样本,我们进行学习,学习的成果就是一个分类器,

利用这个分类器,我们对测试数据或者正式数据进行分类,然后查看这个分类器的效果。

这里我们用一个经典的“隐形眼镜”的例子来说明分类方法,首先有一组样本数据,存于contact-lenses.arff文件,如下:

@relation contact-lenses

@attribute age 			{young, pre-presbyopic, presbyopic}
@attribute spectacle-prescrip	{myope, hypermetrope}
@attribute astigmatism		{no, yes}
@attribute tear-prod-rate	{reduced, normal}
@attribute contact-lenses	{soft, hard, none}

@data
%
% 24 instances
%
young,myope,no,reduced,none
young,myope,no,normal,soft
young,myope,yes,reduced,none
young,myope,yes,normal,hard
young,hypermetrope,no,reduced,none
young,hypermetrope,no,normal,soft
young,hypermetrope,yes,reduced,none
young,hypermetrope,yes,normal,hard
pre-presbyopic,myope,no,reduced,none
pre-presbyopic,myope,no,normal,soft
pre-presbyopic,myope,yes,reduced,none
pre-presbyopic,myope,yes,normal,hard
pre-presbyopic,hypermetrope,no,reduced,none
pre-presbyopic,hypermetrope,no,normal,soft
pre-presbyopic,hypermetrope,yes,reduced,none
pre-presbyopic,hypermetrope,yes,normal,none
presbyopic,myope,no,reduced,none
presbyopic,myope,no,normal,none
presbyopic,myope,yes,reduced,none
presbyopic,myope,yes,normal,hard
presbyopic,hypermetrope,no,reduced,none
presbyopic,hypermetrope,no,normal,soft
presbyopic,hypermetrope,yes,reduced,none
presbyopic,hypermetrope,yes,normal,none
arff文件格式不再重复介绍,可以参考之前的文章或者网上的介绍。

这边根据顾客的年龄、近视|远视、散光、眼泪量来预测顾客会选用隐形眼镜的软硬。

采用朴素贝叶斯算法通过计算先验概率来得到后验概率(条件概率),也就是某个条件成立的情况下,某个情况发生的可能性,

所谓“朴素”就是假设各个属性之间没有关系,算法采用。

这里不详细讨论算法细节了,贴出weka的实现

(要用weka,需要引入包dom4j-1.6.1.jar,jaxen-1.1.4.jar,weka.jar)

import java.io.File;

import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

public class TestWeka {
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Instances ins = null;
		Classifier cfs = null;

		try {
			// 读入训练测试样本
			File file = new File(
					"C:\\Program Files\\Weka-3-7\\data\\contact-lenses.arff");

			ArffLoader loader = new ArffLoader();
			loader.setFile(file);
			ins = loader.getDataSet();
			ins.setClassIndex(ins.numAttributes() - 1);

			// 初始化分类器
			cfs = (Classifier) Class.forName(
					"weka.classifiers.bayes.NaiveBayes").newInstance();

			// 使用训练样本进行分类
			cfs.buildClassifier(ins);

			// 使用测试样本测试分类器的学习效果
			Instance testInst;
			Evaluation testingEvaluation = new Evaluation(ins);
			int length = ins.numInstances();
			for (int i = 0; i < length; i++) {
				testInst = ins.instance(i);
				testingEvaluation.evaluateModelOnceAndRecordPrediction(cfs,
						testInst);
			}

			// 打印分类结果
			System.out.println("分类的正确率" + (1 - testingEvaluation.errorRate()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

执行结果是:分类的正确率0.9583333333333334


引用一段别人文章里的评论:

通常使用回归测试来评估分类器的准确率,最简单的方法是用构造完成的分类器对训练数据进行分类,然后根据结果给出正确率评估。但这不是一个好方法,因为使用训练数据作为检测数据有可能因为过分拟合而导致结果过于乐观,所以一种更好的方法是在构造初期将训练数据一分为二,用一部分构造分类器,然后用另一部分检测分类器的准确率。


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值