Example usage for weka.core Instances numAttributes

List of usage examples for weka.core Instances numAttributes

Introduction

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

Prototype


publicint numAttributes() 

Source Link

Document

Returns the number of attributes.

Usage

From source file:entities.WekaNGGFeatureVector.java

public Instance fillFeatureVector(NGGFeatureVector vSource, Instances data) {

    double[] values = new double[data.numAttributes()];

    values[0] = vSource.getContainmentSimilarityArrayAtIndex(0);
    values[1] = vSource.getSizeSimilarityArrayAtIndex(0);
    values[2] = vSource.getValueSimilarityArrayAtIndex(0);
    values[3] = vSource.getNVSArrayAtIndex(0);
    values[4] = vSource.getContainmentSimilarityArrayAtIndex(1);
    values[5] = vSource.getSizeSimilarityArrayAtIndex(1);
    values[6] = vSource.getValueSimilarityArrayAtIndex(1);
    values[7] = vSource.getNVSArrayAtIndex(1);
    values[8] = data.attribute(8).indexOfValue(vSource.getLabel());

    Instance inst = new DenseInstance(1.0, values);

    return inst;/*from w  w w . j  a va2s. c o  m*/
}

From source file:entities.WekaNGGFeatureVector.java

public Instances fillInstanceSet(ArrayList<NGGFeatureVector> vList, ArrayList<NGGFeatureVector> vList2,
        String datasetType) throws IOException {
    ArrayList<Attribute> attributes = initializeWekaFeatureVector();
    Instances isSet = new Instances(vList.get(0).getLabel(), attributes, vList.size());

    isSet.setClassIndex(isSet.numAttributes() - 1);

    for (NGGFeatureVector NGGv : vList) {

        Instance i = fillFeatureVector(NGGv, isSet);

        isSet.add(i);//  w w  w  .ja va2 s .  c  o  m
    }

    for (NGGFeatureVector NGGv : vList2) {

        Instance i = fillFeatureVector(NGGv, isSet);

        isSet.add(i);
    }

    ArffSaver saver = new ArffSaver();
    saver.setInstances(isSet);
    saver.setFile(new File("./data/" + datasetType + ".arff"));
    saver.writeBatch();

    return isSet;
}

From source file:entity.DatasetFileManager.java

License:Open Source License

