调用weka模拟实现 “主动学习“ 算法
主动学习:
主动学习的过程:需要分类器与标记专家进行交互。一个典型的过程:
(1)基于少量已标记样本构建模型
(2)从未标记样本中选出信息量最大的样本,交给专家进行标记
(3)将这些样本与之前样本进行融合,并构建模型
(4)重复执行步骤(2)和步骤(3),直到stopping criterion(不存在未标记样本或其他条件)满足为止
模拟思路:
1. 将数据分为label 和 unlabel数据集
2. 将 unlabel 分为100个一组,每组样本数组分别求出熵值,按照熵值排序,取前5个样本,添加到 label样本之中
package demo; import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource; //将测试用例,按照熵值进行排序
class InstanceSort implements Comparable<InstanceSort>{
public Instance instance;
public double entropy; public InstanceSort( Instance instance, double entropy){
this.instance = instance;
this.entropy = entropy;
}
@Override
public int compareTo(InstanceSort o) {
// TODO Auto-generated method stub
if (this.entropy < o.entropy){
return 1;
}else if ( this.entropy > o.entropy){
return -1;
} return 0;
}
} public class ActiveLearning { public static Instances getInstances( String fileName) throws Exception{
Instances data = new Instances (new FileReader(fileName));
data.setClassIndex(data.numAttributes()-1);
return data;
} //计算熵
public static double computeEntropy(double predictValue){
double entropy = 0.0;
if ( 1-predictValue < 0.000000001d || predictValue < 0.000000001d){
return 0;
}else {
return -predictValue*(Math.log(predictValue)/Math.log(2.0d))-(1-predictValue)*(Math.log(1-predictValue)/Math.log(2.0d));
}
} public static void classify(Instances train, Instances test) throws Exception{
NaiveBayes classifier = new NaiveBayes();
//训练模型
classifier.buildClassifier(train); //评价模型
Evaluation eval = new Evaluation(test);
eval.evaluateModel(classifier, test);
System.out.println(eval.toClassDetailsString());
} //不确定采样
public static Instances uncertaintySample(Instances labeled, Instances unlabeled, int start, int end) throws Exception{
//用有标签的先训练模型
NaiveBayes classifier = new NaiveBayes();
classifier.buildClassifier(labeled);
//按照熵进行排序
ArrayList <InstanceSort> l = new ArrayList<InstanceSort>(); for (int i = start; i < end; i++) {
double result = classifier.classifyInstance(unlabeled.instance(i));
double entropy = computeEntropy (result);
InstanceSort is = new InstanceSort(unlabeled.instance(i), entropy);
l.add(is);
}
//按照熵值进行排序
Collections.sort(l); DataSource source = new DataSource("NASA//pc1.arff");
Instances A = source.getDataSet();
Instances chosenInstances = new Instances(A, 0);
//每100个里面选择5个熵值最小的实例
for(int i = 0; i < 5; i++){
chosenInstances.add(l.get(i).instance);
} return chosenInstances;
} //采样
public static void sample( Instances instances, Instances test) throws Exception{
Random rand = new Random(1023);
instances.randomize(rand);
instances.stratify(10);
Instances unlabeled = instances.trainCV(10, 0);
Instances labeled = instances.testCV(10, 0); int iterations = unlabeled.numInstances() / 100 +1; for ( int i=0; i< iterations-1 ; i++){
//每100个里面选择5个熵值最小的实例
//100个一组
Instances resultInstances = uncertaintySample(labeled, unlabeled, i*100, (i+1)*100);
for (int j = 0; j < resultInstances.numInstances(); j++){
labeled.add(resultInstances.instance(j));
}
classify(labeled, test);
} Instances resultInstances = uncertaintySample(labeled, unlabeled, (iterations-1)*100, unlabeled.numInstances()); for (int j = 0; j < resultInstances.numInstances(); j++){
labeled.add(resultInstances.instance(j));
} classify(labeled, test); } public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Instances instances = getInstances("NASA//pc1.arff"); //10-fold cross validation
Random rand = new Random(1023);
instances.randomize(rand);
instances.stratify(10);
Instances train = instances.trainCV(10, 0);
Instances test = instances.testCV(10, 0);
// System.out.println(train.numInstances());
// System.out.println(test.numInstances()); sample(train,test); } }
调用weka模拟实现 “主动学习“ 算法的更多相关文章
- 简要介绍Active Learning(主动学习)思想框架,以及从IF(isolation forest)衍生出来的算法:FBIF(Feedback-Guided Anomaly Discovery)
1. 引言 本文所讨论的内容为笔者对外文文献的翻译,并加入了笔者自己的理解和总结,文中涉及到的原始外文论文和相关学习链接我会放在reference里,另外,推荐读者朋友购买 Stephen Boyd的 ...
- 【主动学习】Variational Adversarial Active Learning
本文记录了博主阅读ICCV2019一篇关于主动学习论文的笔记,第一篇博客,以后持续更新哈哈 论文题目:<Variational AdVersarial Active Learning> 原 ...
- 主动学习(Active Learning)
主动学习简介 在某些情况下,没有类标签的数据相当丰富而有类标签的数据相当稀少,并且人工对数据进行标记的成本又相当高昂.在这种情况下,我们可以让学习算法主动地提出要对哪些数据进行标注,之后我们要将这些数 ...
- 主动学习——active learning
阅读目录 1. 写在前面 2. 什么是active learning? 3. active learning的基本思想 4. active learning与半监督学习的不同 5. 参考文献 1. ...
- Active Learning 主动学习
Active Learning 主动学习 2015年09月30日 14:49:29 qrlhl 阅读数 21374 文章标签: 算法机器学习 更多 分类专栏: 机器学习 版权声明:本文为博主原创文 ...
- 第一周-调用weka算法进行数据挖掘
第一周-调用weka算法进行数据挖掘 简单数据集data.txt @relation weather @attribute outlook {sunny, overcast, rainy} @attr ...
- 机器学习:eclipse中调用weka的Classifier分类器代码Demo
weka中实现了很多机器学习算法,不管实验室研究或者公司研发,都会或多或少的要使用weka,我的理解是weka是在本地的SparkML,SparkML是分布式的大数据处理机器学习算法,数据量不是很大的 ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- 调用WEKA包进行kmeans聚类(java)
所用数据文件:data1.txt @RELATION data1 @ATTRIBUTE one REAL @ATTRIBUTE two REAL @DATA 0.184000 0.482000 0.1 ...
随机推荐
- 【Coursera】主成分分析
一.问题 主方向的概念是什么?为什么降低维度的方法是使方差最大化? 假设某两个特征之间成线性关系,在二维平面上的表示就是数据点呈线性分布,那么可以通过将数据在主方向上进行投影,得到一个一维的数据,这个 ...
- 【贪心算法】POJ-3262
一.题目 Description Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the gras ...
- WebGL学习笔记(二)
目录 绘制多个顶点 使用缓冲区对象 类型化数组 使用drawArrays()函数绘制图形 图形的移动 图形的旋转 图形的缩放 绘制多个顶点 使用缓冲区对象 创建缓冲区对象 var vertexBuff ...
- requests爬取知乎话题和子话题
zhihu.py # *_*coding:utf-8 *_* import pymysql import requests from lxml import etree from requests_t ...
- filebeat向kafka中传输数据报WARN Failed to connect to broker DOMSDev07:9092: dial tcp: lookup DOMSDev07: getaddrinfow: No such host is known.解决方法
打开filebeat客户端所在机器C:\Windows\System32\drivers\etc目录,找到hosts文件 以记事本形式打开,在底部追加 “IP 主机名” 即可
- ie8 ajaxSubmit 上传文件提示下载
转载 解决ie下ajaxsubmit上传文件提示下载文件问题 主要是应为放回类型为json,返回text/html
- BZOJ5127 数据校验
第一眼看错题以为只是要求区间值域连续,那莫队一发维护形成的值域段数量就行了. 原题这个条件相当于区间内相邻数差的绝对值不超过1.所以只要对这个做个前缀和就……完了? #include<iostr ...
- Spring点滴二:Spring Bean
Spring Bean: 被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理.bean是一个被实例化,配置.组装并由Spring Ioc容器管理对象. 官网API:A Spri ...
- BZOJ 4316: 小C的独立集 解题报告
4316: 小C的独立集 Description 图论小王子小C经常虐菜,特别是在图论方面,经常把小D虐得很惨很惨. 这不,小C让小D去求一个无向图的最大独立集,通俗地讲就是:在无向图中选出若干个点, ...
- 单点登录(十)-----遇到问题-----cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed
cas启用mongodb验证方式报错com.mongodb.CommandFailureException---Authentication failed. 完整报错信息: 二月 08, 2017 5 ...