@Override public void setRelationName(String newName) { super.setRelationName(newName); }
/** * Apply the change to the relation name in the given Instances object * * @param insts the Instances object to operate on */ protected void applyRelationNameChange(Instances insts) { switch (m_modType) { case REPLACE: insts.setRelationName(m_relationNameModText); break; case APPEND: insts.setRelationName(insts.relationName() + m_relationNameModText); break; case PREPEND: insts.setRelationName(m_relationNameModText + insts.relationName()); break; case REGEX: String rel = insts.relationName(); if (m_replaceAll) { rel = m_regexPattern.matcher(rel).replaceAll(m_relationNameModText); } else { rel = m_regexPattern.matcher(rel).replaceFirst(m_relationNameModText); } insts.setRelationName(rel); break; } }
/** * Fixes the relation name by adding the "-C" attribute to it if necessary. * * @param data the dataset to fix * @param numClassAtts the number of class attributes (0 for none, >0 for attributes at start, <0 for attributes at end) */ public static void fixRelationName(Instances data, int numClassAtts) { if (data.relationName().indexOf(":") == -1) data.setRelationName(data.relationName() + ": -C " + numClassAtts); }
/** * Fixes the relation name by adding the "-C" attribute to it if necessary. * * @param data the dataset to fix * @param numClassAtts the number of class attributes (0 for none, >0 for attributes at start, <0 for attributes at end) */ public static void fixRelationName(Instances data, int numClassAtts) { if (data.relationName().indexOf(":") == -1) data.setRelationName(data.relationName() + ": -C " + numClassAtts); }
/** * Sets the format of output instances. The derived class should use this * method once it has determined the outputformat. The output queue is * cleared. * * @param outputFormat the new output format */ protected void setOutputFormat(Instances outputFormat) { if (outputFormat != null) { m_OutputFormat = outputFormat.stringFreeStructure(); initOutputLocators(m_OutputFormat, null); // Rename the relation String relationName = outputFormat.relationName() + "-" + this.getClass().getName(); if (this instanceof OptionHandler) { String[] options = ((OptionHandler) this).getOptions(); for (String option : options) { relationName += option.trim(); } } m_OutputFormat.setRelationName(relationName); } else { m_OutputFormat = null; } m_OutputQueue = new Queue(); }
@Override public void run() { try { String relName = getData().relationName(); filter.setInputFormat(getData()); Instances filtered = Filter.useFilter(getData(), filter); if (newName == null) filtered.setRelationName(relName); else filtered.setRelationName(newName); getOwner().notifyTabsDataChanged(PreprocessTab.this, filtered); setData(filtered); } catch (Exception e) { throw new IllegalStateException(e); } } };
@Override public void run() { try { String relName = getData().relationName(); filter.setInputFormat(getData()); Instances filtered = Filter.useFilter(getData(), filter); if (newName == null) filtered.setRelationName(relName); else filtered.setRelationName(newName); getOwner().notifyTabsDataChanged(PreprocessTab.this, filtered); setData(filtered); log("Data filtered successfully!"); } catch (Exception e) { throw new IllegalStateException(e); } } };
@Override public void run() { try { String relName = getData().relationName(); filter.setInputFormat(getData()); Instances filtered = Filter.useFilter(getData(), filter); if (newName == null) filtered.setRelationName(relName); else filtered.setRelationName(newName); getOwner().notifyTabsDataChanged(PreprocessTab.this, filtered); setData(filtered); log("Data filtered successfully!"); } catch (Exception e) { throw new IllegalStateException(e); } } };
private Instances makeDataSetProbabilities(Instances insts, Instances format, weka.classifiers.Classifier classifier, String relationNameModifier) throws Exception { // adjust structure for InputMappedClassifier (if necessary) if (classifier instanceof weka.classifiers.misc.InputMappedClassifier) { format = ((weka.classifiers.misc.InputMappedClassifier) classifier) .getModelHeader(new Instances(format, 0)); } String classifierName = classifier.getClass().getName(); classifierName = classifierName.substring(classifierName.lastIndexOf('.') + 1, classifierName.length()); Instances newInstances = new Instances(insts); for (int i = 0; i < format.classAttribute().numValues(); i++) { weka.filters.unsupervised.attribute.Add addF = new weka.filters.unsupervised.attribute.Add(); addF.setAttributeIndex("last"); addF.setAttributeName(classifierName + "_prob_" + format.classAttribute().value(i)); addF.setInputFormat(newInstances); newInstances = weka.filters.Filter.useFilter(newInstances, addF); } newInstances.setRelationName(insts.relationName() + relationNameModifier); return newInstances; }
Instances data = null; try { DatabaseUtils utils = new DatabaseUtils(); InstanceQuery query = new InstanceQuery(); query.setUsername(username); query.setPassword(password); query.setQuery(pSql); data = query.retrieveInstances(); data.setRelationName(pInstanceRelationName); if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } // this line will print out your header + data. // add your header information to your question. System.out.println(data) } catch (Exception e) { throw new RuntimeException(e); }
for (int i = 0; i < classes.length; i++) { classes[i] = new Instances(insts, 0); classes[i].setRelationName(insts.attribute(classIndex).value(i));
private Instances makeClusterDataSetProbabilities(Instances format, weka.clusterers.Clusterer clusterer, String relationNameModifier) throws Exception { Instances newInstances = new Instances(format); for (int i = 0; i < clusterer.numberOfClusters(); i++) { weka.filters.unsupervised.attribute.Add addF = new weka.filters.unsupervised.attribute.Add(); addF.setAttributeIndex("last"); addF.setAttributeName("prob_cluster" + i); addF.setInputFormat(newInstances); newInstances = weka.filters.Filter.useFilter(newInstances, addF); } newInstances.setRelationName(format.relationName() + relationNameModifier); return newInstances; }
private Instances makeClusterDataSetProbabilities(Instances format, weka.clusterers.Clusterer clusterer, String relationNameModifier) throws Exception { Instances newInstances = new Instances(format); for (int i = 0; i < clusterer.numberOfClusters(); i++) { weka.filters.unsupervised.attribute.Add addF = new weka.filters.unsupervised.attribute.Add(); addF.setAttributeIndex("last"); addF.setAttributeName("prob_cluster" + i); addF.setInputFormat(newInstances); newInstances = weka.filters.Filter.useFilter(newInstances, addF); } newInstances.setRelationName(format.relationName() + relationNameModifier); return newInstances; }
/** * Adds an instance number attribute to the plottable instances, */ public void addInstanceNumberAttribute() { String originalRelationName = m_plotInstances.relationName(); int originalClassIndex = m_plotInstances.classIndex(); try { Add addF = new Add(); addF.setAttributeName("Instance_number"); addF.setAttributeIndex("first"); addF.setInputFormat(m_plotInstances); m_plotInstances = Filter.useFilter(m_plotInstances, addF); m_plotInstances.setClassIndex(originalClassIndex + 1); for (int i = 0; i < m_plotInstances.numInstances(); i++) { m_plotInstances.instance(i).setValue(0, i); } m_plotInstances.setRelationName(originalRelationName); } catch (Exception ex) { ex.printStackTrace(); } }
/** * Adds an instance number attribute to the plottable instances, */ public void addInstanceNumberAttribute() { String originalRelationName = m_plotInstances.relationName(); int originalClassIndex = m_plotInstances.classIndex(); try { Add addF = new Add(); addF.setAttributeName("Instance_number"); addF.setAttributeIndex("first"); addF.setInputFormat(m_plotInstances); m_plotInstances = Filter.useFilter(m_plotInstances, addF); m_plotInstances.setClassIndex(originalClassIndex + 1); for (int i = 0; i < m_plotInstances.numInstances(); i++) { m_plotInstances.instance(i).setValue(0, i); } m_plotInstances.setRelationName(originalRelationName); } catch (Exception ex) { ex.printStackTrace(); } }
/** * Applies a filter to reduce the dimension of attributes and reorders them to be used within * Meka * * @param trainData * @param removeFilter * @return a dataset to be used with Meka * @throws Exception */ public static Instances applyAttributeSelectionFilter(Instances trainData, Remove removeFilter) throws Exception { Instances filtered = Filter.useFilter(trainData, removeFilter); filtered.setClassIndex(trainData.classIndex()); // swap attributes to fit MEKA MekaClassAttributes attFilter = new MekaClassAttributes(); attFilter.setAttributeIndices(filtered.numAttributes() - trainData.classIndex() + 1 + "-last"); attFilter.setInputFormat(filtered); filtered = Filter.useFilter(filtered, attFilter); int newClassindex = filtered.classIndex(); filtered.setRelationName(filtered.relationName().replaceAll("\\-C\\s[\\d]+", "-C " + newClassindex)); return filtered; } }
/** * Initializes the format for the dataset produced. Must be called before the * generateExample or generateExamples methods are used. Re-initializes the * random number generator with the given seed. * * @return the format for the dataset * @throws Exception if the generating of the format failed * @see #getSeed() */ @Override public Instances defineDataFormat() throws Exception { BayesNetGenerator bng; bng = new BayesNetGenerator(); bng.setOptions(getGenerator().getOptions()); setGeneratorOption(bng, "M", "1"); bng.generateRandomNetwork(); bng.generateInstances(); bng.m_Instances.renameAttribute(0, "class"); bng.m_Instances.setRelationName(getRelationNameToUse()); return bng.m_Instances; }
/** * Initializes the format for the dataset produced. Must be called before the * generateExample or generateExamples methods are used. Re-initializes the * random number generator with the given seed. * * @return the format for the dataset * @throws Exception if the generating of the format failed * @see #getSeed() */ @Override public Instances defineDataFormat() throws Exception { BayesNetGenerator bng; bng = new BayesNetGenerator(); bng.setOptions(getGenerator().getOptions()); setGeneratorOption(bng, "M", "1"); bng.generateRandomNetwork(); bng.generateInstances(); bng.m_Instances.renameAttribute(0, "class"); bng.m_Instances.setRelationName(getRelationNameToUse()); return bng.m_Instances; }
/** * Generates all examples of the dataset. Re-initializes the random number * generator with the given seed, before generating instances. * * @return the generated dataset * @throws Exception if the format of the dataset is not yet defined * @throws Exception if the generator only works with generateExample, which * means in single mode * @see #getSeed() */ @Override public Instances generateExamples() throws Exception { getGenerator().setOptions(getGenerator().getOptions()); getGenerator().generateRandomNetwork(); getGenerator().generateInstances(); getGenerator().m_Instances.renameAttribute(0, "class"); getGenerator().m_Instances.setRelationName(getRelationNameToUse()); return getGenerator().m_Instances; }
/** * Generates all examples of the dataset. Re-initializes the random number * generator with the given seed, before generating instances. * * @return the generated dataset * @throws Exception if the format of the dataset is not yet defined * @throws Exception if the generator only works with generateExample, which * means in single mode * @see #getSeed() */ @Override public Instances generateExamples() throws Exception { getGenerator().setOptions(getGenerator().getOptions()); getGenerator().generateRandomNetwork(); getGenerator().generateInstances(); getGenerator().m_Instances.renameAttribute(0, "class"); getGenerator().m_Instances.setRelationName(getRelationNameToUse()); return getGenerator().m_Instances; }