weka.core.Attribute Java Examples

The following examples show how to use weka.core.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: 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 #2
Source File: MultivariateProcessing.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public static void exampleUsage(){
//        ECGActivities
            Instances train,test;
            train=DatasetLoading.loadData("Z:\\Data\\MultivariateTSCProblems\\ECGActivities\\ECGActivities_TRAIN");
            test=DatasetLoading.loadData("Z:\\Data\\MultivariateTSCProblems\\ECGActivities\\ECGActivities_TEST");

//        Instances[] split=InstanceTools.resampleTrainAndTestInstances(train, test, 1);
            Instances[] split=MultivariateInstanceTools.resampleMultivariateTrainAndTestInstances(train, test, 1);
            System.out.println("IS it relational ? "+split[0].checkForAttributeType(Attribute.RELATIONAL));
            System.out.println("Fold 1 TRAIN num instances "+split[0].numInstances()+" Num atts ="+(split[0].numAttributes()-1));
//        System.out.println(split[0]+"");
            System.out.println("Fold 1 TRAIN  instance 1 num dimensions "+split[0].instance(0).relationalValue(0).numInstances()+" series length "+split[0].instance(0).relationalValue(0).numAttributes());
            for(Instance ins:split[0])
                System.out.println("Fold TRAIN  instance num dimensions "+ins.relationalValue(0).numInstances()+" series length "+ins.relationalValue(0).numAttributes());

        }
 
Example #3
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 #4
Source File: Reorder.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Sets the format of the input instances.
  *
  * @param instanceInfo an Instances object containing the input instance
  * structure (any instances contained in the object are ignored - only the
  * structure is required).
  * @return true if the outputFormat may be collected immediately
  * @throws Exception if a problem occurs setting the input format
  */
 public boolean setInputFormat(Instances instanceInfo) throws Exception {
   super.setInputFormat(instanceInfo);
   
   FastVector attributes = new FastVector();
   int outputClass = -1;
   m_SelectedAttributes = determineIndices(instanceInfo.numAttributes());
   for (int i = 0; i < m_SelectedAttributes.length; i++) {
     int current = m_SelectedAttributes[i];
     if (instanceInfo.classIndex() == current) {
outputClass = attributes.size();
     }
     Attribute keep = (Attribute)instanceInfo.attribute(current).copy();
     attributes.addElement(keep);
   }
   
   initInputLocators(instanceInfo, m_SelectedAttributes);

   Instances outputFormat = new Instances(instanceInfo.relationName(),
				   attributes, 0); 
   outputFormat.setClassIndex(outputClass);
   setOutputFormat(outputFormat);
   
   return true;
 }
 
Example #5
Source File: PerformanceKnowledgeBase.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
private Attribute getAttribute(final ComponentInstance ci, final Parameter parameter) {
	IParameterDomain domain = parameter.getDefaultDomain();
	if (domain instanceof CategoricalParameterDomain) {
		CategoricalParameterDomain catDomain = (CategoricalParameterDomain) domain;
		return new Attribute(ci.getComponent().getName() + "::" + parameter.getName(), Arrays.asList(catDomain.getValues()));
	} else if (domain instanceof NumericParameterDomain) {
		NumericParameterDomain numDomain = (NumericParameterDomain) domain;
		String range = "[" + numDomain.getMin() + "," + numDomain.getMax() + "]";
		Properties prop = new Properties();
		prop.setProperty("range", range);
		ProtectedProperties metaInfo = new ProtectedProperties(prop);
		return new Attribute(ci.getComponent().getName() + "::" + parameter.getName(), metaInfo);
	} else {
		return null;
	}
}
 
Example #6
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 #7
Source File: ChangeDateFormat.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Set the output format. Changes the format of the specified date
  * attribute.
  */
 private void setOutputFormat() {
   
   // Create new attributes
   FastVector newAtts = new FastVector(getInputFormat().numAttributes());
   for (int j = 0; j < getInputFormat().numAttributes(); j++) {
     Attribute att = getInputFormat().attribute(j);
     if (j == m_AttIndex.getIndex()) {
newAtts.addElement(new Attribute(att.name(), getDateFormat().toPattern()));  
     } else {
newAtts.addElement(att.copy()); 
     }
   }
     
   // Create new header
   Instances newData = new Instances(getInputFormat().relationName(), newAtts, 0);
   newData.setClassIndex(getInputFormat().classIndex());
   m_OutputAttribute = newData.attribute(m_AttIndex.getIndex());
   setOutputFormat(newData);
 }
 
Example #8
Source File: QuestionWord.java    From NLIWOD with GNU Affero General Public License v3.0 6 votes vote down vote up
public QuestionWord() {
	
	ArrayList<String> attributeValues = new ArrayList<String>();
	attributeValues.add("Who");
	attributeValues.add("What");
	attributeValues.add("When");
	attributeValues.add("Where");
	attributeValues.add("Which");
	attributeValues.add(Commands);
	attributeValues.add(AuxVerb);
	attributeValues.add("How");
	attributeValues.add("Misc");

	attribute = new Attribute("QuestionWord", attributeValues);
	
}
 
Example #9
Source File: Analyzer.java    From NLIWOD with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Retrieves the occurrences of all part-of-speech tags from the question. Then iterates over the HashMap and sets all 36 attributes for the instance.
 * @param tmpInstance
 * @param analyzer
 * @param q
 */
private void analyzePOS(Instance tmpInstance, PartOfSpeechTags analyzer, String q) {
	@SuppressWarnings("unchecked")
	LinkedHashMap<String,Integer> map = (LinkedHashMap<String, Integer>) analyzer.analyze(q);
	
	ArrayList<Attribute> attributes = analyzer.getAttributes();
	for(String ind: map.keySet()) {
		Attribute a = null;
		//searches for the attribute object of the HashMap entry
		for(Attribute att: attributes) {
			if(att.name().equals(ind)) {
				a = att;
				break;
			}
		}
		tmpInstance.setValue(a, map.get(ind));
	}
}
 
Example #10
Source File: MLUtils.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
/**
 * InsertZintoD - Insert data Z[][] to Instances D (e.g., as labels).
 * NOTE: Assumes binary labels!
 * @see #addZtoD(Instances, double[][], int)
 */
private static Instances insertZintoD(Instances D, double Z[][]) {

	int L = Z[0].length;

	// add attributes
	for(int j = 0; j < L; j++) {
		D.insertAttributeAt(new Attribute("c"+j,Arrays.asList(new String[]{"0","1"})),j);
	}

	// add values Z[0]...Z[N] to D
	// (note that if D.numInstances() < Z.length, only some are added)
	for(int j = 0; j < L; j++) {
		for(int i = 0; i < D.numInstances(); i++) {
			D.instance(i).setValue(j,Z[i][j] > 0.5 ? 1.0 : 0.0);
		}
	}

	D.setClassIndex(L);
	return D;
}
 
Example #11
Source File: Test.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the test represented by a string in Prolog notation.
 *
 * @return a string representing the test in Prolog notation
 */   
public String toPrologString() {
  Attribute att = m_Dataset.attribute(m_AttIndex);
  StringBuffer str = new StringBuffer();
  String attName = m_Dataset.attribute(m_AttIndex).name();
  if (att.isNumeric()) {
    str = str.append(attName + " ");
    if (m_Not) str = str.append(">= " + Utils.doubleToString(m_Split, 3));
    else str = str.append("< " + Utils.doubleToString(m_Split, 3));
  } else {
    String value = att.value((int)m_Split);
  
    if (value == "false") { str = str.append("not(" + attName + ")"); }      
    else { str = str.append(attName); }
  }
return str.toString();
}
 
Example #12
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 #13
Source File: Prism.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Is this attribute mentioned in the rule?
 *
 * @param attr the attribute to be checked for
 * @param t test contained by rule
 * @return true if the attribute is mentioned in the rule
 */
private static boolean isMentionedIn(Attribute attr, Test t) {

  if (t == null) { 
    return false;
  }
  if (t.m_attr == attr.index()) {
    return true;
  }
  return isMentionedIn(attr, t.m_next);
}
 
Example #14
Source File: ClusteringTask.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates the weka data set for clustering of samples
 *
 * @param rawData Data extracted from selected Raw data files and rows.
 * @return Weka library data set
 */
private Instances createSampleWekaDataset(double[][] rawData) {
  FastVector attributes = new FastVector();

  for (int i = 0; i < rawData[0].length; i++) {
    String varName = "Var" + i;
    Attribute var = new Attribute(varName);
    attributes.addElement(var);
  }

  if (clusteringStep.getModule().getClass().equals(HierarClusterer.class)) {
    Attribute name = new Attribute("name", (FastVector) null);
    attributes.addElement(name);
  }
  Instances data = new Instances("Dataset", attributes, 0);

  for (int i = 0; i < rawData.length; i++) {
    double[] values = new double[data.numAttributes()];
    System.arraycopy(rawData[i], 0, values, 0, rawData[0].length);
    if (clusteringStep.getModule().getClass().equals(HierarClusterer.class)) {
      values[data.numAttributes() - 1] =
          data.attribute("name").addStringValue(this.selectedRawDataFiles[i].getName());
    }
    Instance inst = new SparseInstance(1.0, values);
    data.add(inst);
  }
  return data;
}
 
