List of usage examples for weka.core DenseInstance setDataset
@Override public final void setDataset(Instances instances)
From source file:adams.ml.data.InstancesView.java
License:Open Source License
/** * Appends a row to the spreadsheet.//from ww w .j a va 2 s. c om * * @return the created row */ @Override public DataRow addRow() { DenseInstance inst; inst = new DenseInstance(getColumnCount()); inst.setDataset(m_Data); m_Data.add(inst); return new InstanceView(this, inst); }
From source file:adams.ml.data.InstancesView.java
License:Open Source License
/** * Inserts a row at the specified location. * * @param index the index where to insert the row * @return the created row/* w w w .j a va 2 s. c om*/ */ @Override public DataRow insertRow(int index) { DenseInstance inst; inst = new DenseInstance(getColumnCount()); inst.setDataset(m_Data); m_Data.add(index, inst); return new InstanceView(this, inst); }
From source file:com.actelion.research.orbit.imageAnalysis.modules.ManualClassificationModule.java
License:Open Source License
private void classify() { OrbitImageAnalysis oia = OrbitImageAnalysis.getInstance(); OrbitModel model = oia.getModel();//from ww w. ja v a2 s. c o m if (model.getClassifier() == null || (!model.getClassifier().isBuild())) { JOptionPane.showMessageDialog(this, "Model not trained. Please train a model first.", "Model not trained", JOptionPane.ERROR_MESSAGE); return; } ImageFrame iFrame = oia.getIFrame(); if (iFrame == null) { JOptionPane.showMessageDialog(this, "Please open at lease one image", "No image available", JOptionPane.ERROR_MESSAGE); return; } try { TissueFeatures tissueFeatures = new TissueFeatures(model.getFeatureDescription(), iFrame.recognitionFrame.bimg); double[] feats = tissueFeatures.prepareDoubleArray(); int x, y; Rectangle re; SpotAnnotation spot; DenseInstance inst; for (int i = 0; i < list.getModel().getSize(); i++) { spot = (SpotAnnotation) list.getModel().getElementAt(i); re = spot.getShape().getShapeList().get(0).getBounds(); x = (int) re.getCenterX(); y = (int) re.getCenterY(); feats = tissueFeatures.buildFeatures(null, x, y, Double.NaN); inst = new DenseInstance(1.0d, feats); inst.setDataset(model.getStructure()); double classVal = model.getClassifier().classifyInstance(inst); spot.setProposedClassNum((int) classVal); String classLabel = model.getClassShapes().get((int) classVal).getName(); spot.setDescription(classLabel + "*"); spot.setColor(model.getClassShapes().get((int) classVal).getColor().getRGB()); } repaintFrameAndList(); } catch (Exception e1) { logger.error("Error classifying spots: " + e1.getMessage()); e1.printStackTrace(); } }
From source file:edu.oregonstate.eecs.mcplan.abstraction.Experiments.java
License:Open Source License
/** * Creates a labeled dataset of states pair with optimal actions. Action * labels are represented as indexes into an array list. Mappings in both * directions are also returned.//from w ww .j a v a 2 s .c om * @param config * @param attributes * @param data * @param labels * @param iter * @return */ private static <A extends VirtualConstructor<A>> SingleInstanceDataset<A> makeSingleInstanceDataset( final Configuration config, final ArrayList<Attribute> attributes, final ArrayList<double[]> data, final ArrayList<A> labels, final ArrayList<Pair<ArrayList<A>, TDoubleList>> qtable, final int iter) { // System.out.println( "data.size() = " + data.size() ); final int[] ii = Fn.range(0, data.size()); Fn.shuffle(config.rng, ii); final HashMap<A, Integer> action_to_int = new HashMap<A, Integer>(); final ArrayList<A> int_to_action = new ArrayList<A>(); final ArrayList<Pair<ArrayList<A>, TDoubleList>> abridged_qtable = (qtable != null ? new ArrayList<Pair<ArrayList<A>, TDoubleList>>() : null); final TIntArrayList counts = new TIntArrayList(); final int max_per_label = config.getInt("training.max_per_label"); final int max_instances = config.getInt("training.max_single"); final ArrayList<DenseInstance> instance_list = new ArrayList<DenseInstance>(); for (int i = 0; i < Math.min(data.size(), max_instances); ++i) { final int idx = ii[i]; final A a = labels.get(idx); final Integer idx_obj = action_to_int.get(a); final int label; if (idx_obj == null) { // System.out.println( "\tNew action: " + a ); label = int_to_action.size(); int_to_action.add(a); action_to_int.put(a, label); counts.add(0); } else { // System.out.println( "\tRepeat action: " + a ); label = idx_obj; } final int c = counts.get(label); if (max_per_label <= 0 || c < max_per_label) { // System.out.println( "Adding " + label ); final double[] phi = Fn.append(data.get(idx), label); final DenseInstance instance = new DenseInstance(1.0, phi); instance_list.add(instance); counts.set(label, c + 1); if (qtable != null) { abridged_qtable.add(qtable.get(idx)); } } } final int Nlabels = int_to_action.size(); final ArrayList<Attribute> labeled_attributes = addLabelToAttributes(attributes, Nlabels); final Instances instances = new Instances(deriveDatasetName(config.training_data_single, iter), labeled_attributes, counts.sum()); instances.setClassIndex(instances.numAttributes() - 1); for (final DenseInstance instance : instance_list) { instances.add(instance); instance.setDataset(instances); } return new SingleInstanceDataset<A>(instances, action_to_int, int_to_action, abridged_qtable); }
From source file:edu.teco.context.recognition.WekaManager.java
License:Apache License
private void fillData(double[] featureValues, String className, Instances data) { double[] vals = new double[data.numAttributes()]; if (vals.length != (featureValues.length + 1)) { if (FrameworkContext.WARN) Log.w(TAG, "Number of feature values and weka instance values differs."); }/*from w w w . ja va2 s. co m*/ for (int i = 0; i < featureValues.length; i++) { vals[i] = featureValues[i]; } vals[vals.length - 1] = attClassVals.indexOf(className); DenseInstance instance = new DenseInstance(1.0, vals); if (isLogDirectlyToFile) { instance.setDataset(data); logArffData(instance.toString()); } else { // add data.add(instance); } }
From source file:lu.lippmann.cdb.datasetview.tabs.TimeSeriesSimilarityPanel.java
License:Open Source License
private Instances buildFeatureDS(final Instances dataSet) throws Exception { final int numAttributes = dataSet.numAttributes(); final java.util.List<String> namesOfFeaturesToConsider = new ArrayList<String>(); namesOfFeaturesToConsider.addAll(WekaDataStatsUtil.getAttributeNames(dataSet)); namesOfFeaturesToConsider.removeAll(WekaDataStatsUtil.getDateAttributeNames(dataSet)); final double[][] simMatrix = new double[numAttributes][numAttributes]; for (int i = 0; i < numAttributes; i++) { final double[] arrayI = dataSet.attributeToDoubleArray(i); if (this.withNormalization) MathsUtil.normalize(arrayI); simMatrix[i][i] = 0d;/* w ww . j a v a 2s. co m*/ ; for (int j = i + 1; j < numAttributes; j++) { final double[] arrayJ = dataSet.attributeToDoubleArray(j); if (this.withNormalization) MathsUtil.normalize(arrayJ); simMatrix[i][j] = new DynamicTimeWarping(arrayI, arrayJ).getDistance(); //System.out.println(i+" "+j); } } for (int i = 0; i < numAttributes; i++) { for (int j = 0; j < i + 1; j++) { simMatrix[i][j] = simMatrix[j][i]; } } /*for (int i=0;i<numAttributes;i++) { System.out.println(i+" -> "+FormatterUtil.buildStringFromArrayOfDoubles(simMatrix[i])); }*/ final ArrayList<Attribute> attrs = new ArrayList<Attribute>(numAttributes + 1); for (int i = 0; i < numAttributes; i++) { attrs.add(new Attribute(dataSet.attribute(i).name() + "-feat")); } attrs.add(new Attribute(FEATUREDESC_ATTRNAME, namesOfFeaturesToConsider)); final Instances ds = new Instances("featuresComparisonDs", attrs, 0); ds.setClassIndex(attrs.size() - 1); for (int i = 0; i < simMatrix.length; i++) { final DenseInstance di = new DenseInstance(1.0d, ArraysUtil.concat(simMatrix[i], new double[] { 0d })); di.setDataset(ds); di.setValue(simMatrix.length, dataSet.attribute(i).name()); ds.add(di); } return ds; }
From source file:moa.cluster.Riffle.java
License:Apache License
/** * * @param other cluster/*from w w w .ja va 2 s . c om*/ * @return */ public double getCenterDistance(Riffle other) { if (this.distanceStrategyOption.getChosenIndex() == 13) { DenseInstance x = new DenseInstance(0, other.centroid); x.setDataset(this.instances); return 1.0 - this.getInclusionProbability(x); } else { return VectorDistances.distance(other.centroid, centroid, this.instances, this.distanceStrategyOption.getChosenIndex()); } }
From source file:moa.cluster.Riffle.java
License:Apache License
/** * Converts cluster to an instance/* ww w . j a v a 2 s . com*/ * @return Instance version of the centroid */ public Instance toInstance() { DenseInstance ret = new DenseInstance(0.0, centroid); ret.setWeight(0.0); ret.setDataset(this.instances); return ret; }
From source file:nl.uva.sne.commons.ClusterUtils.java
private static Instances createInstancesWithClasses(String inDir) throws IOException, ParseException, Exception { File dir = new File(inDir); File[] classFolders = dir.listFiles(); List<List<String>> allDocs = new ArrayList<>(); Map<String, List<String>> docs = new HashMap<>(); Set<String> classes = new HashSet<>(); for (File f : classFolders) { if (f.isDirectory()) { List<Term> terms = dir2Terms(f.getAbsolutePath()); classes.add(f.getName());//from w w w . j av a2 s. com for (Term tv : terms) { Set<String> doc = SemanticUtils.getDocument(tv); allDocs.add(new ArrayList<>(doc)); docs.put(tv.getUID() + "," + f.getName(), new ArrayList<>(doc)); } } else { List<Term> terms = new ArrayList<>(); if (FilenameUtils.getExtension(f.getName()).endsWith("json")) { terms.add(TermFactory.create(new FileReader(f))); } classes.add("NON"); for (Term tv : terms) { Set<String> doc = SemanticUtils.getDocument(tv); allDocs.add(new ArrayList<>(doc)); docs.put(tv.getUID() + "," + "NON", new ArrayList<>(doc)); // docs.put(tv.getUID(), new ArrayList<>(doc)); } } } Set<String> allWords = new HashSet<>(); Map<String, Map<String, Double>> featureVectors = new HashMap<>(); for (String k : docs.keySet()) { List<String> doc = docs.get(k); Map<String, Double> featureVector = new TreeMap<>(); for (String term : doc) { allWords.add(term); if (!featureVector.containsKey(term)) { double score = SemanticUtils.tfIdf(doc, allDocs, term); featureVector.put(term, score); } } featureVectors.put(k, featureVector); } for (String t : featureVectors.keySet()) { Map<String, Double> featureV = featureVectors.get(t); for (String word : allWords) { if (!featureV.containsKey(word)) { featureV.put(word, 0.0); } } // System.err.println(t + " " + featureV.size()); featureVectors.put(t, featureV); } ArrayList<Attribute> attributes = buildAttributes(allWords, classes); Instances data = new Instances("Rel", attributes, docs.size()); data.setClassIndex(data.numAttributes() - 1); for (String t : featureVectors.keySet()) { String[] parts = t.split(","); String id = parts[0]; String theClass = parts[parts.length - 1]; int index = 0; double[] vals = new double[data.numAttributes()]; vals[index] = data.attribute(0).addStringValue(id); index++; Map<String, Double> featureV = featureVectors.get(t); for (String w : featureV.keySet()) { vals[index] = featureV.get(w); index++; } DenseInstance inst = new DenseInstance(1.0, vals); inst.setDataset(data); inst.setClassValue(theClass); data.add(inst); } return data; }
From source file:org.barcelonamedia.uima.CAS2WekaInstance.java
License:Open Source License
private static DenseInstance toWekaInternalInstance(List<AttributeValue> attributeValues, Instances wekaInstances) throws CASException { double[] zeroValues = new double[wekaInstances.numAttributes()]; Arrays.fill(zeroValues, 0.0d); DenseInstance wekaInstance = new DenseInstance(1.0d, zeroValues); wekaInstance.setDataset(wekaInstances); Iterator<AttributeValue> attributeValuesIterator = attributeValues.iterator(); while (attributeValuesIterator.hasNext()) { String value = null;//from ww w .j av a2s . c o m String attributeName = null; AttributeValue attributeValue = attributeValuesIterator.next(); attributeName = attributeValue.getAttributeName(); Attribute attribute = wekaInstances.attribute(attributeName); if (attribute == null) continue; if (attributeValue instanceof NumericAttributeValue) { value = ((NumericAttributeValue) attributeValue).getValue(); wekaInstance.setValue(attribute, Double.parseDouble(value)); } else if (attributeValue instanceof DateAttributeValue) { //this isn't actually very smart.... I need to understand this better //any volunteers for the four lines of code I need here? value = ((DateAttributeValue) attributeValue).getValue(); wekaInstance.setValue(attribute, value); } else if (attributeValue instanceof NominalAttributeValue) { value = ((NominalAttributeValue) attributeValue).getValue(); int valueIndex = attribute.indexOfValue(value); wekaInstance.setValue(attribute, (double) valueIndex); } else if (attributeValue instanceof StringAttributeValue) { value = ((StringAttributeValue) attributeValue).getValue(); wekaInstance.setValue(attribute, value); } } Enumeration attributes = wekaInstances.enumerateAttributes(); while (attributes.hasMoreElements()) { Attribute attribute = (Attribute) attributes.nextElement(); if (attribute.isNumeric() && wekaInstance.isMissing(attribute)) { wekaInstance.setValue(attribute, 0); } } return wekaInstance; }