List of usage examples for weka.core Attribute type
public finalint type()
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 ww w .java2 s .c o m*/ * * @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.WekaReorderAttributesToReference.java
License:Open Source License
/** * Executes the flow item.//from ww w. j a v a 2 s .c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances dataOld; Instance instOld; Instances dataNew; Instance instNew; Attribute att; int i; StringBuilder order; List<Add> adds; Add add; int index; StringBuilder labels; int n; List<Filter> filters; Reorder reorder; result = null; if (m_OnTheFly && (m_Reference == null)) { result = setUpReference(); if (result != null) return result; } dataNew = null; instNew = null; // get input data if (m_InputToken.getPayload() instanceof Instance) { instOld = (Instance) m_InputToken.getPayload(); dataOld = instOld.dataset(); } else { instOld = null; dataOld = (Instances) m_InputToken.getPayload(); } // do we need to initialize filter? if (m_InitializeOnce || (m_Reorder == null)) { // check incoming data if (!m_Lenient) { for (i = 0; i < m_Reference.numAttributes(); i++) { att = m_Reference.attribute(i); if (dataOld.attribute(att.name()) == null) { if (result == null) result = "Missing attribute(s) in incoming data: " + att.name(); else result += ", " + att.name(); } } if (result != null) getLogger().severe(result); } if (result == null) { try { // determine indices order = new StringBuilder(); adds = new ArrayList<Add>(); for (i = 0; i < m_Reference.numAttributes(); i++) { att = m_Reference.attribute(i); if (dataOld.attribute(att.name()) == null) { index = dataOld.numAttributes() + adds.size(); add = new Add(); add.setAttributeIndex("last"); add.setAttributeName(att.name()); add.setAttributeType(new SelectedTag(att.type(), Add.TAGS_TYPE)); if (att.isNominal()) { labels = new StringBuilder(); for (n = 0; n < att.numValues(); n++) { if (labels.length() > 0) labels.append(","); labels.append(att.value(n)); } add.setNominalLabels(labels.toString()); } adds.add(add); } else { index = dataOld.attribute(att.name()).index(); } if (order.length() > 0) order.append(","); order.append((index + 1)); } // build reorder filter reorder = new Reorder(); reorder.setAttributeIndices(order.toString()); // build multifilter filters = new ArrayList<Filter>(); filters.addAll(adds); filters.add(reorder); m_Reorder = new MultiFilter(); m_Reorder.setFilters(filters.toArray(new Filter[filters.size()])); // initialize filter m_Reorder.setInputFormat(dataOld); } catch (Exception e) { result = handleException("Failed to initialize reorder filter!", e); } } } // reorder data if (result == null) { try { if (instOld != null) { m_Reorder.input(instOld); m_Reorder.batchFinished(); instNew = m_Reorder.output(); if (m_KeepRelationName) instNew.dataset().setRelationName(dataOld.relationName()); } else { dataNew = Filter.useFilter(dataOld, m_Reorder); if (m_KeepRelationName) dataNew.setRelationName(dataOld.relationName()); } } catch (Exception e) { result = handleException("Failed to reorder data!", e); instNew = null; dataNew = null; } } if (instNew != null) m_OutputToken = new Token(instNew); else if (dataNew != null) m_OutputToken = new Token(dataNew); return result; }
From source file:adams.gui.visualization.instance.InstanceTableModel.java
License:Open Source License
/** * Returns the value at the given position. * * @param row the row in the table/*ww w . j a v a 2 s . c o m*/ * @param column the column in the table * @return the value */ public Object getValueAt(int row, int column) { Attribute att; att = getAttribute(column); if (column == 0) { return row + 1; } else if (att == null) { return ""; } else if (m_Data.instance(row).isMissing(att)) { return null; } else if (att.name().equals(ArffUtils.getDBIDName())) { return (int) m_Data.instance(row).value(att); } else if (att.name().equals(ArffUtils.getIDName())) { return m_Data.instance(row).stringValue(att).replaceAll("\'", ""); } else { switch (att.type()) { case Attribute.NUMERIC: return m_Data.instance(row).value(att); case Attribute.DATE: case Attribute.NOMINAL: case Attribute.STRING: case Attribute.RELATIONAL: return m_Data.instance(row).stringValue(att); default: return "???"; } } }
From source file:adams.gui.visualization.instance.InstanceTableModel.java
License:Open Source License
/** * Returns the class for the column.// w ww .ja va 2 s .c o m * * @param column the column to retrieve the class for * @return the class */ public Class getColumnClass(int column) { Attribute att; att = getAttribute(column); if (column == 0) { return Integer.class; } else if (att == null) { return String.class; } else if (att.name().equals(ArffUtils.getDBIDName())) { // special case return Integer.class; } else { switch (att.type()) { case Attribute.NUMERIC: return Double.class; case Attribute.DATE: case Attribute.NOMINAL: case Attribute.STRING: case Attribute.RELATIONAL: return String.class; default: return String.class; } } }
From source file:adams.gui.visualization.instance.InstanceTableModel.java
License:Open Source License
/** * Tests whether the search matches the specified row. * * @param params the search parameters * @param row the row of the underlying, unsorted model * @return true if the search matches this row *///from ww w . j a v a2s. c om public boolean isSearchMatch(SearchParameters params, int row) { int n; Attribute att; String valStr; for (n = 1; n < m_Data.numAttributes(); n++) { att = m_Data.attribute(n); if ((att.type() == Attribute.NUMERIC) && params.isDouble()) { if (params.matches(m_Data.instance(row).value(att))) return true; } else { if (att.type() == Attribute.NUMERIC) valStr = "" + m_Data.instance(row).value(att); else valStr = m_Data.instance(row).stringValue(att); if (params.matches(valStr)) return true; } } return false; }
From source file:adams.ml.data.InstancesView.java
License:Open Source License
/** * Checks whether the given column is of the specific content type or not. * * @param columnIndex the index of the column to check * @param type the content type to check * @return true if column purely consists of this content type * @see #getContentType(int)/*from ww w .j av a 2s. com*/ */ @Override public boolean isContentType(int columnIndex, ContentType type) { Attribute att; att = m_Data.attribute(columnIndex); if ((type == ContentType.DOUBLE) && (att.type() == Attribute.NUMERIC)) return true; else if ((type == ContentType.DATETIMEMSEC) && (att.type() == Attribute.DATE)) return true; else if ((type == ContentType.STRING) && (att.type() == Attribute.NOMINAL)) return true; else if ((type == ContentType.STRING) && (att.type() == Attribute.STRING)) return true; return false; }
From source file:adams.ml.data.InstancesView.java
License:Open Source License
/** * Returns the all content types of the given column, if available. * * @param columnIndex the index of the column to check * @return the content types that this column consists of *//* w w w .ja va 2 s . c o m*/ @Override public Collection<ContentType> getContentTypes(int columnIndex) { List<ContentType> result; Attribute att; int i; result = new ArrayList<>(); att = m_Data.attribute(columnIndex); if (att.type() == Attribute.NUMERIC) result.add(ContentType.DOUBLE); else if (att.type() == Attribute.DATE) result.add(ContentType.DATETIMEMSEC); else if (att.type() == Attribute.NOMINAL) result.add(ContentType.STRING); else if (att.type() == Attribute.STRING) result.add(ContentType.STRING); // check for missing for (i = 0; i < m_Data.numInstances(); i++) { if (m_Data.instance(i).isMissing(columnIndex)) { result.add(ContentType.MISSING); break; } } return result; }
From source file:adams.ml.data.WekaConverter.java
License:Open Source License
/** * Turns an ADAMS dataset row into a Weka Instance. * * @param data the dataset to use as template * @param row the row to convert// w w w.j a va 2s. com * @return the generated instance * @throws Exception if conversion fails */ public static Instance toInstance(Instances data, Row row) throws Exception { Instance result; double[] values; int i; Cell cell; Attribute att; values = new double[data.numAttributes()]; for (i = 0; i < data.numAttributes(); i++) { values[i] = Utils.missingValue(); if (!row.hasCell(i)) continue; cell = row.getCell(i); if (cell.isMissing()) continue; att = data.attribute(i); switch (att.type()) { case Attribute.NUMERIC: values[i] = cell.toDouble(); break; case Attribute.DATE: values[i] = cell.toAnyDateType().getTime(); break; case Attribute.NOMINAL: values[i] = att.indexOfValue(cell.getContent()); break; case Attribute.STRING: values[i] = att.addStringValue(cell.getContent()); break; default: throw new Exception("Unhandled Weka attribute type: " + Attribute.typeToString(att)); } } result = new DenseInstance(1.0, values); result.setDataset(data); return result; }
From source file:classifiers.ComplexClassifier.java
@Override public void train(Instances inst) throws Exception { Knoten[] k = Model.getDieknoten();//from w ww .j ava2s .c o m Enumeration<Attribute> enu = inst.enumerateAttributes(); int attindex = 0; while (enu.hasMoreElements()) { Attribute att = enu.nextElement(); if (k[attindex].hatEltern()) { switch (att.type()) { case Attribute.NUMERIC: { for (int i = 0; i < k[attindex].anzahlEltern(); i++) { Attribute a = inst.attribute(k[attindex].getEltern(i).getID()); int c = a.index(); switch (a.type()) { case Attribute.NUMERIC: list.add(attindex, (new NumericNumericDistribution(inst, attindex, c))); break; case (Attribute.NOMINAL): list.add(attindex, new NumericNominalDistribution(inst, attindex, c)); break; case (Attribute.STRING): list.add(attindex, new NumericNominalDistribution(inst, attindex, c)); break; default: throw new Exception("Attributetype unbekannt"); } } } break; case Attribute.NOMINAL: { for (int i = 0; i < k[attindex].anzahlEltern(); i++) { Attribute a = inst.attribute(k[attindex].getEltern(i).getID()); int c = a.index(); switch (a.type()) { case Attribute.NUMERIC: list.add(attindex, new NumericNominalDistribution(inst, attindex, c)); break; case (Attribute.NOMINAL): list.add(attindex, new NominalNominalDistribution(inst, attindex, c)); break; case (Attribute.STRING): list.add(attindex, new NominalNominalDistribution(inst, attindex, c)); break; default: { throw new Exception("Attributetype unbekannt"); } } } } break; } } else { switch (att.type()) { case Attribute.NUMERIC: list.add(attindex, new NumericDistribution(inst, attindex)); break; case Attribute.NOMINAL: list.add(attindex, new NominalDistribution(inst, attindex)); break; case Attribute.STRING: list.add(attindex, new NominalDistribution(inst, attindex)); break; default: throw new Exception("Attributetype unbekannt"); } } attindex++; } for (int i = 0; i < inst.numClasses(); i++) { for (int j = 0; j < inst.numInstances(); j++) { if (inst.instance(j).classValue() == i) { Classparam[i]++; } } } for (int i = 0; i < inst.numClasses(); i++) { Classparam[i] /= inst.numInstances(); } }
From source file:classifiers.ComplexClassifierZufall.java
@Override public void train(Instances inst) throws Exception { Knoten[] k = Model.getDieknoten();/*from ww w.j ava 2 s. c o m*/ Enumeration<Attribute> enu = inst.enumerateAttributes(); int attindex = 0; while (enu.hasMoreElements()) { Attribute att = enu.nextElement(); if (k[attindex].hatEltern()) { switch (att.type()) { case Attribute.NUMERIC: { for (int i = 0; i < k[attindex].anzahlEltern(); i++) { Attribute a = inst.attribute(k[attindex].getEltern(i).getID()); int c = a.index(); switch (a.type()) { case Attribute.NUMERIC: list.add(attindex, (new NumericNumericDistribution(inst, attindex, c))); break; case (Attribute.NOMINAL): list.add(attindex, new NumericNominalDistribution(inst, attindex, c)); break; case (Attribute.STRING): list.add(attindex, new NumericNominalDistribution(inst, attindex, c)); break; default: throw new Exception("Attributetype unbekannt"); } } } break; case Attribute.NOMINAL: { for (int i = 0; i < k[attindex].anzahlEltern(); i++) { Attribute a = inst.attribute(k[attindex].getEltern(i).getID()); int c = a.index(); switch (a.type()) { case Attribute.NUMERIC: list.add(attindex, new NumericNominalDistribution(inst, attindex, c)); break; case (Attribute.NOMINAL): list.add(attindex, new NominalNominalDistribution(inst, attindex, c)); break; case (Attribute.STRING): list.add(attindex, new NominalNominalDistribution(inst, attindex, c)); break; default: { throw new Exception("Attributetype unbekannt"); } } } } break; } } else { switch (att.type()) { case Attribute.NUMERIC: list.add(attindex, new NumericDistribution(inst, attindex)); break; case Attribute.NOMINAL: list.add(attindex, new NominalDistribution(inst, attindex)); break; case Attribute.STRING: list.add(attindex, new NominalDistribution(inst, attindex)); break; default: throw new Exception("Attributetype unbekannt"); } } attindex++; } for (int i = 0; i < inst.numClasses(); i++) { for (int j = 0; j < inst.numInstances(); j++) { if (inst.instance(j).classValue() == i) { Classparam[i]++; } } } for (int i = 0; i < inst.numClasses(); i++) { Classparam[i] /= inst.numInstances(); } }