⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hybridsubsetmapper.java

📁 Multi-label classification 和weka集成
💻 JAVA
字号:
package mulan.classifier;
import java.util.Vector;

import mulan.LabelSet;
import weka.core.Instance;
import weka.core.Instances;


@SuppressWarnings("serial")
public class HybridSubsetMapper extends SubsetMapper
{
	private LabelPowersetClassifier classifier;
	
	
	public HybridSubsetMapper(Instances instances, int numLabels, int diff)
	throws Exception
	{
		this(instances, numLabels);
		distanceThreshold = diff;
	}
	
    public HybridSubsetMapper(Instances instances, int numLabels)
    throws Exception
    {
    	super(instances, numLabels);

    	/*
    	LibSVM lsBaseClassifier = new LibSVM();
    	svm.setOptions(Utils.splitOptions(
    		"-Z"
    	));
    	svm.setProbabilityEstimates(true);
    	//*/
    	//*
    	weka.classifiers.lazy.IBk lsBaseClassifier = new weka.classifiers.lazy.IBk();
    	lsBaseClassifier.setKNN(10);
    	//*/
    	classifier = new LabelPowersetClassifier(lsBaseClassifier, numLabels);
    	classifier.buildClassifier(instances);
    	
    }
    
    public Prediction nearestSubset(Instance instance, double[] labels)
    throws Exception
    {
    	LabelSet set = new LabelSet(labels);
    	Vector<LabelSet> neighbors = subsetsWithinDiff(set, distanceThreshold);
    	if (neighbors.size() == 0) 
    		return new Prediction(labels, this.calculateConfidences(set));
    	
    	//Had to hack into LabelPowersetClassifier to expose this information
    	double[] distro = classifier.distributionFromBaseClassifier(instance);
    	
    	//Find the subset with the highest prior probability
    	LabelSet nearest = null;
    	double bestProb = Double.MIN_VALUE;
    	for(LabelSet neighbor : neighbors)
    	{
    		int labelIdx = classifier.indexOfClassValue(neighbor.toBitString());
    		if (distro[labelIdx] > bestProb)
    		{
    			bestProb = distro[labelIdx];
    			nearest  = neighbor;
    		}
    	}
        if (nearest == null)
            return new Prediction(labels, this.calculateConfidences(set));
        
    	return new Prediction(nearest.toDoubleArray(), calculateConfidences(nearest));

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -