Search in sources :

Example 1 with MakeIndicator

use of weka.filters.unsupervised.attribute.MakeIndicator in project umple by umple.

the class MultiClassClassifier method buildClassifier.

/**
 * Builds the classifiers.
 *
 * @param insts the training data.
 * @throws Exception if a classifier can't be built
 */
public void buildClassifier(Instances insts) throws Exception {
    Instances newInsts;
    // can classifier handle the data?
    getCapabilities().testWithFail(insts);
    // zero training instances - could be incremental
    boolean zeroTrainingInstances = insts.numInstances() == 0;
    // remove instances with missing class
    insts = new Instances(insts);
    insts.deleteWithMissingClass();
    if (m_Classifier == null) {
        throw new Exception("No base classifier has been set!");
    }
    m_ZeroR = new ZeroR();
    m_ZeroR.buildClassifier(insts);
    m_TwoClassDataset = null;
    int numClassifiers = insts.numClasses();
    if (numClassifiers <= 2) {
        m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, 1);
        m_Classifiers[0].buildClassifier(insts);
        m_ClassFilters = null;
    } else if (m_Method == METHOD_1_AGAINST_1) {
        // generate fastvector of pairs
        ArrayList<int[]> pairs = new ArrayList<int[]>();
        for (int i = 0; i < insts.numClasses(); i++) {
            for (int j = 0; j < insts.numClasses(); j++) {
                if (j <= i)
                    continue;
                int[] pair = new int[2];
                pair[0] = i;
                pair[1] = j;
                pairs.add(pair);
            }
        }
        numClassifiers = pairs.size();
        m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, numClassifiers);
        m_ClassFilters = new Filter[numClassifiers];
        m_SumOfWeights = new double[numClassifiers];
        // generate the classifiers
        for (int i = 0; i < numClassifiers; i++) {
            RemoveWithValues classFilter = new RemoveWithValues();
            classFilter.setAttributeIndex("" + (insts.classIndex() + 1));
            classFilter.setModifyHeader(true);
            classFilter.setInvertSelection(true);
            classFilter.setNominalIndicesArr((int[]) pairs.get(i));
            Instances tempInstances = new Instances(insts, 0);
            tempInstances.setClassIndex(-1);
            classFilter.setInputFormat(tempInstances);
            newInsts = Filter.useFilter(insts, classFilter);
            if (newInsts.numInstances() > 0 || zeroTrainingInstances) {
                newInsts.setClassIndex(insts.classIndex());
                m_Classifiers[i].buildClassifier(newInsts);
                m_ClassFilters[i] = classFilter;
                m_SumOfWeights[i] = newInsts.sumOfWeights();
            } else {
                m_Classifiers[i] = null;
                m_ClassFilters[i] = null;
            }
        }
        // construct a two-class header version of the dataset
        m_TwoClassDataset = new Instances(insts, 0);
        int classIndex = m_TwoClassDataset.classIndex();
        m_TwoClassDataset.setClassIndex(-1);
        ArrayList<String> classLabels = new ArrayList<String>();
        classLabels.add("class0");
        classLabels.add("class1");
        m_TwoClassDataset.replaceAttributeAt(new Attribute("class", classLabels), classIndex);
        m_TwoClassDataset.setClassIndex(classIndex);
    } else {
        // use error correcting code style methods
        Code code = null;
        switch(m_Method) {
            case METHOD_ERROR_EXHAUSTIVE:
                code = new ExhaustiveCode(numClassifiers);
                break;
            case METHOD_ERROR_RANDOM:
                code = new RandomCode(numClassifiers, (int) (numClassifiers * m_RandomWidthFactor), insts);
                break;
            case METHOD_1_AGAINST_ALL:
                code = new StandardCode(numClassifiers);
                break;
            default:
                throw new Exception("Unrecognized correction code type");
        }
        numClassifiers = code.size();
        m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, numClassifiers);
        m_ClassFilters = new MakeIndicator[numClassifiers];
        for (int i = 0; i < m_Classifiers.length; i++) {
            m_ClassFilters[i] = new MakeIndicator();
            MakeIndicator classFilter = (MakeIndicator) m_ClassFilters[i];
            classFilter.setAttributeIndex("" + (insts.classIndex() + 1));
            classFilter.setValueIndices(code.getIndices(i));
            classFilter.setNumeric(false);
            classFilter.setInputFormat(insts);
            newInsts = Filter.useFilter(insts, m_ClassFilters[i]);
            m_Classifiers[i].buildClassifier(newInsts);
        }
    }
    m_ClassAttribute = insts.classAttribute();
}
Also used : Attribute(weka.core.Attribute) ArrayList(java.util.ArrayList) ZeroR(weka.classifiers.rules.ZeroR) RemoveWithValues(weka.filters.unsupervised.instance.RemoveWithValues) MakeIndicator(weka.filters.unsupervised.attribute.MakeIndicator) Instances(weka.core.Instances) Filter(weka.filters.Filter)

