weka.core.DenseInstance Java Examples

The following examples show how to use weka.core.DenseInstance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BoTSWEnsemble.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public BoTSW_Bag buildTestBag(Instance instnc) throws Exception {
    double[][] features = extractFeatures(toArrayNoClass(instnc));

    //cluster/form histograms
    Instances testFeatures = new Instances(clusterData, features.length);
    double[] hist = new double[params.k];
    for (int i = 0; i < features.length; ++i) {
        testFeatures.add(new DenseInstance(1, features[i]));
        int cluster = kmeans.clusterInstance(testFeatures.get(i));
        ++hist[cluster];
    }

    hist = normaliseHistogramSSR(hist);
    hist = normaliseHistograml2(hist);

    return new BoTSW_Bag(hist, instnc.classValue());
}
 
Example #2
Source File: InstanceTools.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public static Instance randomlyAlterSeries(Instance inst, Random rand){
    Instance newInst = new DenseInstance(inst);

    if (rand.nextBoolean()){
        newInst = reverseSeries(newInst);
    }

    int shift = rand.nextInt(240)-120;
    newInst = shiftSeries(newInst, shift);

    if (rand.nextBoolean()){
        newInst = randomlyAddToSeriesValues(newInst, rand, 0, 5, Integer.MAX_VALUE);
    }
    else{
        newInst = randomlySubtractFromSeriesValues(newInst, rand, 0, 5, 0);
    }

    return newInst;
}
 
Example #3
Source File: DataSetUtils.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Instance matrixToInstance(final INDArray instance, final Instances refInstances) {
	if (instance == null || refInstances == null) {
		throw new IllegalArgumentException("Parameter 'instance' and 'refInstances' must not be null!");
	}

	// Create attributes
	final ArrayList<Attribute> attributes = new ArrayList<>();
	for (int i = 0; i < instance.length(); i++) {
		final Attribute newAtt = new Attribute("val" + i);
		attributes.add(newAtt);
	}

	final List<String> classValues = IntStream.range(0, refInstances.classAttribute().numValues()).asDoubleStream().mapToObj(String::valueOf).collect(Collectors.toList());
	final Attribute classAtt = new Attribute(CLASS_ATT_NAME, classValues);
	attributes.add(classAtt);

	final Instances result = new Instances(INSTANCES_DS_NAME, attributes, refInstances.size());
	result.setClassIndex(result.numAttributes() - 1);

	// Initialize instance
	final Instance inst = new DenseInstance(1, ArrayUtils.addAll(Nd4j.toFlattened(instance).toDoubleVector(), 0));
	inst.setDataset(result);

	return inst;
}
 
Example #4
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Instance getRefactoredInstance(final Instance instance, final List<String> classes) {

		/* modify instance */
		Instances dataset = WekaUtil.getEmptySetOfInstancesWithRefactoredClass(instance.dataset(), classes);
		int numAttributes = instance.numAttributes();
		int classIndex = instance.classIndex();
		Instance iNew = new DenseInstance(numAttributes);
		for (int i = 0; i < numAttributes; i++) {
			Attribute a = instance.attribute(i);
			if (i != classIndex) {
				iNew.setValue(a, instance.value(a));
			} else {
				iNew.setValue(a, 0.0); // the value does not matter since this should only be used for TESTING
			}
		}
		dataset.add(iNew);
		iNew.setDataset(dataset);
		return iNew;
	}
 
Example #5
Source File: InstanceTools.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public static Instance shiftSeries(Instance inst, int shift){
    Instance newInst = new DenseInstance(inst);

    if (shift < 0){
        shift = Math.abs(shift);

        for (int i = 0; i < inst.numAttributes()-shift-1; i++){
            newInst.setValue(i, inst.value(i+shift));
        }

        for (int i = inst.numAttributes()-shift-1; i < inst.numAttributes()-1; i++){
            newInst.setValue(i, 0);
        }
    }
    else if (shift > 0){
        for (int i = 0; i < shift; i++){
            newInst.setValue(i, 0);
        }

        for (int i = shift; i < inst.numAttributes()-1; i++){
            newInst.setValue(i, inst.value(i-shift));
        }
    }

    return newInst;
}
 
