Java Code Examples for weka.core.Instance#attribute()

The following examples show how to use weka.core.Instance#attribute() . 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: ActiveHNode.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void updateNode(Instance inst) throws Exception {
  super.updateDistribution(inst);

  for (int i = 0; i < inst.numAttributes(); i++) {
    Attribute a = inst.attribute(i);
    if (i != inst.classIndex()) {
      ConditionalSufficientStats stats = m_nodeStats.get(a.name());
      if (stats == null) {
        if (a.isNumeric()) {
          stats = new GaussianConditionalSufficientStats();
        } else {
          stats = new NominalConditionalSufficientStats();
        }
        m_nodeStats.put(a.name(), stats);
      }

      stats
          .update(inst.value(a),
              inst.classAttribute().value((int) inst.classValue()),
              inst.weight());
    }
  }
}
 
Example 2
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 3
Source File: RDG1.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generates a new rule for the decision list
 * and classifies the new example.
 *
 * @param random random number generator
 * @param example the instance to classify
 * @return a list of tests
 * @throws Exception if dataset format not defined
 */
private FastVector generateTestList(Random random, Instance example) 
 throws Exception {

  Instances format = getDatasetFormat();
  if (format == null) 
    throw new Exception("Dataset format not defined.");

  int numTests = getNumAttributes() - getNumIrrelevant();
  FastVector TestList = new FastVector(numTests);
  boolean[] irrelevant = getAttList_Irr();

  for (int i = 0; i < getNumAttributes(); i++) {
    if (!irrelevant[i]) {
      Test newTest = null;
      Attribute att = example.attribute(i);
      if (att.isNumeric()) {
        double newSplit = random.nextDouble();
        boolean newNot = newSplit < example.value(i);
        newTest = new Test(i, newSplit, format, newNot);
      } else {
        newTest = new Test(i, example.value(i), format, false);
      }
    TestList.addElement (newTest);     
    }
  }
  
  return TestList;
}
 
Example 4
Source File: DatabaseConnectorTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testGetInstances() throws RetrieveInstancesFromDatabaseFailedException {
	Database db = DBUtils.deserializeFromFile(DATABASE_MODEL_FILE);
	Table customer = DBUtils.getTableByName("Customer", db);
	Table product = DBUtils.getTableByName("Product", db);

	// Select two features (one forward, one backward)
	List<AbstractFeature> selectedFeatures = new ArrayList<>();
	ForwardFeature firstName = new ForwardFeature(DBUtils.getAttributeByName("FirstName", customer));
	BackwardFeature price = new BackwardFeature(DBUtils.getAttributeByName("Price", product));
	Path path = price.getPath();
	path.addPathElement(new BackwardRelationship("Orders", "Product", "OrderId"), AggregationFunction.MAX);
	path.addPathElement(new BackwardRelationship("Customer", "Orders", "CustomerId"), AggregationFunction.AVG);
	selectedFeatures.add(firstName);
	selectedFeatures.add(price);

	// Get instances
	DatabaseConnector dbCon = new DatabaseConnectorImpl(db);
	Instances instances = dbCon.getInstances(selectedFeatures);

	// Cleanup in any case
	dbCon.cleanup();

	// Check correctness for first instance
	Instance i = instances.get(0);
	Attribute a = i.attribute(0);
	assertEquals("Alina", a.value((int) i.value(0)));
	assertEquals(15000, i.value(1), 0);
}