调用weka模拟实现 “主动学习“ 算法

该博客介绍了如何调用weka工具来模拟主动学习过程,包括基于少量已标记样本构建模型,选择信息量最大的未标记样本进行标记,并逐步完善模型,直到满足停止条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用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);

    }

}
posted @ 2018-02-03 22:14 douzujun 阅读( ...) 评论( ...) 编辑 收藏
数据集:Amazon商品数据集 编程环境:Python, Matlab, Markdown 1. 数据预处理 商品信息 提取数据集中的title和description信息 命令:python item_information.py [file1, ..., file3] 用户物品评分信息 提取用户-物品评分,划分train集和test集 将train集中的用户作为用户全集,以防止出现train集中有用户没有评分的情况 命令:python user_information.py [file1, ..., file7] 商品相似度生成 title: 分词 + LDA主题模型(topic number = 15) description: 分词 + LDA主题模型(topic number = 15) 未使用price(缺失值太多) 未使用category(同类商品) 命令:python item_similarity.py [topic number, file1, ..., file6] 商品description和title相似度权重生成 non linear regression Similarity(i1, i2) = weight1 * S_title(i1) + weight2 * S_description(i2) 命令: python similarity_parameters.py [file1, ..., file7] fitnlm(path, param1, param2) 用户相似度生成 评分相似度 命令:python user_similarity.py [file1, ..., file3] 用户聚类 用户聚类依靠用户相似度作为距离度量,使用K-medoids作为聚类算法 问题主要存在于:由于评分稀疏,很多用户之间距离为0 命令:python user_clustering.py input_file number_of_clusters output_file 建树前的准备工作 生成用户聚类对任一物品的平均评分,便于计算时直接调用 利用非线性回归拟合的参数生成相似度矩阵 命令:python buildtree_preparation.py input_file init_ptitle init_pdescrip output_file 2. 建树及预测 树的生成: 三叉树,对应不喜欢、一般般喜欢和喜欢三个节点 生成的节点信息用self.tree和self.node_interval两个变量保存 构建预测模型: 利用Spark的mllib包实现ALS Matrix Factorization 生成伪物品(每个节点)和用户对应的latent vector(对每一层都计算) 预测评分: 对每一个test商品,从树的根节点开始向下走,利用目标叶子节点的latent vector作为它的特征向量 利用特征向量和所有物品的特征向量的点积预测评分,计算RMSE(对每一层都计算) 命令:python build_tree.py [input_file1, ..., input_file5] desired_depth 3. 运行 利用Python脚本运行上述所有步骤:python script.py 代码开头数据集名称(dataset)需相应更改 4. 对比实验 FDT (Factorized Deicision Tree) python factorized_decision_tree.py dataset depth (dataset是数据集的名字,depth决定了树的高度) 输入: I*U 的矩阵 => new-user problem 输入: U*I 的矩阵 => new-item problem CAL (Content-based Active Learning) python content_based_active_learning.py dataset K (dataset是数据集的名字,K决定了选择TopK的用户进行query) CBCF (Content-based Collaborative Filtering)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值