Example #6
Source File: BOSSC45.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
@Override
public double classifyInstance(Instance instance) throws Exception {
    Bag testBag = BOSSTransform(instance);

    //convert bag to instance
    double[] init = new double[bagInsts.numAttributes()];
    init[init.length-1] = testBag.getClassVal();

    //TEMPORARILY create it on the end of the train insts to easily copy over the attribute data.
    bagInsts.add(new DenseInstance(1, init));
    for (Entry<BitWordInt,Integer> entry : testBag.entrySet()) {
        Attribute att = bagInsts.attribute(entry.getKey().toString());
        if (att != null)
            bagInsts.get(bagInsts.size()-1).setValue(att, entry.getValue());
    }

    Instance testInst = bagInsts.remove(bagInsts.size()-1);

    return tree.classifyInstance(testInst);
}
 
Example #7
Source File: DecisionTree.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Classifier j48 = new J48();
    Instances trainingData = GenerateTestVessels.getData();
    j48.buildClassifier(trainingData);
    System.out.println(j48);



    double[] vesselUnderTest = GenerateTestVessels.getBarco(5);

    DenseInstance inst = new DenseInstance(1.0,vesselUnderTest);
    inst.setDataset(trainingData);
    inst.setClassMissing();
    System.out.println(inst);

    double result = j48.classifyInstance(inst);
    System.out.println(GenerateTestVessels.types[(int)result]);

    SerializationHelper.write(new FileOutputStream("tmp"), j48);
    J48 j48Read = (J48)SerializationHelper.read(new FileInputStream("tmp"));
}
 
Example #8
Source File: BOSSC45.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
@Override
public double[] distributionForInstance(Instance instance) throws Exception {
    Bag testBag = BOSSTransform(instance);

    //convert bag to instance
    double[] init = new double[bagInsts.numAttributes()];
    init[init.length-1] = testBag.getClassVal();

    //TEMPORARILY create it on the end of the train isnts to easily copy over the attribute data.
    bagInsts.add(new DenseInstance(1, init));
    for (Entry<BitWordInt,Integer> entry : testBag.entrySet()) {
        Attribute att = bagInsts.attribute(entry.getKey().toString());
        if (att != null)
            bagInsts.get(bagInsts.numInstances()-1).setValue(att, entry.getValue());
    }
    Instance testInst = bagInsts.remove(bagInsts.size()-1);

    return tree.distributionForInstance(testInst);
}
 
Example #9
Source File: OrbitMaskClassificationModel.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int classNum(int x, int y, final Raster raster) throws Exception {
   if (model==null) throw new IllegalStateException("Model is null. Please call initialize() before to set a mask model.");
   if (tissueFeatures==null) throw new IllegalStateException("tissueFeatures is null. Please call initialize() before to set a mask model.");
   int w = model.getFeatureDescription().getWindowSize();
   int xr = x;
   int yr = y;
   if (xr<raster.getMinX()+w) xr = raster.getMinX()+w;
   if (yr<raster.getMinY()+w) yr = raster.getMinY()+w;
   if (xr>raster.getMinX()+raster.getWidth()-1-w) xr = raster.getMinX()+raster.getWidth()-1-w;
   if (yr>raster.getMinY()+raster.getHeight()-1-w) yr = raster.getMinY()+raster.getHeight()-1-w;
   double[] features = tissueFeatures.buildFeatures(raster, xr, yr, Double.NaN);
   Instance instance = new DenseInstance(1d,features);
   instance.setDataset(model.getStructure());
   int clazz = (int) model.getClassifier().classifyInstance(instance);
   return clazz;
}
 
