List of usage examples for weka.filters.unsupervised.attribute Reorder Reorder
Reorder
From source file:adams.flow.transformer.WekaMultiLabelSplitter.java
License:Open Source License
/** * Returns the generated token.//from w ww . j a v a 2 s . c om * * @return the generated token */ @Override public Token output() { Token result; int index; Remove remove; Reorder reorder; StringBuilder indices; int i; int newIndex; Instances processed; result = null; index = m_AttributesToProcess.remove(0); remove = new Remove(); indices = new StringBuilder(); for (i = 0; i < m_ClassAttributes.size(); i++) { if (m_ClassAttributes.get(i) == index) continue; if (indices.length() > 0) indices.append(","); indices.append("" + (m_ClassAttributes.get(i) + 1)); } remove.setAttributeIndices(indices.toString()); try { remove.setInputFormat(m_Dataset); processed = weka.filters.Filter.useFilter(m_Dataset, remove); if (m_UpdateRelationName) processed.setRelationName(m_Dataset.attribute(index).name()); result = new Token(processed); } catch (Exception e) { processed = null; handleException( "Failed to process dataset with following filter setup:\n" + OptionUtils.getCommandLine(remove), e); } if (m_MakeClassLast && (processed != null)) { newIndex = processed.attribute(m_Dataset.attribute(index).name()).index(); indices = new StringBuilder(); for (i = 0; i < processed.numAttributes(); i++) { if (i == newIndex) continue; if (indices.length() > 0) indices.append(","); indices.append("" + (i + 1)); } if (indices.length() > 0) indices.append(","); indices.append("" + (newIndex + 1)); reorder = new Reorder(); try { reorder.setAttributeIndices(indices.toString()); reorder.setInputFormat(processed); processed = weka.filters.Filter.useFilter(processed, reorder); if (m_UpdateRelationName) processed.setRelationName(m_Dataset.attribute(index).name()); result = new Token(processed); } catch (Exception e) { handleException("Failed to process dataset with following filter setup:\n" + OptionUtils.getCommandLine(reorder), e); } } return result; }
From source file:adams.flow.transformer.WekaReorderAttributesToReference.java
License:Open Source License
/** * Executes the flow item./* ww w. j a va2s. 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.instances.InstancesTableModel.java
License:Open Source License
/** * sets the attribute at the given col index as the new class attribute, i.e. * it moves it to the end of the attributes * * @param columnIndex the index of the column *//*from w w w . ja v a 2s . co m*/ public void attributeAsClassAt(int columnIndex) { Reorder reorder; StringBuilder order; int i; int offset; offset = 1; if (m_ShowWeightsColumn) offset++; if ((columnIndex >= offset) && (columnIndex < getColumnCount())) { addUndoPoint(); try { // build order string (1-based!) order = new StringBuilder(); for (i = 1; i < m_Data.numAttributes() + 1; i++) { // skip new class if (i + offset - 1 == columnIndex) continue; if (order.length() != 0) order.append(","); order.append(Integer.toString(i)); } if (order.length() != 0) order.append(","); order.append(Integer.toString(columnIndex - offset + 1)); // process data reorder = new Reorder(); reorder.setAttributeIndices(order.toString()); reorder.setInputFormat(m_Data); m_Data = Filter.useFilter(m_Data, reorder); // set class index m_Data.setClassIndex(m_Data.numAttributes() - 1); } catch (Exception e) { ConsolePanel.getSingleton().append(Level.SEVERE, "Failed to apply reorder filter!", e); undo(); } notifyListener(new TableModelEvent(this, TableModelEvent.HEADER_ROW)); } }
From source file:ffnn.FFNN.java
public static Instances preprocess(Instances i) { try {//from w w w. java2 s. c o m Reorder rfilter = new Reorder(); int classIdx = i.classIndex() + 1; String order; if (classIdx != 1) { order = "1"; for (int j = 2; j <= i.numAttributes(); j++) { if (j != classIdx) { order = order + "," + j; } } } else { order = "2"; for (int j = 3; j <= i.numAttributes(); j++) { order = order + "," + j; } } order = order + "," + classIdx; rfilter.setAttributeIndices(order); rfilter.setInputFormat(i); i = Filter.useFilter(i, rfilter); StringToNominal stnfilter = new StringToNominal(); stnfilter.setAttributeRange("first-last"); stnfilter.setInputFormat(i); i = Filter.useFilter(i, stnfilter); NominalToBinary ntbfilter = new NominalToBinary(); ntbfilter.setInputFormat(i); i = Filter.useFilter(i, ntbfilter); Normalize nfilter = new Normalize(); nfilter.setInputFormat(i); i = Filter.useFilter(i, nfilter); } catch (Exception e) { System.out.println(e.toString()); } return i; }
From source file:meka.gui.dataviewer.DataTableModel.java
License:Open Source License
/** * sets the attribute at the given col index as the new class attribute, i.e. * it moves it to the end of the attributes * * @param columnIndex the index of the column *//*from w ww . ja v a2s .c o m*/ public void attributeAsClassAt(int columnIndex) { Reorder reorder; String order; int i; if ((columnIndex > 0) && (columnIndex < getColumnCount())) { addUndoPoint(); try { // build order string (1-based!) order = ""; for (i = 1; i < m_Data.numAttributes() + 1; i++) { // skip new class if (i == columnIndex) { continue; } if (!order.equals("")) { order += ","; } order += Integer.toString(i); } if (!order.equals("")) { order += ","; } order += Integer.toString(columnIndex); // process data reorder = new Reorder(); reorder.setAttributeIndices(order); reorder.setInputFormat(m_Data); m_Data = Filter.useFilter(m_Data, reorder); // set class index m_Data.setClassIndex(m_Data.numAttributes() - 1); } catch (Exception e) { e.printStackTrace(); undo(); } notifyListener(new TableModelEvent(this, TableModelEvent.HEADER_ROW)); } }
From source file:mulan.classifier.transformation.CalibratedLabelRanking.java
License:Open Source License
@Override protected void buildInternal(MultiLabelInstances trainingSet) throws Exception { // Virtual label models debug("Building calibration label models"); System.out.println("Building calibration label models"); virtualLabelModels = new BinaryRelevance(getBaseClassifier()); virtualLabelModels.setDebug(getDebug()); virtualLabelModels.build(trainingSet); // One-vs-one models numModels = ((numLabels) * (numLabels - 1)) / 2; oneVsOneModels = AbstractClassifier.makeCopies(getBaseClassifier(), numModels); nodata = new boolean[numModels]; metaDataTest = new Instances[numModels]; Instances trainingData = trainingSet.getDataSet(); int counter = 0; // Creation of one-vs-one models for (int label1 = 0; label1 < numLabels - 1; label1++) { // Attribute of label 1 Attribute attrLabel1 = trainingData.attribute(labelIndices[label1]); for (int label2 = label1 + 1; label2 < numLabels; label2++) { debug("Building one-vs-one model " + (counter + 1) + "/" + numModels); System.out.println("Building one-vs-one model " + (counter + 1) + "/" + numModels); // Attribute of label 2 Attribute attrLabel2 = trainingData.attribute(labelIndices[label2]); // initialize training set Instances dataOneVsOne = new Instances(trainingData, 0); // filter out examples with no preference for (int i = 0; i < trainingData.numInstances(); i++) { Instance tempInstance;/*from ww w . j a va 2s . c o m*/ if (trainingData.instance(i) instanceof SparseInstance) { tempInstance = new SparseInstance(trainingData.instance(i)); } else { tempInstance = new DenseInstance(trainingData.instance(i)); } int nominalValueIndex; nominalValueIndex = (int) tempInstance.value(labelIndices[label1]); String value1 = attrLabel1.value(nominalValueIndex); nominalValueIndex = (int) tempInstance.value(labelIndices[label2]); String value2 = attrLabel2.value(nominalValueIndex); if (!value1.equals(value2)) { tempInstance.setValue(attrLabel1, value1); dataOneVsOne.add(tempInstance); } } // remove all labels apart from label1 and place it at the end Reorder filter = new Reorder(); int numPredictors = trainingData.numAttributes() - numLabels; int[] reorderedIndices = new int[numPredictors + 1]; for (int i = 0; i < numPredictors; i++) { reorderedIndices[i] = featureIndices[i]; } reorderedIndices[numPredictors] = labelIndices[label1]; filter.setAttributeIndicesArray(reorderedIndices); filter.setInputFormat(dataOneVsOne); dataOneVsOne = Filter.useFilter(dataOneVsOne, filter); //System.out.println(dataOneVsOne.toString()); dataOneVsOne.setClassIndex(numPredictors); // build model label1 vs label2 if (dataOneVsOne.size() > 0) { oneVsOneModels[counter].buildClassifier(dataOneVsOne); } else { nodata[counter] = true; } dataOneVsOne.delete(); metaDataTest[counter] = dataOneVsOne; counter++; } } }
From source file:mulan.classifier.transformation.TwoStageClassifierChainArchitecture.java
License:Open Source License
@Override protected void buildInternal(MultiLabelInstances trainingSet) throws Exception { // Virtual label models debug("Building calibration label models"); virtualLabelModels = new BinaryRelevance(getBaseClassifier()); virtualLabelModels.setDebug(getDebug()); virtualLabelModels.build(trainingSet); //Generate the chain: Test the same dataset MultiLabelInstances tempTrainingSet = GenerateChain(trainingSet); labelIndices = tempTrainingSet.getLabelIndices(); featureIndices = tempTrainingSet.getFeatureIndices(); // One-vs-one models numModels = ((numLabels) * (numLabels - 1)) / 2; oneVsOneModels = AbstractClassifier.makeCopies(getBaseClassifier(), numModels); nodata = new boolean[numModels]; metaDataTest = new Instances[numModels]; Instances trainingData = tempTrainingSet.getDataSet(); int counter = 0; // Creation of one-vs-one models for (int label1 = 0; label1 < numLabels - 1; label1++) { // Attribute of label 1 Attribute attrLabel1 = trainingData.attribute(labelIndices[label1]); for (int label2 = label1 + 1; label2 < numLabels; label2++) { debug("Building one-vs-one model " + (counter + 1) + "/" + numModels); // Attribute of label 2 Attribute attrLabel2 = trainingData.attribute(labelIndices[label2]); // initialize training set Instances dataOneVsOne = new Instances(trainingData, 0); // filter out examples with no preference for (int i = 0; i < trainingData.numInstances(); i++) { Instance tempInstance;/* w ww .j av a2s . c o m*/ if (trainingData.instance(i) instanceof SparseInstance) { tempInstance = new SparseInstance(trainingData.instance(i)); } else { tempInstance = new DenseInstance(trainingData.instance(i)); } int nominalValueIndex; nominalValueIndex = (int) tempInstance.value(labelIndices[label1]); String value1 = attrLabel1.value(nominalValueIndex); nominalValueIndex = (int) tempInstance.value(labelIndices[label2]); String value2 = attrLabel2.value(nominalValueIndex); if (!value1.equals(value2)) { tempInstance.setValue(attrLabel1, value1); dataOneVsOne.add(tempInstance); } } // remove all labels apart from label1 and place it at the end Reorder filter = new Reorder(); int numPredictors = trainingData.numAttributes() - numLabels; int[] reorderedIndices = new int[numPredictors + 1]; System.arraycopy(featureIndices, 0, reorderedIndices, 0, numPredictors); reorderedIndices[numPredictors] = labelIndices[label1]; filter.setAttributeIndicesArray(reorderedIndices); filter.setInputFormat(dataOneVsOne); dataOneVsOne = Filter.useFilter(dataOneVsOne, filter); //System.out.println(dataOneVsOne.toString()); dataOneVsOne.setClassIndex(numPredictors); // build model label1 vs label2 if (dataOneVsOne.size() > 0) { oneVsOneModels[counter].buildClassifier(dataOneVsOne); } else { nodata[counter] = true; } dataOneVsOne.delete(); metaDataTest[counter] = dataOneVsOne; counter++; } } }
From source file:mulan.classifier.transformation.TwoStagePrunedClassifierChainArchitecture.java
License:Open Source License
@Override protected void buildInternal(MultiLabelInstances trainingSet) throws Exception { // Virtual label models debug("Building calibration label models"); virtualLabelModels = new BinaryRelevance(getBaseClassifier()); virtualLabelModels.setDebug(getDebug()); virtualLabelModels.build(trainingSet); // One-vs-one models numModels = ((numLabels) * (numLabels - 1)) / 2; oneVsOneModels = AbstractClassifier.makeCopies(getBaseClassifier(), numModels); nodata = new boolean[numModels]; metaDataTest = new Instances[numModels]; ArrayList<MultiLabelOutput> predictions; predictions = predictLabels(trainingSet); int counter = 0; // Creation of one-vs-one models for (int label1 = 0; label1 < numLabels - 1; label1++) { for (int label2 = label1 + 1; label2 < numLabels; label2++) { //Generate the chain: Test the same dataset MultiLabelInstances tempTrainingSet = GenerateChain(trainingSet, label1, label2, predictions); Instances trainingData = tempTrainingSet.getDataSet(); labelIndices = tempTrainingSet.getLabelIndices(); featureIndices = tempTrainingSet.getFeatureIndices(); // Attribute of label 1 Attribute attrLabel1 = trainingData.attribute(labelIndices[label1]); debug("Building one-vs-one model " + (counter + 1) + "/" + numModels); // Attribute of label 2 Attribute attrLabel2 = trainingData.attribute(labelIndices[label2]); // initialize training set Instances dataOneVsOne = new Instances(trainingData, 0); // filter out examples with no preference for (int i = 0; i < trainingData.numInstances(); i++) { Instance tempInstance;//from w w w . j a v a2 s.c o m if (trainingData.instance(i) instanceof SparseInstance) { tempInstance = new SparseInstance(trainingData.instance(i)); } else { tempInstance = new DenseInstance(trainingData.instance(i)); } int nominalValueIndex; nominalValueIndex = (int) tempInstance.value(labelIndices[label1]); String value1 = attrLabel1.value(nominalValueIndex); nominalValueIndex = (int) tempInstance.value(labelIndices[label2]); String value2 = attrLabel2.value(nominalValueIndex); if (!value1.equals(value2)) { tempInstance.setValue(attrLabel1, value1); dataOneVsOne.add(tempInstance); } } // remove all labels apart from label1 and place it at the end Reorder filter = new Reorder(); int numPredictors = trainingData.numAttributes() - numLabels; int[] reorderedIndices = new int[numPredictors + 1]; System.arraycopy(featureIndices, 0, reorderedIndices, 0, numPredictors); reorderedIndices[numPredictors] = labelIndices[label1]; filter.setAttributeIndicesArray(reorderedIndices); filter.setInputFormat(dataOneVsOne); dataOneVsOne = Filter.useFilter(dataOneVsOne, filter); //System.out.println(dataOneVsOne.toString()); dataOneVsOne.setClassIndex(numPredictors); // build model label1 vs label2 if (dataOneVsOne.size() > 0) { oneVsOneModels[counter].buildClassifier(dataOneVsOne); } else { nodata[counter] = true; } dataOneVsOne.delete(); metaDataTest[counter] = dataOneVsOne; counter++; } } }
From source file:wekimini.DataManager.java
private void updateFiltersForOutput(int output) throws Exception { Reorder r = new Reorder(); Reorder s = new Reorder(); int[] inputList = inputListsForOutputs.get(output); //includes only "selected" inputs int[] reordering = new int[inputList.length + 1]; int[] saving = new int[numMetaData + inputList.length + 1]; //Metadata/*from w w w .j av a2 s . c o m*/ for (int f = 0; f < numMetaData; f++) { saving[f] = f; } //Features for (int f = 0; f < inputList.length; f++) { reordering[f] = inputList[f] + numMetaData; saving[f + numMetaData] = inputList[f] + numMetaData; } //The actual "class" output reordering[reordering.length - 1] = numMetaData + numInputs + output; saving[saving.length - 1] = numMetaData + numInputs + output; r.setAttributeIndicesArray(reordering); r.setInputFormat(dummyInstances); s.setAttributeIndicesArray(saving); s.setInputFormat(dummyInstances); outputFilters[output] = r; savingFilters[output] = s; }
From source file:wekimini.DataManager.java
private void setupFilters() throws Exception { outputFilters = new Reorder[numOutputs]; savingFilters = new Reorder[numOutputs]; for (int i = 0; i < numOutputs; i++) { Reorder r = new Reorder(); Reorder s = new Reorder(); int[] inputList = inputListsForOutputs.get(i); int[] reordering = new int[inputList.length + 1]; int[] saving = new int[numMetaData + inputList.length + 1]; //Metadata for (int f = 0; f < numMetaData; f++) { saving[f] = f;/*from ww w . j a va 2 s . com*/ } //Features for (int f = 0; f < inputList.length; f++) { reordering[f] = inputList[f] + numMetaData; saving[f + numMetaData] = inputList[f] + numMetaData; } //The actual "class" output reordering[reordering.length - 1] = numMetaData + numInputs + i; saving[saving.length - 1] = numMetaData + numInputs + i; r.setAttributeIndicesArray(reordering); r.setInputFormat(dummyInstances); s.setAttributeIndicesArray(saving); s.setInputFormat(dummyInstances); outputFilters[i] = r; savingFilters[i] = s; } }