List of usage examples for weka.core Instance setValue
public void setValue(Attribute att, String value);
From source file:boa.aggregators.LinearRegressionAggregator.java
License:Apache License
/** {@inheritDoc} */ @Override/*www .j a v a 2 s. co m*/ public void finish() throws IOException, InterruptedException { int NumOfAttributes = this.getVectorSize(); List<Attribute> attribute = new ArrayList<Attribute>(); FastVector fvAttributes = new FastVector(NumOfAttributes); for (int i = 0; i < NumOfAttributes; i++) { attribute.add(new Attribute("Attribute" + i)); fvAttributes.addElement(attribute.get(i)); } Instances trainingSet = new Instances("LinearRegression", fvAttributes, 1); trainingSet.setClassIndex(NumOfAttributes - 1); for (List<Double> vector : this.vectors.values()) { Instance instance = new Instance(NumOfAttributes); for (int i = 0; i < vector.size(); i++) { instance.setValue((Attribute) fvAttributes.elementAt(i), vector.get(i)); } trainingSet.add(instance); } try { this.model = new LinearRegression(); this.model.setOptions(options); this.model.buildClassifier(trainingSet); } catch (Exception ex) { } this.saveModel(this.model); }
From source file:boa.aggregators.NaiveBayesAggregator.java
License:Apache License
/** {@inheritDoc} */ @Override/*from w w w . j a v a 2s. c o m*/ public void finish() throws IOException, InterruptedException { Instances trainingSet = new Instances("NaiveBayes", fvAttributes, 1); trainingSet.setClassIndex(NumOfAttributes - 1); for (List<Double> vector : this.vectors.values()) { Instance instance = new Instance(NumOfAttributes); for (int i = 0; i < vector.size(); i++) { instance.setValue((Attribute) fvAttributes.elementAt(i), vector.get(i)); } trainingSet.add(instance); } try { this.model = new NaiveBayes(); this.model.setOptions(options); this.model.buildClassifier(trainingSet); } catch (Exception ex) { } this.saveModel(this.model); }
From source file:boa.aggregators.RandomForestAggregator.java
License:Apache License
/** {@inheritDoc} */ @Override/* www. j a v a 2 s .co m*/ public void finish() throws IOException, InterruptedException { int NumOfAttributes = this.getVectorSize(); List<Attribute> attributes = new ArrayList<Attribute>(); FastVector fvAttributes = new FastVector(NumOfAttributes); for (int i = 0; i < NumOfAttributes; i++) { attributes.add(new Attribute("Attribute" + i)); fvAttributes.addElement(attributes.get(i)); } Instances trainingSet = new Instances("RandomForest", fvAttributes, 1); trainingSet.setClassIndex(NumOfAttributes - 1); for (List<Double> vector : this.vectors.values()) { Instance instance = new Instance(NumOfAttributes); for (int i = 0; i < vector.size(); i++) { instance.setValue((Attribute) fvAttributes.elementAt(i), vector.get(i)); } trainingSet.add(instance); } try { this.model = new RandomForest(); this.model.setOptions(options); this.model.buildClassifier(trainingSet); } catch (Exception ex) { } this.saveModel(this.model); }
From source file:boa.functions.BoaIntrinsics.java
License:Apache License
/** * Classify instances for given ML model * * @param Take Model Type//from ww w. ja va 2s. c o m * @return Predicted value for a instance */ @FunctionSpec(name = "classify", returnType = "float", formalParameters = { "Model", "array of float" }) public static double classify(final Object model, final double[] vector) throws Exception { List<Attribute> attribute = new ArrayList<Attribute>(); int size = vector.length; int NumOfAttributes = size + 1; FastVector fvAttributes = new FastVector(NumOfAttributes); for (int i = 0; i < NumOfAttributes; i++) { attribute.add(new Attribute("Attribute" + i)); fvAttributes.addElement(attribute.get(i)); } Instances testingSet = new Instances("Classifier", fvAttributes, 1); testingSet.setClassIndex(NumOfAttributes - 1); Instance instance = new Instance(NumOfAttributes); for (int i = 0; i < size; i++) { instance.setValue((Attribute) fvAttributes.elementAt(i), vector[i]); } Classifier classifier = (Classifier) model; double predval = classifier.classifyInstance(instance); return predval; }
From source file:br.fapesp.myutils.MyUtils.java
License:Open Source License
/** * Generates a Gaussian data set with K clusters and m dimensions * /*from w w w . ja v a 2s . co m*/ * @param centers * K x m matrix * @param sigmas * K x m matrix * @param pointsPerCluster * number of points per cluster * @param seed * for the RNG * @param randomize * should the order of the instances be randomized? * @param supervised * should class label be present? if true, the class is the m+1 * attribute * * @return */ public static Instances genGaussianDataset(double[][] centers, double[][] sigmas, int pointsPerCluster, long seed, boolean randomize, boolean supervised) { Random r = new Random(seed); int K = centers.length; // number of clusters int m = centers[0].length; // number of dimensions FastVector atts = new FastVector(m); for (int i = 0; i < m; i++) atts.addElement(new Attribute("at" + i)); if (supervised) { FastVector cls = new FastVector(K); for (int i = 0; i < K; i++) cls.addElement("Gauss-" + i); atts.addElement(new Attribute("Class", cls)); } Instances data; if (supervised) data = new Instances(K + "-Gaussians-supervised", atts, K * pointsPerCluster); else data = new Instances(K + "-Gaussians", atts, K * pointsPerCluster); if (supervised) data.setClassIndex(m); Instance ith; for (int i = 0; i < K; i++) { for (int j = 0; j < pointsPerCluster; j++) { if (!supervised) ith = new DenseInstance(m); else ith = new DenseInstance(m + 1); ith.setDataset(data); for (int k = 0; k < m; k++) ith.setValue(k, centers[i][k] + (r.nextGaussian() * sigmas[i][k])); if (supervised) ith.setValue(m, "Gauss-" + i); data.add(ith); } } // run randomization filter if desired if (randomize) data.randomize(r); return data; }
From source file:br.puc_rio.ele.lvc.interimage.datamining.DataParser.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" }) public Instances parseData(Object objData) { try {// www . j a v a 2 s .co m Instances dataInstance; DataBag values = (DataBag) objData; int numAttributes = values.iterator().next().size(); // N_Features + 1 Class int bagSize = 0; // To set the number of train samples // To find the number of samples (instances in a bag) for (Iterator<Tuple> it = values.iterator(); it.hasNext();) { it.next(); bagSize = bagSize + 1; } // Code for find the different classes names in the input String[] inputClass = new String[bagSize]; // String vector with the samples class's names int index = 0; for (Iterator<Tuple> it = values.iterator(); it.hasNext();) { Tuple tuple = it.next(); inputClass[index] = DataType.toString(tuple.get(numAttributes - 1)); index = index + 1; } HashSet classSet = new HashSet(Arrays.asList(inputClass)); String[] classValue = (String[]) classSet.toArray(new String[0]); // To set the classes names in the attribute for the instance FastVector classNames = new FastVector(); for (int i = 0; i < classValue.length; i++) classNames.addElement(classValue[i]); // Creating the instance model N_Features + 1_ClassNames FastVector atts = new FastVector(); for (int i = 0; i < numAttributes - 1; i++) atts.addElement(new Attribute("att" + i)); dataInstance = new Instances("MyRelation", atts, numAttributes); dataInstance.insertAttributeAt(new Attribute("ClassNames", classNames), numAttributes - 1); // To set the instance values for the dataInstance model created Instance tmpData = new DenseInstance(numAttributes); index = 0; for (Iterator<Tuple> it = values.iterator(); it.hasNext();) { Tuple tuple = it.next(); for (int i = 0; i < numAttributes - 1; i++) tmpData.setValue((weka.core.Attribute) atts.elementAt(i), DataType.toDouble(tuple.get(i))); //tmpData.setValue((weka.core.Attribute) atts.elementAt(numAttributes-1), DataType.toString(tuple.get(numAttributes-1))); dataInstance.add(tmpData); dataInstance.instance(index).setValue(numAttributes - 1, DataType.toString(tuple.get(numAttributes - 1))); index = index + 1; } // Setting the class index dataInstance.setClassIndex(dataInstance.numAttributes() - 1); return dataInstance; } catch (Exception e) { System.err.println("Failed to process input; error - " + e.getMessage()); return null; } }
From source file:br.puc_rio.ele.lvc.interimage.datamining.DataParser.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" }) public Instances parseData(BufferedReader buff) { try {/* ww w .j a va 2 s .c o m*/ Instances dataInstance; //DataBag values = (DataBag)objData; int numAttributes = 0; // N_Features + 1 Class List<String> inputClass = new ArrayList<String>(); List<String[]> dataset = new ArrayList<String[]>(); // To find the number of samples (instances in a bag) String line; while ((line = buff.readLine()) != null) { if (!line.isEmpty()) { String[] data = line.split(","); if (numAttributes == 0) numAttributes = data.length; inputClass.add(data[data.length - 1]); dataset.add(data); } } HashSet classSet = new HashSet(inputClass); String[] classValue = (String[]) classSet.toArray(new String[0]); // To set the classes names in the attribute for the instance FastVector classNames = new FastVector(); for (int i = 0; i < classValue.length; i++) classNames.addElement(classValue[i]); // Creating the instance model N_Features + 1_ClassNames FastVector atts = new FastVector(); for (int i = 0; i < numAttributes - 1; i++) atts.addElement(new Attribute("att" + i)); dataInstance = new Instances("MyRelation", atts, numAttributes); dataInstance.insertAttributeAt(new Attribute("ClassNames", classNames), numAttributes - 1); // To set the instance values for the dataInstance model created Instance tmpData = new DenseInstance(numAttributes); int index = 0; for (int k = 0; k < dataset.size(); k++) { for (int i = 0; i < numAttributes - 1; i++) tmpData.setValue((weka.core.Attribute) atts.elementAt(i), DataType.toDouble(dataset.get(k)[i])); //tmpData.setValue((weka.core.Attribute) atts.elementAt(numAttributes-1), DataType.toString(tuple.get(numAttributes-1))); dataInstance.add(tmpData); dataInstance.instance(index).setValue(numAttributes - 1, DataType.toString(dataset.get(k)[numAttributes - 1])); index = index + 1; } // Setting the class index dataInstance.setClassIndex(dataInstance.numAttributes() - 1); return dataInstance; } catch (Exception e) { System.err.println("Failed to process input; error - " + e.getMessage()); return null; } }
From source file:br.puc_rio.ele.lvc.interimage.datamining.udf.BayesClassifier.java
License:Apache License
@Override public String exec(Tuple input) throws IOException { if (input == null) return null; if (_trainData == null) { //Reads train data try {/*w w w .ja v a 2s . c o m*/ if (!_trainUrl.isEmpty()) { URL url = new URL(_trainUrl); URLConnection urlConn = url.openConnection(); urlConn.connect(); InputStreamReader inStream = new InputStreamReader(urlConn.getInputStream()); BufferedReader buff = new BufferedReader(inStream); _trainData = _dataParser.parseData(buff); } } catch (Exception e) { throw new IOException("Caught exception reading training data file ", e); } } try { Integer numFeatures = input.size(); double[] testData; testData = new double[numFeatures]; for (int i = 0; i < numFeatures; i++) testData[i] = DataType.toDouble(input.get(i)); Classifier csfr = null; csfr = (Classifier) Class.forName("weka.classifiers.bayes.NaiveBayes").newInstance(); csfr.buildClassifier(_trainData); double classification = 0; Instance myinstance = _trainData.instance(0); for (int i = 0; i < numFeatures; i++) myinstance.setValue(i, testData[i]); classification = csfr.classifyInstance(myinstance); return myinstance.attribute(_trainData.classIndex()).value((int) classification); } catch (Exception e) { throw new IOException("Caught exception processing input row ", e); } }
From source file:br.ufpe.cin.mpos.offload.DynamicDecisionSystem.java
License:Apache License
public synchronized boolean isRemoteAdvantage(int InputSize, Remotable.Classifier classifierRemotable) { boolean resp = false; try {/*from ww w .ja va 2 s . co m*/ if ((!(this.classifierModel.equals(classifierRemotable.toString()))) || this.classifier == null) { Log.d("classificacao", "classificador=" + classifierRemotable.toString()); this.classifierModel = classifierRemotable.toString(); loadClassifier(classifierRemotable); } Cursor c = dc.getData(); int colunas = c.getColumnCount(); Instance instance = new DenseInstance(colunas - 2); ArrayList<String> values = new ArrayList<String>(); ArrayList<Attribute> atts = new ArrayList<Attribute>(); if (c.moveToFirst()) { for (int i = 1; i <= colunas - 2; i++) { String feature = c.getColumnName(i); String value = c.getString(i); Attribute attribute; if (feature.equals(DatabaseManager.InputSize)) { values.add("" + InputSize); attribute = new Attribute(DatabaseManager.InputSize); } else { String[] strings = populateAttributes(i); ArrayList<String> attValues = new ArrayList<String>(Arrays.asList(strings)); attribute = new Attribute(feature, attValues); if (value != null) { values.add(value); } } atts.add(attribute); } Instances instances = new Instances("header", atts, atts.size()); instances.setClassIndex(instances.numAttributes() - 1); instance.setDataset(instances); for (int i = 0; i < atts.size(); i++) { if (i == 9) { instance.setMissing(atts.get(9)); } else if (atts.get(i).name().equals(DatabaseManager.InputSize)) { instance.setValue(atts.get(i), InputSize); } else { instance.setValue(atts.get(i), values.get(i)); } } double value = -1; value = classifier.distributionForInstance(instance)[0]; Log.d("classificacao", instance.toString() + " classifiquei com o seguinte valor" + value); resp = (0.7 <= value); if (resp) { Log.d("classificacao", "sim"); Log.d("Finalizado", "classifiquei " + instance.toString() + " com sim"); } else { Log.d("classificacao", "nao"); Log.d("Finalizado", "classifiquei " + instance.toString() + " com nao"); } } } catch (Exception e) { e.printStackTrace(); Log.e("sqlLite", e.getMessage()); Log.e("sqlLite", "Causa: " + e.getCause()); } return resp; }
From source file:br.unicamp.ic.recod.gpsi.gp.gpsiJGAPRoiFitnessFunction.java
@Override protected double evaluate(IGPProgram igpp) { double mean_accuracy = 0.0; Object[] noargs = new Object[0]; gpsiRoiBandCombiner roiBandCombinator = new gpsiRoiBandCombiner(new gpsiJGAPVoxelCombiner(super.b, igpp)); // TODO: The ROI descriptors must combine the images first //roiBandCombinator.combineEntity(this.dataset.getTrainingEntities()); gpsiMLDataset mlDataset = new gpsiMLDataset(this.descriptor); try {// ww w. ja va 2 s . c o m mlDataset.loadWholeDataset(this.dataset, true); } catch (Exception ex) { Logger.getLogger(gpsiJGAPRoiFitnessFunction.class.getName()).log(Level.SEVERE, null, ex); } int dimensionality = mlDataset.getDimensionality(); int n_classes = mlDataset.getTrainingEntities().keySet().size(); int n_entities = mlDataset.getNumberOfTrainingEntities(); ArrayList<Byte> listOfClasses = new ArrayList<>(mlDataset.getTrainingEntities().keySet()); Attribute[] attributes = new Attribute[dimensionality]; FastVector fvClassVal = new FastVector(n_classes); int i, j; for (i = 0; i < dimensionality; i++) attributes[i] = new Attribute("f" + Integer.toString(i)); for (i = 0; i < n_classes; i++) fvClassVal.addElement(Integer.toString(listOfClasses.get(i))); Attribute classes = new Attribute("class", fvClassVal); FastVector fvWekaAttributes = new FastVector(dimensionality + 1); for (i = 0; i < dimensionality; i++) fvWekaAttributes.addElement(attributes[i]); fvWekaAttributes.addElement(classes); Instances instances = new Instances("Rel", fvWekaAttributes, n_entities); instances.setClassIndex(dimensionality); Instance iExample; for (byte label : mlDataset.getTrainingEntities().keySet()) { for (double[] featureVector : mlDataset.getTrainingEntities().get(label)) { iExample = new Instance(dimensionality + 1); for (j = 0; j < dimensionality; j++) iExample.setValue(i, featureVector[i]); iExample.setValue(dimensionality, label); instances.add(iExample); } } int folds = 5; Random rand = new Random(); Instances randData = new Instances(instances); randData.randomize(rand); Instances trainingSet, testingSet; Classifier cModel; Evaluation eTest; try { for (i = 0; i < folds; i++) { cModel = (Classifier) new SimpleLogistic(); trainingSet = randData.trainCV(folds, i); testingSet = randData.testCV(folds, i); cModel.buildClassifier(trainingSet); eTest = new Evaluation(trainingSet); eTest.evaluateModel(cModel, testingSet); mean_accuracy += eTest.pctCorrect(); } } catch (Exception ex) { Logger.getLogger(gpsiJGAPRoiFitnessFunction.class.getName()).log(Level.SEVERE, null, ex); } mean_accuracy /= (folds * 100); return mean_accuracy; }