主动学习:

主动学习的过程:需要分类器与标记专家进行交互。一个典型的过程:

(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模拟实现 “主动学习“ 算法的更多相关文章

  1. 简要介绍Active Learning(主动学习)思想框架,以及从IF(isolation forest)衍生出来的算法:FBIF(Feedback-Guided Anomaly Discovery)

    1. 引言 本文所讨论的内容为笔者对外文文献的翻译,并加入了笔者自己的理解和总结,文中涉及到的原始外文论文和相关学习链接我会放在reference里,另外,推荐读者朋友购买 Stephen Boyd的 ...

  2. 【主动学习】Variational Adversarial Active Learning

    本文记录了博主阅读ICCV2019一篇关于主动学习论文的笔记,第一篇博客,以后持续更新哈哈 论文题目:<Variational AdVersarial Active Learning> 原 ...

  3. 主动学习(Active Learning)

    主动学习简介 在某些情况下,没有类标签的数据相当丰富而有类标签的数据相当稀少,并且人工对数据进行标记的成本又相当高昂.在这种情况下,我们可以让学习算法主动地提出要对哪些数据进行标注,之后我们要将这些数 ...

  4. 主动学习——active learning

    阅读目录 1. 写在前面 2. 什么是active learning? 3. active learning的基本思想 4. active learning与半监督学习的不同 5. 参考文献   1. ...

  5. Active Learning 主动学习

    Active Learning 主动学习 2015年09月30日 14:49:29 qrlhl 阅读数 21374 文章标签: 算法机器学习 更多 分类专栏: 机器学习   版权声明:本文为博主原创文 ...

  6. 第一周-调用weka算法进行数据挖掘

    第一周-调用weka算法进行数据挖掘 简单数据集data.txt @relation weather @attribute outlook {sunny, overcast, rainy} @attr ...

  7. 机器学习:eclipse中调用weka的Classifier分类器代码Demo

    weka中实现了很多机器学习算法,不管实验室研究或者公司研发,都会或多或少的要使用weka,我的理解是weka是在本地的SparkML,SparkML是分布式的大数据处理机器学习算法,数据量不是很大的 ...

  8. [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序

    用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html  目录 马桶排序(令人 ...

  9. 调用WEKA包进行kmeans聚类(java)

    所用数据文件:data1.txt @RELATION data1 @ATTRIBUTE one REAL @ATTRIBUTE two REAL @DATA 0.184000 0.482000 0.1 ...

随机推荐

  1. week4b:个人博客作业

    下面是week4做程序的过程. 1.在做之前先做客户需求,要求使用的使用mul图. 自己第一次听到这个名字,网上查UML为, http://www.cnblogs.com/wangkangluo1/a ...

  2. 微信小程序wx:for和wx:for-item的正确用法

    wx:for="{{list}}"用来循环数组,而list即为数组名wx:for-item="items" 即用来定义一个循环过程中每个元素的变量的 如果是一维 ...

  3. oracle 3大范式 理解

    CREATE OR REPLACE PACKAGE pack3 AS FUNCTION fun_calc (num1 NUMBER ,num2 NUMBER ) RETURN number ; fun ...

  4. Linux_Apache 安装

    1.下载依赖扩展 apr.apr-util.pcre(正则依赖) https://apr.apache.org/download.cgi#aprutil1 apr:http://mirrors.shu ...

  5. 笔记之分布式文件系统(DFS)

    不知何故,老外都挺喜欢使用DFS,但是国内公司用这个的不多.一个具体的需求就是,备份服务器在国外,所以启用DFS把国内的数据同步一份到国外进行备份.最近有机会接触DFS,把一些心得体会记录一下. 1. ...

  6. uva 1513(线段树)

    题目链接:1513 - Movie collection 题意:有一堆电影,按1-n顺序排,有m次操作,每次询问第ai个电影之前有多少个电影,然后将其抽出放在堆顶. 分析:线段树应用. 因为每次查询后 ...

  7. java将字符串存入GridF并通过id或文件名查询

    import static org.bson.codecs.configuration.CodecRegistries.fromProviders; import static org.bson.co ...

  8. 随机场(Random field)

    一.随机场定义 http://zh.wikipedia.org/zh-cn/随机场 随机场(Random field)定义如下: 在概率论中, 由样本空间Ω = {0, 1, …, G − 1}n取样 ...

  9. mysql查看表中列信息

    查看所有数据库中所有表的数据库名和表名 SELECT `TABLES`.`TABLE_SCHEMA`, `TABLES`.`TABLE_NAME` FROM `information_schema`. ...

  10. 分享关于js解析URL中的参数的方法

    function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...