@SuppressWarnings("unchecked") @Override public Enumeration enumerateAttributes() { return super.enumerateAttributes(); }
@Override public Enumeration enumerateAttributes() { return dataset().enumerateAttributes(); }
/** * Returns an enumeration of all the attributes. * * @return enumeration of all the attributes * @throws UnassignedDatasetException if the instance doesn't have access to a * dataset */ // @ requires m_Dataset != null; @Override public/* @pure@ */Enumeration<Attribute> enumerateAttributes() { if (m_Dataset == null) { throw new UnassignedDatasetException( "Instance doesn't have access to a dataset!"); } return m_Dataset.enumerateAttributes(); }
/** * Returns an enumeration of all the attributes. * * @return enumeration of all the attributes * @throws UnassignedDatasetException if the instance doesn't have access to a * dataset */ // @ requires m_Dataset != null; @Override public/* @pure@ */Enumeration<Attribute> enumerateAttributes() { if (m_Dataset == null) { throw new UnassignedDatasetException( "Instance doesn't have access to a dataset!"); } return m_Dataset.enumerateAttributes(); }
/** * @return The offset of the instanceId attribute within the weka instance */ @SuppressWarnings("unchecked") public static int getInstanceIdAttributeOffset(Instances data) { int attOffset = 1; Enumeration<Attribute> enumeration = data.enumerateAttributes(); while (enumeration.hasMoreElements()) { Attribute att = enumeration.nextElement(); // System.out.println(att.name()); if (att.name().equals(AddIdFeatureExtractor.ID_FEATURE_NAME)) { return attOffset; } attOffset++; } return -1; }
/** * Check if a set of Weka instances has 3D image attributes * (given by the presence of the attribute names from FeatureStack3D * which are not present in FeatureStack). * @param data set of Weka instances * @return true if the set contains exclusive 3D attributes, false otherwise */ public boolean containsExclusive3DFeatures( Instances data ) { // make list of exclusive 3D feature names final ArrayList<String> names3D = new ArrayList<String>(); for( int i=0; i<FeatureStack3D.availableFeatures.length; i++ ) names3D.add( FeatureStack3D.availableFeatures[ i ] ); for( int i=0; i<FeatureStack.availableFeatures.length; i++ ) { if( names3D.contains( FeatureStack.availableFeatures[ i ] ) ) names3D.remove( FeatureStack.availableFeatures[ i ] ); } Enumeration<Attribute> attributes = data.enumerateAttributes(); while(attributes.hasMoreElements()) { final Attribute a = attributes.nextElement(); for( String s : names3D ) if( a.name().startsWith( s ) ) return true; } return false; } /**
/** * Check if a set of Weka instances has 2D image attributes * (given by the presence of the attribute names from FeatureStack * which are not present in FeatureStack3D). * @param data set of Weka instances * @return true if the set contains exclusive 2D attributes, false otherwise */ public boolean containsExclusive2DFeatures( Instances data ) { // make list of exclusive 2D feature names final ArrayList<String> names2D = new ArrayList<String>(); for( int i=0; i<FeatureStack.availableFeatures.length; i++ ) names2D.add( FeatureStack.availableFeatures[ i ] ); for( int i=0; i<FeatureStack3D.availableFeatures.length; i++ ) { if( names2D.contains( FeatureStack3D.availableFeatures[ i ] ) ) names2D.remove( FeatureStack3D.availableFeatures[ i ] ); } Enumeration<Attribute> attributes = data.enumerateAttributes(); while(attributes.hasMoreElements()) { final Attribute a = attributes.nextElement(); for( String s : names2D ) if( a.name().startsWith( s ) ) return true; } return false; } /**
/** * Check if a set of Weka instances has 2D image attributes * (given by the presence of the attribute names from FeatureStack * which are not present in FeatureStack3D). * @param data set of Weka instances * @return true if the set contains exclusive 2D attributes, false otherwise */ public boolean containsExclusive2DFeatures( Instances data ) { // make list of exclusive 2D feature names final ArrayList<String> names2D = new ArrayList<String>(); for( int i=0; i<FeatureStack.availableFeatures.length; i++ ) names2D.add( FeatureStack.availableFeatures[ i ] ); for( int i=0; i<FeatureStack3D.availableFeatures.length; i++ ) { if( names2D.contains( FeatureStack3D.availableFeatures[ i ] ) ) names2D.remove( FeatureStack3D.availableFeatures[ i ] ); } Enumeration<Attribute> attributes = data.enumerateAttributes(); while(attributes.hasMoreElements()) { final Attribute a = attributes.nextElement(); for( String s : names2D ) if( a.name().startsWith( s ) ) return true; } return false; } /**
/** * Check if a set of Weka instances has 3D image attributes * (given by the presence of the attribute names from FeatureStack3D * which are not present in FeatureStack). * @param data set of Weka instances * @return true if the set contains exclusive 3D attributes, false otherwise */ public boolean containsExclusive3DFeatures( Instances data ) { // make list of exclusive 3D feature names final ArrayList<String> names3D = new ArrayList<String>(); for( int i=0; i<FeatureStack3D.availableFeatures.length; i++ ) names3D.add( FeatureStack3D.availableFeatures[ i ] ); for( int i=0; i<FeatureStack.availableFeatures.length; i++ ) { if( names3D.contains( FeatureStack.availableFeatures[ i ] ) ) names3D.remove( FeatureStack.availableFeatures[ i ] ); } Enumeration<Attribute> attributes = data.enumerateAttributes(); while(attributes.hasMoreElements()) { final Attribute a = attributes.nextElement(); for( String s : names3D ) if( a.name().startsWith( s ) ) return true; } return false; } /**
/** * Creates a set of {@link Attributes} from a given {@link weka.core.Instances} object. * @param modelContext a {@link weka.core.Instances} object. * @return a set of {@link Attributes}. */ public static Attributes convertAttributes(Instances modelContext){ Enumeration attributesWeka = modelContext.enumerateAttributes(); return convertAttributes(attributesWeka, modelContext.classAttribute()); }
/** * Check if a set of Weka instances has color attributes * (given by the presence of the attribute names "Hue", * "Saturation" or "Brightness"). * @param data set of Weka instances * @return true if the set contains color attributes, false otherwise */ public boolean containsColorFeatures( Instances data ) { Enumeration<Attribute> attributes = data.enumerateAttributes(); while(attributes.hasMoreElements()) { final Attribute a = attributes.nextElement(); if( a.name().equals( "Hue" ) || a.name().equals( "Saturation" ) || a.name().equals( "Brightness" ) ) return true; } return false; } /**
/** * Check if a set of Weka instances has color attributes * (given by the presence of the attribute names "Hue", * "Saturation" or "Brightness"). * @param data set of Weka instances * @return true if the set contains color attributes, false otherwise */ public boolean containsColorFeatures( Instances data ) { Enumeration<Attribute> attributes = data.enumerateAttributes(); while(attributes.hasMoreElements()) { final Attribute a = attributes.nextElement(); if( a.name().equals( "Hue" ) || a.name().equals( "Saturation" ) || a.name().equals( "Brightness" ) ) return true; } return false; } /**
/** * Constructor. * @param inst a Weka Instances instance */ @SuppressWarnings("unchecked") public WekaObviousTable(Instances inst) { this.instances = inst; this.schema = new SchemaImpl(); Enumeration attributes = instances.enumerateAttributes(); while (attributes.hasMoreElements()) { Attribute att = (Attribute) attributes.nextElement(); Class<?> c = checkClass(att); schema.addColumn(att.name(), c, null); } }
/** * Compute the number of all possible conditions that could appear in a rule * of a given data. For nominal attributes, it's the number of values that * could appear; for numeric attributes, it's the number of values * 2, i.e. * <= and >= are counted as different possible conditions. * * @param data the given data * @return number of all conditions of the data */ public static double numAllConditions(Instances data) { double total = 0; Enumeration<Attribute> attEnum = data.enumerateAttributes(); while (attEnum.hasMoreElements()) { Attribute att = attEnum.nextElement(); if (att.isNominal()) { total += att.numValues(); } else { total += 2.0 * data.numDistinctValues(att); } } return total; }
/** * Compute the number of all possible conditions that could appear in a rule * of a given data. For nominal attributes, it's the number of values that * could appear; for numeric attributes, it's the number of values * 2, i.e. * <= and >= are counted as different possible conditions. * * @param data the given data * @return number of all conditions of the data */ public static double numAllConditions(Instances data) { double total = 0; Enumeration<Attribute> attEnum = data.enumerateAttributes(); while (attEnum.hasMoreElements()) { Attribute att = attEnum.nextElement(); if (att.isNominal()) { total += att.numValues(); } else { total += 2.0 * data.numDistinctValues(att); } } return total; }
/** * Updates the classifier with the given instance. * * @param instance the new training instance to include in the model * @exception Exception if the instance could not be incorporated in the * model. */ public void updateClassifier(Instance instance) throws Exception { if (!instance.classIsMissing()) { Enumeration<Attribute> enumAtts = m_Instances.enumerateAttributes(); int attIndex = 0; while (enumAtts.hasMoreElements()) { Attribute attribute = enumAtts.nextElement(); if (!instance.isMissing(attribute)) { m_Distributions[attIndex][(int) instance.classValue()].addValue( instance.value(attribute), instance.weight()); } attIndex++; } m_ClassDistribution.addValue(instance.classValue(), instance.weight()); } }
/** * Updates the classifier with the given instance. * * @param instance the new training instance to include in the model * @exception Exception if the instance could not be incorporated in the * model. */ public void updateClassifier(Instance instance) throws Exception { if (!instance.classIsMissing()) { Enumeration<Attribute> enumAtts = m_Instances.enumerateAttributes(); int attIndex = 0; while (enumAtts.hasMoreElements()) { Attribute attribute = enumAtts.nextElement(); if (!instance.isMissing(attribute)) { m_Distributions[attIndex][(int) instance.classValue()].addValue( instance.value(attribute), instance.weight()); } attIndex++; } m_ClassDistribution.addValue(instance.classValue(), instance.weight()); } }
/** * ensure that all variables are nominal and that there are no missing values * * @param instances data set to check and quantize and/or fill in missing * values * @return filtered instances * @throws Exception if a filter (Discretize, ReplaceMissingValues) fails */ protected Instances normalizeDataSet(Instances instances) throws Exception { m_nNonDiscreteAttribute = -1; Enumeration<Attribute> enu = instances.enumerateAttributes(); while (enu.hasMoreElements()) { Attribute attribute = enu.nextElement(); if (attribute.type() != Attribute.NOMINAL) { m_nNonDiscreteAttribute = attribute.index(); } } if ((m_nNonDiscreteAttribute > -1) && (instances.attribute(m_nNonDiscreteAttribute).type() != Attribute.NOMINAL)) { m_DiscretizeFilter = new Discretize(); m_DiscretizeFilter.setInputFormat(instances); instances = Filter.useFilter(instances, m_DiscretizeFilter); } m_MissingValuesFilter = new ReplaceMissingValues(); m_MissingValuesFilter.setInputFormat(instances); instances = Filter.useFilter(instances, m_MissingValuesFilter); return instances; } // normalizeDataSet
/** * ensure that all variables are nominal and that there are no missing values * * @param instances data set to check and quantize and/or fill in missing * values * @return filtered instances * @throws Exception if a filter (Discretize, ReplaceMissingValues) fails */ protected Instances normalizeDataSet(Instances instances) throws Exception { m_nNonDiscreteAttribute = -1; Enumeration<Attribute> enu = instances.enumerateAttributes(); while (enu.hasMoreElements()) { Attribute attribute = enu.nextElement(); if (attribute.type() != Attribute.NOMINAL) { m_nNonDiscreteAttribute = attribute.index(); } } if ((m_nNonDiscreteAttribute > -1) && (instances.attribute(m_nNonDiscreteAttribute).type() != Attribute.NOMINAL)) { m_DiscretizeFilter = new Discretize(); m_DiscretizeFilter.setInputFormat(instances); instances = Filter.useFilter(instances, m_DiscretizeFilter); } m_MissingValuesFilter = new ReplaceMissingValues(); m_MissingValuesFilter.setInputFormat(instances); instances = Filter.useFilter(instances, m_MissingValuesFilter); return instances; } // normalizeDataSet
@Override public void buildClusterer(Instances data) throws Exception { attributes_ = Converter.convertAttributes(data.enumerateAttributes()); Variables modelHeader = new Variables(attributes_); clusterVar_ = modelHeader.newMultinomialVariable("clusterVar", this.numberOfClusters()); inferenceAlgorithm_ = new ImportanceSampling(); inferenceAlgorithm_.setSeed(this.getSeed()); dag = new DAG(modelHeader); /* Set DAG structure. */ /* Add the hidden cluster variable as a parent of all the predictive attributes. */ dag.getParentSets().stream() .filter(w -> w.getMainVar().getVarID() != clusterVar_.getVarID()) .filter(w -> w.getMainVar().isObservable()) .forEach(w -> w.addParent(clusterVar_)); System.out.println(dag.toString()); parameterLearningAlgorithm_ = new SVB(); parameterLearningAlgorithm_.setDAG(dag); DataOnMemoryListContainer<DataInstance> batch_ = new DataOnMemoryListContainer(attributes_); data.stream().forEach(instance -> batch_.add(new DataInstanceFromDataRow(new DataRowWeka(instance, this.attributes_))) ); parameterLearningAlgorithm_.setDataStream(batch_); parameterLearningAlgorithm_.initLearning(); parameterLearningAlgorithm_.runLearning(); bnModel_ = parameterLearningAlgorithm_.getLearntBayesianNetwork(); System.out.println(bnModel_); inferenceAlgorithm_.setModel(bnModel_); }