Refine search
/** * Creates an Instances object with the attributes we will be calculating. * * @return the Instances structure. */ private Instances makeHeader() { ArrayList<Attribute> fv = new ArrayList<Attribute>(); fv.add(new Attribute("Margin")); fv.add(new Attribute("Current")); fv.add(new Attribute("Cumulative")); return new Instances("MarginCurve", fv, 100); }
private Instance makeOutputInstance(Instances output, Instance source) { double[] newVals = new double[output.numAttributes()]; for (int i = 0; i < newVals.length; i++) { newVals[i] = Utils.missingValue(); } for (int i = 0; i < source.numAttributes(); i++) { if (!source.isMissing(i)) { Attribute s = source.attribute(i); int outputIndex = output.attribute(s.name()).index(); if (s.isNumeric()) { newVals[outputIndex] = source.value(s); } else if (s.isString()) { String sVal = source.stringValue(s); newVals[outputIndex] = output.attribute(outputIndex).addStringValue( sVal); } else if (s.isRelationValued()) { Instances rVal = source.relationalValue(s); newVals[outputIndex] = output.attribute(outputIndex) .addRelation(rVal); } else if (s.isNominal()) { String nomVal = source.stringValue(s); newVals[outputIndex] = output.attribute(outputIndex).indexOfValue( nomVal); } } } Instance newInst = new DenseInstance(source.weight(), newVals); newInst.setDataset(output); return newInst; }
/** * Adds random instances to the dataset. * * @param dataset the dataset * @param numInstances the number of instances * @param random a random number generator */ protected void addRandomInstances( Instances dataset, int numInstances, Random random ) { int n = dataset.numAttributes(); double [] v = new double[ n ]; for( int i = 0; i < numInstances; i++ ) { for( int j = 0; j < n; j++ ) { Attribute att = dataset.attribute( j ); if( att.isNumeric() ) { v[ j ] = random.nextDouble(); } else if ( att.isNominal() ) { v[ j ] = random.nextInt( att.numValues() ); } } dataset.add( new DenseInstance( 1, v ) ); } }
/** * Determines the corresponding Obvious class for a Weka instances. * @param att * @return */ private Class<?> checkClass(Attribute att) { if (att.isDate()) { return Date.class; } else if (att.isNumeric()) { return Number.class; } else if (att.isNominal() || att.isString()) { return String.class; } return String.class; }
/** * Returns the number of attribute values. Returns 0 for attributes that are * not either nominal, string, or relation-valued. * * @return the number of attribute values */ public final/* @ pure @ */int numValues() { if (!isNominal() && !isString() && !isRelationValued()) { return 0; } else { return ((NominalAttributeInfo)m_AttributeInfo).m_Values.size(); } }
/** * bag class for getting the result of the loaded classifier */ private static class LoadedClassifier { private AbstractClassifier newClassifier = null; private Instances newHeader = null; }
/** * generates a class association rule out of a given premise. It randomly * chooses a class label as consequence. * * @param itemArray the (randomly constructed) premise of the class * association rule * @return a class association rule stored in a RuleItem */ public final RuleItem addCons(int[] itemArray) { ItemSet premise = new ItemSet(itemArray); int[] cons = new int[itemArray.length]; for (int i = 0; i < itemArray.length; i++) { cons[i] = -1; } cons[m_instances.classIndex()] = m_randNum.nextInt((m_instances .attribute(m_instances.classIndex())).numValues()); ItemSet consequence = new ItemSet(cons); RuleItem current = new RuleItem(); current.m_premise = premise; current.m_consequence = consequence; return current; }
double value = random.nextDouble(); if (format.attribute(i).isNumeric()) { attributes[i] = value; } else { if (format.attribute(i).isNominal()) { attributes[i] = (value > 0.5) ? 1.0 : 0.0; } else { example = new DenseInstance(1.0, attributes); example.setDataset(format); example.setClassMissing();
newInst = new DenseInstance(model.numAttributes()); newInst.setDataset(model); for (int i = 0, j = 0; i < model.numAttributes(); i++) { if (model.attribute(i).isNumeric()) { if (j >= m_Elements.length) throw new Exception("Datatypes are not compatible."); newInst.setValue(i, m_Elements[j++]); if (model.attribute(i).isNominal()) { int newVal = (int) (random.nextDouble() * (double) (model.attribute(i).numValues())); if (newVal == (int) model.attribute(i).numValues()) newVal -= 1; newInst.setValue(i, newVal);
/** * SwitchAttributes - Move label attributes from End to Beginning of attribute space (MULAN format to MEKA format). * Note: can use e.g.: java weka.filters.unsupervised.attribute.Reorder -i thyroid.arff -R 30-last,1-29" */ public static final Instances switchAttributes(Instances D, int L) { int d = D.numAttributes(); for(int j = 0; j < L; j++) { D.insertAttributeAt(D.attribute(d-1).copy(D.attribute(d-1).name()+"-"),0); for(int i = 0; i < D.numInstances(); i++) { D.instance(i).setValue(0,D.instance(i).value(d)); } D.deleteAttributeAt(d); } return D; }
/** * Sets split point to greatest value in given data smaller or equal to old * split point. (C4.5 does this for some strange reason). */ public final void setSplitPoint(Instances allInstances) { double newSplitPoint = -Double.MAX_VALUE; if ((allInstances.attribute(m_attIndex).isNumeric()) && (m_numSubsets > 1)) { for (int i = 0; i < allInstances.numInstances(); i++) { Instance instance = allInstances.instance(i); double tempValue = instance.value(m_attIndex); if (!Utils.isMissingValue(tempValue)) { if ((tempValue > newSplitPoint) && (tempValue <= m_splitPoint)) { newSplitPoint = tempValue; } } } m_splitPoint = newSplitPoint; } }
/** * Makes a level-1 instance from the given instance. * * @param instance the instance to be transformed * @return the level-1 instance * @throws Exception if the instance generation fails */ protected Instance metaInstance(Instance instance) throws Exception { double[] values = new double[m_MetaFormat.numAttributes()]; Instance metaInstance; int i = 0; for (int k = 0; k < m_Classifiers.length; k++) { Classifier classifier = getClassifier(k); if (m_BaseFormat.classAttribute().isNumeric()) { values[i++] = classifier.classifyInstance(instance); } else { double[] dist = classifier.distributionForInstance(instance); for (int j = 0; j < dist.length; j++) { values[i++] = dist[j]; } } } values[i] = instance.classValue(); metaInstance = new DenseInstance(1, values); metaInstance.setDataset(m_MetaFormat); return metaInstance; }
@Override public void buildClassifier(Instances D) throws Exception { testCapabilities(D); int L = D.classIndex(); // Transform Instances if(getDebug()) System.out.print("Transforming Instances ..."); Instances D_ = PSUtils.LCTransformation(D,L); m_InstancesTemplate = new Instances(D_,0); // Set Info ; Build Classifier info = "K = "+m_InstancesTemplate.attribute(0).numValues() + ", N = "+D_.numInstances(); if(getDebug()) System.out.print("Building Classifier ("+info+"), ..."); m_Classifier.buildClassifier(D_); if(getDebug()) System.out.println("Done"); }
public void testTypical() { Instances result = useFilter(); // Number of attributes and instances shouldn't change assertEquals(m_Instances.numAttributes(), result.numAttributes()); assertEquals(m_Instances.numInstances(), result.numInstances()); assertEquals("Attribute type should now be NOMINAL", Attribute.NOMINAL, result.attribute(0).type()); assertEquals(14, result.attribute(0).numValues()); }
/** * Convert a list of Results into an Instances. * @param results An ArrayList of Results * @return Instances */ public static Instances getResultsAsInstances(ArrayList<HashMap<String,Object>> metrics) { HashMap<String,Object> o_master = metrics.get(0); ArrayList<Attribute> attInfo = new ArrayList<Attribute>(); for (String key : o_master.keySet()) { if (o_master.get(key) instanceof Double) { //System.out.println("key="+key); attInfo.add(new Attribute(key)); } } Instances resultInstances = new Instances("Results",attInfo,metrics.size()); for (HashMap<String,Object> o : metrics) { Instance rx = new DenseInstance(attInfo.size()); for (Attribute att : attInfo) { String name = att.name(); rx.setValue(att,(double)o.get(name)); } resultInstances.add(rx); } //System.out.println(""+resultInstances); return resultInstances; }
@Override public String classify(Instance instance) throws Exception { weka.core.Instance wekaInstance = new weka.core.Instance(wekaTrainingData.numAttributes()); wekaInstance.setDataset(wekaTrainingData); double[] histogramPercent = instance.getHistogramPercent(); for (int i = 0; i < histogramPercent.length; i++) { wekaInstance.setValue(i, histogramPercent[i]); } wekaInstance.setMissing(wekaTrainingData.attribute("class")); double wekaClassification = classifier.classifyInstance(wekaInstance); String classification = wekaTrainingData.attribute("class").value((int)wekaClassification); return classification; }
@Override public void buildClassifier(Instances D) throws Exception { testCapabilities(D); int L = D.classIndex(); if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): "); m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L); m_Templates = new Instances[L]; for(int j = 0; j < L; j++) { //Select only class attribute 'j' m_Templates[j] = MLUtils.keepAttributesAt(new Instances(D),new int[]{j},L); m_Templates[j].setClassIndex(0); //Build the classifier for that class m_MultiClassifiers[j].buildClassifier(m_Templates[j]); if(getDebug()) System.out.print(" " + (m_Templates[j].classAttribute().name())); m_Templates[j] = new Instances(m_Templates[j], 0); } }
/** * jPMF - Joint PMF. * @return the joint PMF of the j-th and k-th labels in D. */ public static double[][] jPMF(Instances D, int j, int k) { double JOINT[][] = new double[D.attribute(j).numValues()][D.attribute(k).numValues()]; int N = D.numInstances(); for(int i = 0; i < N; i++) { int v_j = (int)Math.round(D.instance(i).value(j)); int v_k = (int)Math.round(D.instance(i).value(k)); JOINT[v_j][v_k] += (1.0 / (double)N); } return JOINT; }
public void testTypical() { m_Filter = getFilter("1,2"); Instances result = useFilter(); int origNum = m_Instances.numAttributes(); assertEquals(origNum + 2, result.numAttributes()); assertTrue(result.attribute(origNum).name().endsWith(m_Instances.attribute(0).name())); assertTrue(result.attribute(origNum + 1).name().endsWith(m_Instances.attribute(1).name())); }