Example #10
Source File: RankOrder.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public Instances process(Instances inst) throws Exception {
//Set input instance format		  
		 
		  Instances result = new Instances(determineOutputFormat(inst), 0);
		  rankOrder(inst);
//Stuff into new set of instances
		  for(int i=0;i<inst.numInstances();i++) {
//Create a deep copy, think this is necessary to maintain meta data?
			  Instance in=new DenseInstance(inst.instance(i)); 
//Reset to the ranks
			  for(int j=0;j<numAtts;j++)
				  in.setValue(j, ranks[i][j]);
			  result.add(in);
		  }
		  if(normalise){
			  NormalizeAttribute na=new NormalizeAttribute(result);
			  result=na.process(result);
		  }
		  return result;

	  }
 
Example #11
Source File: ThreeWayMNBTrainer.java    From weka-mnb-sentiment-analysis-template-project with Apache License 2.0 6 votes vote down vote up
public SentimentClass.ThreeWayClazz classify(String sentence) throws Exception {
    double[] instanceValue = new double[dataRaw.numAttributes()];
    instanceValue[0] = dataRaw.attribute(0).addStringValue(sentence);

    Instance toClassify = new DenseInstance(1.0, instanceValue);
    dataRaw.setClassIndex(1);
    toClassify.setDataset(dataRaw);

    double prediction = this.classifier.classifyInstance(toClassify);

    double distribution[] = this.classifier.distributionForInstance(toClassify);

    if (distribution[0] != distribution[1])
        return SentimentClass.ThreeWayClazz.values()[(int)prediction];
    else
        return SentimentClass.ThreeWayClazz.NEUTRAL;
}
 
Example #12
Source File: WekaInstancesUtil.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Instances datasetToWekaInstances(final ILabeledDataset<? extends ILabeledInstance> dataset) throws UnsupportedAttributeTypeException {
	Instances wekaInstances = createDatasetFromSchema(dataset.getInstanceSchema());
	int expectedAttributes = dataset.getInstanceSchema().getNumAttributes();
	for (ILabeledInstance inst : dataset) {
		if (inst.getNumAttributes() != expectedAttributes) {
			throw new IllegalStateException("Dataset scheme defines a number of " + expectedAttributes + " attributes, but instance has " + inst.getNumAttributes() + ". Attributes in scheme: " + dataset.getInstanceSchema().getAttributeList().stream().map(a -> "\n\t" + a.getName() + " (" + a.toString() + ")").collect(Collectors.joining()) + ". Attributes in instance: " + Arrays.stream(inst.getAttributes()).map(a -> "\n\t" + a.toString()).collect(Collectors.joining()));
		}
		double[] point = inst.getPoint();
		double[] pointWithLabel = Arrays.copyOf(point, point.length + 1);
		DenseInstance iNew = new DenseInstance(1, pointWithLabel);
		iNew.setDataset(wekaInstances);
		if (dataset.getLabelAttribute() instanceof ICategoricalAttribute) {
			iNew.setClassValue(((ICategoricalAttribute) dataset.getLabelAttribute()).getLabelOfCategory((int)inst.getLabel()));
		} else {
			iNew.setClassValue(Double.parseDouble(inst.getLabel().toString()));
		}
		wekaInstances.add(iNew); // this MUST come here AFTER having set the class value; otherwise, the class is not registered correctly in the Instances object!!
	}
	return wekaInstances;
}
 
Example #13
Source File: StackingOnDists.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Instance buildInst(double[][] dists, Double classVal) {
    double[] instData = new double[numOutputAtts];
    
    int i = 0;
    for (int m = 0; m < dists.length; m++) 
        for (int c = 0; c < numClasses; c++) 
            instData[i++] = dists[m][c];
    
    assert(i == numOutputAtts-1);
    
    if (classVal != null)
        instData[i] = classVal; 
    //else irrelevent 
    
    instsHeader.add(new DenseInstance(1.0, instData));
    return instsHeader.remove(0);
}
 
Example #14
Source File: WekaTimeseriesUtil.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Converts a double[][] matrix (number of instances x number of attributes) to
 * Weka instances without any class attribute.
 *
 * @param matrix
 *            The double[][] matrix storing all the attribute values of the
 *            instances
 * @return Returns the Weka Instances object consisting of all instances and the
 *         attribute values
 */