public Instances generateWekaInstancesForDataset(String datasetFilePath) {

    // datasource creation from file
    DataSource datasource = null;
    try {//from w  ww.  j a va  2s .  c o  m
        datasource = new DataSource(datasetFilePath);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Instances instances = null;

    // instances creation from datasource
    try {
        instances = datasource.getDataSet();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // last attribute is for classification
    instances.setClassIndex(instances.numAttributes() - 1);

    if (verbose)
        System.out.println("\nDataset:\n");
    if (verbose)
        System.out.println(instances);

    return instances;
}

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

License:Open Source License

/**
 * Sets the format of the input instances.
 *
 * @param instanceInfo an Instances object containing the input
 * instance structure (any instances contained in the object are
 * ignored - only the structure is required).
 * @return true if the outputFormat may be collected immediately
 * @throws Exception if the input format can't be set
 * successfully/*from  ww w .  j a v a 2 s .com*/
 */
public boolean setInputFormat(Instances instanceInfo) throws Exception {

    super.setInputFormat(instanceInfo);

    m_Columns.setUpper(instanceInfo.numAttributes() - 1);
    if (this.m_OperClassName == null
            || (this.m_OperClass = FeatureModifier.createHandler(m_OperClassName)) == null) {
        throw new Exception("The operating class must be set and a name of an existing filter class.");
    }
    setOutputFormat();
    return true;
}

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

License:Open Source License

/**
 * Sets the format of the input instances.
 *
 * @param instanceInfo an Instances object containing the input
 * instance structure (any instances contained in the object are
 * ignored - only the structure is required).
 * @return true if the outputFormat may be collected immediately
 * @throws Exception if the input format can't be set
 * successfully//from  w  ww  .j  a v a 2 s . com
 */
public boolean setInputFormat(Instances instanceInfo) throws Exception {

    super.setInputFormat(instanceInfo);

    m_Columns.setUpper(instanceInfo.numAttributes() - 1);
    setOutputFormat();
    return true;
}

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

License:Open Source License

/**
 * Sets the format of the input instances.
 *
 * @param instanceInfo an Instances object containing the input
 * instance structure (any instances contained in the object are
 * ignored - only the structure is required).
 * @return true if the outputFormat may be collected immediately
 * @throws Exception if the input format can't be set
 * successfully//from   w  ww. j  a va 2 s .  c o m
 */
public boolean setInputFormat(Instances instanceInfo) throws Exception {

    super.setInputFormat(instanceInfo);

    m_Columns.setUpper(instanceInfo.numAttributes() - 1);

    setOutputFormat();
    return true;
}

From source file:es.bsc.autonomic.powermodeller.tools.classifiers.WekaWrapper.java

License:Apache License

public static DataSet processDataSet(DataSet ds, VariableParser parser) {

    String independent = ds.getIndependent();

    if (independent == null)
        throw new WekaWrapperException("Independent variable is not set in dataset.");

    HashMap<String, String> expression_list = parser.getNewMetrics();
    Instances data = convertDataSetToInstances(ds);

    try {//from   w  w  w .j  av a  2 s. co m
        // Apply filters for all the new variables
        for (Map.Entry<String, String> entry : expression_list.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            logger.debug("Generating new variable " + key + " as " + value);

            AddExpression add_filter = new AddExpression();
            add_filter.setName(key);
            add_filter.setExpression(value);
            add_filter.setInputFormat(data);

            data = useFilter(data, add_filter);

        }

    } catch (Exception e) {
        logger.error("Error while processing new variables", e);
        throw new WekaWrapperException("Error while processing new variables");
    }

    // Iterate over all the columns and keep only the ones contained in variables list
    List<String> variables = parser.getColumns();

    // Append independent variable to the list of variables to keep
    variables.add(independent);

    // Remove unneeded attributes
    try {

        // it's important to iterate from last to first, because when we remove
        // an instance, the rest shifts by one position.
        for (int i = data.numAttributes() - 1; i >= 0; i--) {
            String n = data.attribute(i).name();
            if (!variables.contains(data.attribute(i).name())) {
                logger.trace("Deleting unnecessary attribute " + data.attribute(i).name());
                data.deleteAttributeAt(i);
            }
        }

        data.toString();
    } catch (Exception e) {
        logger.error("Error while removing unneeded variables", e);
        throw new WekaWrapperException("Error while removing unneeded variables");
    }

    // Convert Instances in csv and return the new DataSet
    String new_path = CoreConfiguration.getNewCSVFileName();
    try {
        CSVSaver saver = new CSVSaver();
        saver.setInstances(data);
        saver.setFile(new File(new_path));
        saver.writeBatch();
    } catch (Exception e) {
        logger.error("Error while removing unneeded variables", e);
        throw new WekaWrapperException("Error while removing unneeded variables");
    }

    DataSet ret = new DataSet(new_path);
    ret.setIndependent(independent);
    return ret;
}

From source file:es.jarias.FMC.ClassCompoundTransformation.java

License:Open Source License

/**
 * /*ww  w.  jav  a2s .c  om*/
 * @param mlData
 * @return the transformed instances
 * @throws Exception
 */
public Instances transformInstances(MultiLabelInstances mlData) throws Exception {
    data = mlData.getDataSet();
    numLabels = mlData.getNumLabels();
    labelIndices = mlData.getLabelIndices();

    Instances newData = null;

    // This must be different in order to combine ALL class states, not only existing ones.
    // gather distinct label combinations
    // ASSUME CLASSES ARE BINARY

    ArrayList<LabelSet> labelSets = new ArrayList<LabelSet>();

    double[] dblLabels = new double[numLabels];
    double nCombinations = Math.pow(2, numLabels);

    for (int i = 0; i < nCombinations; i++) {
        for (int l = 0; l < numLabels; l++) {
            int digit = (int) Math.pow(2, numLabels - 1 - l);
            dblLabels[l] = (digit & i) / digit;
        }

        LabelSet labelSet = new LabelSet(dblLabels);
        labelSets.add(labelSet);
    }

    //        for (int i = 0; i < numInstances; i++) {
    //            // construct labelset
    //            double[] dblLabels = new double[numLabels];
    //            for (int j = 0; j < numLabels; j++) {
    //                int index = labelIndices[j];
    //                dblLabels[j] = Double.parseDouble(data.attribute(index).value((int) data.instance(i).value(index)));
    //            }
    //            LabelSet labelSet = new LabelSet(dblLabels);
    //
    //            // add labelset if not already present
    //            labelSets.add(labelSet);
    //        }

    // create class attribute
    ArrayList<String> classValues = new ArrayList<String>(labelSets.size());
    for (LabelSet subset : labelSets) {
        classValues.add(subset.toBitString());
    }
    newClass = new Attribute("class", classValues);

    //        for (String s : classValues)
    //        {
    //           System.out.print(s+", ");
    //           
    //        }
    //        System.out.println();

    // remove all labels
    newData = RemoveAllLabels.transformInstances(data, labelIndices);

    // add new class attribute
    newData.insertAttributeAt(newClass, newData.numAttributes());
    newData.setClassIndex(newData.numAttributes() - 1);

    // add class values
    for (int i = 0; i < newData.numInstances(); i++) {
        //System.out.println(newData.instance(i).toString());
        String strClass = "";
        for (int j = 0; j < numLabels; j++) {
            int index = labelIndices[j];
            strClass = strClass + data.attribute(index).value((int) data.instance(i).value(index));
        }
        //System.out.println(strClass);
        newData.instance(i).setClassValue(strClass);
    }
    transformedFormat = new Instances(newData, 0);
    return newData;
}

From source file:es.ubu.XRayDetector.datos.GestorArff.java

License:Open Source License

/**
 * Reads an ARFF file./*from w w  w .j  a  v  a 2  s . c o m*/
 * 
 * @param url The PATH of the ARFF file.
 * @return The instances include in the ARFF file.
 */
public Instances leerArff(String url) {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(url));
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    ArffReader arff = null;
    try {
        arff = new ArffReader(reader);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Instances data = arff.getData();
    data.setClassIndex(data.numAttributes() - 1);

    return data;
}

From source file:es.ubu.XRayDetector.modelo.Fachada.java

License:Open Source License

/**
 * Creates a model training a classifier using bagging.
 * //w  w  w.  j a v a  2  s.com
 * @param data Contains all the instances of the ARFF file
 * @param sizeWindow The size of the window
 */
public void createModel(Instances data, String sizeWindow) {

    // se crea, opciones, setiputformat
    Classifier cls = null;
    //String separator = System.getProperty("file.separator");
    String path = prop.getPathModel();

    int opcionClasificacion = prop.getTipoClasificacion();

    switch (opcionClasificacion) {
    case 0:
        //CLASIFICADOR CLASES NOMINALES (TRUE,FALSE)
        Classifier base;
        base = new REPTree();
        cls = new Bagging();
        ((Bagging) cls).setNumIterations(25);
        ((Bagging) cls).setBagSizePercent(100);
        ((Bagging) cls).setNumExecutionSlots(Runtime.getRuntime().availableProcessors());
        ((Bagging) cls).setClassifier(base);
        break;
    case 1:
        //REGRESIN LINEAL (CLASES NUMRICAS, 1,0)
        cls = new REPTree();
        break;
    }

    ObjectOutputStream oos = null;

    try {
        data.setClassIndex(data.numAttributes() - 1);
        cls.buildClassifier(data);

        /*if (arffName.contains("mejores"))
           oos = new ObjectOutputStream(new FileOutputStream((path
          + separator + "Modelos" + separator + "Bagging_"
          + "mejores_" + sizeWindow + ".model")));
                
        if (arffName.contains("todas"))*/
        oos = new ObjectOutputStream(new FileOutputStream((path + "todas_" + sizeWindow + ".model")));

        oos.writeObject(cls);
        oos.flush();
        oos.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}