List of usage examples for weka.core DenseInstance DenseInstance
public DenseInstance(double weight, double[] attValues)
From source file:PrincipalComponents.java
License:Open Source License
/** * Convert a pc transformed instance back to the original space * * @param inst the instance to convert//from ww w. java 2s . c o m * @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 DenseInstance(inst.weight(), newVals); } }
From source file:PrincipalComponents.java
License:Open Source License
/** * Transform an instance in original (unormalized) format. Convert back to * the original space if requested./*w ww . j ava 2 s . c o m*/ * * @param instance an instance in the original (unormalized) format * @return a transformed instance * @throws Exception if instance cant be transformed */ @Override public Instance convertInstance(Instance instance) throws Exception { if (m_eigenvalues == null) { throw new Exception("convertInstance: Principal components not " + "built yet"); } double[] newVals = new double[m_outputNumAtts]; Instance tempInst = (Instance) instance.copy(); if (!instance.dataset().equalHeaders(m_trainHeader)) { throw new Exception("Can't convert instance: header's don't match: " + "PrincipalComponents\n" + instance.dataset().equalHeadersMsg(m_trainHeader)); } m_replaceMissingFilter.input(tempInst); m_replaceMissingFilter.batchFinished(); tempInst = m_replaceMissingFilter.output(); /* * if (m_normalize) { m_normalizeFilter.input(tempInst); * m_normalizeFilter.batchFinished(); tempInst = * m_normalizeFilter.output(); } */ m_nominalToBinFilter.input(tempInst); m_nominalToBinFilter.batchFinished(); tempInst = m_nominalToBinFilter.output(); if (m_attributeFilter != null) { m_attributeFilter.input(tempInst); m_attributeFilter.batchFinished(); tempInst = m_attributeFilter.output(); } if (!m_center) { m_standardizeFilter.input(tempInst); m_standardizeFilter.batchFinished(); tempInst = m_standardizeFilter.output(); } else { m_centerFilter.input(tempInst); m_centerFilter.batchFinished(); tempInst = m_centerFilter.output(); } if (m_hasClass) { newVals[m_outputNumAtts - 1] = instance.value(instance.classIndex()); } double cumulative = 0; int numAttAdded = 0; for (int i = m_numAttribs - 1; i >= 0; i--) { double tempval = 0.0; for (int j = 0; j < m_numAttribs; j++) { tempval += (m_eigenvectors[j][m_sortedEigens[i]] * tempInst.value(j)); } newVals[m_numAttribs - i - 1] = tempval; cumulative += m_eigenvalues[m_sortedEigens[i]]; if ((cumulative / m_sumOfEigenValues) >= m_coverVariance) { break; } if (numAttAdded > m_maxNumAttr) { break; } numAttAdded++; } if (!m_transBackToOriginal) { if (instance instanceof SparseInstance) { return new SparseInstance(instance.weight(), newVals); } else { return new DenseInstance(instance.weight(), newVals); } } else { if (instance instanceof SparseInstance) { return convertInstanceToOriginal(new SparseInstance(instance.weight(), newVals)); } else { return convertInstanceToOriginal(new DenseInstance(instance.weight(), newVals)); } } }
From source file:aaa.util.test.CreateArff.java
License:Open Source License
/** * Generates the Instances object and outputs it in ARFF format to stdout. * * @param args ignored//from ww w . j a va 2 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); }
From source file:activeSegmentation.filterImpl.FilterUtil.java
License:Open Source License
/** * Create instance (feature vector) of a specific coordinate * /*w w w .j av a 2 s . c o m*/ * @param x x- axis coordinate * @param y y- axis coordinate * @param classValue class value to be assigned * @return corresponding instance */ public DenseInstance createInstance(int x, int y, int classValue, ImageStack stack, boolean colorFeatures, boolean oldColorFormat) { final int size = stack.getSize(); final double[] values = new double[size + 1]; int n = 0; if (colorFeatures == false || oldColorFormat == true) { for (int z = 0; z < size; z++, n++) values[z] = stack.getVoxel(x, y, z); } else { for (int z = 0; z < size; z++, n++) { int c = (int) stack.getVoxel(x, y, z); int r = (c & 0xff0000) >> 16; int g = (c & 0xff00) >> 8; int b = c & 0xff; values[z] = (r + g + b) / 3.0; } } // Assign class values[values.length - 1] = (double) classValue; return new DenseInstance(1.0, values); }
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//from w w w.j a v a 2s. c o 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.data.conversion.ReportToWekaInstance.java
License:Open Source License
/** * Performs the actual conversion./*from www . j a v a 2s. c o m*/ * * @return the converted data * @throws Exception if something goes wrong with the conversion */ protected Object doConvert() throws Exception { Report report; Instance result; ArrayList atts; ArrayList attValues; int i; double[] values; report = (Report) m_Input; // generate header if (m_Header == null) { atts = new ArrayList(); for (i = 0; i < m_Fields.length; i++) { switch (m_Fields[i].getDataType()) { case NUMERIC: atts.add(new Attribute(m_Fields[i].getName())); break; case BOOLEAN: attValues = new ArrayList(); attValues.add("false"); attValues.add("true"); atts.add(new Attribute(m_Fields[i].getName(), attValues)); break; default: atts.add(new Attribute(m_Fields[i].getName(), (List) null)); break; } } m_Header = new Instances(getClass().getName(), atts, 0); } // generate instance values = new double[m_Header.numAttributes()]; for (i = 0; i < m_Fields.length; i++) { if (report.hasValue(m_Fields[i])) { switch (m_Fields[i].getDataType()) { case NUMERIC: values[i] = report.getDoubleValue(m_Fields[i]); break; case BOOLEAN: if (report.getBooleanValue(m_Fields[i])) values[i] = 1; else values[i] = 0; break; default: values[i] = m_Header.attribute(i).addStringValue("" + report.getValue(m_Fields[i])); break; } } else { values[i] = weka.core.Utils.missingValue(); } } result = new DenseInstance(1.0, values); result.setDataset(m_Header); return result; }
From source file:adams.data.conversion.SpreadSheetToWekaInstances.java
License:Open Source License
/** * Performs the actual conversion.// w w w. j a v a 2 s. c om * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Instances result; SpreadSheet sheet; DenseInstance inst; ArrayList<Attribute> atts; HashSet<String> unique; ArrayList<String> labels; Row row; Cell cell; int i; int n; double[] values; Collection<ContentType> types; ContentType type; boolean added; int[] classIndices; sheet = (SpreadSheet) m_Input; // create header atts = new ArrayList<>(); for (i = 0; i < sheet.getColumnCount(); i++) { added = false; types = sheet.getContentTypes(i); if (types.contains(ContentType.DOUBLE)) types.remove(ContentType.LONG); if (types.contains(ContentType.LONG)) { types.add(ContentType.DOUBLE); types.remove(ContentType.LONG); } if (types.size() == 1) { type = (ContentType) types.toArray()[0]; if (type == ContentType.DOUBLE) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent())); added = true; } else if (type == ContentType.DATE) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIMESTAMP_FORMAT)); added = true; } else if (type == ContentType.TIME) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIME_FORMAT)); added = true; } } if (!added) { unique = new HashSet<>(); for (n = 0; n < sheet.getRowCount(); n++) { row = sheet.getRow(n); cell = row.getCell(i); if ((cell != null) && !cell.isMissing()) unique.add(cell.getContent()); } if ((unique.size() > m_MaxLabels) || (m_MaxLabels < 1)) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), (FastVector) null)); } else { labels = new ArrayList<>(unique); Collections.sort(labels); atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), labels)); } } } result = new Instances(Environment.getInstance().getProject(), atts, sheet.getRowCount()); if (sheet.hasName()) result.setRelationName(sheet.getName()); // add data for (n = 0; n < sheet.getRowCount(); n++) { row = sheet.getRow(n); values = new double[result.numAttributes()]; for (i = 0; i < result.numAttributes(); i++) { cell = row.getCell(i); values[i] = weka.core.Utils.missingValue(); if ((cell != null) && !cell.isMissing()) { if (result.attribute(i).type() == Attribute.DATE) { if (cell.isTime()) values[i] = cell.toTime().getTime(); else values[i] = cell.toDate().getTime(); } else if (result.attribute(i).isNumeric()) { values[i] = Utils.toDouble(cell.getContent()); } else if (result.attribute(i).isString()) { values[i] = result.attribute(i).addStringValue(cell.getContent()); } else { values[i] = result.attribute(i).indexOfValue(cell.getContent()); } } } inst = new DenseInstance(1.0, values); result.add(inst); } if (sheet instanceof Dataset) { classIndices = ((Dataset) sheet).getClassAttributeIndices(); if (classIndices.length > 0) result.setClassIndex(classIndices[0]); } return result; }
From source file:adams.data.conversion.TimeseriesToWekaInstances.java
License:Open Source License
/** * Performs the actual conversion./*from w ww. j a va 2 s .com*/ * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Instances result; ArrayList<Attribute> atts; Instance inst; Timeseries series; TimeseriesPoint point; double[] value; series = (Timeseries) m_Input; atts = new ArrayList<Attribute>(); atts.add(new Attribute("Timestamp", m_Format.getValue())); atts.add(new Attribute("Value")); result = new Instances(series.getID(), atts, series.size()); for (Object obj : series.toList()) { point = (TimeseriesPoint) obj; value = new double[2]; value[0] = point.getTimestamp().getTime(); value[1] = point.getValue(); inst = new DenseInstance(1.0, value); result.add(inst); } return result; }
From source file:adams.data.featureconverter.Weka.java
License:Open Source License
/** * Performs the actual generation of a row from the raw data. * /* w w w . ja v a 2 s . co m*/ * @param data the data of the row, elements can be null (= missing) * @return the dataset structure */ @Override protected Instance doGenerateRow(List<Object> data) { Instance result; int i; Object obj; double[] values; values = new double[m_Header.numAttributes()]; for (i = 0; i < data.size(); i++) { obj = data.get(i); if (obj == null) { values[i] = Utils.missingValue(); continue; } switch (m_HeaderDefinition.getType(i)) { case BOOLEAN: values[i] = ((Boolean) obj) ? 0.0 : 1.0; break; case NUMERIC: values[i] = ((Number) obj).doubleValue(); break; case STRING: case UNKNOWN: values[i] = m_Header.attribute(i).addStringValue(obj.toString()); break; } } result = new DenseInstance(1.0, values); result.setDataset(m_Header); return result; }
From source file:adams.data.instances.AbstractInstanceGenerator.java
License:Open Source License
/** * For adding IDs, notes, additional fields to the data. * * @param data the input data/*from w w w .j av a 2s. c o m*/ * @param inst the generated instance * @return the processed instance */ protected Instance postProcessOutput(T data, Instance inst) { Instance result; double[] values; int index; Report report; values = inst.toDoubleArray(); report = data.getReport(); if (m_AddDatabaseID) { index = m_OutputHeader.attribute(ArffUtils.getDBIDName()).index(); values[index] = report.getDatabaseID(); } result = new DenseInstance(1.0, values); result.setDataset(m_OutputHeader); return result; }