Example usage for weka.core Instance numAttributes

List of usage examples for weka.core Instance numAttributes

Introduction

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

Prototype

public int numAttributes();

Source Link

Document

Returns the number of attributes.

Usage

From source file:cotraining.copy.Evaluation_D.java

License:Open Source License

/**
 * Builds a string listing the attribute values in a specified range of indices,
 * separated by commas and enclosed in brackets.
 *
 * @param instance the instance to print the values from
 * @param attRange the range of the attributes to list
 * @return a string listing values of the attributes in the range
 *//*from   w  w w .  j  av a 2s  .  co m*/
protected static String attributeValuesString(Instance instance, Range attRange) {
    StringBuffer text = new StringBuffer();
    if (attRange != null) {
        boolean firstOutput = true;
        attRange.setUpper(instance.numAttributes() - 1);
        for (int i = 0; i < instance.numAttributes(); i++)
            if (attRange.isInRange(i) && i != instance.classIndex()) {
                if (firstOutput)
                    text.append("(");
                else
                    text.append(",");
                text.append(instance.toString(i));
                firstOutput = false;
            }
        if (!firstOutput)
            text.append(")");
    }
    return text.toString();
}

From source file:couchdb.CouchDBService.java

/**
 * Metoda do zapisywania danych do bazy.
 *
 * @param fileName nazwa pliku z danymi, akceptowalny format to pliki ARFF
 * @param dataBaseName nazwa bazy danych
 *//*www . j av  a2 s  . c  o  m*/
public void importData(String fileName, String dataBaseName) {
    WekaService ws = new WekaService(fileName);
    ArrayList<String> listOfAttributesNames = ws.getAttributesName();
    ArrayList<Instance> listOfInstances = ws.getInstances();
    Database db = session.getDatabase(dataBaseName);
    for (int i = 0; i < listOfInstances.size(); i++) {
        Instance instance = listOfInstances.get(i);
        Document doc = new Document();
        doc.setId(String.valueOf(i));
        for (int j = 0; j < instance.numAttributes(); j++) {
            doc.put(listOfAttributesNames.get(j), instance.toString(j));
        }
        db.saveDocument(doc);
    }

}

From source file:couchdb.PrepareData.java

/**
 * Metoda do zmiany danych przechowywanych w dokumentach JSON na dane
 * przechowywane w obiekcie typu Instances.
 *
 * @return Zbir danych typu Instances./* w  w w.ja va  2s.co m*/
 */
public Instances getDataForWeka() {
    CouchDBService cdbs = new CouchDBService();
    ArrayList<String> listOfValues = cdbs.getValues(simpleDocumentList);
    ParseJSON p = new ParseJSON();
    ArrayList<String> listOfSimpleAttributes = p.getAttributes(simpleDocumentList.get(0));
    ArrayList<String> listOfComplexAttributes = new ArrayList<>();

    FastVector listOfAttributes = getAtributes(simpleDocumentList, listOfValues);
    Instances instances = new Instances(nameData, listOfAttributes, 0);

    for (int j = 0; j < simpleDocumentList.size(); j++) {
        Instance instance = new Instance(listOfAttributes.size());
        instances.add(instance);
    }

    int k = 0;
    for (int i = 0; i < instances.numInstances(); i++) {
        Instance ins = instances.instance(i);
        for (int j = 0; j < ins.numAttributes(); j++) {
            String s = listOfValues.get(k);
            if (s.equals("nn")) {
                return null;
            }
            if (s.equals("")) {
                k++;
                j--;
                continue;
            }
            if (ins.attribute(j).type() == 0) {
                double d = 0;
                try {
                    d = Double.parseDouble(s);
                } catch (NumberFormatException ex) {
                    d = 0;
                }
                ins.setValue(j, d);
            } else {

                ins.setValue(j, s);
            }
            k++;
        }
    }

    return instances;
}

From source file:cyber009.udal.functions.LinearFunction.java

public void syntacticLabelFunction(Instance set) {
    double sum = 0.0D;
    for (int n = 0; n < set.numAttributes() - 1; n++) {
        sum += set.value(n) * coefficients[n];
    }/*from w w w  .  ja  v a2 s  . co m*/
    if (sum < 0.0D) {
        set.setClassValue("1");
    } else {
        set.setClassValue("0");
    }
}

From source file:cyber009.udal.functions.StatisticalAnalysis.java

public static boolean instanceCMPWithoutClass(Instance a, Instance b) {
    if (a.numAttributes() == b.numAttributes()) {
        for (int i = 0; i < a.numAttributes(); i++) {
            if (a.value(i) != b.value(i)) {
                return false;
            }//from w w w.j  a va 2 s  .c o  m
        }
    } else {
        return false;
    }
    return true;
}

From source file:data.generation.target.utils.PrincipalComponents.java

License:Open Source License

/**
 * Convert a pc transformed instance back to the original space
 * /*from   w w w.  ja va 2  s  .c o m*/
 * @param inst        the instance to convert
 * @return            the processed instance
 * @throws Exception  if something goes wrong
 */
