Example usage for weka.core Attribute Attribute

List of usage examples for weka.core Attribute Attribute

Introduction

In this page you can find the example usage for weka.core Attribute Attribute.

Prototype





public Attribute(String attributeName, int index) 

Source Link

Document

Constructor for a numeric attribute with a particular index.

Usage

From source file:TextDirectoryLoader.java

License:Open Source License

/**
 * Determines and returns (if possible) the structure (internally the 
 * header) of the data set as an empty set of instances.
 *
 * @return          the structure of the data set as an empty 
 *             set of Instances//from w ww .jav a2s  .  co  m
 * @throws IOException    if an error occurs
 */
public Instances getStructure() throws IOException {
    if (getDirectory() == null) {
        throw new IOException("No directory/source has been specified");
    }

    // determine class labels, i.e., sub-dirs
    if (m_structure == null) {
        String directoryPath = getDirectory().getAbsolutePath();
        FastVector atts = new FastVector();
        FastVector classes = new FastVector();

        File dir = new File(directoryPath);
        String[] subdirs = dir.list();

        for (int i = 0; i < subdirs.length; i++) {
            File subdir = new File(directoryPath + File.separator + subdirs[i]);
            if (subdir.isDirectory())
                classes.addElement(subdirs[i]);
        }

        atts.addElement(new Attribute("text", (FastVector) null));
        if (m_OutputFilename)
            atts.addElement(new Attribute("filename", (FastVector) null));
        // make sure that the name of the class attribute is unlikely to 
        // clash with any attribute created via the StringToWordVector filter
        atts.addElement(new Attribute("@@class@@", classes));

        String relName = directoryPath.replaceAll("/", "_");
        relName = relName.replaceAll("\\\\", "_").replaceAll(":", "_");
        m_structure = new Instances(relName, atts, 0);
        m_structure.setClassIndex(m_structure.numAttributes() - 1);
    }

    return m_structure;
}

From source file:MultiClassClassifier.java

License:Open Source License

/**
 * Builds the classifiers./* w w w .j a  va2  s.com*/
 *
 * @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();

    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 = Classifier.makeCopies(m_Classifier, 1);
        m_Classifiers[0].buildClassifier(insts);

        m_ClassFilters = null;

    } else if (m_Method == METHOD_1_AGAINST_1) {
        // generate fastvector of pairs
        FastVector pairs = new FastVector();
        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.addElement(pair);
            }
        }

        numClassifiers = pairs.size();
        m_Classifiers = Classifier.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.elementAt(i));
            Instances tempInstances = new Instances(insts, 0);
            tempInstances.setClassIndex(-1);
            classFilter.setInputFormat(tempInstances);
            newInsts = Filter.useFilter(insts, classFilter);
            if (newInsts.numInstances() > 0) {
                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);
        m_TwoClassDataset.deleteAttributeAt(classIndex);
        FastVector classLabels = new FastVector();
        classLabels.addElement("class0");
        classLabels.addElement("class1");
        m_TwoClassDataset.insertAttributeAt(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 = Classifier.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();
}

From source file:TextDirectoryToArff.java

License:Open Source License

public Instances createDataset(String directoryPath) throws Exception {

    FastVector atts = new FastVector(2);
    atts.addElement(new Attribute("filename", (FastVector) null));
    atts.addElement(new Attribute("contents", (FastVector) null));
    Instances data = new Instances("text_files_in_" + directoryPath, atts, 0);

    File dir = new File(directoryPath);
    String[] files = dir.list();/*from   www.  j a v a2  s.  c  o  m*/
    for (int i = 0; i < files.length; i++) {
        if (files[i].endsWith(".txt")) {
            try {
                double[] newInst = new double[2];
                newInst[0] = (double) data.attribute(0).addStringValue(files[i]);
                File txt = new File(directoryPath + File.separator + files[i]);
                InputStreamReader is;
                is = new InputStreamReader(new FileInputStream(txt));
                StringBuffer txtStr = new StringBuffer();
                int c;
                while ((c = is.read()) != -1) {
                    txtStr.append((char) c);
                }
                newInst[1] = (double) data.attribute(1).addStringValue(txtStr.toString());
                data.add(new Instance(1.0, newInst));
            } catch (Exception e) {
                //System.err.println("failed to convert file: " + directoryPath + File.separator + files[i]);
            }
        }
    }
    return data;
}

From source file:PCADetector.java

License:Apache License

public Instances getInstances() {
    int numAtts = m_oriDataMatrix.size();
    if (numAtts < 0)
        return null;
    ArrayList<Attribute> atts = new ArrayList<Attribute>(numAtts);
    for (int att = 0; att < numAtts; att++) {
        atts.add(new Attribute(Integer.toString(att), att));
    }/* ww w  .j a  va  2s  .c o m*/
    int numInstances = m_oriDataMatrix.get(0).size();
    if (numInstances <= 0)
        return null;
    Instances dataset = new Instances("MetricInstances", atts, numInstances);
    for (int inst = 0; inst < numInstances; inst++) {
        Instance newInst = new DenseInstance(numAtts);
        for (int att = 0; att < numAtts; att++) {
            newInst.setValue(att, m_oriDataMatrix.get(att).get(inst));
        }
        dataset.add(newInst);
    }
    return dataset;
}