Example #15
Source File: CheckClassifier.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks whether nominal schemes can handle more than two classes.
 * If a scheme is only designed for two-class problems it should
 * throw an appropriate exception for multi-class problems.
 *
 * @param nominalPredictor if true use nominal predictor attributes
 * @param numericPredictor if true use numeric predictor attributes
 * @param stringPredictor if true use string predictor attributes
 * @param datePredictor if true use date predictor attributes
 * @param relationalPredictor if true use relational predictor attributes
 * @param multiInstance whether multi-instance is needed
 * @param numClasses the number of classes to test
 * @return index 0 is true if the test was passed, index 1 is true if test
 *         was acceptable
 */
protected boolean[] canHandleNClasses(
    boolean nominalPredictor,
    boolean numericPredictor,
    boolean stringPredictor,
    boolean datePredictor,
    boolean relationalPredictor,
    boolean multiInstance,
    int numClasses) {

  print("more than two class problems");
  printAttributeSummary(
      nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, Attribute.NOMINAL);
  print("...");
  FastVector accepts = new FastVector();
  accepts.addElement("number");
  accepts.addElement("class");
  int numTrain = getNumInstances(), numTest = getNumInstances(),
  missingLevel = 0;
  boolean predictorMissing = false, classMissing = false;

  return runBasicTest(nominalPredictor, numericPredictor, stringPredictor,
                      datePredictor, relationalPredictor,
                      multiInstance,
                      Attribute.NOMINAL,
                      missingLevel, predictorMissing, classMissing,
                      numTrain, numTest, numClasses,
                      accepts);
}
 
Example #16
Source File: WekaInstance.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
private Object transformAttributeValueToData(final Attribute att) {
	if (att.isNominal() || att.isString() || att.isRelationValued() || att.isDate() || att.isRegular()) {
		return att.value((int) this.getElement().value(att));
	} else {
		return this.getElement().value(att);
	}
}
 
Example #17
Source File: Remove.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Sets the format of the input instances.
  *
  * @param instanceInfo an Instances object containing the input instance
  * structure (any instances contained in the object are ignored - only the
  * structure is required).
  * @return true if the outputFormat may be collected immediately
  * @throws Exception if the format couldn't be set successfully
  */
 public boolean setInputFormat(Instances instanceInfo) throws Exception {

   super.setInputFormat(instanceInfo);
   
   m_SelectCols.setUpper(instanceInfo.numAttributes() - 1);

   // Create the output buffer
   FastVector attributes = new FastVector();
   int outputClass = -1;
   m_SelectedAttributes = m_SelectCols.getSelection();
   for (int i = 0; i < m_SelectedAttributes.length; i++) {
     int current = m_SelectedAttributes[i];
     if (instanceInfo.classIndex() == current) {
outputClass = attributes.size();
     }
     Attribute keep = (Attribute)instanceInfo.attribute(current).copy();
     attributes.addElement(keep);
   }
   //initInputLocators(instanceInfo, m_SelectedAttributes);
   initInputLocators(getInputFormat(), m_SelectedAttributes);
   Instances outputFormat = new Instances(instanceInfo.relationName(),
				   attributes, 0); 
   outputFormat.setClassIndex(outputClass);
   setOutputFormat(outputFormat);
   return true;
 }
 
Example #18
Source File: ARMA.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
    protected Instances determineOutputFormat(Instances inputFormat)
                    throws Exception {
            //Check all attributes are real valued, otherwise throw exception
            for(int i=0;i<inputFormat.numAttributes();i++)
                    if(inputFormat.classIndex()!=i)
                            if(!inputFormat.attribute(i).isNumeric())
                                    throw new Exception("Non numeric attribute not allowed in ACF");

            if(inputFormat.classIndex()>=0)	//Classification set, dont transform the target class!
                    maxLag=(inputFormat.numAttributes()-1>maxLag)?maxLag:inputFormat.numAttributes()-1;
            else
                    maxLag=(inputFormat.numAttributes()>maxLag)?maxLag:inputFormat.numAttributes();
            //Set up instances size and format. 
            ArrayList<Attribute> atts=new ArrayList<>();
            String name;
            for(int i=0;i<maxLag;i++){
                    name = "ARMA_"+i;
                    atts.add(new Attribute(name));
            }
            if(inputFormat.classIndex()>=0){	//Classification set, set class 
//Get the class values as a fast vector			
                    Attribute target =inputFormat.attribute(inputFormat.classIndex());

                    ArrayList<String> vals=new ArrayList<>(target.numValues());
                    for(int i=0;i<target.numValues();i++)
                            vals.add(target.value(i));
                    atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals));

            }	
            Instances result = new Instances("ARMA"+inputFormat.relationName(),atts,inputFormat.numInstances());
            if(inputFormat.classIndex()>=0)
                    result.setClassIndex(result.numAttributes()-1);
            return result;	}
 