public static Instances matrixToWekaInstances(final double[][] matrix) {
	final ArrayList<Attribute> attributes = new ArrayList<>();
	for (int i = 0; i < matrix[0].length; i++) {
		final Attribute newAtt = new Attribute("val" + i);
		attributes.add(newAtt);
	}
	Instances wekaInstances = new Instances(I_NAME, attributes, matrix.length);
	for (int i = 0; i < matrix[0].length; i++) {
		final Instance inst = new DenseInstance(1, matrix[i]);
		inst.setDataset(wekaInstances);
		wekaInstances.add(inst);
	}

	return wekaInstances;
}
 
Example #15
Source File: AttributeFilterBridge.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public Instance filterInstance(Instance ins){
	int nosDeleted=0;
	int nosKept=0;
	int dataPos=0;
	Instance newIns=new DenseInstance(ins);
		//Advance to the next to keep
	while(dataPos<newIns.numAttributes()-1 && nosKept<attsToKeep.length){
		while(dataPos!=attsToKeep[nosKept]-nosDeleted && dataPos<newIns.numAttributes()-1){
			newIns.deleteAttributeAt(dataPos);
			nosDeleted++;
		}
		nosKept++;
		dataPos++;
	}
	while(dataPos<newIns.numAttributes()-1)
		newIns.deleteAttributeAt(dataPos);
	return newIns;
	
}
 
Example #16
Source File: MergeInfrequentNominalValues.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Processes the given data.
 *
 * @param instances   the data to process
 * @return            the modified data
 * @throws Exception  in case the processing goes wrong
 */
public Instances process(Instances instances) throws Exception {

  // Generate the output and return it
  Instances result = new Instances(getOutputFormat(), instances.numInstances());
  for (int i = 0; i < instances.numInstances(); i++) {
    Instance inst = instances.instance(i);
    double[] newData = new double[instances.numAttributes()];
    for (int j = 0; j < instances.numAttributes(); j++) {
      if (m_AttToBeModified[j] && !inst.isMissing(j)) {
        newData[j] = m_NewValues[j][(int)inst.value(j)];
      } else {
        newData[j] = inst.value(j);
      }
    }
    DenseInstance instNew = new DenseInstance(1.0, newData);
    instNew.setDataset(result);
    
    // copy possible strings, relational values...
    copyValues(instNew, false, inst.dataset(), getOutputFormat());

    // Add instance to output
    result.add(instNew);
  }
  return result;
}
 
Example #17
Source File: DataSetUtilsTest.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public void cifar10InstancesAttributesTest() {
    ArrayList<Attribute> atts = new ArrayList<>();
    for (int i = 0; i < 32 * 32 * 3 + 1; i++) {
        atts.add(new Attribute("blub" + i));
    }
    Instances instances = new Instances("test", atts, 1);
    DenseInstance inst = new DenseInstance(atts.size());
    for (int i = 0; i < inst.numAttributes(); i++) {
        inst.setValue(i, 1d);
    }
    inst.setDataset(instances);
    instances.add(inst);

    INDArray result = DataSetUtils.cifar10InstanceToMatrix(inst);
    Assert.assertArrayEquals(new long[]{32, 32, 3}, result.shape());
}
 
Example #18
Source File: MINND.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Scale the given exemplar so that the returned exemplar
 * has the value of 0 to 1 for each dimension
 * 
 * @param before the given exemplar
 * @return the resultant exemplar after scaling
 * @throws Exception if given exampler cannot be scaled properly
 */
private Instance scale(Instance before) throws Exception{

  Instances afterInsts = before.relationalValue(1).stringFreeStructure();
  Instance after = new DenseInstance(before.numAttributes());
  after.setDataset(m_Attributes);

  for(int i=0; i < before.relationalValue(1).numInstances(); i++){
    Instance datum = before.relationalValue(1).instance(i);
    Instance inst = (Instance)datum.copy();

    for(int j=0; j < m_Dimension; j++){
      if(before.relationalValue(1).attribute(j).isNumeric())
        inst.setValue(j, (datum.value(j) - m_MinArray[j])/(m_MaxArray[j] - m_MinArray[j]));	
    }
    afterInsts.add(inst);
  }

  int attValue = after.attribute(1).addRelation(afterInsts);
  after.setValue(0, before.value( 0));
  after.setValue(1, attValue);	
  after.setValue(2, before.value( 2));

  return after;
}
 