From source file:activeSegmentation.feature.FeatureExtraction.java

License:Open Source License

/**
 * Create training instances out of the user markings
 * @return set of instances (feature vectors in Weka format)
 *//*w w  w .j  a v a2  s. c om*/
@Override
public void createTrainingInstance(List<String> classLabels, int classes,
        List<Vector<ArrayList<Roi>>> examples) {
    ArrayList<Attribute> attributes = createFeatureHeader();
    attributes.add(new Attribute(Common.CLASS, addClasstoHeader(classes, classLabels)));

    // create initial set of instances
    trainingData = new Instances(Common.INSTANCE_NAME, attributes, 1);
    // Set the index of the class attribute
    trainingData.setClassIndex(filterManager.getNumOfFeatures());
    for (int classIndex = 0; classIndex < classes; classIndex++) {
        int nl = 0;

        // Read all lists of examples
        for (int sliceNum = 1; sliceNum <= filterManager.getOriginalImageSize(); sliceNum++)
            for (int j = 0; j < examples.get(sliceNum - 1).get(classIndex).size(); j++) {
                Roi r = examples.get(sliceNum - 1).get(classIndex).get(j);
                nl += addRectangleRoiInstances(trainingData, classIndex, sliceNum, r);
            }
        IJ.log("# of pixels selected as " + classLabels.get(classIndex) + ": " + nl);
    }

}

From source file:activeSegmentation.feature.FeatureExtraction.java

License:Open Source License

/**
 * Add training samples from a rectangular roi
 * //from ww  w.  j a va 2 s .  c o  m
 * @param trainingData set of instances to add to
 * @param classIndex class index value
 * @param sliceNum number of 2d slice being processed
 * @param r shape roi
 * @return number of instances added
 */
private Instances addRectangleRoiInstances(int sliceNum, List<String> classLabels, int classes) {

    Instances testingData;
    ArrayList<Attribute> attributes = createFeatureHeader();
    attributes.add(new Attribute(Common.CLASS, addClasstoHeader(classes, classLabels)));
    System.out.println(attributes.toString());
    // create initial set of instances
    testingData = new Instances(Common.INSTANCE_NAME, attributes, 1);
    // Set the index of the class attribute
    testingData.setClassIndex(filterManager.getNumOfFeatures());

    for (int x = 0; x < originalImage.getWidth(); x++) {
        for (int y = 0; y < originalImage.getHeight(); y++) {

            testingData.add(filterManager.createInstance(x, y, 0, sliceNum));

        }
    }
    // increase number of instances for this class

    System.out.println("SIZe" + testingData.size());
    System.out.println(testingData.get(1).toString());
    return testingData;
}

From source file:adams.data.conversion.TimeseriesToWekaInstances.java

License:Open Source License

/**
 * Performs the actual conversion.// ww  w . j av  a  2s . co m
 *
 * @return      the converted data
 * @throws Exception   if something goes wrong with the conversion
 */
@Override
protected Object doConvert() throws Exception {
    Instances result;
    ArrayList<Attribute> atts;
    Instance inst;
    Timeseries series;
    TimeseriesPoint point;
    double[] value;

    series = (Timeseries) m_Input;

    atts = new ArrayList<Attribute>();
    atts.add(new Attribute("Timestamp", m_Format.getValue()));
    atts.add(new Attribute("Value"));

    result = new Instances(series.getID(), atts, series.size());
    for (Object obj : series.toList()) {
        point = (TimeseriesPoint) obj;
        value = new double[2];
        value[0] = point.getTimestamp().getTime();
        value[1] = point.getValue();
        inst = new DenseInstance(1.0, value);
        result.add(inst);
    }

    return result;
}

From source file:adams.data.featureconverter.Weka.java

License:Open Source License

/**
 * Performs the actual generation of a row from the raw data.
 * /*from   ww  w.j av a  2s  .c o m*/
 * @param data   the data of the row, elements can be null (= missing)
 * @return      the dataset structure
 */
@Override
protected Instances doGenerateHeader(HeaderDefinition header) {
    Instances result;
    ArrayList<Attribute> atts;
    ArrayList<String> values;
    int i;

    atts = new ArrayList<Attribute>();
    for (i = 0; i < header.size(); i++) {
        switch (header.getType(i)) {
        case BOOLEAN:
            values = new ArrayList<String>();
            values.add("yes");
            values.add("no");
            atts.add(new Attribute(header.getName(i), values));
            break;
        case NUMERIC:
            atts.add(new Attribute(header.getName(i)));
            break;
        case STRING:
        case UNKNOWN:
            atts.add(new Attribute(header.getName(i), (List<String>) null));
            break;
        }
    }

    result = new Instances(header.getDataset(), atts, 0);

    return result;
}

