Example usage for weka.core Instances relationName

List of usage examples for weka.core Instances relationName

Introduction

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

Prototype


publicString relationName() 

Source Link

Document

Returns the relation's name.

Usage

From source file:core.DatabaseSaverEx.java

License:Open Source License

/** 
 * Writes the structure (header information) to a database by creating a new table.
 * /* w  w w . j  a  va  2  s.co m*/
 * @throws Exception if something goes wrong
 */
private void writeStructure() throws Exception {

    StringBuffer query = new StringBuffer();
    Instances structure = getInstances();
    query.append("CREATE TABLE ");
    if (m_tabName || m_tableName.equals(""))
        m_tableName = m_DataBaseConnection.maskKeyword(structure.relationName());
    if (m_DataBaseConnection.getUpperCase()) {
        m_tableName = m_tableName.toUpperCase();
        m_createInt = m_createInt.toUpperCase();
        m_createDouble = m_createDouble.toUpperCase();
        m_createText = m_createText.toUpperCase();
        m_createDate = m_createDate.toUpperCase();
    }
    m_tableName = m_tableName.replaceAll("[^\\w]", "_");
    m_tableName = m_DataBaseConnection.maskKeyword(m_tableName);
    query.append(m_tableName);
    if (structure.numAttributes() == 0)
        throw new Exception("Instances have no attribute.");
    query.append(" ( ");
    if (m_id) {
        if (m_DataBaseConnection.getUpperCase())
            m_idColumn = m_idColumn.toUpperCase();
        query.append(m_DataBaseConnection.maskKeyword(m_idColumn));
        query.append(" ");
        query.append(m_createInt);
        query.append(" PRIMARY KEY,");
    }
    for (int i = 0; i < structure.numAttributes(); i++) {
        Attribute att = structure.attribute(i);
        String attName = att.name();
        attName = attName.replaceAll("[^\\w]", "_");
        attName = m_DataBaseConnection.maskKeyword(attName);
        if (m_DataBaseConnection.getUpperCase())
            query.append(attName.toUpperCase());
        else
            query.append(attName);
        if (att.isDate())
            query.append(" " + m_createDate);
        else {
            if (att.isNumeric())
                query.append(" " + m_createDouble);
            else
                query.append(" " + m_createText);
        }
        if (i != structure.numAttributes() - 1)
            query.append(", ");
    }
    query.append(" )");
    //System.out.println(query.toString());
    m_DataBaseConnection.update(query.toString());
    m_DataBaseConnection.close();
    if (!m_DataBaseConnection.tableExists(m_tableName)) {
        throw new IOException("Table cannot be built.");
    }
}

From source file:cs.man.ac.uk.classifiers.GetAUC.java

License:Open Source License

/**
 * Computes the AUC for the supplied learner.
 * @param learner the learning algorithm to use.
 * @return the AUC as a double value./*  www  .j  av a 2s.c o m*/
 */
@SuppressWarnings("unused")
private static double validate(Classifier learner) {
    try {

        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(learner, data, 2, new Random(1));

        // generate curve
        ThresholdCurve tc = new ThresholdCurve();
        int classIndex = 0;
        Instances result = tc.getCurve(eval.predictions(), classIndex);

        // plot curve
        vmc = new ThresholdVisualizePanel();
        double AUC = ThresholdCurve.getROCArea(result);
        vmc.setROCString(
                "(Area under ROC = " + Utils.doubleToString(ThresholdCurve.getROCArea(result), 9) + ")");
        vmc.setName(result.relationName());

        PlotData2D tempd = new PlotData2D(result);
        tempd.setPlotName(result.relationName());
        tempd.addInstanceNumberAttribute();

        // 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
        vmc.addPlot(tempd);

        return AUC;
    } catch (Exception e) {
        System.out.println("Exception validating data!");
        return 0;
    }
}

From source file:cyber009.udal.mains.WekaUDAL.java