private Instance convertInstanceToOriginal(Instance inst) throws Exception {
    double[] newVals = null;

    if (m_hasClass) {
        newVals = new double[m_numAttribs + 1];
    } else {
        newVals = new double[m_numAttribs];
    }

    if (m_hasClass) {
        // class is always appended as the last attribute
        newVals[m_numAttribs] = inst.value(inst.numAttributes() - 1);
    }

    for (int i = 0; i < m_eTranspose[0].length; i++) {
        double tempval = 0.0;
        for (int j = 1; j < m_eTranspose.length; j++) {
            tempval += (m_eTranspose[j][i] * inst.value(j - 1));
        }
        newVals[i] = tempval;
        if (!m_center) {
            newVals[i] *= m_stdDevs[i];
        }
        newVals[i] += m_means[i];
    }

    if (inst instanceof SparseInstance) {
        return new SparseInstance(inst.weight(), newVals);
    } else {
        return new Instance(inst.weight(), newVals);
    }
}

From source file:dataMining.KMeans.java

/**
 * Metoda do sprawdzania czy instancje podane jako parametr s takie same.
 *
 * @param insA piewrsza instanca//ww  w  .  ja v a  2s. c o m
 * @param insB druga instancja
 * @return True, jeli s takie same, false w przeciwnym wypadku.
 */
private boolean equals(Instance insA, Instance insB) {
    if (insA.numAttributes() != insB.numAttributes()) {
        throw new NullPointerException("Rna liczba atrybutw");
    } else {
        int countTheSame = 0;
        for (int i = 0; i < insA.numAttributes(); i++) {
            double a = 0;
            double b = 0;
            try {
                a = Double.parseDouble(insA.toString(i));
            } catch (NumberFormatException ex) {
                a = 0;
            }
            try {
                b = Double.parseDouble(insB.toString(i));
            } catch (NumberFormatException ex) {
                b = 0;
            }
            if (Math.abs(a - b) == 0) {
                countTheSame++;
            }

        }
        int countAttr = insB.numAttributes();

        if (countTheSame == countAttr) {
            return true;
        } else {
            return false;
        }

    }

}

From source file:dataMining.KMeans.java

/**
 * Metoda do wyznaczania nowych rodkw grup.
 *
 * @return lista zawierajca nowe rodki./*ww  w. ja  va 2  s.c om*/
 */
private ArrayList<Instance> makeNewMeans() {
    ArrayList<Instance> listOfMeans = new ArrayList<>();
    for (Instance i : groups.keySet()) {
        ArrayList<Instance> list = groups.get(i);
        double[] tab = new double[i.numAttributes()];
        for (Instance in : list) {
            for (int j = 0; j < tab.length; j++) {
                double d = 0;
                try {
                    d = Double.parseDouble(in.toString(j));
                } catch (NumberFormatException ex) {
                    d = 0;
                }
                tab[j] = tab[j] + d;
            }
        }
        for (int j = 0; j < tab.length; j++) {
            tab[j] = tab[j] / list.size();
        }
        Instance ins = new Instance(tab.length);
        for (int j = 0; j < tab.length; j++) {
            ins.setValue(j, tab[j]);
        }
        listOfMeans.add(ins);
    }
    return listOfMeans;
}

From source file:dataMining.KMeans.java

/**
 * Metoda do wyznaczania odlegoci pomiedzy obiektami podanymi jako
 * parametr. Odlego liczona metryk Euklidesow.
 *
 * @param a pierwszy obiekt//from  w w w . ja  v a 2  s .  c o m
 * @param b drugi obiekt
 * @return Odlego pomidzy obiektami.
 */
private double euclid(Instance a, Instance b) {
    float sum = 0;
    float attrA = 0;
    float attrB = 0;
    for (int i = 0; i < a.numAttributes(); i++) {
        try {
            attrA = Float.parseFloat(a.toString(i));
        } catch (NumberFormatException ex) {
            attrA = 0;
        }
        try {
            attrB = Float.parseFloat(b.toString(i));
        } catch (NumberFormatException ex) {
            attrB = 0;
        }

        float d = attrA - attrB;
        sum = sum + d * d;
    }
    return Math.sqrt(sum);
}

From source file:dataMining.kNN.java

/**
 * Metoda do liczenia odlegoci metryk Manhattan.
 *
 * @param a pierwszy obiekt/* www  .  j  a  v  a  2  s  .  c o  m*/
 * @param b drugi obiekt
 * @return odlego
 */
private int manhattan(Instance a, Instance b) {
    int sum = 0;
    for (int i = 0; i < a.numAttributes(); i++) {
        if (i == numAtt) {
            continue;
        } else {
            int tmp = 0;
            try {
                tmp = Math.abs(Integer.parseInt(a.toString(i)) - Integer.parseInt(b.toString(i)));
            } catch (NumberFormatException ex) {
                tmp = 0;
            }
            sum = sum + tmp;
        }
    }
    return sum;
}