List of usage examples for weka.core Instance weight
public double weight();
From source file:myclassifier.myC45Pack.ClassDistribution.java
/** * Adds given instance to all bags weighting it according to given weights. * * @exception Exception if something goes wrong *//*from w w w . j ava 2s. c o m*/ public void addWeights(Instance instance, double[] weights) throws Exception { int classIndex; classIndex = (int) instance.classValue(); for (int i = 0; i < w_perSubdataset.length; i++) { double weight = instance.weight() * weights[i]; w_perClassPerSubdataset[i][classIndex] += weight; w_perSubdataset[i] += weight; w_perClass[classIndex] += weight; totalWeights += weight; } }
From source file:myclassifier.myC45Pack.ClassDistribution.java
/** * Move instances in given range from one bag to another one. * * @exception Exception if something goes wrong *//* w ww .jav a 2s . c o m*/ public void moveInstancesWithRange(int from, int to, Instances dataSet, int startIndex, int lastIndex) throws Exception { int classIndex; double weight; Instance data; for (int i = startIndex; i < lastIndex; i++) { data = (Instance) dataSet.instance(i); classIndex = (int) data.classValue(); weight = data.weight(); w_perClassPerSubdataset[from][classIndex] -= weight; w_perClassPerSubdataset[to][classIndex] += weight; w_perSubdataset[from] -= weight; w_perSubdataset[to] += weight; } }
From source file:myclusterer.MyKMeans.java
private Instance createCentroid(Instances members) { double[] vals = new double[members.numAttributes()]; double[][] nominalDists = new double[members.numAttributes()][]; double[] weightMissing = new double[members.numAttributes()]; double[] weightNonMissing = new double[members.numAttributes()]; for (int j = 0; j < members.numAttributes(); j++) { if (members.attribute(j).isNominal()) { nominalDists[j] = new double[members.attribute(j).numValues()]; }//w ww. j a va 2 s .com } for (int i = 0; i < members.numInstances(); ++i) { Instance inst = members.instance(i); for (int j = 0; j < members.numAttributes(); j++) { if (inst.isMissing(j)) { weightMissing[j] += inst.weight(); } else { weightNonMissing[j] += inst.weight(); if (members.attribute(j).isNumeric()) vals[j] += inst.weight() * inst.value(j); else nominalDists[j][(int) inst.value(j)] += inst.weight(); } } } for (int i = 0; i < members.numAttributes(); i++) { if (members.attribute(i).isNumeric()) { if (weightNonMissing[i] > 0) { vals[i] /= weightNonMissing[i]; } else { vals[i] = Instance.missingValue(); } } else { double max = -Double.MAX_VALUE; double maxIndex = -1; for (int j = 0; j < nominalDists[i].length; j++) { if (nominalDists[i][j] > max) { max = nominalDists[i][j]; maxIndex = j; } vals[i] = max < weightMissing[i] ? Instance.missingValue() : maxIndex; } } } return new Instance(1.0, vals); }
From source file:net.sf.bddbddb.order.MyId3.java
License:LGPL
/** * Method for building an Id3 tree./*from ww w . java2s . co m*/ * * @param data * the training data * @exception Exception * if decision tree can't be built successfully */ private void makeTree(Instances data) throws Exception { // Check if no instances have reached this node. if (data.numInstances() == 0) { m_Attribute = null; m_ClassValue = Instance.missingValue(); m_Distribution = new double[data.numClasses()]; double sum = 0; laplaceSmooth(m_Distribution, sum, data.numClasses()); return; } // Compute attribute with maximum information gain. double[] infoGains = new double[data.numAttributes()]; Enumeration attEnum = data.enumerateAttributes(); while (attEnum.hasMoreElements()) { Attribute att = (Attribute) attEnum.nextElement(); infoGains[att.index()] = computeInfoGain(data, att); } m_Attribute = data.attribute(Utils.maxIndex(infoGains)); boolean makeLeaf; makeLeaf = Utils.eq(infoGains[m_Attribute.index()], 0); Instances[] splitData = null; if (!makeLeaf) { splitData = splitData(data, m_Attribute); for (int i = 0; i < splitData.length; ++i) { if (splitData[i].numInstances() == data.numInstances()) { //System.out.println("When splitting on attrib // "+m_Attribute+", child "+i+" is same size as current, // making into leaf."); makeLeaf = true; break; } } } // Make leaf if information gain is zero. // Otherwise create successors. if (makeLeaf) { m_Attribute = null; m_Distribution = new double[data.numClasses()]; Enumeration instEnum = data.enumerateInstances(); double sum = 0; while (instEnum.hasMoreElements()) { Instance inst = (Instance) instEnum.nextElement(); m_Distribution[(int) inst.classValue()]++; sum += inst.weight(); } //laplace smooth the distribution instead laplaceSmooth(m_Distribution, sum, data.numClasses()); //Utils.normalize(m_Distribution); m_ClassValue = Utils.maxIndex(m_Distribution); m_ClassAttribute = data.classAttribute(); } else { m_Successors = new MyId3[m_Attribute.numValues()]; for (int j = 0; j < m_Attribute.numValues(); j++) { m_Successors[j] = new MyId3(); m_Successors[j].buildClassifier(splitData[j]); } } }
From source file:net.sf.bddbddb.order.WekaInterface.java
License:LGPL
public static double cvError(int numFolds, Instances data0, String cClassName) { if (data0.numInstances() < numFolds) return Double.NaN; //more folds than elements if (numFolds == 0) return Double.NaN; // no folds if (data0.numInstances() == 0) return 0; //no instances Instances data = new Instances(data0); //data.randomize(new Random(System.currentTimeMillis())); data.stratify(numFolds);/* ww w.ja va2 s . c om*/ Assert._assert(data.classAttribute() != null); double[] estimates = new double[numFolds]; for (int i = 0; i < numFolds; ++i) { Instances trainData = data.trainCV(numFolds, i); Assert._assert(trainData.classAttribute() != null); Assert._assert(trainData.numInstances() != 0, "Cannot train classifier on 0 instances."); Instances testData = data.testCV(numFolds, i); Assert._assert(testData.classAttribute() != null); Assert._assert(testData.numInstances() != 0, "Cannot test classifier on 0 instances."); int temp = FindBestDomainOrder.TRACE; FindBestDomainOrder.TRACE = 0; Classifier classifier = buildClassifier(cClassName, trainData); FindBestDomainOrder.TRACE = temp; int count = testData.numInstances(); double loss = 0; double sum = 0; for (Enumeration e = testData.enumerateInstances(); e.hasMoreElements();) { Instance instance = (Instance) e.nextElement(); Assert._assert(instance != null); Assert._assert(instance.classAttribute() != null && instance.classAttribute() == trainData.classAttribute()); try { double testClass = classifier.classifyInstance(instance); double weight = instance.weight(); if (testClass != instance.classValue()) loss += weight; sum += weight; } catch (Exception ex) { FindBestDomainOrder.out.println("Exception while classifying: " + instance + "\n" + ex); } } estimates[i] = 1 - loss / sum; } double average = 0; for (int i = 0; i < numFolds; ++i) average += estimates[i]; return average / numFolds; }
From source file:org.dynamicfactory.property.InstanceFactory.java
License:Open Source License
@Override public String exportToString(Instance type, Graph g) { double weight = type.weight(); double[] values = type.toDoubleArray(); StringBuffer buffer = new StringBuffer(); buffer.append(Double.toString(weight)); for (int i = 0; i < values.length; ++i) { buffer.append(",").append(Double.toString(values[i])); }// ww w . j a v a 2 s. co m return buffer.toString(); }
From source file:org.knime.knip.suise.node.boundarymodel.contourdata.IRI.java
License:Open Source License
private Pair<IntervalRule, Double> createRule(Instances flatData, Instances miData, int iterations) throws Exception { // store for the distances between the reference distance and all others double[] distances = new double[flatData.numInstances()]; // the distance function DistanceFunction distFunc = new EuclideanDistance(flatData); // permutation which sorts the distances Integer[] perm = new Integer[flatData.numInstances()]; IntervalRule bestRule = null;/*www .ja v a2 s .c om*/ double bestRuleScore = -Double.MAX_VALUE; // retrieve the best rule heuristically for a number of iterations for (int ruleIterations = 0; ruleIterations < iterations; ruleIterations++) { // System.out.println("------- Iteration " + ruleIterations // + "----------"); // randomly select an initial instance, i.e. selecting a positive // bag // randomly and taking the instance with the largest weight Random r = new Random(); int bagIdx; while (miData.get(bagIdx = r.nextInt(miData.numInstances())).value(2) == 0) ; // the reference instance for the next rule Instance refInstance = miData.get(bagIdx).relationalValue(1).firstInstance(); for (Instance i : miData.get(bagIdx).relationalValue(1)) { if (i.weight() > refInstance.weight()) { refInstance = i; } } // System.out.println("\tRef Instance: " + refInstance); IntervalRule rule = new IntervalRule(); rule.updateClassifier(refInstance); // calculate the distance from that particular reference instance to // all other // positive instances (negatives are set to NaN) and sort them Arrays.fill(distances, Double.NaN); for (int i = 0; i < distances.length; i++) { if (flatData.get(i).classValue() == 1) { distances[i] = distFunc.distance(refInstance, flatData.get(i)); } } PermutationSort.sortPermInPlace(distances, perm); double ruleScore = 0; double tmpRuleScore = 0; // extend the rule successively by the nearest instances till the // score doesn't increase anymore int instanceIdx = 0; while (true) { if (!Double.isNaN(distances[perm[instanceIdx]])) { IntervalRule tmpRule = new IntervalRule(rule); tmpRule.updateClassifier(flatData.get(perm[instanceIdx])); // System.out.println("\tNext Instance: " // + flatData.get(perm[instanceIdx])); // System.out.println("\tCurrent Rule: " + tmpRule); // evaluate rule tmpRuleScore = ruleScore(tmpRule, flatData); if (tmpRuleScore >= ruleScore) { ruleScore = tmpRuleScore; rule = tmpRule; } else { break; } } instanceIdx++; } if (ruleScore > bestRuleScore) { bestRuleScore = ruleScore; bestRule = rule; } } // iterations per rule return new ValuePair<IntervalRule, Double>(bestRule, bestRuleScore); }
From source file:org.knime.knip.suise.node.boundarymodel.contourdata.IRI.java
License:Open Source License
private double ruleScore(IntervalRule rule, Instances data) throws Exception { double posCount = 0; double negCount = 0; double posSumWeights = 0; for (Instance inst : data) { if (inst.weight() > 0) { double dist[] = rule.distributionForInstance(inst); if (dist[1] > dist[0]) { if (inst.classValue() == 1) { posSumWeights += inst.weight(); posCount++;//from w w w . j a v a2s . c o m } else { negCount++; } } } } double score = posSumWeights / (posCount + negCount + m_bias); // System.out.println("\tpSW=" + posSumWeights + ";pC=" + posCount // + ";nC=" + negCount + ";score=" + score); return score; }
From source file:org.knime.knip.suise.node.boundarymodel.contourdata.IRI.java
License:Open Source License
private Instances toSingleInstanceDataset(Instances miData, Instances flatData) throws Exception { MultiInstanceToPropositional convertToProp = new MultiInstanceToPropositional(); convertToProp.setInputFormat(miData); for (int i = 0; i < miData.numInstances(); i++) { convertToProp.input(miData.instance(i)); }/*from w w w . ja v a 2s . c o m*/ convertToProp.batchFinished(); if (flatData == null) { flatData = convertToProp.getOutputFormat(); flatData.deleteAttributeAt(0); // remove the bag index attribute } Instance processed; while ((processed = convertToProp.output()) != null) { processed.setDataset(null); processed.deleteAttributeAt(0); // remove the bag index attribute flatData.add(processed); } // remove class attribute // flatData.setClassIndex(-1); // flatData.deleteAttributeAt(flatData.numAttributes() - 1); // set weights int instanceIdx = 0; for (Instance bag : miData) { for (Instance instance : bag.relationalValue(1)) { flatData.get(instanceIdx).setWeight(instance.weight()); instanceIdx++; } } return flatData; }
From source file:org.mcennis.graphrat.property.database.InstanceDB.java
License:Open Source License
public int put(Instance object) { int ret = -1; ResultSet rs = null;/*w ww.jav a 2 s . c o m*/ try { put.clearParameters(); getID.clearParameters(); put.setDouble(1, object.weight()); getID.setDouble(1, object.weight()); put.executeUpdate(); rs = getID.executeQuery(); if (rs.next()) { ret = rs.getInt("id"); for (int i = 0; i < object.numValues(); ++i) { putValue.clearParameters(); putValue.setInt(1, ret); putValue.setInt(2, i); putValue.setDouble(3, object.value(i)); putValue.executeUpdate(); } } } catch (SQLException ex) { Logger.getLogger(URLDB.class.getName()).log(Level.SEVERE, null, ex); } finally { if (rs != null) { try { rs.close(); } catch (SQLException ex) { } rs = null; } } return ret; }