public void showPlot(Instances dataSet) {
    PlotData2D p2D = new PlotData2D(dataSet);
    p2D.setPlotName(dataSet.relationName());
    VisualizePanel vp = new VisualizePanel();
    vp.setName(dataSet.relationName());/*from  w w w .j  a  v  a  2 s .  c  o m*/
    try {
        vp.addPlot(p2D);
        JFrame frame = new JFrame(dataSet.relationName());
        frame.setSize(600, 600);
        frame.setVisible(true);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(vp, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    } catch (Exception ex) {
        Logger.getLogger(WekaUDAL.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:de.ugoe.cs.cpdp.dataprocessing.MORPH.java

License:Apache License

/**
 * <p>/*  w w w.  j  av a2s  . com*/
 * Applies MORPH to a single instance
 * </p>
 *
 * @param instance
 *            instance that is morphed
 * @param data
 *            data based on which the instance is morphed
 */
public void morphInstance(Instance instance, Instances data) {
    Instance nearestUnlikeNeighbor = getNearestUnlikeNeighbor(instance, data);
    if (nearestUnlikeNeighbor == null) {
        throw new RuntimeException(
                "could not find nearest unlike neighbor within the data: " + data.relationName());
    }
    for (int j = 0; j < data.numAttributes(); j++) {
        if (data.attribute(j) != data.classAttribute() && data.attribute(j).isNumeric()) {
            double randVal = rand.nextDouble() * (beta - alpha) + alpha;
            instance.setValue(j,
                    instance.value(j) + randVal * (instance.value(j) - nearestUnlikeNeighbor.value(j)));
        }
    }
}

From source file:DiversifyQuery.DivTopK.java

/**
 * Sets the format of the filtered instances that are output. I.e. will
 * include k attributes each shapelet distance and a class value
 *
 * @param inputFormat the format of the input data
 * @return a new Instances object in the desired output format
 * @throws Exception if all required parameters of the filter are not
 * initialised correctly//from   w  w  w  . java2 s .c  o m
 */
protected Instances determineOutputFormat(Instances inputFormat, ArrayList<LegacyShapelet> shapelets)
        throws Exception {

    //Set up instances size and format.
    //int length = this.numShapelets;
    int length = shapelets.size();
    FastVector atts = new FastVector();
    String name;
    for (int i = 0; i < length; i++) {
        name = "Shapelet_" + i;
        atts.addElement(new Attribute(name));
    }

    if (inputFormat.classIndex() >= 0) { //Classification set, set class
        //Get the class values as a fast vector
        Attribute target = inputFormat.attribute(inputFormat.classIndex());

        FastVector vals = new FastVector(target.numValues());
        for (int i = 0; i < target.numValues(); i++) {
            vals.addElement(target.value(i));
        }
        atts.addElement(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(), vals));
    }
    Instances result = new Instances("Shapelets" + inputFormat.relationName(), atts,
            inputFormat.numInstances());
    if (inputFormat.classIndex() >= 0) {
        result.setClassIndex(result.numAttributes() - 1);
    }
    return result;
}

From source file:DiversifyTopKShaepelet.DiversifyTopKShaepelet.java

/**
 * Sets the format of the filtered instances that are output. I.e. will
 * include k attributes each shapelet distance and a class value
 *
 * @param inputFormat the format of the input data
 * @return a new Instances object in the desired output format
 * @throws Exception if all required parameters of the filter are not
 * initialised correctly/* w  w w . jav  a  2 s .  c  o  m*/
 */
@Override
protected Instances determineOutputFormat(Instances inputFormat) throws Exception {

    if (this.numShapelets < 1) {
        throw new Exception(
                "ShapeletFilter not initialised correctly - please specify a value of k that is greater than or equal to 1");
    }

    //Set up instances size and format.
    //int length = this.numShapelets;
    int length = this.shapelets.size();
    FastVector atts = new FastVector();
    String name;
    for (int i = 0; i < length; i++) {
        name = "Shapelet_" + i;
        atts.addElement(new Attribute(name));
    }

    if (inputFormat.classIndex() >= 0) { //Classification set, set class
        //Get the class values as a fast vector
        Attribute target = inputFormat.attribute(inputFormat.classIndex());

        FastVector vals = new FastVector(target.numValues());
        for (int i = 0; i < target.numValues(); i++) {
            vals.addElement(target.value(i));
        }
        atts.addElement(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(), vals));
    }
    Instances result = new Instances("Shapelets" + inputFormat.relationName(), atts,
            inputFormat.numInstances());
    if (inputFormat.classIndex() >= 0) {
        result.setClassIndex(result.numAttributes() - 1);
    }
    return result;
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.EvaluateSimilarityFunction.java

License:Open Source License

public static Instances transformInstances(final Instances src, final CoordinateTransform transform) {
    final ArrayList<Attribute> out_attributes = new ArrayList<Attribute>();
    for (int i = 0; i < transform.outDimension(); ++i) {
        out_attributes.add(new Attribute("x" + i));
    }//  www. j  av a  2s.  c o  m
    out_attributes.add((Attribute) src.classAttribute().copy());
    final Instances out = new Instances(src.relationName() + "_" + transform.name(), out_attributes, 0);
    for (int i = 0; i < src.size(); ++i) {
        final Instance inst = src.get(i);
        final RealVector flat = new ArrayRealVector(WekaUtil.unlabeledFeatures(inst));
        final RealVector transformed_vector = transform.encode(flat).x;
        final double[] transformed = new double[transformed_vector.getDimension() + 1];
        for (int j = 0; j < transformed_vector.getDimension(); ++j) {
            transformed[j] = transformed_vector.getEntry(j);
        }
        transformed[transformed.length - 1] = inst.classValue();
        final Instance transformed_instance = new DenseInstance(inst.weight(), transformed);
        out.add(transformed_instance);
        transformed_instance.setDataset(out);
    }
    out.setClassIndex(out.numAttributes() - 1);
    return out;
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.WekaUtil.java

License:Open Source License

/**
 * Save an Instances object, using the relation name for the file name.
 * @param root//from  w  ww  .  j av a 2s .  com
 * @param x
 */
public static void writeDataset(final File root, final Instances x) {
    writeDataset(root, x.relationName(), x);
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.WekaUtil.java

License:Open Source License

public static Instances powerSet(final Instances D, final int n) {
    final Attribute class_attr = D.classAttribute();

    final ImmutableSet.Builder<Integer> b = new ImmutableSet.Builder<Integer>();
    final int Nattr = class_attr != null ? D.numAttributes() - 1 : D.numAttributes();
    for (final int i : Fn.range(1, Nattr)) {
        b.add(i);//from  w ww  .ja v  a  2 s.c o  m
    }
    final Set<Set<Integer>> index = Sets.powerSet(b.build());

    final ArrayList<Attribute> attributes = new ArrayList<Attribute>();
    for (final Set<Integer> subset : index) {
        if (subset.isEmpty() || subset.size() > n) {
            continue;
        }

        final StringBuilder attr_name = new StringBuilder();
        int count = 0;
        for (final Integer i : subset) {
            if (count++ > 0) {
                attr_name.append("_x_");
            }
            attr_name.append(D.attribute(i).name());
        }

        attributes.add(new Attribute(attr_name.toString()));
    }
    if (class_attr != null) {
        assert (class_attr.isNominal());
        attributes.add(WekaUtil.createNominalAttribute(class_attr.name(), class_attr.numValues()));
    }

    final String Pname = "P" + n + "_" + D.relationName();
    final Instances P = new Instances(Pname, attributes, 0);
    if (class_attr != null) {
        P.setClassIndex(attributes.size() - 1);
    }

    for (final Instance inst : D) {
        final double[] xp = new double[attributes.size()];
        int idx = 0;
        for (final Set<Integer> subset : index) {
            if (subset.isEmpty() || subset.size() > n) {
                continue;
            }

            double p = 1.0;
            for (final Integer i : subset) {
                p *= inst.value(i);
            }
            xp[idx++] = p;
        }
        if (class_attr != null) {
            xp[idx++] = inst.classValue();
        }

        WekaUtil.addInstance(P, new DenseInstance(inst.weight(), xp));
    }

    return P;
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.WekaUtil.java

License:Open Source License

public static Instances allPairwiseProducts(final Instances single, final boolean reflexive,
        final boolean symmetric) {
    final int c = single.classIndex();
    System.out.println("Class attribute = " + c);

    final ArrayList<Attribute> pair_attributes = new ArrayList<Attribute>();
    for (int i = 0; i < single.numAttributes(); ++i) {
        if (i == c) {
            continue;
        }//  w ww .  j a  v a 2  s.com
        final Attribute ai = single.attribute(i);
        final int j0 = (symmetric ? 0 : i);
        for (int j = j0; j < single.numAttributes(); ++j) {
            if (j == c) {
                continue;
            }
            if (!reflexive && i == j) {
                continue;
            }

            final Attribute aj = single.attribute(j);

            final String name = ai.name() + "_x_" + aj.name();
            pair_attributes.add(new Attribute(name));
        }
    }

    String pair_name = single.relationName();
    pair_name += "_x";
    if (reflexive) {
        pair_name += "r";
    }
    if (symmetric) {
        pair_name += "s";
    }
    pair_name += "_";
    pair_name += single.relationName();
    final Instances result = new Instances(pair_name, pair_attributes, 0);

    for (final Instance inst : single) {
        final double[] xp = new double[pair_attributes.size()];
        int idx = 0;
        for (int i = 0; i < single.numAttributes(); ++i) {
            if (i == c) {
                continue;
            }
            final double xi = inst.value(i);
            final int j0 = (symmetric ? 0 : i);
            for (int j = j0; j < single.numAttributes(); ++j) {
                if (j == c) {
                    continue;
                }
                if (!reflexive && i == j) {
                    continue;
                }
                final double xj = inst.value(j);
                xp[idx++] = xi * xj;
            }
        }
        WekaUtil.addInstance(result, new DenseInstance(inst.weight(), xp));
    }

    return result;
}