Example #19
Source File: MarginCurve.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates an Instances object with the attributes we will be calculating.
 *
 * @return the Instances structure.
 */
private Instances makeHeader() {

  FastVector fv = new FastVector();
  fv.addElement(new Attribute("Margin"));
  fv.addElement(new Attribute("Current"));
  fv.addElement(new Attribute("Cumulative"));
  return new Instances("MarginCurve", fv, 100);
}
 
Example #20
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 #21
Source File: NBNodeAdaptive.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double[] getDistribution(Instance inst, Attribute classAtt)
    throws Exception {

  if (m_majClassCorrectWeight > m_nbCorrectWeight) {
    return super.bypassNB(inst, classAtt);
  }

  return super.getDistribution(inst, classAtt);
}
 
Example #22
Source File: Add.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the current settings of the filter.
 *
 * @return an array of strings suitable for passing to setOptions
 */
public String [] getOptions() {
  Vector<String>	result;
  
  result = new Vector<String>();
  
  if (m_AttributeType != Attribute.NUMERIC) {
    result.add("-T");
    result.add("" + getAttributeType());
  }
  
  result.add("-N");
  result.add(Utils.backQuoteChars(getAttributeName()));
  
  if (m_AttributeType == Attribute.NOMINAL) {
    result.add("-L");
    result.add(getNominalLabels());
  }
  else if (m_AttributeType == Attribute.NOMINAL) {
    result.add("-F");
    result.add(getDateFormat());
  }
  
  result.add("-C");
  result.add("" + getAttributeIndex());

  return result.toArray(new String[result.size()]);
}
 
Example #23
Source File: DataSetUtils.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
static Instances matricesToInstances(final List<INDArray> matrices, final Instances refInstances) {
	if (matrices == null || matrices.isEmpty()) {
		throw new IllegalArgumentException("Parameter 'matrices' must not be null or empty!");
	}

	// Create attributes
	final ArrayList<Attribute> attributes = new ArrayList<>();
	for (int i = 0; i < matrices.get(0).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);

	for (int i = 0; i < matrices.size(); i++) {

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

		// Set class value
		inst.setClassValue(refInstances.get(i).classValue());

		result.add(inst);
	}

	return result;

}
 
Example #24
Source File: NominalItem.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a new NominalItem.
 * 
 * @param att the attribute that backs the item.
 * @param valueIndex the index of the value for this item.
 * @throws Exception if the NominalItem can't be constructed.
 */
public NominalItem(Attribute att, int valueIndex) throws Exception {
  
  super(att);
  
  if (att.isNumeric()) {
    throw new Exception("NominalItem must be constructed using a nominal attribute");
  }
  m_attribute = att;
  if (m_attribute.numValues() == 1) {
    m_valueIndex = 0; // unary attribute (? used to indicate absence from a basket)
  } else {
    m_valueIndex = valueIndex;
  }
}
 
Example #25
Source File: RnnSequenceClassifierTest.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testClassIndexAtPosZero() throws Exception {
  data = DatasetLoader.loadAnger();

  RnnOutputLayer out = new RnnOutputLayer();
  out.setLossFn(new LossMSE());
  out.setActivationFunction(new ActivationIdentity());
  clf.setLayers(out);

  // Create reversed attribute list
  ArrayList<Attribute> attsReversed = new ArrayList<>();
  for (int i = data.numAttributes() - 1; i >= 0; i--) {
    attsReversed.add(data.attribute(i));
  }

  // Create copy with class at pos 0 and text at pos 1
  Instances copy = new Instances("reversed", attsReversed, data.numInstances());
  data.forEach(
      d -> {
        Instance inst = new DenseInstance(2);
        inst.setDataset(copy);
        inst.setValue(0, d.classValue());
        inst.setValue(1, d.stringValue(0));
        copy.add(inst);
      });

  copy.setClassIndex(0);

  TestUtil.holdout(clf, copy);
}
 
