throw new Exception("Class index is negativ (not set)!");
throw new Exception("Class index is not set!");
  public Instances applyCostMatrix(double[][] weights, Random random)        throws Exception {    double sumOfWeightFactors = 0, sumOfMissClassWeights,      sumOfWeights;    double [] weightOfInstancesInClass, weightFactor, weightOfInstances;    Instances newData;    if (m_ClassIndex < 0) {      throw new Exception("Class index is negativ (not set)!");    }    if (weights.length != numClasses()) {      throw new Exception("Misclassification cost matrix has "+			  "wrong format!");    }    weightFactor = new double [numClasses()];    weightOfInstancesInClass = new double [numClasses()];    for (int j = 0; j < numInstances(); j++) {      weightOfInstancesInClass[(int)instance(j).classValue()] += 	instance(j).weight();    }    sumOfWeights = Utils.sum(weightOfInstancesInClass);    for (int i = 0; i < numClasses(); i++) {      if (weights[i].length != numClasses()) {	throw new Exception("Misclassification cost matrix has "+			    "wrong format!");      }      if (!Utils.eq(weights[i][i],0)) {	throw new Exception("Diagonal of misclassification cost "+			    "matrix not zero!");      }      // Using Kai Ming Ting's formula for deriving weights for       // the classes and Breiman's heuristic for multiclass       // problems.      sumOfMissClassWeights = 0;      for (int j = 0; j < numClasses(); j++) {	if (Utils.sm(weights[i][j],0)) {	  throw new Exception("Neg. weights in misclassification "+			      "cost matrix!"); 	}	sumOfMissClassWeights += weights[i][j];      }      weightFactor[i] = sumOfMissClassWeights * sumOfWeights;      sumOfWeightFactors += sumOfMissClassWeights * 	weightOfInstancesInClass[i];    }    for (int i = 0; i < numClasses(); i++) {      weightFactor[i] /= sumOfWeightFactors;    }        // Store new weights    weightOfInstances = new double[numInstances()];    for (int i = 0; i < numInstances(); i++) {      weightOfInstances[i] =instance(i).weight()*	weightFactor[(int)instance(i).classValue()];    }     // Change instances weight or do resampling    if (random != null) {      return resampleWithWeights(random, weightOfInstances);    } else {       Instances instances = new Instances(this);      for (int i = 0; i < numInstances(); i++) {	instances.instance(i).setWeight(weightOfInstances[i]);      }      return instances;    }  }