List of usage examples for weka.core Instances attribute
publicAttribute attribute(String name)
From source file:distributions.ClassdistributionNominal.java
/** Konstruktor // w w w. ja v a 2 s. c o m * @param inst * @param classID * @param kID */ public ClassdistributionNominal(Instances inst, int classID, int kID) { this.inst = new Instances(inst); this.classID = classID; this.kID = kID; conditionalprobs = new double[inst.attribute(kID).numValues()]; attindexen = new int[inst.attribute(kID).numValues()]; for (int i = 0; i < attindexen.length; i++) { attindexen[i] = i; } }
From source file:distributions.NominalDistribution.java
public NominalDistribution(Instances inst, int kID) { this.inst = new Instances(inst); this.kID = kID; probs = new double[inst.attribute(kID).numValues()]; attwerten1 = new int[inst.attribute(kID).numValues()]; for (int i = 0; i < attwerten1.length; i++) { attwerten1[i] = i;/*from ww w . j a v a2 s . co m*/ } }
From source file:distributions.NominalNominalDistribution.java
public NominalNominalDistribution(Instances inst, int kID, int pID) { this.inst = new Instances(inst); this.kID = kID; this.pID = pID; attwerten1 = new int[inst.attribute(kID).numValues()]; attwerten2 = new int[inst.attribute(pID).numValues()]; for (int i = 0; i < attwerten1.length; i++) { attwerten1[i] = i;// w ww . j a va2s .co m } for (int i = 0; i < attwerten2.length; i++) { attwerten2[i] = i; } probs = new double[inst.attribute(kID).numValues()][inst.attribute(pID).numValues()]; zaelt = new double[this.inst.attribute(kID).numValues()][inst.attribute(pID).numValues()]; }
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 . jav a2 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//from w w w . j a v 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.cmu.lti.oaqa.baseqa.providers.ml.classifiers.WekaProvider.java
License:Apache License
private static Instance newInstance(Map<String, Double> features, String label, double weight, Instances dataset) { double[] values = new double[dataset.numAttributes()]; for (Map.Entry<String, Double> entry : features.entrySet()) { Attribute attribute = dataset.attribute(entry.getKey()); if (attribute == null) continue; values[attribute.index()] = entry.getValue(); }//from www . j a va 2s .c om SparseInstance instance = new SparseInstance(weight, values); instance.setDataset(dataset); if (label != null) instance.setClassValue(label); return instance; }
From source file:edu.columbia.cs.ltrie.sampling.queries.generation.ChiSquaredWithYatesCorrectionAttributeEval.java
License:Open Source License
/** * Initializes a chi-squared attribute evaluator. * Discretizes all attributes that are numeric. * * @param data set of instances serving as training data * @throws Exception if the evaluator has not been * generated successfully//from w w w . j av a 2 s . c om */ public void buildEvaluator(Instances data) throws Exception { // can evaluator handle data? getCapabilities().testWithFail(data); int classIndex = data.classIndex(); int numInstances = data.numInstances(); if (!m_Binarize) { Discretize disTransform = new Discretize(); disTransform.setUseBetterEncoding(true); disTransform.setInputFormat(data); data = Filter.useFilter(data, disTransform); } else { NumericToBinary binTransform = new NumericToBinary(); binTransform.setInputFormat(data); data = Filter.useFilter(data, binTransform); } int numClasses = data.attribute(classIndex).numValues(); // Reserve space and initialize counters double[][][] counts = new double[data.numAttributes()][][]; for (int k = 0; k < data.numAttributes(); k++) { if (k != classIndex) { int numValues = data.attribute(k).numValues(); counts[k] = new double[numValues + 1][numClasses + 1]; } } // Initialize counters double[] temp = new double[numClasses + 1]; for (int k = 0; k < numInstances; k++) { Instance inst = data.instance(k); if (inst.classIsMissing()) { temp[numClasses] += inst.weight(); } else { temp[(int) inst.classValue()] += inst.weight(); } } for (int k = 0; k < counts.length; k++) { if (k != classIndex) { for (int i = 0; i < temp.length; i++) { counts[k][0][i] = temp[i]; } } } // Get counts for (int k = 0; k < numInstances; k++) { Instance inst = data.instance(k); for (int i = 0; i < inst.numValues(); i++) { if (inst.index(i) != classIndex) { if (inst.isMissingSparse(i) || inst.classIsMissing()) { if (!inst.isMissingSparse(i)) { counts[inst.index(i)][(int) inst.valueSparse(i)][numClasses] += inst.weight(); counts[inst.index(i)][0][numClasses] -= inst.weight(); } else if (!inst.classIsMissing()) { counts[inst.index(i)][data.attribute(inst.index(i)).numValues()][(int) inst .classValue()] += inst.weight(); counts[inst.index(i)][0][(int) inst.classValue()] -= inst.weight(); } else { counts[inst.index(i)][data.attribute(inst.index(i)).numValues()][numClasses] += inst .weight(); counts[inst.index(i)][0][numClasses] -= inst.weight(); } } else { counts[inst.index(i)][(int) inst.valueSparse(i)][(int) inst.classValue()] += inst.weight(); counts[inst.index(i)][0][(int) inst.classValue()] -= inst.weight(); } } } } // distribute missing counts if required if (m_missing_merge) { for (int k = 0; k < data.numAttributes(); k++) { if (k != classIndex) { int numValues = data.attribute(k).numValues(); // Compute marginals double[] rowSums = new double[numValues]; double[] columnSums = new double[numClasses]; double sum = 0; for (int i = 0; i < numValues; i++) { for (int j = 0; j < numClasses; j++) { rowSums[i] += counts[k][i][j]; columnSums[j] += counts[k][i][j]; } sum += rowSums[i]; } if (Utils.gr(sum, 0)) { double[][] additions = new double[numValues][numClasses]; // Compute what needs to be added to each row for (int i = 0; i < numValues; i++) { for (int j = 0; j < numClasses; j++) { additions[i][j] = (rowSums[i] / sum) * counts[k][numValues][j]; } } // Compute what needs to be added to each column for (int i = 0; i < numClasses; i++) { for (int j = 0; j < numValues; j++) { additions[j][i] += (columnSums[i] / sum) * counts[k][j][numClasses]; } } // Compute what needs to be added to each cell for (int i = 0; i < numClasses; i++) { for (int j = 0; j < numValues; j++) { additions[j][i] += (counts[k][j][i] / sum) * counts[k][numValues][numClasses]; } } // Make new contingency table double[][] newTable = new double[numValues][numClasses]; for (int i = 0; i < numValues; i++) { for (int j = 0; j < numClasses; j++) { newTable[i][j] = counts[k][i][j] + additions[i][j]; } } counts[k] = newTable; } } } } // Compute chi-squared values m_ChiSquareds = new double[data.numAttributes()]; for (int i = 0; i < data.numAttributes(); i++) { if (i != classIndex) { m_ChiSquareds[i] = chiVal(ContingencyTables.reduceMatrix(counts[i])); } } }
From source file:edu.cuny.qc.speech.AuToBI.util.ClassifierUtils.java
License:Open Source License
/** * Given a (possibly empty) Instances object containing the required weka Attributes, generates a weka Instance for a * single data point.//from ww w. j a va 2 s . co m * * @param instances the weka Instances object containing attributes * @param data_point the data point to convert * @return a weka instance with assigned attributes */ protected static Instance assignWekaAttributes(Instances instances, Word data_point) { double[] instance = new double[instances.numAttributes()]; for (int i = 0; i < instances.numAttributes(); ++i) { Attribute attribute = instances.attribute(i); if (data_point.hasAttribute(attribute.name()) && !data_point.getAttribute(attribute.name()).toString().equals("?")) { switch (attribute.type()) { case Attribute.NOMINAL: int index = attribute.indexOfValue(data_point.getAttribute(attribute.name()).toString()); instance[i] = (double) index; break; case Attribute.NUMERIC: // Check if value is really a number. try { instance[i] = Double.valueOf(data_point.getAttribute(attribute.name()).toString()); } catch (NumberFormatException e) { AuToBIUtils.error("Number expected for feature: " + attribute.name()); } break; case Attribute.STRING: instance[i] = attribute.addStringValue(data_point.getAttribute(attribute.name()).toString()); break; default: AuToBIUtils.error("Unknown attribute type"); } } else { instance[i] = Utils.missingValue(); } } Instance inst = new DenseInstance(1, instance); inst.setDataset(instances); return inst; }
From source file:edu.cuny.qc.speech.AuToBI.util.ClassifierUtils.java
License:Open Source License
/** * Assigns a class attribute to a weka Instances object. * <p/>/*from ww w.j av a 2 s . co m*/ * If no class attribute is given, or if the class attribute is not found in the list of attributes, the last * attribute is set to the class attribute. * * @param instances the instances object * @param class_attribute the desired class attribute. */ static void setWekaClassAttribute(Instances instances, String class_attribute) { if (class_attribute != null) { int i = 0; boolean set = false; while (i < instances.numAttributes() && !set) { Attribute attr = instances.attribute(i); if (class_attribute.equals(attr.name())) { instances.setClassIndex(i); set = true; } ++i; } if (!set) { instances.setClassIndex(instances.numAttributes() - 1); } } else { instances.setClassIndex(instances.numAttributes() - 1); } }
From source file:edu.insight.finlaw.multilabel.rough.CreateInstances.java
License:Open Source License
/** * Generates the Instances object and outputs it in ARFF format to stdout. * * @param args ignored/*w w w . j a v a2 s . co m*/ * @throws Exception if generation of instances fails */ public static void main(String[] args) throws Exception { ArrayList<Attribute> atts; ArrayList<Attribute> attsRel; ArrayList<String> attVals; ArrayList<String> attValsRel; Instances data; Instances dataRel; double[] vals; double[] valsRel; int i; // 1. set up attributes atts = new ArrayList<Attribute>(); // - numeric atts.add(new Attribute("att1")); // - nominal attVals = new ArrayList<String>(); for (i = 0; i < 5; i++) attVals.add("val" + (i + 1)); atts.add(new Attribute("att2", attVals)); // - string atts.add(new Attribute("att3", (ArrayList<String>) null)); // - date atts.add(new Attribute("att4", "yyyy-MM-dd")); // - relational attsRel = new ArrayList<Attribute>(); // -- numeric attsRel.add(new Attribute("att5.1")); // -- nominal attValsRel = new ArrayList<String>(); for (i = 0; i < 5; i++) attValsRel.add("val5." + (i + 1)); attsRel.add(new Attribute("att5.2", attValsRel)); dataRel = new Instances("att5", attsRel, 0); atts.add(new Attribute("att5", dataRel, 0)); // 2. create Instances object data = new Instances("MyRelation", atts, 0); // 3. fill with data // first instance vals = new double[data.numAttributes()]; // - numeric vals[0] = Math.PI; // - nominal vals[1] = attVals.indexOf("val3"); // - string vals[2] = data.attribute(2).addStringValue("This is a string!"); // - date vals[3] = data.attribute(3).parseDate("2001-11-09"); // - relational dataRel = new Instances(data.attribute(4).relation(), 0); // -- first instance valsRel = new double[2]; valsRel[0] = Math.PI + 1; valsRel[1] = attValsRel.indexOf("val5.3"); dataRel.add(new DenseInstance(1.0, valsRel)); // -- second instance valsRel = new double[2]; valsRel[0] = Math.PI + 2; valsRel[1] = attValsRel.indexOf("val5.2"); dataRel.add(new DenseInstance(1.0, valsRel)); vals[4] = data.attribute(4).addRelation(dataRel); // add data.add(new DenseInstance(1.0, vals)); // second instance vals = new double[data.numAttributes()]; // important: needs NEW array! // - numeric vals[0] = Math.E; // - nominal vals[1] = attVals.indexOf("val1"); // - string vals[2] = data.attribute(2).addStringValue("And another one!"); // - date vals[3] = data.attribute(3).parseDate("2000-12-01"); // - relational dataRel = new Instances(data.attribute(4).relation(), 0); // -- first instance valsRel = new double[2]; valsRel[0] = Math.E + 1; valsRel[1] = attValsRel.indexOf("val5.4"); dataRel.add(new DenseInstance(1.0, valsRel)); // -- second instance valsRel = new double[2]; valsRel[0] = Math.E + 2; valsRel[1] = attValsRel.indexOf("val5.1"); dataRel.add(new DenseInstance(1.0, valsRel)); vals[4] = data.attribute(4).addRelation(dataRel); // add data.add(new DenseInstance(1.0, vals)); // 4. output data System.out.println(data); }