List of usage examples for weka.core Instances setClassIndex
public void setClassIndex(int classIndex)
From source file:entities.WekaNGGFeatureVector.java
public Instances fillInstanceSet(ArrayList<NGGFeatureVector> vList, ArrayList<NGGFeatureVector> vList2, String datasetType) throws IOException { ArrayList<Attribute> attributes = initializeWekaFeatureVector(); Instances isSet = new Instances(vList.get(0).getLabel(), attributes, vList.size()); isSet.setClassIndex(isSet.numAttributes() - 1); for (NGGFeatureVector NGGv : vList) { Instance i = fillFeatureVector(NGGv, isSet); isSet.add(i);/*from ww w. ja va 2 s . c o m*/ } for (NGGFeatureVector NGGv : vList2) { Instance i = fillFeatureVector(NGGv, isSet); isSet.add(i); } ArffSaver saver = new ArffSaver(); saver.setInstances(isSet); saver.setFile(new File("./data/" + datasetType + ".arff")); saver.writeBatch(); return isSet; }
From source file:entity.DatasetFileManager.java
License:Open Source License
public Instances generateWekaInstancesForDataset(String datasetFilePath) { // datasource creation from file DataSource datasource = null; try {// ww w . j a v a 2 s . c om datasource = new DataSource(datasetFilePath); } catch (Exception e) { e.printStackTrace(); } Instances instances = null; // instances creation from datasource try { instances = datasource.getDataSet(); } catch (Exception e) { e.printStackTrace(); } // last attribute is for classification instances.setClassIndex(instances.numAttributes() - 1); if (verbose) System.out.println("\nDataset:\n"); if (verbose) System.out.println(instances); return instances; }
From source file:en_deep.mlprocess.manipulation.featmodif.FeatureModifierFilter.java
License:Open Source License
/** * Set the output format if the class is nominal. *///from www . j a va2 s . c o m private void setOutputFormat() { FastVector newAtts; int newClassIndex; Instances outputFormat; newClassIndex = getInputFormat().classIndex(); newAtts = new FastVector(); BitSet attrSrc = new BitSet(), attrDest = new BitSet(); int attSoFar = 0; for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (!m_Columns.isInRange(j)) { newAtts.addElement(att.copy()); attrSrc.set(j); attrDest.set(attSoFar++); } else { ArrayList<Attribute> valueAttrs = getAttributeOutputFormat(att); if (newClassIndex >= 0 && j < getInputFormat().classIndex()) { newClassIndex += valueAttrs.size() - 1; } newAtts.addAll(valueAttrs); if (m_PreserveOriginals) { attrSrc.set(j); attrDest.set(attSoFar); } attSoFar += valueAttrs.size(); } } outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0); outputFormat.setClassIndex(newClassIndex); setOutputFormat(outputFormat); m_StringToCopySrc = new AttributeLocator(getInputFormat(), Attribute.STRING, MathUtils.findTrue(attrSrc)); m_StringToCopyDst = new AttributeLocator(outputFormat, Attribute.STRING, MathUtils.findTrue(attrDest)); }
From source file:en_deep.mlprocess.manipulation.featmodif.ReplaceMissing.java
License:Open Source License
/** * Set the output format if the class is nominal. *///from w ww .j a v a 2 s .c o m private void setOutputFormat() { FastVector newAtts; Instances outputFormat; newAtts = new FastVector(); BitSet attrSrc = new BitSet(); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = null; Attribute srcAtt = getInputFormat().attribute(j); if (!m_Columns.isInRange(j) || srcAtt.indexOfValue(m_ReplVal) >= 0) { att = (Attribute) srcAtt.copy(); } else if (srcAtt.isNominal()) { Enumeration<String> valsEnum = srcAtt.enumerateValues(); ArrayList<String> valsList = new ArrayList<String>(); while (valsEnum.hasMoreElements()) { valsList.add(valsEnum.nextElement()); } valsList.add(m_ReplVal); att = new Attribute(srcAtt.name(), valsList); } else { // string attributes att = (Attribute) srcAtt.copy(); att.addStringValue(m_ReplVal); } newAtts.addElement(att); attrSrc.set(j); } outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0); outputFormat.setClassIndex(getInputFormat().classIndex()); setOutputFormat(outputFormat); m_StringToCopy = new AttributeLocator(getInputFormat(), Attribute.STRING, MathUtils.findTrue(attrSrc)); }
From source file:en_deep.mlprocess.manipulation.SetAwareNominalToBinary.java
License:Open Source License
/** * Set the output format if the class is nominal. *///from ww w . jav a 2s . co m private void setOutputFormat() { FastVector newAtts; int newClassIndex; Instances outputFormat; // Compute new attributes m_producedAttVals = new HashMap[getInputFormat().numAttributes()]; newClassIndex = getInputFormat().classIndex(); newAtts = new FastVector(); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (!att.isNominal() || (j == getInputFormat().classIndex()) || !m_Columns.isInRange(j)) { newAtts.addElement(att.copy()); } else { if ((att.numValues() <= 2) && (!m_TransformAll)) { if (m_Numeric) { newAtts.addElement(new Attribute(att.name())); } else { newAtts.addElement(att.copy()); } } else { ArrayList<Attribute> valueAttrs = convertAttribute(att); if (newClassIndex >= 0 && j < getInputFormat().classIndex()) { newClassIndex += valueAttrs.size() - 1; } newAtts.addAll(valueAttrs); } } } outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0); outputFormat.setClassIndex(newClassIndex); setOutputFormat(outputFormat); }
From source file:es.bsc.autonomic.powermodeller.tools.classifiers.BaggingClassifier.java
License:Apache License
@Override protected Classifier buildClassifier(DataSet training_ds) { logger.debug("Building Bagging classifier."); Classifier model = null;/*w ww .j a v a 2 s. c om*/ // Get the independent variable index String independent = training_ds.getIndependent(); if (independent == null) throw new WekaWrapperException("Independent variable is not set in dataset."); try { // Read all the instances in the file (ARFF, CSV, XRFF, ...) ConverterUtils.DataSource source = new ConverterUtils.DataSource(training_ds.getFilePath()); Instances instances = source.getDataSet(); // Set the independent variable (powerWatts). instances.setClassIndex(instances.attribute(independent).index()); // Builds a regression model for the given data. model = new weka.classifiers.meta.Bagging(); // Build Linear Regression model.buildClassifier(instances); } catch (WekaWrapperException e) { logger.error("Error while creating Bagging classifier.", e); throw new WekaWrapperException("Error while creating Bagging classifier."); } catch (Exception e) { logger.error("Error while applying Bagging to data set instances.", e); throw new WekaWrapperException("Error while applying Bagging to data set instances."); } return model; }
From source file:es.bsc.autonomic.powermodeller.tools.classifiers.LinearRegressionClassifier.java
License:Apache License
@Override public Classifier buildClassifier(DataSet training_ds) { logger.debug("Building LinearRegression classifier."); Classifier model;/*from w ww . j av a2 s . c om*/ // Get the independent variable index String independent = training_ds.getIndependent(); if (independent == null) throw new WekaWrapperException("Independent variable is not set in dataset."); try { // Read all the instances in the file (ARFF, CSV, XRFF, ...) ConverterUtils.DataSource source = new ConverterUtils.DataSource(training_ds.getFilePath()); Instances instances = source.getDataSet(); // Set the independent variable (powerWatts). instances.setClassIndex(instances.attribute(independent).index()); // Builds a regression model for the given data. model = new weka.classifiers.functions.LinearRegression(); // Build Linear Regression model.buildClassifier(instances); } catch (WekaWrapperException e) { logger.error("Error while creating Linear Regression classifier.", e); throw new WekaWrapperException("Error while creating Linear Regression classifier."); } catch (Exception e) { logger.error("Error while applying Linear Regression to data set instances.", e); throw new WekaWrapperException("Error while applying Linear Regression to data set instances."); } return model; }
From source file:es.bsc.autonomic.powermodeller.tools.classifiers.MultilayerPerceptronClassifier.java
License:Apache License
@Override protected Classifier buildClassifier(DataSet training_ds) { logger.debug("Building MultilayerPerceptron classifier."); MultilayerPerceptron model;/*from w w w. ja v a 2s.c o m*/ // Get the independent variable index String independent = training_ds.getIndependent(); if (independent == null) throw new WekaWrapperException("Independent variable is not set in dataset."); try { // Read all the instances in the file (ARFF, CSV, XRFF, ...) ConverterUtils.DataSource source = new ConverterUtils.DataSource(training_ds.getFilePath()); Instances instances = source.getDataSet(); // Set the independent variable (powerWatts). instances.setClassIndex(instances.attribute(independent).index()); // Builds a regression model for the given data. model = new weka.classifiers.functions.MultilayerPerceptron(); model.setHiddenLayers("4"); model.setTrainingTime(20); // Build Linear Regression model.buildClassifier(instances); } catch (WekaWrapperException e) { logger.error("Error while creating Linear Regression classifier.", e); throw new WekaWrapperException("Error while creating Linear Regression classifier."); } catch (Exception e) { logger.error("Error while applying Linear Regression to data set instances.", e); throw new WekaWrapperException("Error while applying Linear Regression to data set instances."); } return model; }
From source file:es.bsc.autonomic.powermodeller.tools.classifiers.RepTreeClassifier.java
License:Apache License
@Override public Classifier buildClassifier(DataSet training_ds) { logger.debug("Building RepTree classifier."); Classifier model;/*w ww . j a v a 2s .c o m*/ // Get the independent variable index String independent = training_ds.getIndependent(); if (independent == null) throw new WekaWrapperException("Independent variable is not set in dataset."); try { // Read all the instances in the file (ARFF, CSV, XRFF, ...) ConverterUtils.DataSource source = new ConverterUtils.DataSource(training_ds.getFilePath()); Instances instances = source.getDataSet(); // Set the independent variable (powerWatts). instances.setClassIndex(instances.attribute(independent).index()); // Builds a regression model for the given data. model = new weka.classifiers.trees.REPTree(); // Build Linear Regression model.buildClassifier(instances); } catch (WekaWrapperException e) { logger.error("Error while creating Linear Regression classifier.", e); throw new WekaWrapperException("Error while creating Linear Regression classifier."); } catch (Exception e) { logger.error("Error while applying Linear Regression to data set instances.", e); throw new WekaWrapperException("Error while applying Linear Regression to data set instances."); } return model; }
From source file:es.bsc.autonomic.powermodeller.tools.classifiers.WekaWrapper.java
License:Apache License
public static Instances convertDataSetToInstances(DataSet ds) { Instances instances; try {//from w w w .j a v a 2 s.com // Read all the instances in the file and initialize data ConverterUtils.DataSource source = new ConverterUtils.DataSource(ds.getFilePath()); instances = source.getDataSet(); instances.setClassIndex(instances.attribute(ds.getIndependent()).index()); } catch (Exception e) { logger.error("Error while reading input DataSet", e); throw new WekaWrapperException("Error while reading input DataSet"); } return instances; }