Example #19
Source File: EntityTypeTest.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void organizationTest2() {
	EntityOrganization organa = new EntityOrganization();
	ArrayList<Attribute> fvWekaAttributes = new ArrayList<Attribute>();
	fvWekaAttributes.add(organa.getAttribute());
	new Instances("Test", fvWekaAttributes, 1);
	Instance testinstance = new DenseInstance(fvWekaAttributes.size());
	testinstance.setValue(organa.getAttribute(), (String) organa.analyze("Bart is a person."));
	assertTrue(testinstance.stringValue(organa.getAttribute()).equals("NoOrganization"));
}
 
Example #20
Source File: InstanceTools.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static Instance randomlyAddToSeriesValues(Instance inst, Random rand, int minAdd, int maxAdd, int maxValue){
    Instance newInst = new DenseInstance(inst);

    for (int i = 0; i < inst.numAttributes()-1; i++){
        int n = rand.nextInt(maxAdd+1-minAdd)+minAdd;
        double newVal = inst.value(i)+n;
        if (newVal > maxValue) newVal = maxValue;
        newInst.setValue(i, newVal);
    }

    return newInst;
}
 
Example #21
Source File: Analyzer.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Analyzes the question and extracts all features that were set for this Analyzer.
 * @param q question string
 * @return feature vector for the input question
 */
public Instance analyze(String q) {
	Instance tmpInstance = new DenseInstance(fvWekaAttributes.size());
	
	for (IAnalyzer analyzer : analyzers) {
		//special case for PartOfSpeechTags, need to set 36 attributes
		if(analyzer instanceof PartOfSpeechTags) {
			analyzePOS(tmpInstance, (PartOfSpeechTags) analyzer, q);
			continue;
		}		
		
		//special case for Dependencies, need to set 18 attributes
		if(analyzer instanceof Dependencies) {
			analyzeDeps(tmpInstance, (Dependencies) analyzer, q);
			continue;
		}
		
		Attribute attribute = analyzer.getAttribute();
		if (attribute.isNumeric()) {
			tmpInstance.setValue(attribute, (double) analyzer.analyze(q));
		} else if (attribute.isNominal() || attribute.isString()) {
			String value = (String) analyzer.analyze(q);
			tmpInstance.setValue(attribute,value);
			tmpInstance.setDataset(null);
		}
	}
	return tmpInstance;
}
 
Example #22
Source File: PLSNominalClassifier.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void buildClassifier(Instances data) throws Exception {
    Instances train = new Instances(data);
    
    numClasses = train.numClasses();
    classind = train.classIndex();
    classAttribute = train.classAttribute();
    
    FastVector<Attribute> atts = new FastVector<>(train.numAttributes());
    for (int i = 0; i < train.numAttributes(); i++) {
        if (i != classind)
            atts.add(train.attribute(i));
        else {
            //class attribute
            Attribute numericClassAtt = new Attribute(train.attribute(i).name());
            atts.add(numericClassAtt);
        }
    }
    
    Instances temp = new Instances(train.relationName(), atts, train.numInstances());
    temp.setClassIndex(classind);
    
    for (int i = 0; i < train.numInstances(); i++) {
        temp.add(new DenseInstance(1.0, train.instance(i).toDoubleArray()));
        temp.instance(i).setClassValue(train.instance(i).classValue());
    }
    
    train = temp;
    
    //datset is in the proper format, now do the model fitting as normal
    super.buildClassifier(train);
}
 