From source file:adams.flow.sink.WekaCostBenefitAnalysis.java

License:Open Source License

/**
 * Plots the token (the panel and dialog have already been created at
 * this stage).//from   w ww.  j a v a 2s  .  c o m
 *
 * @param token   the token to display
 */
@Override
protected void display(Token token) {
    Evaluation eval;
    Attribute classAtt;
    Attribute classAttToUse;
    int classValue;
    ThresholdCurve tc;
    Instances result;
    ArrayList<String> newNames;
    CostBenefitAnalysis cbAnalysis;
    PlotData2D tempd;
    boolean[] cp;
    int n;

    try {
        if (token.getPayload() instanceof WekaEvaluationContainer)
            eval = (Evaluation) ((WekaEvaluationContainer) token.getPayload())
                    .getValue(WekaEvaluationContainer.VALUE_EVALUATION);
        else
            eval = (Evaluation) token.getPayload();
        if (eval.predictions() == null) {
            getLogger().severe("No predictions available from Evaluation object!");
            return;
        }
        classAtt = eval.getHeader().classAttribute();
        m_ClassIndex.setData(classAtt);
        classValue = m_ClassIndex.getIntIndex();
        tc = new ThresholdCurve();
        result = tc.getCurve(eval.predictions(), classValue);

        // Create a dummy class attribute with the chosen
        // class value as index 0 (if necessary).
        classAttToUse = eval.getHeader().classAttribute();
        if (classValue != 0) {
            newNames = new ArrayList<>();
            newNames.add(classAtt.value(classValue));
            for (int k = 0; k < classAtt.numValues(); k++) {
                if (k != classValue)
                    newNames.add(classAtt.value(k));
            }
            classAttToUse = new Attribute(classAtt.name(), newNames);
        }
        // assemble plot data
        tempd = new PlotData2D(result);
        tempd.setPlotName(result.relationName());
        tempd.m_alwaysDisplayPointsOfThisSize = 10;
        // specify which points are connected
        cp = new boolean[result.numInstances()];
        for (n = 1; n < cp.length; n++)
            cp[n] = true;
        tempd.setConnectPoints(cp);
        // add plot
        m_CostBenefitPanel.setCurveData(tempd, classAttToUse);
    } catch (Exception e) {
        handleException("Failed to display token: " + token, e);
    }
}

From source file:adams.flow.sink.WekaCostBenefitAnalysis.java

License:Open Source License

/**
 * Creates a new panel for the token.//from  ww w.  j a v  a  2 s  .co m
 *
 * @param token   the token to display in a new panel, can be null
 * @return      the generated panel
 */
public AbstractDisplayPanel createDisplayPanel(Token token) {
    AbstractDisplayPanel result;
    String name;

    if (token != null)
        name = "Cost curve (" + getEvaluation(token).getHeader().relationName() + ")";
    else
        name = "Cost curve";

    result = new AbstractComponentDisplayPanel(name) {
        private static final long serialVersionUID = -3513994354297811163L;
        protected CostBenefitAnalysis m_VisualizePanel;

        @Override
        protected void initGUI() {
            super.initGUI();
            setLayout(new BorderLayout());
            m_VisualizePanel = new CostBenefitAnalysis();
            add(m_VisualizePanel, BorderLayout.CENTER);
        }

        @Override
        public void display(Token token) {
            try {
                Evaluation eval = getEvaluation(token);
                Attribute classAtt = eval.getHeader().classAttribute();
                m_ClassIndex.setData(classAtt);
                int classValue = m_ClassIndex.getIntIndex();
                ThresholdCurve tc = new ThresholdCurve();
                Instances result = tc.getCurve(eval.predictions(), classValue);

                // Create a dummy class attribute with the chosen
                // class value as index 0 (if necessary).
                Attribute classAttToUse = eval.getHeader().classAttribute();
                if (classValue != 0) {
                    ArrayList<String> newNames = new ArrayList<>();
                    newNames.add(classAtt.value(classValue));
                    for (int k = 0; k < classAtt.numValues(); k++) {
                        if (k != classValue)
                            newNames.add(classAtt.value(k));
                    }
                    classAttToUse = new Attribute(classAtt.name(), newNames);
                }
                // assemble plot data
                PlotData2D tempd = new PlotData2D(result);
                tempd.setPlotName(result.relationName());
                tempd.m_alwaysDisplayPointsOfThisSize = 10;
                // specify which points are connected
                boolean[] cp = new boolean[result.numInstances()];
                for (int n = 1; n < cp.length; n++)
                    cp[n] = true;
                tempd.setConnectPoints(cp);
                // add plot
                m_VisualizePanel.setCurveData(tempd, classAttToUse);
            } catch (Exception e) {
                getLogger().log(Level.SEVERE, "Failed to display token: " + token, e);
            }
        }

        @Override
        public JComponent supplyComponent() {
            return m_VisualizePanel;
        }

        @Override
        public void clearPanel() {
        }

        public void cleanUp() {
        }
    };

    if (token != null)
        result.display(token);

    return result;
}