package finalWork;
/**
* Description: Use tri-training to exploit unlabeled data.
*
* Reference: Z.-H. Zhou, Y. Jiang, M. Li. Tri-Training: exploiting unlabeled data using three classifiers.
* IEEE Transactions on Knowledge and Data Engineering. in press.
*
* ATTN: This package is free for academic usage. You can run it at your own risk.
* For other purposes, please contact Prof. Zhi-Hua Zhou (zhouzh@nju.edu.cn).
*
* Requirement: To use this package, the whole WEKA environment (ver 3.4) must be available.
* refer: I.H. Witten and E. Frank. Data Mining: Practical Machine Learning
* Tools and Techniques with Java Implementations. Morgan Kaufmann,
* San Francisco, CA, 2000.
*
* Data format: Both the input and output formats are the same as those used by WEKA.
*
* ATTN2: This package was developed by Mr. Ming Li (lim@lamda.nju.edu.cn). There
* is a ReadMe file provided for roughly explaining the codes. But for any
* problem concerning the code, please feel free to contact with Mr. Li.
*
*/
import java.io.*;
import java.util.*;
import weka.core.*;
import weka.classifiers.Classifier;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.functions.LibSVM;
import weka.classifiers.functions.SMO;
import weka.classifiers.pmml.consumer.NeuralNetwork;
import weka.classifiers.trees.*;
public class test
{
/** base classifiers */
private Classifier m_baseClassifier = null;
/** classifiers */
private Classifier[] m_classifiers;
private Random m_rand = new Random(1);
/**
* The constructor
*/
public test(Classifier c1,Classifier c2,Classifier c3)
{
m_classifiers = new Classifier[3];
m_classifiers[0] = c1;
m_classifiers[1] = c2;
m_classifiers[2] = c3;
}
/**
* Sets the base classifier
*
* @param c Classifier The base classifier
*/
public void setClassifier(Classifier c)
{
m_baseClassifier = c;
}
public void setRandomObject(Random random)
{
m_rand = random;
}
/**
* Builds classifier via tri-training
*
* @param labeled Instances The labeled set
* @param unlabeled Instances The unlabeled set
* @throws Exception Some exception
*/
public void buildClassifier(Instances labeled, Instances unlabeled) throws Exception
{
double[] err = new double[3]; // e_i
double[] err_prime = new double[3]; // e'_i
double[] s_prime = new double[3]; // l'_i
/*if (m_baseClassifier == null)
throw new Exception("Base classifier should be set before the building process");*/
if (!labeled.classAttribute().isNominal())
throw new Exception("The class value should be nominal");
//m_classifiers = Classifier.makeCopies(m_baseClassifier, 3);
Instances[] labeleds = new Instances[3];
for(int i = 0; i < 3; i++)
{
labeleds[i] = new Instances(labeled.resampleWithWeights(m_rand)); //L_i <-- Bootstrap(L)
m_classifiers[i].buildClassifier(labeleds[i]); //h_i <-- Learn(L_i)
err_prime[i] = 0.5; //e'_i <-- .5
s_prime[i] = 0; //l'_i <-- 0
}
boolean bChanged = true;
/** repeat until none of h_i ( i \in {1...3} ) changes */
while(bChanged)
{
bChanged = false;
boolean[] bUpdate = new boolean[m_classifiers.length];
Instances[] Li = new Instances[3];
/** for i \in {1...3} do */
for(int i = 0; i < 3; i++)
{
Li[i] = new Instances(labeled, 0); //L_i <-- \phi
err[i] = measureError(labeled, i); //e_i <-- MeasureError(h_j & h_k) (j, k \ne i)
/** if (e_i < e'_i) */
if(err[i] < err_prime[i])
{
/** for every x \in U do */
for(int j = 0; j < unlabeled.numInstances(); j++)
{
Instance curInst = new Instance(unlabeled.instance(j));
curInst.setDataset(Li[i]);
double classval = m_classifiers[(i+1)%3].classifyInstance(curInst);
/** if h_j(x) = h_k(x) (j,k \ne i) */
if(classval == m_classifiers[(i+2)%3].classifyInstance(curInst))
{
curInst.setClassValue(classval);
Li[i].add(curInst); //L_i <-- L_i \cup {(x, h_j(x))}
}
}// end of for j
/** if (l'_i == 0 ) */
if(s_prime[i] == 0)
s_prime[i] = Math.floor(err[i] / (err_prime[i] - err[i]) + 1); //l'_i <-- floor(e_i/(e'_i-e_i) +1)
/** if (l'_i < |L_i| ) */
if(s_prime[i] < Li[i].numInstances())
{
/** if ( e_i * |L_i| < e'_i * l'_i) */
if(err[i] * Li[i].numInstances() < err_prime[i] * s_prime[i])
bUpdate[i] = true; // update_i <-- TURE
/** else if (l'_i > (e_i / (e'_i - e_i))) */
else if (s_prime[i] > (err[i] / (err_prime[i] - err[i])))
{
int numInstAfterSubsample = (int) Math.ceil(err_prime[i] * s_prime[i] / err[i] - 1);
Li[i].randomize(m_rand);
Li[i] = new Instances(Li[i], 0, numInstAfterSubsample); //L_i <-- Subsample(L_i, ceilling(e'_i*l'_i/e_i-1)
bUpdate[i] = true; //update_i <-- TRUE
}
}
}
}//end for i = 1...3
//update
for(int i = 0; i < 3; i++)
{
/** if update_i = TRUE */
if(bUpdate[i])
{
int size = Li[i].numInstances();
bChanged = true;
m_classifiers[i].buildClassifier(combine(labeled, Li[i])); //h_i <-- Learn(L \cup L_i)
err_prime[i] = err[i]; //e'_i <-- e_i
s_prime[i] = size; //l'_i <-- |L_i|
}
}// end fo for
} //end of repeat
}
/**
* Returns the probability label of a given instance
*
* @param inst Instance The instance
* @return double[] The probability label
* @throws Exception Some exception
*/
public double[] distributionForInstance(Instance inst) throws Exception
{
double[] res = new double[inst.numClasses()];
for(int i = 0; i < m_classifiers.length; i++)
{
double[] distr = m_classifiers[i].distributionForInstance(inst);
for(int j = 0; j < res.length; j++)
res[j] += distr[j];
}
Utils.normalize(res);
return res;
}
/**
* Classifies a given instance
*
* @param inst Instance The instance
* @return double The class value
* @throws Exception Some Exception
*/
public double classifyInstance(Instance inst) throws Exception
{
double[] distr = distributionForInstance(inst);
return Utils.maxIndex(distr);
}
/**
* Adds the instances in initial training set L to the newly labeled set Li
*
* @param L Instances The initial training set
* @param Li Instances The newly labeled set
* @return Instances The combined data set
*/
private Instances combine(Instances L, Instances Li)
{
for(int i = 0; i < L.numInstances(); i++)
Li.add(L.instance(i));
return Li;
}
/**
* Measure combined error excluded the classifier 'id' on the given data set
*
* @param data Instances The data set
* @param id int The id of classifier to be excluded
* @return double The error
* @throws Exception Some Exception
*/
protected double measureError(Instances data, int id) throws Exception
{
Classifier c1 = m_classifiers[(id+1)%3];
Classifier c2 = m_classifiers[(id+2)%3];
double err = 0;
int count =
没有合适的资源?快使用搜索试试~ 我知道了~
hand_writing.zip_Java识别
共22个文件
java:7个
class:7个
rar:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 191 浏览量
2022-07-15
05:01:19
上传
评论
收藏 51KB ZIP 举报
温馨提示
用Java实现了手写体数字识别,采用了WEKA工具包
资源详情
资源评论
资源推荐
收起资源包目录
hand_writing.zip (22个子文件)
hand_writing
bin
weka
wekaTest.class 3KB
Test1.class 2KB
finalWork
algorithm.rar 5KB
tri_train.class 6KB
DataDivision.class 1KB
testss.class 2KB
test.class 7KB
finalWork.rar 5KB
dataProcess.class 7KB
.settings
org.eclipse.core.resources.prefs 143B
org.eclipse.jdt.core.prefs 629B
src
weka
wekaTest.java 4KB
Test1.java 2KB
finalWork
tri_train.java 9KB
test.java 10KB
algorithm.rar 5KB
dataProcess.java 7KB
finalWork.rar 5KB
testss.java 1KB
DataDivision.java 530B
.project 388B
.classpath 496B
共 22 条
- 1
APei
- 粉丝: 63
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0