List of usage examples for weka.core Attribute NUMERIC
int NUMERIC
To view the source code for weka.core Attribute NUMERIC.
Click Source Link
From source file:adams.data.conversion.AbstractMatchWekaInstanceAgainstHeader.java
License:Open Source License
/** * Matches the input instance against the header. * * @param input the Instance to align to the header * @return the aligned Instance// ww w . j a v a 2 s. com */ protected Instance match(Instance input) { Instance result; double[] values; int i; values = new double[m_Dataset.numAttributes()]; for (i = 0; i < m_Dataset.numAttributes(); i++) { values[i] = Utils.missingValue(); switch (m_Dataset.attribute(i).type()) { case Attribute.NUMERIC: case Attribute.DATE: values[i] = input.value(i); break; case Attribute.NOMINAL: if (m_Dataset.attribute(i).indexOfValue(input.stringValue(i)) != -1) values[i] = m_Dataset.attribute(i).indexOfValue(input.stringValue(i)); break; case Attribute.STRING: values[i] = m_Dataset.attribute(i).addStringValue(input.stringValue(i)); break; case Attribute.RELATIONAL: values[i] = m_Dataset.attribute(i).addRelation(input.relationalValue(i)); break; default: throw new IllegalStateException( "Unhandled attribute type: " + Attribute.typeToString(m_Dataset.attribute(i).type())); } } if (input instanceof SparseInstance) result = new SparseInstance(input.weight(), values); else result = new DenseInstance(input.weight(), values); result.setDataset(m_Dataset); // fix class index, if necessary if ((input.classIndex() != m_Dataset.classIndex()) && (m_Dataset.classIndex() < 0)) m_Dataset.setClassIndex(input.classIndex()); return result; }
From source file:adams.data.conversion.WekaInstancesToSpreadSheet.java
License:Open Source License
/** * Performs the actual conversion./*from w w w. j a va 2 s . c om*/ * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { SpreadSheet result; Instances data; Row row; int i; int n; String str; data = (Instances) m_Input; // special case for InstancesViews if (m_SpreadSheetType instanceof InstancesView) { result = new InstancesView((Instances) m_Input); return result; } // create header result = m_SpreadSheetType.newInstance(); result.setDataRowClass(m_DataRowType.getClass()); row = result.getHeaderRow(); for (n = 0; n < data.numAttributes(); n++) row.addCell("" + n).setContent(data.attribute(n).name()); if (result instanceof Dataset) { if (data.classIndex() != -1) ((Dataset) result).setClassAttribute(data.classIndex(), true); } // fill spreadsheet for (i = 0; i < data.numInstances(); i++) { row = result.addRow("" + i); for (n = 0; n < data.numAttributes(); n++) { if (data.instance(i).isMissing(n)) continue; if (data.attribute(n).type() == Attribute.DATE) { row.addCell("" + n).setContent(new DateTimeMsec(new Date((long) data.instance(i).value(n)))); } else if (data.attribute(n).type() == Attribute.NUMERIC) { row.addCell("" + n).setContent(data.instance(i).value(n)); } else { str = data.instance(i).stringValue(n); if (str.equals(SpreadSheet.MISSING_VALUE)) row.addCell("" + n).setContentAsString("'" + str + "'"); else row.addCell("" + n).setContentAsString(str); } } } return result; }
From source file:adams.data.instances.AbstractInstanceGenerator.java
License:Open Source License
/** * Adds IDs, notes, additional fields to header. * * @param data the input data/* w w w . ja v a2s. c om*/ */ protected void postProcessHeader(T data) { Add add; // add the database ID to the output? if (m_AddDatabaseID) { try { add = new Add(); add.setAttributeIndex("1"); add.setAttributeName(ArffUtils.getDBIDName()); add.setAttributeType(new SelectedTag(Attribute.NUMERIC, Add.TAGS_TYPE)); add.setInputFormat(m_OutputHeader); m_OutputHeader = Filter.useFilter(m_OutputHeader, add); } catch (Exception e) { getLogger().log(Level.SEVERE, "Error initializing the Add filter for database ID!", e); } } }
From source file:adams.flow.transformer.WekaGetInstancesValue.java
License:Open Source License
/** * Executes the flow item./*w ww.ja va2 s . c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances inst; int index; int row; result = null; inst = (Instances) m_InputToken.getPayload(); m_Column.setData(inst); m_Row.setMax(inst.numInstances()); index = m_Column.getIntIndex(); row = m_Row.getIntIndex(); if (row == -1) result = "Failed to retrieve row: " + m_Row.getIndex(); else if (index == -1) result = "Failed to retrieve column: " + m_Column.getIndex(); if (result == null) { try { if (inst.instance(row).isMissing(index)) { m_OutputToken = new Token("?"); } else { switch (inst.attribute(index).type()) { case Attribute.NUMERIC: m_OutputToken = new Token(inst.instance(row).value(index)); break; case Attribute.DATE: case Attribute.NOMINAL: case Attribute.STRING: case Attribute.RELATIONAL: m_OutputToken = new Token(inst.instance(row).stringValue(index)); break; default: result = "Unhandled attribute type: " + inst.attribute(index).type(); } } } catch (Exception e) { result = handleException("Failed to obtain value from dataset:", e); } } return result; }
From source file:adams.flow.transformer.WekaGetInstanceValue.java
License:Open Source License
/** * Executes the flow item.//from www. jav a 2s . c om * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instance inst; int index; result = null; inst = (Instance) m_InputToken.getPayload(); try { if (m_AttributeName.length() > 0) { index = inst.dataset().attribute(m_AttributeName).index(); } else { m_Index.setMax(inst.numAttributes()); index = m_Index.getIntIndex(); } if (inst.isMissing(index)) { m_OutputToken = new Token("?"); } else { switch (inst.attribute(index).type()) { case Attribute.NUMERIC: m_OutputToken = new Token(inst.value(index)); break; case Attribute.DATE: case Attribute.NOMINAL: case Attribute.STRING: case Attribute.RELATIONAL: m_OutputToken = new Token(inst.stringValue(index)); break; default: result = "Unhandled attribute type: " + inst.attribute(index).type(); } } } catch (Exception e) { result = handleException("Failed to obtain value from instance:\n" + inst, e); } return result; }
From source file:adams.flow.transformer.WekaInstanceEvaluator.java
License:Open Source License
/** * Generates the new header for the data. * * @param inst the instance to get the original data format from * @return null if everything is fine, otherwise error message * @see #m_Header/* ww w.j a v a 2s . c o m*/ * @see #m_Filter */ protected String generateHeader(Instance inst) { String result; result = null; m_Filter = new Add(); m_Filter.setAttributeName(determineAttributeName(inst.dataset())); m_Filter.setAttributeType(new SelectedTag(Attribute.NUMERIC, Add.TAGS_TYPE)); if (inst.dataset().classIndex() == inst.dataset().numAttributes() - 1) m_Filter.setAttributeIndex("" + inst.dataset().numAttributes()); else m_Filter.setAttributeIndex("" + (inst.dataset().numAttributes() + 1)); try { m_Filter.setInputFormat(inst.dataset()); m_Header = weka.filters.Filter.useFilter(inst.dataset(), m_Filter); } catch (Exception e) { result = handleException("Failed to generate header:", e); } return result; }
From source file:adams.flow.transformer.WekaInstancesMerge.java
License:Open Source License
/** * Updates the IDs in the hashset with the ones stored in the ID attribute * of the provided dataset.//from w w w . j a va 2 s .c om * * @param instIndex the dataset index * @param inst the dataset to obtain the IDs from * @param ids the hashset to store the IDs in */ protected void updateIDs(int instIndex, Instances inst, HashSet ids) { Attribute att; int i; boolean numeric; HashSet current; Object id; att = inst.attribute(m_UniqueID); if (att == null) throw new IllegalStateException("Attribute '" + m_UniqueID + "' not found in relation '" + inst.relationName() + "' (#" + (instIndex + 1) + ")!"); // determine/check type if (m_AttType == -1) { if ((att.type() == Attribute.NUMERIC) || (att.type() == Attribute.STRING)) m_AttType = att.type(); else throw new IllegalStateException("Attribute '" + m_UniqueID + "' must be either NUMERIC or STRING (#" + (instIndex + 1) + ")!"); } else { if (m_AttType != att.type()) throw new IllegalStateException("Attribute '" + m_UniqueID + "' must have same attribute type in all the datasets (#" + (instIndex + 1) + ")!"); } // get IDs numeric = m_AttType == Attribute.NUMERIC; current = new HashSet(); for (i = 0; i < inst.numInstances(); i++) { if (numeric) id = inst.instance(i).value(att); else id = inst.instance(i).stringValue(att); if (m_Strict && current.contains(id)) throw new IllegalStateException( "ID '" + id + "' is not unique in dataset #" + (instIndex + 1) + "!"); current.add(id); } ids.addAll(current); }
From source file:adams.flow.transformer.WekaInstancesMerge.java
License:Open Source License
/** * Merges the datasets based on the collected IDs. * * @param orig the original datasets//from w w w. j a v a2s . co m * @param inst the processed datasets to merge into one * @param ids the IDs for identifying the rows * @return the merged dataset */ protected Instances merge(Instances[] orig, Instances[] inst, HashSet ids) { Instances result; ArrayList<Attribute> atts; int i; int n; int m; int index; String relation; List sortedIDs; Attribute att; int[] indexStart; double value; double[] values; HashMap<Integer, Integer> hashmap; HashSet<Instance> hs; // create header if (isLoggingEnabled()) getLogger().info("Creating merged header..."); atts = new ArrayList<>(); relation = ""; indexStart = new int[inst.length]; for (i = 0; i < inst.length; i++) { indexStart[i] = atts.size(); for (n = 0; n < inst[i].numAttributes(); n++) atts.add((Attribute) inst[i].attribute(n).copy()); // assemble relation name if (i > 0) relation += "_"; relation += inst[i].relationName(); } result = new Instances(relation, atts, ids.size()); // fill with missing values if (isLoggingEnabled()) getLogger().info("Filling with missing values..."); for (i = 0; i < ids.size(); i++) { if (isStopped()) return null; // progress if (isLoggingEnabled() && ((i + 1) % 1000 == 0)) getLogger().info("" + (i + 1)); result.add(new DenseInstance(result.numAttributes())); } // sort IDs if (isLoggingEnabled()) getLogger().info("Sorting indices..."); sortedIDs = new ArrayList(ids); Collections.sort(sortedIDs); // generate rows hashmap = new HashMap<>(); for (i = 0; i < inst.length; i++) { if (isStopped()) return null; if (isLoggingEnabled()) getLogger().info("Adding file #" + (i + 1)); att = orig[i].attribute(m_UniqueID); for (n = 0; n < inst[i].numInstances(); n++) { // progress if (isLoggingEnabled() && ((n + 1) % 1000 == 0)) getLogger().info("" + (n + 1)); // determine index of row if (m_AttType == Attribute.NUMERIC) index = Collections.binarySearch(sortedIDs, inst[i].instance(n).value(att)); else index = Collections.binarySearch(sortedIDs, inst[i].instance(n).stringValue(att)); if (index < 0) throw new IllegalStateException( "Failed to determine index for row #" + (n + 1) + " of dataset #" + (i + 1) + "!"); if (!hashmap.containsKey(index)) hashmap.put(index, 0); hashmap.put(index, hashmap.get(index) + 1); // use internal representation for faster access values = result.instance(index).toDoubleArray(); // add attribute values for (m = 0; m < inst[i].numAttributes(); m++) { // missing value? if (inst[i].instance(n).isMissing(m)) continue; switch (inst[i].attribute(m).type()) { case Attribute.NUMERIC: case Attribute.DATE: case Attribute.NOMINAL: values[indexStart[i] + m] = inst[i].instance(n).value(m); break; case Attribute.STRING: value = result.attribute(indexStart[i] + m) .addStringValue(inst[i].instance(n).stringValue(m)); values[indexStart[i] + m] = value; break; case Attribute.RELATIONAL: value = result.attribute(indexStart[i] + m) .addRelation(inst[i].instance(n).relationalValue(m)); values[indexStart[i] + m] = value; break; default: throw new IllegalStateException("Unhandled attribute type: " + inst[i].attribute(m).type()); } } // update row result.set(index, new DenseInstance(1.0, values)); } } if (getRemove()) { hs = new HashSet<>(); for (Integer x : hashmap.keySet()) { if (hashmap.get(x) != inst.length) hs.add(result.get(x)); } result.removeAll(hs); } return result; }
From source file:adams.flow.transformer.WekaSetInstancesValue.java
License:Open Source License
/** * Executes the flow item./*from ww w . jav a 2s .c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances inst; int row; int index; result = null; inst = (Instances) m_InputToken.getPayload(); inst = new Instances(inst); m_Row.setMax(inst.numInstances()); m_Column.setData(inst); row = m_Row.getIntIndex(); index = m_Column.getIntIndex(); if (row == -1) result = "Failed to retrieve row: " + m_Row.getIndex(); else if (index == -1) result = "Failed to retrieve column: " + m_Column.getIndex(); if (result == null) { try { if (m_Value.equals("?")) { inst.instance(row).setMissing(index); } else { switch (inst.attribute(index).type()) { case Attribute.NUMERIC: inst.instance(row).setValue(index, Utils.toDouble(m_Value)); break; case Attribute.DATE: inst.instance(row).setValue(index, inst.attribute(index).parseDate(m_Value)); break; case Attribute.NOMINAL: case Attribute.STRING: inst.instance(row).setValue(index, m_Value); break; case Attribute.RELATIONAL: result = "Relational attributes cannot be set!"; break; default: result = "Unhandled attribute type: " + inst.attribute(index).type(); } } } catch (Exception e) { result = handleException("Failed to set value: " + m_Column.getIndex() + " -> " + m_Value, e); } } // broadcast data if (result == null) m_OutputToken = new Token(inst); return result; }
From source file:adams.flow.transformer.WekaSetInstanceValue.java
License:Open Source License
/** * Executes the flow item.//from ww w . j a va 2s.c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instance inst; int index; result = null; inst = (Instance) m_InputToken.getPayload(); inst = (Instance) inst.copy(); m_Index.setData(inst.dataset()); index = m_Index.getIntIndex(); try { if (m_Value.equals("?")) { inst.setMissing(index); } else { switch (inst.attribute(index).type()) { case Attribute.NUMERIC: inst.setValue(index, Utils.toDouble(m_Value)); break; case Attribute.DATE: inst.setValue(index, inst.attribute(index).parseDate(m_Value)); break; case Attribute.NOMINAL: case Attribute.STRING: inst.setValue(index, m_Value); break; case Attribute.RELATIONAL: result = "Relational attributes cannot be set!"; break; default: result = "Unhandled attribute type: " + inst.attribute(index).type(); } } } catch (Exception e) { result = handleException("Failed to set value: " + m_Index.getIndex() + " -> " + m_Value, e); } // broadcast data if (result == null) m_OutputToken = new Token(inst); return result; }