Example #23
Source File: ExtendedM5TreeTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void giveData() throws Exception {
	Instances trainingData = this.params.getTrainingData();
	ExtendedM5Tree tree = new ExtendedM5Tree();
	tree.buildClassifier(trainingData);

	StringBuilder X = new StringBuilder("[");
	StringBuilder Y = new StringBuilder("[");

	for (double i = -5; i <= 5; i += 0.01) {
		Instance instance = new DenseInstance(1);
		instance.setValue(0, i);
		double y = tree.classifyInstance(instance);
		if (i + 0.01 >= 5) {
			X.append(i + "]");
			Y.append(y + "]");

		} else {
			X.append(i + ",");
			Y.append(y + ",");
		}
	}
	Instance testInstance = new DenseInstance(2);
	testInstance.setValue(0, -3.7);
	testInstance.setValue(1, -2.3);
	tree.predictInterval(testInstance);
	assertTrue(true);
}
 
Example #24
Source File: RelExTool.java    From Criteria2Query with Apache License 2.0 5 votes vote down vote up
public String predict(String en1, String en2, Double e1e, Double e2s, Double dis, Double shortestdeppath)
		throws Exception {
	List entity1_type = Arrays.asList(GlobalSetting.primaryEntities);
	List entity2_type = Arrays.asList(GlobalSetting.atrributes);
	List rel = Arrays.asList(GlobalSetting.relations);
	Attribute entity1_end_index = new Attribute("entity1_end_index");
	Attribute entity2_start_index = new Attribute("entity2_start_index");
	Attribute distance = new Attribute("distance");
	Attribute shortestdep = new Attribute("shortestdep");
	Attribute entity1_type_attr = new Attribute("entity1_type", entity1_type);
	Attribute entity2_type_attr = new Attribute("entity2_type", entity2_type);
	Attribute rel_attr = new Attribute("rel", rel);

	ArrayList<Attribute> atts = new ArrayList<Attribute>();
	atts.add(entity1_type_attr);
	atts.add(entity2_type_attr);
	atts.add(entity1_end_index);
	atts.add(entity2_start_index);
	atts.add(distance);
	atts.add(shortestdep);
	atts.add(rel_attr);
	Instances adataset = new Instances("TestDataSet", atts, 1);
	Instance inst = new DenseInstance(7);
	inst.setValue(entity1_type_attr, en1);
	inst.setValue(entity2_type_attr, en2);
	inst.setValue(entity2_start_index, e2s);
	inst.setValue(entity1_end_index, e1e);
	inst.setValue(distance, dis);
	inst.setValue(shortestdep, shortestdeppath);
	// inst.setValue(rel_attr, "has-relation");
	inst.setDataset(adataset);
	adataset.setClassIndex(6);
	Double d = classifier.classifyInstance(inst);
	// System.out.println("?="+d);
	return (String) rel.get(d.intValue());
}
 
Example #25
Source File: ClusterMembership.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Convert a single instance over. The converted instance is added to 
  * the end of the output queue.
  *
  * @param instance the instance to convert
  * @throws Exception if something goes wrong
  */
 protected void convertInstance(Instance instance) throws Exception {
   
   // set up values
   double [] instanceVals = new double[outputFormatPeek().numAttributes()];
   double [] tempvals;
   if (instance.classIndex() >= 0) {
     tempvals = new double[outputFormatPeek().numAttributes() - 1];
   } else {
     tempvals = new double[outputFormatPeek().numAttributes()];
   }
   int pos = 0;
   for (int j = 0; j < m_clusterers.length; j++) {
     if (m_clusterers[j] != null) {
double [] probs;
if (m_removeAttributes != null) {
  m_removeAttributes.input(instance);
  probs = logs2densities(j, m_removeAttributes.output());
} else {
  probs = logs2densities(j, instance);
}
System.arraycopy(probs, 0, tempvals, pos, probs.length);
pos += probs.length;
     }
   }
   tempvals = Utils.logs2probs(tempvals);
   System.arraycopy(tempvals, 0, instanceVals, 0, tempvals.length);
   if (instance.classIndex() >= 0) {
     instanceVals[instanceVals.length - 1] = instance.classValue();
   }
   
   push(new DenseInstance(instance.weight(), instanceVals));
 }
 
