List of usage examples for weka.core Attribute typeToString
public static String typeToString(int type)
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/*w w w. j a v a 2 s .co m*/ */ 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.flow.transformer.WekaInstancesInfo.java
License:Open Source License
/** * Executes the flow item.// ww w.j av a 2 s . c om * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances inst; int index; int labelIndex; double[] dist; Enumeration enm; int i; result = null; if (m_InputToken.getPayload() instanceof Instance) inst = ((Instance) m_InputToken.getPayload()).dataset(); else inst = (Instances) m_InputToken.getPayload(); m_AttributeIndex.setData(inst); index = m_AttributeIndex.getIntIndex(); m_Queue.clear(); switch (m_Type) { case FULL: m_Queue.add(inst.toSummaryString()); break; case FULL_ATTRIBUTE: m_Queue.add(getAttributeStats(inst, index)); break; case FULL_CLASS: if (inst.classIndex() > -1) m_Queue.add(getAttributeStats(inst, inst.classIndex())); break; case HEADER: m_Queue.add(new Instances(inst, 0).toString()); break; case RELATION_NAME: m_Queue.add(inst.relationName()); break; case ATTRIBUTE_NAME: if (index != -1) m_Queue.add(inst.attribute(index).name()); break; case ATTRIBUTE_NAMES: for (i = 0; i < inst.numAttributes(); i++) m_Queue.add(inst.attribute(i).name()); break; case LABELS: if (index != -1) { enm = inst.attribute(index).enumerateValues(); while (enm.hasMoreElements()) m_Queue.add(enm.nextElement()); } break; case CLASS_LABELS: if (inst.classIndex() > -1) { enm = inst.classAttribute().enumerateValues(); while (enm.hasMoreElements()) m_Queue.add(enm.nextElement()); } break; case LABEL_COUNT: if (index > -1) { m_LabelIndex.setData(inst.attribute(index)); labelIndex = m_LabelIndex.getIntIndex(); m_Queue.add(inst.attributeStats(index).nominalCounts[labelIndex]); } break; case LABEL_COUNTS: if (index > -1) m_Queue.add(StatUtils.toNumberArray(inst.attributeStats(index).nominalCounts)); break; case LABEL_DISTRIBUTION: if (index > -1) { dist = new double[inst.attributeStats(index).nominalCounts.length]; for (i = 0; i < dist.length; i++) dist[i] = inst.attributeStats(index).nominalCounts[i]; Utils.normalize(dist); m_Queue.add(StatUtils.toNumberArray(dist)); } break; case CLASS_LABEL_COUNT: if (inst.classIndex() > -1) { m_LabelIndex.setData(inst.classAttribute()); labelIndex = m_LabelIndex.getIntIndex(); m_Queue.add(inst.attributeStats(inst.classIndex()).nominalCounts[labelIndex]); } break; case CLASS_LABEL_COUNTS: if (inst.classIndex() > -1) m_Queue.add(StatUtils.toNumberArray(inst.attributeStats(inst.classIndex()).nominalCounts)); break; case CLASS_LABEL_DISTRIBUTION: if (inst.classIndex() > -1) { dist = new double[inst.attributeStats(inst.classIndex()).nominalCounts.length]; for (i = 0; i < dist.length; i++) dist[i] = inst.attributeStats(inst.classIndex()).nominalCounts[i]; Utils.normalize(dist); m_Queue.add(StatUtils.toNumberArray(dist)); } break; case NUM_ATTRIBUTES: m_Queue.add(inst.numAttributes()); break; case NUM_INSTANCES: m_Queue.add(inst.numInstances()); break; case NUM_CLASS_LABELS: if ((inst.classIndex() != -1) && inst.classAttribute().isNominal()) m_Queue.add(inst.classAttribute().numValues()); break; case NUM_LABELS: if ((index != -1) && inst.attribute(index).isNominal()) m_Queue.add(inst.attribute(index).numValues()); break; case NUM_DISTINCT_VALUES: if (index != -1) m_Queue.add(inst.attributeStats(index).distinctCount); break; case NUM_UNIQUE_VALUES: if (index != -1) m_Queue.add(inst.attributeStats(index).uniqueCount); break; case NUM_MISSING_VALUES: if (index != -1) m_Queue.add(inst.attributeStats(index).missingCount); break; case MIN: if ((index != -1) && inst.attribute(index).isNumeric()) m_Queue.add(inst.attributeStats(index).numericStats.min); break; case MAX: if ((index != -1) && inst.attribute(index).isNumeric()) m_Queue.add(inst.attributeStats(index).numericStats.max); break; case MEAN: if ((index != -1) && inst.attribute(index).isNumeric()) m_Queue.add(inst.attributeStats(index).numericStats.mean); break; case STDEV: if ((index != -1) && inst.attribute(index).isNumeric()) m_Queue.add(inst.attributeStats(index).numericStats.stdDev); break; case ATTRIBUTE_TYPE: if (index != -1) m_Queue.add(Attribute.typeToString(inst.attribute(index))); break; case CLASS_TYPE: if (inst.classIndex() != -1) m_Queue.add(Attribute.typeToString(inst.classAttribute())); break; default: result = "Unhandled info type: " + m_Type; } return result; }
From source file:adams.gui.menu.AppendDatasets.java
License:Open Source License
/** * Performs the append.// w ww .j a v a2 s .c o m * * @param frame the frame to close * @param input the files to merge * @param output the output file */ protected void doAppend(ChildFrame frame, File[] input, File output) { Instances[] data; Instances full; int i; int n; AbstractFileLoader loader; DataSink sink; int count; TIntArrayList transferAtt; int index; if (input.length < 2) { GUIHelper.showErrorMessage(getOwner(), "At least two files are required!"); return; } // load and check compatibility loader = ConverterUtils.getLoaderForFile(input[0]); data = new Instances[input.length]; count = 0; transferAtt = new TIntArrayList(); for (i = 0; i < input.length; i++) { try { loader.setFile(input[i]); data[i] = DataSource.read(loader); if (i > 0) { if (!data[0].equalHeaders(data[i])) { GUIHelper.showErrorMessage(getOwner(), "Datasets '" + input[0] + "' and '" + input[i] + "' are not compatible!\n" + data[0].equalHeadersMsg(data[i])); return; } } else { for (n = 0; n < data[0].numAttributes(); n++) { if (data[0].attribute(n).isString() || data[0].attribute(n).isRelationValued()) transferAtt.add(n); } } count += data[i].numInstances(); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Failed to read '" + input[i] + "'!\n" + Utils.throwableToString(e)); return; } } // combine full = new Instances(data[0], count); for (i = 0; i < data.length; i++) { for (Instance inst : data[i]) { if (transferAtt.size() > 0) { for (n = 0; n < transferAtt.size(); n++) { index = transferAtt.get(n); if (inst.attribute(index).isString()) full.attribute(index).addStringValue(inst.stringValue(index)); else if (inst.attribute(n).isRelationValued()) full.attribute(index).addRelation(inst.relationalValue(index)); else throw new IllegalStateException( "Unhandled attribute type: " + Attribute.typeToString(inst.attribute(index))); } } full.add(inst); } } // save try { sink = new DataSink(output.getAbsolutePath()); sink.write(full); } catch (Exception e) { GUIHelper.showErrorMessage(getOwner(), "Failed to save data to '" + output + "'!\n" + Utils.throwableToString(e)); return; } GUIHelper.showInformationMessage(null, "Successfully appended!\n" + output); frame.dispose(); }
From source file:adams.gui.visualization.debug.objectrenderer.WekaInstancesRenderer.java
License:Open Source License
/** * Performs the actual rendering.//from w ww . java 2s . c o m * * @param obj the object to render * @param panel the panel to render into * @return null if successful, otherwise error message */ @Override protected String doRender(Object obj, JPanel panel) { Instance inst; Instances data; InstancesTable table; InstancesTableModel model; BaseScrollPane scrollPane; PlainTextRenderer plain; SpreadSheet sheet; Row row; int i; SpreadSheetRenderer sprenderer; if (obj instanceof Instances) { data = (Instances) obj; if (data.numInstances() == 0) { sheet = new DefaultSpreadSheet(); row = sheet.getHeaderRow(); row.addCell("I").setContentAsString("Index"); row.addCell("N").setContentAsString("Name"); row.addCell("T").setContentAsString("Type"); row.addCell("C").setContentAsString("Class"); for (i = 0; i < data.numAttributes(); i++) { row = sheet.addRow(); row.addCell("I").setContent(i + 1); row.addCell("N").setContentAsString(data.attribute(i).name()); row.addCell("T").setContentAsString(Attribute.typeToString(data.attribute(i))); row.addCell("C").setContent((i == data.classIndex()) ? "true" : ""); } sprenderer = new SpreadSheetRenderer(); sprenderer.render(sheet, panel); } else { model = new InstancesTableModel(data); model.setShowAttributeIndex(true); table = new InstancesTable(model); scrollPane = new BaseScrollPane(table); panel.add(scrollPane, BorderLayout.CENTER); } } else { inst = (Instance) obj; if (inst.dataset() != null) { data = new Instances(inst.dataset(), 0); data.add((Instance) inst.copy()); table = new InstancesTable(data); scrollPane = new BaseScrollPane(table); panel.add(scrollPane, BorderLayout.CENTER); } else { plain = new PlainTextRenderer(); plain.render(obj, panel); } } return null; }
From source file:adams.ml.data.DataCellView.java
License:Open Source License
/** * Obtains the content/type of the other cell, but not the owner. * * @param cell the cell to get the content/type from *///w ww . j a v a2 s .c o m @Override public void assign(Cell cell) { switch (m_Owner.getData().attribute(m_ColIndex).type()) { case Attribute.NUMERIC: m_Owner.getData().setValue(m_ColIndex, cell.toDouble()); break; case Attribute.DATE: m_Owner.getData().setValue(m_ColIndex, cell.toAnyDateType().getTime()); break; case Attribute.NOMINAL: case Attribute.STRING: m_Owner.getData().setValue(m_ColIndex, cell.getContent()); break; default: throw new IllegalArgumentException("Cannot handle attribute type: " + Attribute.typeToString(m_Owner.getData().attribute(m_ColIndex).type())); } }
From source file:adams.ml.data.DataCellView.java
License:Open Source License
/** * Returns the content of the cell.// w ww . j a va 2s . c o m * * @return the content */ @Override public String getContent() { switch (m_Owner.getData().attribute(m_ColIndex).type()) { case Attribute.NUMERIC: return "" + m_Owner.getData().value(m_ColIndex); case Attribute.DATE: case Attribute.NOMINAL: case Attribute.STRING: return m_Owner.getData().stringValue(m_ColIndex); default: throw new IllegalStateException("Unhandled attribute type: " + Attribute.typeToString(m_Owner.getData().attribute(m_ColIndex).type())); } }
From source file:adams.ml.data.DataCellView.java
License:Open Source License
/** * Returns the content type.//w w w . java2 s . c o m * * @return the type */ @Override public ContentType getContentType() { switch (m_Owner.getData().attribute(m_ColIndex).type()) { case Attribute.NUMERIC: return ContentType.DOUBLE; case Attribute.DATE: return ContentType.DATETIMEMSEC; case Attribute.NOMINAL: case Attribute.STRING: return ContentType.STRING; default: throw new IllegalStateException("Unhandled attribute type: " + Attribute.typeToString(m_Owner.getData().attribute(m_ColIndex).type())); } }
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//from ww w.ja v a 2 s .c o m * @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:adams.tools.CompareDatasets.java
License:Open Source License
/** * Before the actual run is executed./*www .ja va2 s . c om*/ */ @Override protected void preRun() { super.preRun(); if (!m_Dataset1.exists()) throw new IllegalArgumentException("Input file 1 '" + m_Dataset1 + "' does not exist?"); if (!m_Dataset2.exists()) throw new IllegalArgumentException("Input file 2 '" + m_Dataset2 + "' does not exist?"); if (m_Dataset1.isDirectory()) throw new IllegalArgumentException("Input 1 '" + m_Dataset1 + "' is a directory!"); if (m_Dataset2.isDirectory()) throw new IllegalArgumentException("Input 2 '" + m_Dataset2 + "' is a directory!"); if (m_OutputFile.isDirectory()) throw new IllegalArgumentException("Output '" + m_OutputFile + "' is pointing to a directory!"); try { m_Data1 = DataSource.read(m_Dataset1.getAbsolutePath()); m_Data2 = DataSource.read(m_Dataset2.getAbsolutePath()); } catch (Exception e) { throw new IllegalArgumentException(e); } m_Range1.setMax(m_Data1.numAttributes()); m_Range2.setMax(m_Data2.numAttributes()); if (m_Range1.getIntIndices().length != m_Range2.getIntIndices().length) throw new IllegalArgumentException("Different range of attributes: " + m_Range1.getIntIndices().length + " != " + m_Range2.getIntIndices().length); m_Indices1 = m_Range1.getIntIndices(); m_Indices2 = m_Range2.getIntIndices(); m_RowAttribute1.setMax(m_Data1.numAttributes()); m_RowAttribute2.setMax(m_Data2.numAttributes()); m_UseRowAttribute = null; m_Lookup2 = null; if (getUseRowAttribute()) { if (m_Data1.attribute(m_RowAttribute1.getIntIndex()).type() != m_Data2 .attribute(m_RowAttribute2.getIntIndex()).type()) throw new IllegalArgumentException("The attributes types of the two row attributes differ: " + Attribute.typeToString(m_Data1.attribute(m_RowAttribute1.getIntIndex())) + " != " + Attribute.typeToString(m_Data2.attribute(m_RowAttribute2.getIntIndex()))); m_RowAttributeIsString = m_Data1.attribute(m_RowAttribute1.getIntIndex()).isNominal() || m_Data1.attribute(m_RowAttribute1.getIntIndex()).isString(); } }
From source file:moa.core.VectorDistances.java
License:Apache License
/** * Average distance, which is a modification of Euclidian distance * @param src first data point to compare from * @param dst second data point to compare to * @param header data set header used to determine attribute/feature type for mixed distance * @return component-averaged Euclidian distance *//*from w w w . java 2 s . c o m*/ public static synchronized double distanceGower(double[] src, double[] dst, Instances header) { double ret = 0.0; int minSize = Math.min(src.length, Math.min(dst.length, header.numAttributes())); if (minSize < 1) { return Double.MAX_VALUE; } double wSum = 0.0; for (int i = 0; i < minSize; i++) { Attribute att = header.attribute(i); double d = 0.0; double w = header.attribute(i).weight(); if (att == null) { continue; } switch (att.type()) { case Attribute.NUMERIC: w = (src[i] == 0 || dst[i] == 0) ? 0.0 : 1.0; double sigma = Math.abs( header.attribute(i).getUpperNumericBound() - header.attribute(i).getLowerNumericBound()); d = (Double.isFinite(sigma) && sigma > 0) ? Math.abs(src[i] - dst[i]) / sigma : Math.abs(src[i] - dst[i]) / 1;//Math.max(src[i], dst[i]); break; case Attribute.NOMINAL: case Attribute.STRING: d = (src[i] == dst[i]) ? 0.0 : 1.0; break; case Attribute.DATE: case Attribute.RELATIONAL: default: System.err.println("Attribute type " + Attribute.typeToString(att) + " is not yet supported... ignoring feature " + i); d = 0.0; w = 0; } wSum += w; ret += d * d * w; } ret = (wSum > 0) ? Math.sqrt(ret / wSum) : 0.0; // Safety... if (Double.isInfinite(ret)) { ret = Double.MAX_VALUE; } else if (Double.isNaN(ret)) { ret = 0.0; } return ret; }