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

📄 adacost.java

📁 AdaCost算法源程序 java编写 可添加到weka系统中
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package weka.classifiers.meta;

import weka.classifiers.rules.JRip;
import java.io.*;
import java.util.*;
import weka.classifiers.*;
import weka.core.*;


public class AdaCost extends  RandomizableIteratedSingleClassifierEnhancer
    implements OptionHandler, Drawable,WeightedInstancesHandler, Sourcable, TechnicalInformationHandler
 
{
	    
    protected String defaultClassifierString()
    {
        return "weka.classifiers.rules.JRip";
    }

    public AdaCost()
    {
        m_MatrixSource = 1;
        m_OnDemandDirectory = new File(System.getProperty("user.dir"));
        m_CostMatrix = new CostMatrix(1);
        m_Classifier = new JRip();
        m_WeightThreshold = 100;
    }
    
    public String globalInfo()
    {
        return (new StringBuilder()).append("Class for cost-sensitive boosting a nominal class classifier ").append(getTechnicalInformation().toString()).toString();
    }

    public TechnicalInformation getTechnicalInformation()
    {
        TechnicalInformation technicalinformation = new TechnicalInformation(weka.core.TechnicalInformation.Type.INPROCEEDINGS);
        technicalinformation.setValue(weka.core.TechnicalInformation.Field.AUTHOR, "Wei Fan, Salvatore J.Stolfo");
        technicalinformation.setValue(weka.core.TechnicalInformation.Field.TITLE, "Experiments with a cost-sensitive boosting algorithm");
        technicalinformation.setValue(weka.core.TechnicalInformation.Field.BOOKTITLE, "Sixteenth International Conference on Machine Learning");
        technicalinformation.setValue(weka.core.TechnicalInformation.Field.YEAR, "1999");
        technicalinformation.setValue(weka.core.TechnicalInformation.Field.PAGES, "97-105");
        return technicalinformation;
    }

    
     public Enumeration listOptions()
    {
        Vector vector = new Vector(5);
        vector.addElement(new Option("\tMinimize expected misclassification cost. Default is to\n\treweight training instances according to costs per class", "M", 0, "-M"));
        vector.addElement(new Option("\tFile name of a cost matrix to use. If this is not supplied,\n\ta cost matrix will be loaded on demand. The name of the\n\ton-demand file is the relation name of the training data\n\tplus \".cost\", and the path to the on-demand file is\n\tspecified with the -N option.", "C", 1, "-C <cost file name>"));
        vector.addElement(new Option("\tName of a directory to search for cost files when loading\n\tcosts on demand (default current directory).", "N", 1, "-N <directory>"));
        vector.addElement(new Option("\tThe cost matrix in Matlab single line format.", "cost-matrix", 1, "-cost-matrix <matrix>"));
        vector.addElement(new Option("\tPercentage of weight mass to base training on.\n\t(default 100, reduce to around 90 speed up)", "P", 1, "-P <num>"));
        vector.addElement(new Option("\tUse resampling for boosting.", "Q", 0, "-Q"));
              
        for(Enumeration enumeration = super.listOptions(); enumeration.hasMoreElements(); vector.addElement(enumeration.nextElement()));
        return vector.elements();

    }
    
    
    public void setOptions(String as[])
        throws Exception
    {
        
        String s = Utils.getOption('P', as);
        if(s.length() != 0)
            setWeightThreshold(Integer.parseInt(s));
        else
            setWeightThreshold(100);
            
           
        setMinimizeExpectedCost(Utils.getFlag('M', as));
        String s1 = Utils.getOption('C', as);
        if(s1.length() != 0)
        {
            try
            {
                setCostMatrix(new CostMatrix(new BufferedReader(new FileReader(s1))));
            }
            catch(Exception exception)
            {
                setCostMatrix(null);
            }
            setCostMatrixSource(new SelectedTag(2, TAGS_MATRIX_SOURCE));
            m_CostFile = s1;
        } else
        {
            setCostMatrixSource(new SelectedTag(1, TAGS_MATRIX_SOURCE));
        }
        String s2 = Utils.getOption('D', as);
        if(s2.length() != 0)
            setOnDemandDirectory(new File(s2));
        String s3 = Utils.getOption("cost-matrix", as);
        if(s3.length() != 0)
        {
            StringWriter stringwriter = new StringWriter();
            CostMatrix.parseMatlab(s3).write(stringwriter);
            setCostMatrix(new CostMatrix(new StringReader(stringwriter.toString())));
            setCostMatrixSource(new SelectedTag(2, TAGS_MATRIX_SOURCE));
        }
        
            
        setUseResampling(Utils.getFlag('Q', as));
        if(m_UseResampling && s.length() != 0)
        {
            throw new Exception("Weight pruning with resamplingnot allowed.");
        } else
        {
            super.setOptions(as);
            return;
        }
        
    }
    
    
    public String[] getOptions()
    {
    	  String as[] = super.getOptions();
        String as1[] = new String[as.length + 9];
        int i = 0;
        if(m_MatrixSource == 2)
        {
            if(m_CostFile != null)
            {
                as1[i++] = "-C";
                as1[i++] = (new StringBuilder()).append("").append(m_CostFile).toString();
            } else
            {
                as1[i++] = "-cost-matrix";
                as1[i++] = getCostMatrix().toMatlab();
            }
        } else
        {
            as1[i++] = "-N";
            as1[i++] = (new StringBuilder()).append("").append(getOnDemandDirectory()).toString();
        }
        if(getMinimizeExpectedCost())
            as1[i++] = "-M";
       
    
        if(getUseResampling())
        {
            as1[i++] = "-Q";
            as1[i++] = "";
        } else
        {
            as1[i++] = "-P";
            as1[i++] = (new StringBuilder()).append("").append(getWeightThreshold()).toString();
        }
        System.arraycopy(as, 0, as1, i, as.length);        
          
            for(; i < as1.length; i++)
            if(as1[i] == null)
                as1[i] = "";
                
        return as1;
    }
    
    
    
    protected Instances selectWeightQuantile(Instances instances, double d)
    {
        int i = instances.numInstances();
        Instances instances1 = new Instances(instances, i);
        double ad[] = new double[i];
        double d1 = 0.0D;
        for(int j = 0; j < i; j++)
        {
            ad[j] = instances.instance(j).weight();
            d1 += ad[j];
        }

        double d2 = d1 * d;
        int ai[] = Utils.sort(ad);
        d1 = 0.0D;
        int k = i - 1;
        do
        {
            if(k < 0)
                break;
            Instance instance = (Instance)instances.instance(ai[k]).copy();
            instances1.add(instance);
            d1 += ad[ai[k]];
            if(d1 > d2 && k > 0 && ad[ai[k]] != ad[ai[k - 1]])
                break;
            k--;
        } while(true);
        if(m_Debug)
            System.err.println((new StringBuilder()).append("Selected ").append(instances1.numInstances()).append(" out of ").append(i).toString());
        return instances1;
    }
    
    
    
    public String weightThresholdTipText()
    {
        return "Weight threshold for weight pruning.";
    }

    public void setWeightThreshold(int i)
    {
        m_WeightThreshold = i;
    }

    public int getWeightThreshold()
    {
        return m_WeightThreshold;
    }

    public String useResamplingTipText()
    {
        return "Whether resampling is used instead of reweighting.";
    }

    public void setUseResampling(boolean flag)
    {
        m_UseResampling = flag;
    }

    public boolean getUseResampling()
    {
        return m_UseResampling;
    }


    public String costMatrixSourceTipText()
    {
        return (new StringBuilder()).append("Sets where to get the cost matrix. The two options areto use the supplied explicit cost matrix (the setting of the costMatrix property), or to load a cost matrix from a file when required (this file will be loaded from the directory set by the onDemandDirectory property and will be named relation_name").append(CostMatrix.FILE_EXTENSION).append(").").toString();
    }

    public SelectedTag getCostMatrixSource()
    {
        return new SelectedTag(m_MatrixSource, TAGS_MATRIX_SOURCE);
    }

    public void setCostMatrixSource(SelectedTag selectedtag)
    {
        if(selectedtag.getTags() == TAGS_MATRIX_SOURCE)
            m_MatrixSource = selectedtag.getSelectedTag().getID();
    }

    public String onDemandDirectoryTipText()
    {
        return "Sets the directory where cost files are loaded from. This option is used when the costMatrixSource is set to \"On Demand\".";
    }

    public File getOnDemandDirectory()
    {
        return m_OnDemandDirectory;
    }

    public void setOnDemandDirectory(File file)
    {
        if(file.isDirectory())
            m_OnDemandDirectory = file;
        else
            m_OnDemandDirectory = new File(file.getParent());
        m_MatrixSource = 1;
    }

    public String minimizeExpectedCostTipText()
    {
        return "Sets whether the minimum expected cost criteria will be used. If this is false, the training data will be reweighted according to the costs assigned to each class. If true, the minimum expected cost criteria will be used.";
    }

    public boolean getMinimizeExpectedCost()
    {
        return m_MinimizeExpectedCost;
    }

    public void setMinimizeExpectedCost(boolean flag)
    {
        m_MinimizeExpectedCost = flag;
    }

    protected String getClassifierSpec()
    {
        Classifier classifier = getClassifier();
        if(classifier instanceof OptionHandler)
            return (new StringBuilder()).append(classifier.getClass().getName()).append(" ").append(Utils.joinOptions(classifier.getOptions())).toString();
        else
            return classifier.getClass().getName();
    }

    public String costMatrixTipText()
    {
        return "Sets the cost matrix explicitly. This matrix is used if the costMatrixSource property is set to \"Supplied\".";
    }

    public CostMatrix getCostMatrix()
    {
        return m_CostMatrix;
    }

    public void setCostMatrix(CostMatrix costmatrix)
    {
        m_CostMatrix = costmatrix;
        m_MatrixSource = 2;
    }

    public Capabilities getCapabilities()
    {

⌨️ 快捷键说明

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