Example #26
Source File: FPGrowth.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
private Instances parseTransactionsMustContain(Instances data) {
  String[] split = m_transactionsMustContain.trim().split(",");
  boolean[] transactionsMustContainIndexes = new boolean[data.numAttributes()];
  int numInTransactionsMustContainList = split.length;
  
  for (int i = 0; i < split.length; i++) {
    String attName = split[i].trim();
    Attribute att = data.attribute(attName);
    if (att == null) {
      System.err.println("[FPGrowth] : WARNING - can't find attribute " 
          + attName + " in the data.");
      numInTransactionsMustContainList--;
    } else {
      transactionsMustContainIndexes[att.index()] = true;
    }
  }
  
  if (numInTransactionsMustContainList == 0) {
    return data;
  } else {
    Instances newInsts = new Instances(data, 0);
    for (int i = 0; i < data.numInstances(); i++) {
      if (passesMustContain(data.instance(i), 
          transactionsMustContainIndexes, numInTransactionsMustContainList)) {
        newInsts.add(data.instance(i));
      }
    }
    newInsts.compactify();
    return newInsts;
  }
}
 
Example #27
Source File: CheckEstimator.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public Vector getVectorOfAttrTypes() {
  Vector attrs = new Vector();
  if (nominal) attrs.add(new Integer(Attribute.NOMINAL));
  if (numeric) attrs.add(new Integer(Attribute.NUMERIC));
  if (string) attrs.add(new Integer(Attribute.STRING));
  if (date) attrs.add(new Integer(Attribute.DATE));
  if (relational) attrs.add(new Integer(Attribute.RELATIONAL));
  return attrs;
}
 
Example #28
Source File: AnchorpointsCreationTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void anchorpointsAreCreatedAndHaveTheValues() throws IOException, InvalidAnchorPointsException, AlgorithmException, InterruptedException, ClassNotFoundException, DatasetCreationException {
	int[] xValues = new int[] { 2, 4, 8, 16, 32, 64 };
	Instances dataset = null;
	OpenmlConnector client = new OpenmlConnector();
	try {
		DataSetDescription description = client.dataGet(42);
		File file = client.datasetGet(description);
		DataSource source = new DataSource(file.getCanonicalPath());
		dataset = source.getDataSet();
		dataset.setClassIndex(dataset.numAttributes() - 1);
		Attribute targetAttribute = dataset.attribute(description.getDefault_target_attribute());
		dataset.setClassIndex(targetAttribute.index());
	} catch (Exception e) {
		throw new IOException("Could not load data set from OpenML!", e);
	}

	// final LearningCurveExtrapolationMethod extrapolationMethod, final ISupervisedLearner<I, D> learner, final D dataset, final double trainsplit, final int[] anchorPoints,
	// final ISamplingAlgorithmFactory<?, D, ? extends ASamplingAlgorithm<D>> samplingAlgorithmFactory, final long seed

	WekaInstances simpleDataset = new WekaInstances(dataset);
	LearningCurveExtrapolator extrapolator = new LearningCurveExtrapolator((x, y, ds) -> {
		Assert.assertArrayEquals(x, xValues);
		for (int i = 0; i < y.length; i++) {
			Assert.assertTrue(y[i] > 0.0d);
		}
		return null;
	}, new WekaClassifier(new J48()), simpleDataset, 0.7d, xValues, new SystematicSamplingFactory<>(), 1l);
	extrapolator.extrapolateLearningCurve();
}
 
Example #29
Source File: DataTableModel.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * returns the TYPE of the attribute at the given position
 *
 * @param rowIndex the index of the row
 * @param columnIndex the index of the column
 * @return the attribute type
 */
public int getType(int rowIndex, int columnIndex) {
	int result;

	result = Attribute.STRING;

	if ((rowIndex < 0) && columnIndex > 0 && columnIndex < getColumnCount()) {
		result = m_Data.attribute(columnIndex - 1).type();
	} else if ((rowIndex >= 0) && (rowIndex < getRowCount())
		&& (columnIndex > 0) && (columnIndex < getColumnCount())) {
		result = m_Data.instance(rowIndex).attribute(columnIndex - 1).type();
	}

	return result;
}
 
Example #30
Source File: BinaryItem.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param att the attribute that backs this item.
 * @param valueIndex the index of the value for this item.
 * @throws Exception if the backing attribute is not binary or unary.
 */
public BinaryItem(Attribute att, int valueIndex) throws Exception {
  super(att, valueIndex);
  
  if (att.isNumeric() || (att.isNominal() && att.numValues() > 2)) {
    throw new Exception("BinaryItem must be constructed using a nominal attribute" +
    		" with at most 2 values!");
  }
}