List of usage examples for weka.core Instances Instances
public Instances(Instances dataset)
From source file:activeSegmentation.learning.WekaDataSet.java
License:Open Source License
/** * Constructs a WekaDataset//ww w. j av a2 s . c o m * * @param arffFilePath The path to the arff file */ public WekaDataSet(String arffFilePath) { try { dataset = new Instances(new FileReader(arffFilePath)); } catch (IOException ex) { Logger.getLogger(WekaDataSet.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:activeSegmentation.learning.WekaDataSet.java
License:Open Source License
/** * Creates a Weka dataset from an Instances object * * @param dataset The weka dataset//from www.j a va2 s. c o m */ public WekaDataSet(Instances dataset) { this.dataset = new Instances(dataset); }
From source file:activeSegmentation.learning.WekaDataSet.java
License:Open Source License
/** * Creates a Weka Dataset from other Weka Dataset. * * @param dataset The dataset to use.// w w w . j a v a 2 s . c om */ public WekaDataSet(IDataSet dataset) { this.dataset = new Instances(dataset.getDataset()); }
From source file:adams.data.instancesanalysis.pls.PLS1.java
License:Open Source License
/** * Performs predictions on the data./*from ww w .j a v a 2 s . com*/ * * @param data the input data * @return the predicted data */ protected Instances predict(Instances data) { Instances result; Instances tmpInst; int i; int j; Matrix x; Matrix X; Matrix T; Matrix t; result = new Instances(getOutputFormat()); for (i = 0; i < data.numInstances(); i++) { // work on each instance tmpInst = new Instances(data, 0); tmpInst.add((Instance) data.instance(i).copy()); x = MatrixHelper.getX(tmpInst); X = new Matrix(1, getNumComponents()); T = new Matrix(1, getNumComponents()); for (j = 0; j < getNumComponents(); j++) { MatrixHelper.setVector(x, X, j); // 1. step: tj = xj * wj t = x.times(MatrixHelper.getVector(m_W, j)); MatrixHelper.setVector(t, T, j); // 2. step: xj+1 = xj - tj*pj^T (tj is 1x1 matrix!) x = x.minus(MatrixHelper.getVector(m_P, j).transpose().times(t.get(0, 0))); } switch (m_PredictionType) { case ALL: tmpInst = MatrixHelper.toInstances(getOutputFormat(), T, T.times(m_b_hat)); break; case NONE: case EXCEPT_CLASS: tmpInst = MatrixHelper.toInstances(getOutputFormat(), T, MatrixHelper.getY(tmpInst)); break; default: throw new IllegalStateException("Unhandled prediction type: " + m_PredictionType); } result.add(tmpInst.instance(0)); } return result; }
From source file:adams.data.instancesanalysis.PLS.java
License:Open Source License
/** * Performs the actual analysis./* w w w .ja v a2s. com*/ * * @param data the data to analyze * @return null if successful, otherwise error message * @throws Exception if analysis fails */ @Override protected String doAnalyze(Instances data) throws Exception { String result; Remove remove; weka.filters.supervised.attribute.PLS pls; WekaInstancesToSpreadSheet conv; SpreadSheet transformed; Matrix matrix; SpreadSheet loadings; Row row; int i; int n; m_Loadings = null; m_Scores = null; data = new Instances(data); data.deleteWithMissingClass(); if (!m_AttributeRange.isAllRange()) { if (isLoggingEnabled()) getLogger().info("Filtering attribute range: " + m_AttributeRange.getRange()); remove = new Remove(); remove.setAttributeIndicesArray(m_AttributeRange.getIntIndices()); remove.setInvertSelection(true); remove.setInputFormat(data); data = Filter.useFilter(data, remove); } if (isLoggingEnabled()) getLogger().info("Performing PLS..."); pls = new weka.filters.supervised.attribute.PLS(); pls.setAlgorithm(m_Algorithm); pls.setInputFormat(data); data = Filter.useFilter(data, pls); conv = new WekaInstancesToSpreadSheet(); conv.setInput(data); result = conv.convert(); if (result == null) { transformed = (SpreadSheet) conv.getOutput(); matrix = pls.getLoadings(); loadings = new DefaultSpreadSheet(); for (i = 0; i < matrix.getColumnDimension(); i++) loadings.getHeaderRow().addCell("L-" + (i + 1)).setContentAsString("Loading-" + (i + 1)); for (n = 0; n < matrix.getRowDimension(); n++) { row = loadings.addRow(); for (i = 0; i < matrix.getColumnDimension(); i++) row.addCell("L-" + (i + 1)).setContent(matrix.get(n, i)); } m_Loadings = loadings; m_Scores = transformed; } return result; }
From source file:adams.flow.transformer.WekaAttributeSelection.java
License:Open Source License
/** * Executes the flow item.//w w w .j av a 2s .c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances data; Instances reduced; Instances transformed; AttributeSelection eval; boolean crossValidate; int fold; Instances train; WekaAttributeSelectionContainer cont; SpreadSheet stats; int i; Row row; int[] selected; double[][] ranked; Range range; String rangeStr; boolean useReduced; result = null; try { if (m_InputToken.getPayload() instanceof Instances) data = (Instances) m_InputToken.getPayload(); else data = (Instances) ((WekaTrainTestSetContainer) m_InputToken.getPayload()) .getValue(WekaTrainTestSetContainer.VALUE_TRAIN); if (result == null) { crossValidate = (m_Folds >= 2); // setup evaluation eval = new AttributeSelection(); eval.setEvaluator(m_Evaluator); eval.setSearch(m_Search); eval.setFolds(m_Folds); eval.setSeed((int) m_Seed); eval.setXval(crossValidate); // select attributes if (crossValidate) { Random random = new Random(m_Seed); data = new Instances(data); data.randomize(random); if ((data.classIndex() > -1) && data.classAttribute().isNominal()) { if (isLoggingEnabled()) getLogger().info("Stratifying instances..."); data.stratify(m_Folds); } for (fold = 0; fold < m_Folds; fold++) { if (isLoggingEnabled()) getLogger().info("Creating splits for fold " + (fold + 1) + "..."); train = data.trainCV(m_Folds, fold, random); if (isLoggingEnabled()) getLogger().info("Selecting attributes using all but fold " + (fold + 1) + "..."); eval.selectAttributesCVSplit(train); } } else { eval.SelectAttributes(data); } // generate reduced/transformed dataset reduced = null; transformed = null; if (!crossValidate) { reduced = eval.reduceDimensionality(data); if (m_Evaluator instanceof AttributeTransformer) transformed = ((AttributeTransformer) m_Evaluator).transformedData(data); } // generated stats stats = null; if (!crossValidate) { stats = new DefaultSpreadSheet(); row = stats.getHeaderRow(); useReduced = false; if (m_Search instanceof RankedOutputSearch) { i = reduced.numAttributes(); if (reduced.classIndex() > -1) i--; ranked = eval.rankedAttributes(); useReduced = (ranked.length == i); } if (useReduced) { for (i = 0; i < reduced.numAttributes(); i++) row.addCell("" + i).setContent(reduced.attribute(i).name()); row = stats.addRow(); for (i = 0; i < reduced.numAttributes(); i++) row.addCell(i).setContent(0.0); } else { for (i = 0; i < data.numAttributes(); i++) row.addCell("" + i).setContent(data.attribute(i).name()); row = stats.addRow(); for (i = 0; i < data.numAttributes(); i++) row.addCell(i).setContent(0.0); } if (m_Search instanceof RankedOutputSearch) { ranked = eval.rankedAttributes(); for (i = 0; i < ranked.length; i++) row.getCell((int) ranked[i][0]).setContent(ranked[i][1]); } else { selected = eval.selectedAttributes(); for (i = 0; i < selected.length; i++) row.getCell(selected[i]).setContent(1.0); } } // selected attributes rangeStr = null; if (!crossValidate) { range = new Range(); range.setIndices(eval.selectedAttributes()); rangeStr = range.getRange(); } // setup container if (crossValidate) cont = new WekaAttributeSelectionContainer(data, reduced, transformed, eval, m_Seed, m_Folds); else cont = new WekaAttributeSelectionContainer(data, reduced, transformed, eval, stats, rangeStr); m_OutputToken = new Token(cont); } } catch (Exception e) { m_OutputToken = null; result = handleException("Failed to process data:", e); } return result; }
From source file:adams.flow.transformer.WekaInstancesAppend.java
License:Open Source License
/** * Executes the flow item./*from ww w. j av a 2 s.c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; String[] filesStr; File[] files; int i; int n; Instances[] inst; Instances full; String msg; StringBuilder relation; double[] values; result = null; // get filenames files = null; inst = null; if (m_InputToken.getPayload() instanceof String[]) { filesStr = (String[]) m_InputToken.getPayload(); files = new File[filesStr.length]; for (i = 0; i < filesStr.length; i++) files[i] = new PlaceholderFile(filesStr[i]); } else if (m_InputToken.getPayload() instanceof File[]) { files = (File[]) m_InputToken.getPayload(); } else if (m_InputToken.getPayload() instanceof Instances[]) { inst = (Instances[]) m_InputToken.getPayload(); } else { throw new IllegalStateException("Unhandled input type: " + m_InputToken.getPayload().getClass()); } // load data? if (files != null) { inst = new Instances[files.length]; for (i = 0; i < files.length; i++) { try { inst[i] = DataSource.read(files[i].getAbsolutePath()); } catch (Exception e) { result = handleException("Failed to load dataset: " + files[i], e); break; } } } // test compatibility if (result == null) { for (i = 0; i < inst.length - 1; i++) { for (n = i + 1; n < inst.length; n++) { if ((msg = inst[i].equalHeadersMsg(inst[n])) != null) { result = "Dataset #" + (i + 1) + " and #" + (n + 1) + " are not compatible:\n" + msg; break; } } if (result != null) break; } } // append if (result == null) { full = new Instances(inst[0]); relation = new StringBuilder(inst[0].relationName()); for (i = 1; i < inst.length; i++) { relation.append("+" + inst[i].relationName()); for (Instance row : inst[i]) { values = row.toDoubleArray(); for (n = 0; n < values.length; n++) { if (row.attribute(n).isString()) values[n] = full.attribute(n).addStringValue(row.stringValue(n)); else if (row.attribute(n).isRelationValued()) values[n] = full.attribute(n).addRelation(row.relationalValue(n)); } if (row instanceof SparseInstance) row = new SparseInstance(row.weight(), values); else row = new DenseInstance(row.weight(), values); full.add(row); } } full.setRelationName(relation.toString()); m_OutputToken = new Token(full); } return result; }
From source file:adams.flow.transformer.WekaRandomSplit.java
License:Open Source License
/** * Executes the flow item.//from ww w . j a v a2 s . c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances inst; RandomSplitGenerator generator; result = null; inst = new Instances((Instances) m_InputToken.getPayload()); try { generator = (RandomSplitGenerator) OptionUtils.shallowCopy(m_Generator); generator.setData(inst); generator.setSeed(m_Seed); generator.setPercentage(m_Percentage); generator.setPreserveOrder(m_PreserveOrder); generator.setUseViews(m_CreateView); } catch (Exception e) { generator = null; result = handleException("Failed to generate split!", e); } if (result == null) m_OutputToken = new Token(generator.next()); updateProvenance(m_OutputToken); return result; }
From source file:adams.flow.transformer.WekaSetInstancesValue.java
License:Open Source License
/** * Executes the flow item.//from ww w. java 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.WekaSubsets.java
License:Open Source License
/** * Executes the flow item.// w ww.j a v a2s . c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Instances data; Double old; Double curr; int i; int index; Instance inst; result = null; m_Queue.clear(); // copy and sort data data = new Instances((Instances) m_InputToken.getPayload()); m_Index.setData(data); ; index = m_Index.getIntIndex(); data.sort(index); // create subsets old = null; i = 0; while (i < data.numInstances()) { inst = data.instance(i); curr = inst.value(index); if ((old == null) || !curr.equals(old)) { m_Queue.add(new Instances(data, data.numInstances())); old = curr; } m_Queue.get(m_Queue.size() - 1).add(inst); i++; } // compact subsets for (Instances sub : m_Queue) sub.compactify(); return result; }