protected Instance newDenseInstance(int numberAttributes) { Instance inst = new DenseInstance(numberAttributes); //inst.setInstanceInformation(this.instanceInformation); return inst; }
@Override protected Instance newDenseInstance(int numAttributes) { // numAttributes is this.instanceInformation.numAttributes() this.range.setUpper(numAttributes); return new DenseInstance(numAttributes); }
public void learnObject(double[] features){ DenseInstance inst = new DenseInstance(features.length); for(int i=0; i<features.length; i++){ inst.setValue(i, features[i]); } trainOnInstance(inst); }
@Override public InstanceExample nextInstance() { Centroid centroid = this.centroids[MiscUtils.chooseRandomIndexBasedOnWeights(this.centroidWeights, this.instanceRandom)]; int numAtts = this.numAttsOption.getValue(); double[] attVals = new double[numAtts + 1]; for (int i = 0; i < numAtts; i++) { attVals[i] = (this.instanceRandom.nextDouble() * 2.0) - 1.0; } double magnitude = 0.0; for (int i = 0; i < numAtts; i++) { magnitude += attVals[i] * attVals[i]; } magnitude = Math.sqrt(magnitude); double desiredMag = this.instanceRandom.nextGaussian() * centroid.stdDev; double scale = desiredMag / magnitude; for (int i = 0; i < numAtts; i++) { attVals[i] = centroid.centre[i] + attVals[i] * scale; } Instance inst = new DenseInstance(1.0, attVals); inst.setDataset(getHeader()); inst.setClassValue(centroid.classLabel); return new InstanceExample(inst); }
public Instance extendWithOldLabels(Instance instance) { if (this.header == null) { initHeader(instance.dataset()); this.baseLearner.setModelContext(new InstancesHeader(this.header)); } int numLabels = this.oldLabels.length; if (numLabels == 0) { return instance; } double[] x = instance.toDoubleArray(); double[] x2 = Arrays.copyOfRange(this.oldLabels, 0, numLabels + x.length); System.arraycopy(x, 0, x2, numLabels, x.length); Instance extendedInstance = new DenseInstance(instance.weight(), x2); extendedInstance.setDataset(this.header); //System.out.println( extendedInstance); return extendedInstance; }
for(int c = 0; c < kernels.size(); c++){ for(int m = 0; m < kernels.get(c).microClusters.size(); m++){ Instance inst = new DenseInstance(1, sample); if(kernels.get(c).microClusters.get(m).getInclusionProbability(inst) > 0){ incluster = true;
@Override public Example<Instance> nextInstance() { int numAtts = this.numAttsOption.getValue(); double[] attVals = new double[numAtts + 1]; double sum = 0.0; double sumWeights = 0.0; for (int i = 0; i < numAtts; i++) { attVals[i] = this.instanceRandom.nextDouble(); sum += this.weights[i] * attVals[i]; sumWeights += this.weights[i]; } int classLabel; if (sum >= sumWeights * 0.5) { classLabel = 1; } else { classLabel = 0; } // Add Noise if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) { classLabel = (classLabel == 0 ? 1 : 0); } Instance inst = new DenseInstance(1.0, attVals); inst.setDataset(getHeader()); inst.setClassValue(classLabel); addDrift(); return new InstanceExample(inst); }
@Override public InstanceExample nextInstance() { InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); inst.setDataset(header); int selected = this.instanceRandom.nextInt(10); for (int i = 0; i < 7; i++) { if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) { inst.setValue(i, originalInstances[selected][i] == 0 ? 1 : 0); } else { inst.setValue(i, originalInstances[selected][i]); } } if (!this.suppressIrrelevantAttributesOption.isSet()) { for (int i = 0; i < NUM_IRRELEVANT_ATTRIBUTES; i++) { inst.setValue(i + 7, this.instanceRandom.nextInt(2)); } } inst.setClassValue(selected); return new InstanceExample(inst); }
@Override public InstanceExample nextInstance() { InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); inst.setDataset(header); int selected = this.instanceRandom.nextInt(10); for (int i = 0; i < 7; i++) { if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) { inst.setValue(this.numberAttribute[i], originalInstances[selected][i] == 0 ? 1 : 0); } else { inst.setValue(this.numberAttribute[i], originalInstances[selected][i]); } } if (!this.suppressIrrelevantAttributesOption.isSet()) { for (int i = 0; i < NUM_IRRELEVANT_ATTRIBUTES; i++) { inst.setValue(this.numberAttribute[i + 7], this.instanceRandom.nextInt(2)); } } inst.setClassValue(selected); return new InstanceExample(inst); }
@Override public InstanceExample nextInstance() { double[] attVals = new double[this.numNominalsOption.getValue() + this.numNumericsOption.getValue()]; InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); for (int i = 0; i < attVals.length; i++) { attVals[i] = i < this.numNominalsOption.getValue() ? this.instanceRandom.nextInt(this.numValsPerNominalOption.getValue()) : this.instanceRandom.nextDouble(); inst.setValue(i, attVals[i]); } inst.setDataset(header); inst.setClassValue(classifyInstance(this.treeRoot, attVals)); return new InstanceExample(inst); }
@Override public InstanceExample nextInstance() { double[] attVals = new double[this.numNominalsOption.getValue() + this.numNumericsOption.getValue()]; InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); for (int i = 0; i < attVals.length; i++) { attVals[i] = i < this.numNominalsOption.getValue() ? this.instanceRandom.nextInt(this.numValsPerNominalOption.getValue()) : this.instanceRandom.nextDouble(); inst.setValue(i, attVals[i]); } inst.setDataset(header); inst.setClassValue(classifyInstance(this.treeRoot, attVals)); return new InstanceExample(inst); }
@Override public InstanceExample nextInstance() { double[] attVals = new double[this.numNominalsOption.getValue() + this.numNumericsOption.getValue()]; InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); for (int i = 0; i < attVals.length; i++) { attVals[i] = i < this.numNominalsOption.getValue() ? this.instanceRandom.nextInt(this.numValsPerNominalOption.getValue()) : this.instanceRandom.nextDouble(); inst.setValue(i, attVals[i]); } inst.setDataset(header); inst.setClassValue(classifyInstance(this.treeRoot, attVals)); return new InstanceExample(inst); }
public void evaluateClusteringSamoa(Clustering clustering, Clustering trueClustering, ArrayList<Instance> points) throws Exception { double BSS_GT = 1.0; double BSS; int dimension = points.get(0).numAttributes() - 1; SphereCluster sc = new SphereCluster(points, dimension); // DO INTERNAL EVALUATION //clustering.getClustering().get(0).getCenter(); BSS = getBSS(clustering, sc.getCenter()); if (trueClustering != null) { List<Instance> listInstances = new ArrayList<>(); for (Cluster c : trueClustering.getClustering()) { DenseInstance inst = new DenseInstance(c.getWeight(), c.getCenter()); listInstances.add(inst); } SphereCluster gt = new SphereCluster(listInstances, dimension); BSS_GT = getBSS(trueClustering, gt.getCenter()); } addValue("BSS", BSS); addValue("BSS-GT", BSS_GT); addValue("BSS-Ratio", BSS / BSS_GT); }
public void train(DataSet trainingSet) { // TODO fix not working builder! // ClusTree private variables are not updated but are mandatory for the algorithm to function. // if (UseBulkLoadingOption.isSet()) { // // Use BulkLoading // EMTopDownTreeBuilder builder = new EMTopDownTreeBuilder(); // try { // this.root = builder.buildTree(trainingSet); // } catch (Exception e) { // e.printStackTrace(); // } // } else { //Use traditional initialization for (DataObject o : trainingSet.getDataObjectArray()){ DenseInstance inst = new DenseInstance(o.getFeatures().length); for(int i=0; i<o.getFeatures().length; i++){ inst.setValue(i, o.getFeatures()[i]); } trainOnInstance(inst); } // } }
List<Instance> listInstances = new ArrayList<>(); for (Cluster c : trueClustering.getClustering()) { DenseInstance inst = new DenseInstance(c.getWeight(), c.getCenter()); listInstances.add(inst); s += " " + c.getWeight();
public void evaluateClusteringSamoa(Clustering clustering, Clustering trueClustering, ArrayList<Instance> points) throws Exception { double BSS_GT = 1.0; double BSS = 0.0; int dimension = points.get(0).numAttributes() - 1; SphereCluster sc = new SphereCluster(points, dimension); // DO INTERNAL EVALUATION //clustering.getClustering().get(0).getCenter(); BSS = getBSS(clustering, sc.getCenter()); if (trueClustering != null) { String s = ""; List<Instance> listInstances = new ArrayList<Instance>(); for (Cluster c : trueClustering.getClustering()) { DenseInstance inst = new DenseInstance(c.getWeight(), c.getCenter()); listInstances.add(inst); s += " " + c.getWeight(); } // System.out.println(s); SphereCluster gt = new SphereCluster(listInstances, dimension); BSS_GT = getBSS(trueClustering, gt.getCenter()); } addValue("BSS", BSS); addValue("BSS-GT", BSS_GT); addValue("BSS-Ratio", BSS / BSS_GT); }
public Clustering getClusteringResult() { Clustering clustering = null; weka.core.Instances wekaInstances= this.instanceConverter.wekaInstances(instances); try { clusterer.buildClusterer(wekaInstances); int numClusters = clusterer.numberOfClusters(); Instances dataset = getDataset(instances.numAttributes(), numClusters); List<Instance> newInstances = new ArrayList<Instance>() ; //Instances(dataset); for (int i = 0; i < wekaInstances.numInstances(); i++) { weka.core.Instance inst = wekaInstances.get(i); int cnum = clusterer.clusterInstance(inst); Instance newInst = new DenseInstance(instances.instance(cnum)); newInst.insertAttributeAt(inst.numAttributes()); newInst.setDataset(dataset); newInst.setClassValue(cnum); newInstances.add(newInst); } clustering = new Clustering(newInstances); } catch (Exception e) { e.printStackTrace(); } instances = null; return clustering; }
@Override public Example<Instance> nextInstance() { Example<Instance> original = originalStream.nextInstance(); // copies the original values double values[] = new double[this.newHeader.numAttributes()]; int ix = 0; for(int i = 0; i < original.getData().dataset().numAttributes(); i++){ if(original.getData().dataset().classIndex() != i) { values[ix] = original.getData().value(i); ix++; } } // appends the new values while(ix < values.length - 1){ Attribute att = this.newHeader.attribute(ix); if(att.isNumeric()) values[ix] = this.random.nextDouble(); else values[ix] = this.random.nextInt(numValuesCategoricalFeatureOption.getValue()); ix++; } //copies the class value if(original.getData().classIndex() != -1) { values[values.length - 1] = original.getData().classValue(); } // instantiates and returns the actual instance Instance instnc = new DenseInstance(1.0, values); instnc.setDataset(this.newHeader); return new InstanceExample(instnc); }
public void weka() { try{ Class.forName("weka.gui.Logger"); } catch (Exception e){ m_logPanel.addText("Please add weka.jar to the classpath to use the Weka explorer."); return; } Clustering wekaClustering; wekaClustering = null; if(wekaClustering == null || wekaClustering.size()==0){ m_logPanel.addText("Empty Clustering"); return; } int dims = wekaClustering.get(0).getCenter().length; FastVector attributes = new FastVector(); for(int i = 0; i < dims; i++) attributes.addElement( new Attribute("att" + i) ); Instances instances = new Instances("trainset",attributes,0); for(int c = 0; c < wekaClustering.size(); c++){ Cluster cluster = wekaClustering.get(c); Instance inst = new DenseInstance(cluster.getWeight(), cluster.getCenter()); inst.setDataset(instances); instances.add(inst); } WekaExplorer explorer = new WekaExplorer(instances); }
public InstanceExample nextInstance() { this.numInstances++; InstancesHeader header = getHeader(); Instance inst = new DenseInstance(header.numAttributes()); inst.setDataset(header); double nextValue = this.nextValue(); if (this.notBinaryStreamOption.isSet()) { inst.setValue(0, nextValue); } else { inst.setValue(0, this.nextbinaryValue(nextValue)); } //Ground truth inst.setValue(1, this.getChange() ? 1 : 0); if (this.getChange() == true) { //this.clusterEvents.add(new ClusterEvent(this, this.numInstances, "Change", "Drift")); } inst.setValue(2, nextValue); return new InstanceExample(inst); }