Example usage for weka.core Attribute copy

List of usage examples for weka.core Attribute copy

Introduction

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

Prototype


@Override
publicObject copy() 

Source Link

Document

Produces a shallow copy of this attribute.

Usage

From source file:datalib.SparseInstances.java

License:Open Source License

public void addAttribute(/* @non_null@ */Attribute attribute) {

    Attribute newAttribute = (Attribute) attribute.copy();

    m_Attributes.addElement(newAttribute);
}

From source file:en_deep.mlprocess.manipulation.featmodif.FeatureModifierFilter.java

License:Open Source License

/**
 * Set the output format if the class is nominal.
 *///from w w w.  j  ava  2s  . c o  m
private void setOutputFormat() {

    FastVector newAtts;
    int newClassIndex;
    Instances outputFormat;

    newClassIndex = getInputFormat().classIndex();
    newAtts = new FastVector();

    BitSet attrSrc = new BitSet(), attrDest = new BitSet();

    int attSoFar = 0;

    for (int j = 0; j < getInputFormat().numAttributes(); j++) {

        Attribute att = getInputFormat().attribute(j);

        if (!m_Columns.isInRange(j)) {
            newAtts.addElement(att.copy());

            attrSrc.set(j);
            attrDest.set(attSoFar++);

        } else {

            ArrayList<Attribute> valueAttrs = getAttributeOutputFormat(att);

            if (newClassIndex >= 0 && j < getInputFormat().classIndex()) {
                newClassIndex += valueAttrs.size() - 1;
            }
            newAtts.addAll(valueAttrs);

            if (m_PreserveOriginals) {
                attrSrc.set(j);
                attrDest.set(attSoFar);
            }
            attSoFar += valueAttrs.size();
        }
    }

    outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0);
    outputFormat.setClassIndex(newClassIndex);
    setOutputFormat(outputFormat);

    m_StringToCopySrc = new AttributeLocator(getInputFormat(), Attribute.STRING, MathUtils.findTrue(attrSrc));
    m_StringToCopyDst = new AttributeLocator(outputFormat, Attribute.STRING, MathUtils.findTrue(attrDest));
}

From source file:en_deep.mlprocess.manipulation.featmodif.FeatureModifierFilter.java

License:Open Source License

/**
 * Return a list of new attributes for the given attribute, when the filter class will be applied to it.
 *
 * @param att the attribute to be converted
 * @return a list of output attributes for this attribute
 */// www. j a v a2 s. c  o  m
private ArrayList<Attribute> getAttributeOutputFormat(Attribute att) {

    ArrayList newAtts = new ArrayList<Attribute>();

    if (this.m_PreserveOriginals) {
        newAtts.add(att.copy());
    }

    String[] outputNames = this.m_OperClass.getOutputFeatsList(att.name());

    for (int i = 0; i < outputNames.length; ++i) {
        newAtts.add(new Attribute(outputNames[i], (List<String>) null));
    }

    return newAtts;
}

From source file:en_deep.mlprocess.manipulation.featmodif.ReplaceMissing.java

License:Open Source License

/**
 * Set the output format if the class is nominal.
 */// w w  w .  j a v a2  s  .  com
private void setOutputFormat() {

    FastVector newAtts;
    Instances outputFormat;

    newAtts = new FastVector();

    BitSet attrSrc = new BitSet();

    for (int j = 0; j < getInputFormat().numAttributes(); j++) {

        Attribute att = null;
        Attribute srcAtt = getInputFormat().attribute(j);

        if (!m_Columns.isInRange(j) || srcAtt.indexOfValue(m_ReplVal) >= 0) {
            att = (Attribute) srcAtt.copy();
        } else if (srcAtt.isNominal()) {

            Enumeration<String> valsEnum = srcAtt.enumerateValues();
            ArrayList<String> valsList = new ArrayList<String>();

            while (valsEnum.hasMoreElements()) {
                valsList.add(valsEnum.nextElement());
            }
            valsList.add(m_ReplVal);

            att = new Attribute(srcAtt.name(), valsList);
        } else { // string attributes
            att = (Attribute) srcAtt.copy();
            att.addStringValue(m_ReplVal);
        }

        newAtts.addElement(att);
        attrSrc.set(j);
    }

    outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0);
    outputFormat.setClassIndex(getInputFormat().classIndex());

    setOutputFormat(outputFormat);

    m_StringToCopy = new AttributeLocator(getInputFormat(), Attribute.STRING, MathUtils.findTrue(attrSrc));
}

From source file:en_deep.mlprocess.manipulation.SetAwareNominalToBinary.java

License:Open Source License

/**
 * Set the output format if the class is nominal.
 *//*from w w  w. ja  v a 2s.c  om*/
private void setOutputFormat() {

    FastVector newAtts;
    int newClassIndex;
    Instances outputFormat;

    // Compute new attributes

    m_producedAttVals = new HashMap[getInputFormat().numAttributes()];
    newClassIndex = getInputFormat().classIndex();
    newAtts = new FastVector();

    for (int j = 0; j < getInputFormat().numAttributes(); j++) {
        Attribute att = getInputFormat().attribute(j);
        if (!att.isNominal() || (j == getInputFormat().classIndex()) || !m_Columns.isInRange(j)) {
            newAtts.addElement(att.copy());
        } else {
            if ((att.numValues() <= 2) && (!m_TransformAll)) {
                if (m_Numeric) {
                    newAtts.addElement(new Attribute(att.name()));
                } else {
                    newAtts.addElement(att.copy());
                }
            } else {

                ArrayList<Attribute> valueAttrs = convertAttribute(att);

                if (newClassIndex >= 0 && j < getInputFormat().classIndex()) {
                    newClassIndex += valueAttrs.size() - 1;
                }
                newAtts.addAll(valueAttrs);
            }
        }
    }
    outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0);
    outputFormat.setClassIndex(newClassIndex);
    setOutputFormat(outputFormat);
}