Example 2 with MakeIndicator

use of weka.filters.unsupervised.attribute.MakeIndicator in project umple by umple.

the class MultiClassClassifier method toString.

/**
 * Prints the classifiers.
 *
 * @return a string representation of the classifier
 */
public String toString() {
    if (m_Classifiers == null) {
        return "MultiClassClassifier: No model built yet.";
    }
    StringBuffer text = new StringBuffer();
    text.append("MultiClassClassifier\n\n");
    for (int i = 0; i < m_Classifiers.length; i++) {
        text.append("Classifier ").append(i + 1);
        if (m_Classifiers[i] != null) {
            if ((m_ClassFilters != null) && (m_ClassFilters[i] != null)) {
                if (m_ClassFilters[i] instanceof RemoveWithValues) {
                    Range range = new Range(((RemoveWithValues) m_ClassFilters[i]).getNominalIndices());
                    range.setUpper(m_ClassAttribute.numValues());
                    int[] pair = range.getSelection();
                    text.append(", " + (pair[0] + 1) + " vs " + (pair[1] + 1));
                } else if (m_ClassFilters[i] instanceof MakeIndicator) {
                    text.append(", using indicator values: ");
                    text.append(((MakeIndicator) m_ClassFilters[i]).getValueRange());
                }
            }
            text.append('\n');
            text.append(m_Classifiers[i].toString() + "\n\n");
        } else {
            text.append(" Skipped (no training examples)\n");
        }
    }
    return text.toString();
}
Also used : RemoveWithValues(weka.filters.unsupervised.instance.RemoveWithValues) MakeIndicator(weka.filters.unsupervised.attribute.MakeIndicator) Range(weka.core.Range)

Example 3 with MakeIndicator

use of weka.filters.unsupervised.attribute.MakeIndicator in project umple by umple.

the class ClassificationViaRegression method buildClassifier.

/**
 * Builds the classifiers.
 *
 * @param insts the training data.
 * @throws Exception if a classifier can't be built
 */
public void buildClassifier(Instances insts) throws Exception {
    Instances newInsts;
    // can classifier handle the data?
    getCapabilities().testWithFail(insts);
    // remove instances with missing class
    insts = new Instances(insts);
    insts.deleteWithMissingClass();
    m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, insts.numClasses());
    m_ClassFilters = new MakeIndicator[insts.numClasses()];
    for (int i = 0; i < insts.numClasses(); i++) {
        m_ClassFilters[i] = new MakeIndicator();
        m_ClassFilters[i].setAttributeIndex("" + (insts.classIndex() + 1));
        m_ClassFilters[i].setValueIndex(i);
        m_ClassFilters[i].setNumeric(true);
        m_ClassFilters[i].setInputFormat(insts);
        newInsts = Filter.useFilter(insts, m_ClassFilters[i]);
        m_Classifiers[i].buildClassifier(newInsts);
    }
}
Also used : Instances(weka.core.Instances) MakeIndicator(weka.filters.unsupervised.attribute.MakeIndicator)

Aggregations

MakeIndicator (weka.filters.unsupervised.attribute.MakeIndicator)3 Instances (weka.core.Instances)2 RemoveWithValues (weka.filters.unsupervised.instance.RemoveWithValues)2 ArrayList (java.util.ArrayList)1 ZeroR (weka.classifiers.rules.ZeroR)1 Attribute (weka.core.Attribute)1 Range (weka.core.Range)1 Filter (weka.filters.Filter)1