工厂模式+单例模式封装封装WekaFactory来构造Weka中各种对象

 

 摘要:更方便更简单起见,封装了Weka

 

 

 

 

 

 

// 测试WekaFactory

package cn.edu.xmu.bdm.wekainjava.utils;

import java.io.File;

import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import weka.core.Instances;

public class TestWekaFactory {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		File trainFile = new File(
				"C:\\Program Files\\Weka-3-6\\data\\cpu.with.vendor.arff");
		File testFile = new File(
				"C:\\Program Files\\Weka-3-6\\data\\cpu.with.vendor.arff");
		/**
		 * 1. 获取weka工厂类
		 */
		WekaFactory wi = WekaFactoryImpl.getInstance();
		/**
		 * 2. 从工厂中获取分类器 具体使用哪一种特定的分类器可以选择 这样就构建了一个简单的分类器
		 */
		Classifier j48 = (Classifier) wi.getClassifier(J48.class);

		/**
		 * 3. 从工厂中获取训练样本和测试样本实例
		 */
		Instances instancesTrain = wi.getInstance(trainFile);
		instancesTrain.setClassIndex(0);

		Instances instancesTest = wi.getInstance(testFile);
		instancesTest.setClassIndex(0);

		/**
		 * 4.使用训练样本训练分类器
		 */
		j48.buildClassifier(instancesTrain);

		/**
		 * 5. 从工厂中获取使用Evaluation,测试样本测试分类器的学习效果
		 */
		double sum = instancesTrain.numInstances();
		Evaluation testingEvaluation = wi.getEvaluation(j48, instancesTest);
		int length = instancesTest.numInstances();
		for (int i = 0; i < length; i++) {
			// 通过这个方法来用每个测试样本测试分类器的效果
			testingEvaluation.evaluateModelOnceAndRecordPrediction(j48,
					instancesTest.instance(i));
		}

		System.out.println("分类器的正确率:" + (1 - testingEvaluation.errorRate()));
	}

}

 

 

 

 

 

工厂模式封装的优点:
    1. 良好的封装性,代码结构清晰
    2. 扩展性很好
    3. 屏蔽产品类
    4. 典型的解耦框架

单例模式的优点:
   1. 减少内存开支
    2. 减少系统性能开销
    3. 避免对资源的多重占用
    4. 可以在系统设置全局访问点,优化和共享资源访问

单例的创建使用双重检查加锁方式,延迟加载以及线程安全

package cn.edu.xmu.bdm.wekainjava.utils;

import java.io.File;

import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Instances;

/**
 * desc:weka各种类工厂
 * <code>WekaFactory</code>
 * @version 1.0
 * @author chenwq
 *
 */
public abstract  class WekaFactory {
	/**
	 * java.lang.Object
  	 * 		——weka.core.Instances
	 * @param clazz
	 * @return
	 */
	public abstract <T extends Classifier> T getClassifier(Class<T> clazz);
	
	/**
	 * 从.arff文件中获取样本Instances;
	 * @param fileName 获得instances的文件名
	 * @return
	 */
	public abstract <T extends Instances> T getInstance(String fileName);
	/**
	 * 从.arff文件中获取样本Instances;
	 * @param file 获得instances的File对象
	 * @return
	 */
	public abstract <T extends Instances> T getInstance(File file);
	
	/**
	 * 获得一个Evaluation对象
	 * @param h 一个已经训练过的分类器 
	 * @param ins  测试样本
	 * @return
	 */
	public abstract <T extends Evaluation> T getEvaluation(Classifier h, Instances ins);
}

  

 

package cn.edu.xmu.bdm.wekainjava.utils;

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;

/**
 *@see cn.edu.xmu.bdm.wekainjava.utils.WekaFactory
 */
public class WekaFactoryImpl extends WekaFactory{
	private volatile static WekaFactoryImpl instance = null;

	private WekaFactoryImpl() {
	}

	public synchronized static WekaFactoryImpl getInstance() {
		if (instance == null) {
			instance = new WekaFactoryImpl();
		}
		return instance;
	}
	
	@Override
	/**
	 * @
	 */
	public <T extends Classifier> T getClassifier(Class<T> clazz) {
		Classifier classifier  = null;
		try{
			classifier = (Classifier)Class.forName(clazz.getName()).newInstance();
		}catch(Exception e){
			//TODO:创建Classifier异常处理
		}
		return (T)classifier;
	}

	@Override
	public <T extends Instances> T getInstance(String fileName) {
		File file = new File(fileName);
		return getInstance(file);
	}

	@Override
	public <T extends Instances> T getInstance(File file) {
		Instances inst = null;
		try {
			ArffLoader loader = new ArffLoader();
			loader.setFile(file);
			inst = loader.getDataSet();
		} catch (Exception e) {
			//TODO:创建Instances异常处理
		}
		return (T)inst;
	}

	@Override
	public <T extends Evaluation> T getEvaluation(Classifier h, Instances ins) {
		try {
			Instance testInst;
			/*
			 * Evaluation: Class for evaluating machine learning models
			 * 即它是用于检测分类模型的类
			 */
			Evaluation testingEvaluation = new Evaluation(ins);
			int length = ins.numInstances();
			for (int i = 0; i < length; i++) {
				testInst = ins.instance(i);
				// 通过这个方法来用每个测试样本测试分类器的效果
				testingEvaluation.evaluateModelOnceAndRecordPrediction(h,
						testInst);
			}
			return (T)testingEvaluation;
		} catch (Exception e) {
			System.out.println("haha bug!");
			System.out.println(e);
		}
		return null;
	}
}

 

 

你可能感兴趣的:(factory)