List of usage examples for weka.core Instance setValue
public void setValue(Attribute att, String value);
From source file:meka.classifiers.multilabel.meta.MBR.java
License:Open Source License
@Override public double[] distributionForInstance(Instance instance) throws Exception { int c = instance.classIndex(); double result[] = m_BASE.distributionForInstance(instance); instance.setDataset(null);/*from ww w . j av a2 s . com*/ for (int i = 0; i < c; i++) { instance.insertAttributeAt(c); } instance.setDataset(m_InstancesTemplate); for (int i = 0; i < c; i++) { instance.setValue(c + i, result[i]); } return m_META.distributionForInstance(instance); }
From source file:meka.core.CCUtils.java
License:Open Source License
/** * SetPath - set 'path[]' into the first L attributes of Instance 'xy'. * @param xy an Example (x,y)//from ww w . j ava2 s . co m * @param path a label vector */ public static void setPath(Instance xy, double path[]) { int L = xy.classIndex(); // = path.length for (int j = 0; j < L; j++) { xy.setValue(j, (int) Math.round(path[j])); // x = x + path_j } }
From source file:meka.core.Metrics.java
License:Open Source License
/** Get Data for Plotting PR and ROC curves. */ public static Instances curveDataMacroAveraged(int Y[][], double P[][]) { // Note: 'Threshold' contains the probability threshold that gives rise to the previous performance values. Instances curveData[] = curveData(Y, P); int L = curveData.length; int noNullIndex = -1; for (int i = 0; i < curveData.length; i++) { if (curveData[i] == null) { L--;// w w w. j a v a2s . com } else { if (noNullIndex == -1) { // checking for the first curveData that is not null (=does not consist of // only missing values or 0s) noNullIndex = i; } } } Instances avgCurve = new Instances(curveData[noNullIndex], 0); int D = avgCurve.numAttributes(); for (double t = 0.0; t < 1.; t += 0.01) { Instance x = (Instance) curveData[noNullIndex].instance(0).copy(); //System.out.println("x1\n"+x); boolean firstloop = true; for (int j = 0; j < L; j++) { // if there are only missing values in a column, curveData[j] is null if (curveData[j] == null) { continue; } int i = ThresholdCurve.getThresholdInstance(curveData[j], t); if (firstloop) { // reset for (int a = 0; a < D; a++) { x.setValue(a, curveData[j].instance(i).value(a) * 1. / L); } firstloop = false; } else { // add for (int a = 0; a < D; a++) { double v = x.value(a); x.setValue(a, v + curveData[j].instance(i).value(a) * 1. / L); } } } //System.out.println("x2\n"+x); avgCurve.add(x); } /* System.out.println(avgCurve); System.exit(1); // Average everything for (int i = 0; i < avgCurve.numInstances(); i++) { for(int j = 0; j < L; j++) { for (int a = 0; a < D; a++) { double o = avgCurve.instance(i).value(a); avgCurve.instance(i).setValue(a, o / L); } } } */ return avgCurve; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * CopyValues - Set x_dest[j+offset] = x_src[i+from]. */// w w w. jav a 2s .c o m public static final Instance copyValues(Instance x_dest, Instance x_src, int from, int offset) { int d = x_src.numAttributes(); for (int i = from, j = 0; i < d; i++, j++) { x_dest.setValue(j + offset, x_src.value(i)); } return x_dest; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * CopyValues - Set x_dest[i++] = x_src[j] for all j in indices[]. *//*from w ww .java2 s .c o m*/ public static final Instance copyValues(Instance x_dest, Instance x_src, int indices[]) { int i = 0; for (int j : indices) { x_dest.setValue(i++, x_src.value(j)); } return x_dest; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** * SetValues - set the attribute values in Instsance x (having L labels) to z[]. * TODO: call above method/*w ww . j a va2 s.co m*/ */ public static final Instance setValues(Instance x, double z[], int L) { for (int a = 0; a < z.length; a++) { x.setValue(L + a, z[a]); } return x; }
From source file:meka.core.MLUtils.java
License:Open Source License
/** Clear Labels -- set the value of all label attributes to 0.0 */ public static void clearLabels(Instance x) { int L = x.classIndex(); for (int j = 0; j < L; j++) x.setValue(j, 0.0); }
From source file:meka.core.Result.java
License:Open Source License
/** * Convert a list of Results into an Instances. * @param results An ArrayList of Results * @return Instances/* w w w .j av a2 s.c o m*/ */ 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; }
From source file:meka.core.SuperLabelUtils.java
License:Open Source License
/** * Super Label Transformation - transform dataset D into a dataset with <code>k</code> multi-class target attributes. * Use the NSR/PS-style pruning and recomposition, according to partition 'indices', and pruning values 'p' and 'n'. * @see PSUtils.PSTransformation// w ww . java2 s.c o m * @param indices m by k: m super variables, each relating to k original variables * @param D either multi-label or multi-target dataset * @param p pruning value * @param n subset relpacement value * @return a multi-target dataset */ public static Instances SLTransformation(Instances D, int indices[][], int p, int n) { int L = D.classIndex(); int K = indices.length; ArrayList<String> values[] = new ArrayList[K]; HashMap<String, Integer> counts[] = new HashMap[K]; // create D_ Instances D_ = new Instances(D); // clear D_ // F.removeLabels(D_,L); for (int j = 0; j < L; j++) { D_.deleteAttributeAt(0); } // create atts for (int j = 0; j < K; j++) { int att[] = indices[j]; //int values[] = new int[2]; //getValues(indices,D,p); counts[j] = getCounts(D, att, p); Set<String> vals = counts[j].keySet(); //getValues(D,att,p); values[j] = new ArrayList(vals); D_.insertAttributeAt(new Attribute(encodeClass(att), new ArrayList(vals)), j); } // copy over values ArrayList<Integer> deleteList = new ArrayList<Integer>(); for (int i = 0; i < D.numInstances(); i++) { Instance x = D.instance(i); for (int j = 0; j < K; j++) { String y = encodeValue(x, indices[j]); try { D_.instance(i).setValue(j, y); // y = } catch (Exception e) { // value not allowed deleteList.add(i); // mark it for deletion String y_close[] = getTopNSubsets(y, counts[j], n); // get N subsets for (int m = 0; m < y_close.length; m++) { //System.out.println("add "+y_close[m]+" "+counts[j]); Instance x_copy = (Instance) D_.instance(i).copy(); x_copy.setValue(j, y_close[m]); x_copy.setWeight(1.0 / y_close.length); D_.add(x_copy); } } } } // clean up Collections.sort(deleteList, Collections.reverseOrder()); //System.out.println("Deleting "+deleteList.size()+" defunct instances."); for (int i : deleteList) { D_.delete(i); } // set class D_.setClassIndex(K); // done! return D_; }
From source file:meka.filters.multilabel.SuperNodeFilter.java
License:Open Source License
/** * Merge Labels - Make a new 'D', with labels made into superlabels, according to partition 'indices', and pruning values 'p' and 'n'. * @param D assume attributes in D labeled by original index * @return Instances with attributes at j and k moved to position L as (j,k), with classIndex = L-1 *///from w w w. j a v a 2s . c om public static Instances mergeLabels(Instances D, int indices[][], int p, int n) { int L = D.classIndex(); int K = indices.length; ArrayList<String> values[] = new ArrayList[K]; HashMap<String, Integer> counts[] = new HashMap[K]; // create D_ Instances D_ = new Instances(D); // clear D_ for (int j = 0; j < L; j++) { D_.deleteAttributeAt(0); } // create atts for (int j = 0; j < K; j++) { int att[] = indices[j]; //int values[] = new int[2]; //getValues(indices,D,p); counts[j] = getCounts(D, att, p); Set<String> vals = counts[j].keySet(); //getValues(D,att,p); values[j] = new ArrayList(vals); D_.insertAttributeAt(new Attribute(encodeClass(att), new ArrayList(vals)), j); } // copy over values ArrayList<Integer> deleteList = new ArrayList<Integer>(); for (int i = 0; i < D.numInstances(); i++) { Instance x = D.instance(i); for (int j = 0; j < K; j++) { String y = encodeValue(x, indices[j]); try { D_.instance(i).setValue(j, y); // y = } catch (Exception e) { // value not allowed deleteList.add(i); // mark it for deletion String y_close[] = NSR.getTopNSubsets(y, counts[j], n); // get N subsets for (int m = 0; m < y_close.length; m++) { //System.out.println("add "+y_close[m]+" "+counts[j]); Instance x_copy = (Instance) D_.instance(i).copy(); x_copy.setValue(j, y_close[m]); x_copy.setWeight(1.0 / y_close.length); D_.add(x_copy); } } } } // clean up Collections.sort(deleteList, Collections.reverseOrder()); //System.out.println("Deleting "+deleteList.size()+" defunct instances."); for (int i : deleteList) { D_.delete(i); } // set class D_.setClassIndex(K); // done! D = null; return D_; }