Example #26
Source File: EntityTypeTest.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void personTest2() {
	EntityPerson personana = new EntityPerson();
	ArrayList<Attribute> fvWekaAttributes = new ArrayList<Attribute>();
	fvWekaAttributes.add(personana.getAttribute());
	new Instances("Test", fvWekaAttributes, 1);
	Instance testinstance = new DenseInstance(fvWekaAttributes.size());
	testinstance.setValue(personana.getAttribute(), (String) personana.analyze("Berlin is a city."));
	assertTrue(testinstance.stringValue(personana.getAttribute()).equals("NoPerson"));
}
 
Example #27
Source File: PropositionalToMultiInstance.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * adds a new bag out of the given data and adds it to the output
 * 
 * @param input       the intput dataset
 * @param output      the dataset this bag is added to
 * @param bagInsts    the instances in this bag
 * @param bagIndex    the bagIndex of this bag
 * @param classValue  the associated class value
 * @param bagWeight   the weight of the bag
 */
protected void addBag(
    Instances input,
    Instances output,
    Instances bagInsts, 
    int bagIndex, 
    double classValue, 
    double bagWeight) {
  
  // copy strings/relational values
  for (int i = 0; i < bagInsts.numInstances(); i++) {
    RelationalLocator.copyRelationalValues(
 bagInsts.instance(i), false, 
 input, m_InputRelAtts,
 bagInsts, m_BagRelAtts);

    StringLocator.copyStringValues(
 bagInsts.instance(i), false, 
 input, m_InputStringAtts,
 bagInsts, m_BagStringAtts);
  }
  
  int value = output.attribute(1).addRelation(bagInsts);
  Instance newBag = new DenseInstance(output.numAttributes());        
  newBag.setValue(0, bagIndex);
  newBag.setValue(2, classValue);
  newBag.setValue(1, value);
  newBag.setWeight(bagWeight);
  newBag.setDataset(output);
  output.add(newBag);
}
 
Example #28
Source File: MergeNominalValues.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Processes the given data.
 * 
 * @param instances the data to process
 * @return the modified data
 * @throws Exception in case the processing goes wrong
 */
@Override
public Instances process(Instances instances) throws Exception {

  // Generate the output and return it
  Instances result = new Instances(getOutputFormat(),
      instances.numInstances());
  for (int i = 0; i < instances.numInstances(); i++) {
    Instance inst = instances.instance(i);
    double[] newData = new double[instances.numAttributes()];
    for (int j = 0; j < instances.numAttributes(); j++) {
      if (m_AttToBeModified[j] && !inst.isMissing(j)) {
        newData[j] = m_Indicators[j][(int) inst.value(j)];
      } else {
        newData[j] = inst.value(j);
      }
    }
    DenseInstance instNew = new DenseInstance(1.0, newData);
    instNew.setDataset(result);

    // copy possible strings, relational values...
    copyValues(instNew, false, inst.dataset(), getOutputFormat());

    // Add instance to output
    result.add(instNew);
  }
  return result;
}
 
Example #29
Source File: EntityTypeTest.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void moneyTest1() {
	EntityMoney monana = new EntityMoney();
	ArrayList<Attribute> fvWekaAttributes = new ArrayList<Attribute>();
	fvWekaAttributes.add(monana.getAttribute());
	new Instances("Test", fvWekaAttributes, 1);
	Instance testinstance = new DenseInstance(fvWekaAttributes.size());
	testinstance.setValue(monana.getAttribute(), (String) monana.analyze("One Dollar is worth more than one Yen."));
	assertTrue(testinstance.stringValue(monana.getAttribute()).equals("Money"));
}
 
Example #30
Source File: EntityTypeTest.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void percentTest2() {
	EntityPercent percana = new EntityPercent();
	ArrayList<Attribute> fvWekaAttributes = new ArrayList<Attribute>();
	fvWekaAttributes.add(percana.getAttribute());
	new Instances("Test", fvWekaAttributes, 1);
	Instance testinstance = new DenseInstance(fvWekaAttributes.size());
	testinstance.setValue(percana.getAttribute(), (String) percana.analyze("Bart lives in Berlin."));
	assertTrue(testinstance.stringValue(percana.getAttribute()).equals("NoPercent"));
}