List of usage examples for weka.filters.unsupervised.attribute Reorder setInputFormat
@Override public boolean setInputFormat(Instances instanceInfo) throws Exception
From source file:adams.flow.transformer.WekaMultiLabelSplitter.java
License:Open Source License
/** * Returns the generated token.//from ww w. j a va 2s.c o m * * @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.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 */// w w w .jav a 2s . c o 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 {//w ww . j a va2 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 */// w w w . j a v a2 s . 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;/* ww w .j av a2s . c om*/ 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;// www. j a v a 2 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: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.ja v a 2 s.com*/ 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 ww w . j av a 2 s . com 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;/* w ww . j ava 2 s .c o m*/ } //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; } }
From source file:wekimini.InputGenerator.java
public void buildDataset() throws Exception { int storedNumAttributes = storedInputs.numAttributes(); String storedString = Integer.toString(storedNumAttributes); logger.log(Level.SEVERE, "storedInputs number attributes:{0}", storedString); int storedNumInstances = storedInputs.numInstances(); String storedString2 = Integer.toString(storedNumInstances); logger.log(Level.SEVERE, "storedInputs number instances:{0}", storedString2); Reorder r = new Reorder(); int[] reordering = new int[numInputs]; for (int i = 0; i < numInputs; i++) { reordering[i] = i + numMetaData; }// w ww . ja v a 2 s . c om r.setAttributeIndicesArray(reordering); r.setInputFormat(storedInputs); dataset = Filter.useFilter(storedInputs, r); int datasetNumAttributes = dataset.numAttributes(); String datasetString = Integer.toString(datasetNumAttributes); logger.log(Level.SEVERE, "Dataset number attributes:{0}", datasetString); int datasetNumInstances = dataset.numInstances(); String datasetString2 = Integer.toString(datasetNumInstances); logger.log(Level.SEVERE, "Dataset number instances:{0}", datasetString2); }