From source file:org.opentox.jaqpot3.qsar.InstancesUtil.java

License:Open Source License

/**
 * Accepts //from  ww  w.j  a v a 2 s. c  o  m
 * @param features
 * @param data
 * @param compoundURIposition
 *      Position where the compound URI should be placed. If set to <code>-1</code>
 *      the compound URI will not be included in the created dataset.
 * @return
 *      A subset of the provided dataset (parameter data in this method) with the
 *      features specified in the provided list with that exact order. The compound
 *      URI feature (string) is placed in the position specified by the parameter
 *      compoundURIposition.
 * @throws JaqpotException
 *      A JaqpotException is thrown with error code {@link ErrorCause#FeatureNotInDataset FeatureNotInDataset}
 *      in case you provide a feature that is not found in the sumbitted Instances.
 */
public static Instances sortByFeatureAttrList(List<String> features, final Instances data,
        int compoundURIposition) throws JaqpotException {
    int position = compoundURIposition > features.size() ? features.size() : compoundURIposition;
    if (compoundURIposition != -1) {
        features.add(position, "compound_uri");
    }
    FastVector vector = new FastVector(features.size());
    for (int i = 0; i < features.size(); i++) {
        String feature = features.get(i);
        Attribute attribute = data.attribute(feature);
        if (attribute == null) {
            throw new JaqpotException("The Dataset you provided does not contain feature:" + feature);
        }
        vector.addElement(attribute.copy());
    }
    Instances result = new Instances(data.relationName(), vector, 0);
    Enumeration instances = data.enumerateInstances();
    while (instances.hasMoreElements()) {
        Instance instance = (Instance) instances.nextElement();
        double[] vals = new double[features.size()];
        for (int i = 0; i < features.size(); i++) {
            vals[i] = instance.value(data.attribute(result.attribute(i).name()));
        }
        Instance in = new Instance(1.0, vals);
        result.add(in);
    }
    return result;
}

From source file:org.opentox.jaqpot3.qsar.InstancesUtil.java

License:Open Source License

public static Instances sortForPMMLModel(List<Feature> list, List<Integer> trFieldsAttrIndex,
        final Instances data, int compoundURIposition) throws JaqpotException {
    List<String> features = new ArrayList<String>();
    for (Feature feature : list) {
        features.add(feature.getUri().toString());
    }/*from   ww w . ja  v a 2s.c om*/

    int position = compoundURIposition > features.size() ? features.size() : compoundURIposition;
    if (compoundURIposition != -1) {
        features.add(position, "compound_uri");
    }
    FastVector vector = new FastVector(features.size());
    for (int i = 0; i < features.size(); i++) {
        String feature = features.get(i);
        Attribute attribute = data.attribute(feature);
        if (attribute == null) {
            throw new JaqpotException("The Dataset you provided does not contain feature:" + feature);
        }
        vector.addElement(attribute.copy());
    }
    int attributeSize = features.size();

    if (trFieldsAttrIndex.size() > 0) {
        for (int i = 0; i < trFieldsAttrIndex.size(); i++) {
            Attribute attribute = data.attribute(trFieldsAttrIndex.get(i));
            if (attribute == null) {
                throw new JaqpotException("The Dataset you provided does not contain this pmml feature");
            }
            vector.addElement(attribute.copy());
        }
        attributeSize += trFieldsAttrIndex.size();
    }

    Instances result = new Instances(data.relationName(), vector, 0);
    Enumeration instances = data.enumerateInstances();
    while (instances.hasMoreElements()) {
        Instance instance = (Instance) instances.nextElement();
        double[] vals = new double[attributeSize];
        for (int i = 0; i < attributeSize; i++) {
            vals[i] = instance.value(data.attribute(result.attribute(i).name()));
        }
        Instance in = new Instance(1.0, vals);
        result.add(in);
    }
    return result;
}

From source file:sg.edu.nus.comp.nlp.ims.classifiers.CMultiClassesSVM.java

License:Open Source License

/**
 * get output format//from  w  w  w . j a v a 2s . co m
 *
 * @param p_Instances
 *            input format
 */
protected void getOutputFormat(Instances p_Instances) {
    FastVector newAtts, newVals;
    // Compute new attributes
    newAtts = new FastVector(p_Instances.numAttributes());
    for (int j = 0; j < p_Instances.numAttributes(); j++) {
        Attribute att = p_Instances.attribute(j);
        if (j != p_Instances.classIndex()) {
            newAtts.addElement(att.copy());
        } else {
            if (p_Instances.classAttribute().isNumeric()) {
                newAtts.addElement(new Attribute(att.name()));
            } else {
                newVals = new FastVector(2);
                newVals.addElement("negative");
                newVals.addElement("positive");
                newAtts.addElement(new Attribute(att.name(), newVals));
            }
        }
    }

    // Construct new header
    this.m_OutputFormat = new Instances(p_Instances.relationName(), newAtts, 0);
    this.m_OutputFormat.setClassIndex(p_Instances.classIndex());
    if (this.m_IndexOfID >= 0) {
        this.m_OutputFormat.deleteAttributeAt(this.m_IndexOfID);
    }
}