📄 mioptimalball.java
字号:
* calculate the distances from each instance in a positive bag to each bag. * All result distances are stored in m_Distance[i][j][k], where * m_Distance[i][j][k] refers the distances from the jth instance in ith bag * to the kth bag * * @param train the multi-instance dataset (with relational attribute) */ public void calculateDistance (Instances train) { int numBags =train.numInstances(); int numInstances; Instance tempCenter; m_Distance = new double [numBags][][]; for (int i=0; i<numBags; i++) { if (train.instance(i).classValue() == 1.0) { //positive bag numInstances = train.instance(i).relationalValue(1).numInstances(); m_Distance[i]= new double[numInstances][]; for (int j=0; j<numInstances; j++) { tempCenter = train.instance(i).relationalValue(1).instance(j); m_Distance[i][j]=new double [numBags]; //store the distance from one center to all the bags for (int k=0; k<numBags; k++){ if (i==k) m_Distance[i][j][k]= 0; else m_Distance[i][j][k]= minBagDistance (tempCenter, train.instance(k)); } } } } } /** * Calculate the distance from one data point to a bag * * @param center the data point in instance space * @param bag the bag * @return the double value as the distance. */ public double minBagDistance (Instance center, Instance bag){ double distance; double minDistance = Double.MAX_VALUE; Instances temp = bag.relationalValue(1); //calculate the distance from the data point to each instance in the bag and return the minimum distance for (int i=0; i<temp.numInstances(); i++){ distance =0; for (int j=0; j<center.numAttributes(); j++) distance += (center.value(j)-temp.instance(i).value(j))*(center.value(j)-temp.instance(i).value(j)); if (minDistance>distance) minDistance = distance; } return Math.sqrt(minDistance); } /** * Find the maximum radius for the optimal ball. * * @param train the multi-instance data */ public void findRadius(Instances train) { int numBags, numInstances; double radius, bagDistance; int highestCount=0; numBags = train.numInstances(); //try each instance in all positive bag as a ball center (tempCenter), for (int i=0; i<numBags; i++) { if (train.instance(i).classValue()== 1.0) {//positive bag numInstances = train.instance(i).relationalValue(1).numInstances(); for (int j=0; j<numInstances; j++) { Instance tempCenter = train.instance(i).relationalValue(1).instance(j); //set the possible set of ball radius corresponding to each tempCenter, double sortedDistance[] = sortArray(m_Distance[i][j]); //sort the distance value for (int k=1; k<sortedDistance.length; k++){ radius = sortedDistance[k]-(sortedDistance[k]-sortedDistance[k-1])/2.0 ; //evaluate the performance on the training data according to //the curren selected tempCenter and the set of radius int correctCount =0; for (int n=0; n<numBags; n++){ bagDistance=m_Distance[i][j][n]; if ((bagDistance <= radius && train.instance(n).classValue()==1.0) ||(bagDistance > radius && train.instance(n).classValue ()==0.0)) correctCount += train.instance(n).weight(); } //and keep the track of the ball center and the maximum radius which can achieve the highest accuracy. if (correctCount > highestCount || (correctCount==highestCount && radius > m_Radius)){ highestCount = correctCount; m_Radius = radius; for (int p=0; p<tempCenter.numAttributes(); p++) m_Center[p]= tempCenter.value(p); } } } } } } /** * Sort the array. * * @param distance the array need to be sorted * @return sorted array */ public double [] sortArray(double [] distance) { double [] sorted = new double [distance.length]; //make a copy of the array double []disCopy = new double[distance.length]; for (int i=0;i<distance.length; i++) disCopy[i]= distance[i]; DoubleVector sortVector = new DoubleVector(disCopy); sortVector.sort(); sorted = sortVector.getArrayCopy(); return sorted; } /** * Computes the distribution for a given multiple instance * * @param newBag the instance for which distribution is computed * @return the distribution * @throws Exception if the distribution can't be computed successfully */ public double[] distributionForInstance(Instance newBag) throws Exception { double [] distribution = new double[2]; double distance; distribution[0]=0; distribution[1]=0; Instances insts = new Instances(newBag.dataset(),0); insts.add(newBag); // Filter instances insts= Filter.useFilter( insts, m_ConvertToSI); if (m_Filter!=null) insts = Filter.useFilter(insts, m_Filter); //calculate the distance from each single instance to the ball center int numInsts = insts.numInstances(); insts.deleteAttributeAt(0); //remove the bagIndex attribute, no use for the distance calculation for (int i=0; i<numInsts; i++){ distance =0; for (int j=0; j<insts.numAttributes()-1; j++) distance += (insts.instance(i).value(j) - m_Center[j])*(insts.instance(i).value(j)-m_Center[j]); if (distance <=m_Radius*m_Radius){ // check whether this single instance is inside the ball distribution[1]=1.0; //predicted as a positive bag break; } } distribution[0]= 1-distribution[1]; return distribution; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result = new Vector(); result.addElement(new Option( "\tWhether to 0=normalize/1=standardize/2=neither. \n" + "\t(default 0=normalize)", "N", 1, "-N <num>")); return result.elements(); } /** * Gets the current settings of the classifier. * * @return an array of strings suitable for passing to setOptions */ public String[] getOptions() { Vector result; result = new Vector(); if (getDebug()) result.add("-D"); result.add("-N"); result.add("" + m_filterType); return (String[]) result.toArray(new String[result.size()]); } /** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -N <num> * Whether to 0=normalize/1=standardize/2=neither. * (default 0=normalize)</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { setDebug(Utils.getFlag('D', options)); String nString = Utils.getOption('N', options); if (nString.length() != 0) { setFilterType(new SelectedTag(Integer.parseInt(nString), TAGS_FILTER)); } else { setFilterType(new SelectedTag(FILTER_NORMALIZE, TAGS_FILTER)); } } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String filterTypeTipText() { return "The filter type for transforming the training data."; } /** * Sets how the training data will be transformed. Should be one of * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE. * * @param newType the new filtering mode */ public void setFilterType(SelectedTag newType) { if (newType.getTags() == TAGS_FILTER) { m_filterType = newType.getSelectedTag().getID(); } } /** * Gets how the training data will be transformed. Will be one of * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE. * * @return the filtering mode */ public SelectedTag getFilterType() { return new SelectedTag(m_filterType, TAGS_FILTER); } /** * Main method for testing this class. * * @param argv should contain the command line arguments to the * scheme (see Evaluation) */ public static void main(String[] argv) { runClassifier(new MIOptimalBall(), argv); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -