Changeset 135 for trunk/CrossPare/src/de/ugoe/cs/cpdp/util
- Timestamp:
- 07/18/16 12:26:03 (8 years ago)
- Location:
- trunk/CrossPare/src/de/ugoe/cs/cpdp/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/util/SortUtils.java
r61 r135 1 1 2 package de.ugoe.cs.cpdp.util; 2 3 4 /** 5 * <p> 6 * Utility functions for sorting. 7 * </p> 8 * 9 * @author Steffen Herbold 10 */ 3 11 public class SortUtils { 4 12 13 /** 14 * <p> 15 * Implements a quick sort that sorts an index set together with the array. 16 * </p> 17 * 18 * @param main 19 * the array that is sorted 20 * @param index 21 * the index set for the array 22 */ 5 23 public static <T extends Comparable<T>> void quicksort(T[] main, int[] index) { 6 24 quicksort(main, index, 0, index.length - 1, false); 7 25 } 8 9 public static <T extends Comparable<T>> void quicksort(T[] main, int[] index, boolean descending) { 26 27 /** 28 * <p> 29 * Implements a quick sort that sorts an index set together with the array. 30 * </p> 31 * 32 * @param main 33 * the array that is sorted 34 * @param index 35 * the index set for the array 36 * @param descending 37 * defines the sorting order 38 */ 39 public static <T extends Comparable<T>> void quicksort(T[] main, 40 int[] index, 41 boolean descending) 42 { 10 43 quicksort(main, index, 0, index.length - 1, descending); 11 44 } 12 45 13 // quicksort a[left] to a[right] 14 private static <T extends Comparable<T>> void quicksort(T[] a, int[] index, int left, int right, boolean descending) { 46 /** 47 * <p> 48 * internal quicksort implementation 49 * </p> 50 * 51 * @param main 52 * the array that is sorted 53 * @param index 54 * the index set for the array 55 * @param left 56 * defines the current partition 57 * @param right 58 * defines the current partition 59 * @param descending 60 * defines the sorting order 61 */ 62 private static <T extends Comparable<T>> void quicksort(T[] main, 63 int[] index, 64 int left, 65 int right, 66 boolean descending) 67 { 15 68 if (right <= left) 16 69 return; 17 int i = partition( a, index, left, right, descending);18 quicksort( a, index, left, i - 1, descending);19 quicksort( a, index, i + 1, right, descending);70 int i = partition(main, index, left, right, descending); 71 quicksort(main, index, left, i - 1, descending); 72 quicksort(main, index, i + 1, right, descending); 20 73 } 21 74 22 // partition a[left] to a[right], assumes left < right 23 private static <T extends Comparable<T>> int partition(T[] a, int[] index, int left, int right, boolean descending) { 75 /** 76 * <p> 77 * internal partitioning of the quicksort implementation 78 * </p> 79 * 80 * @param main 81 * the array that is sorted 82 * @param index 83 * the index set for the array 84 * @param left 85 * defines the current partition 86 * @param right 87 * defines the current partition 88 * @param descending 89 * defines the sorting order 90 */ 91 private static <T extends Comparable<T>> int partition(T[] main, 92 int[] index, 93 int left, 94 int right, 95 boolean descending) 96 { 24 97 int i = left - 1; 25 98 int j = right; 26 99 while (true) { 27 while (compare( a[++i], a[right], descending)) // find item on left to swap100 while (compare(main[++i], main[right], descending)) // find item on left to swap 28 101 ; // a[right] acts as sentinel 29 while (compare( a[right], a[--j], descending)) // find item on right to swap102 while (compare(main[right], main[--j], descending)) // find item on right to swap 30 103 if (j == left) 31 104 break; // don't go out-of-bounds 32 105 if (i >= j) 33 106 break; // check if pointers cross 34 exch(a, index, i, j); // swap two elements into place107 swap(main, index, i, j); // swap two elements into place 35 108 } 36 exch(a, index, i, right); // swap with partition element109 swap(main, index, i, right); // swap with partition element 37 110 return i; 38 111 } 39 112 40 // is x < y ? 113 /** 114 * <p> 115 * helper function for comparator evaluation 116 * </p> 117 * 118 * @param x 119 * first element that is compared 120 * @param y 121 * second element that is compared 122 * @param descending 123 * defines the sorting order 124 * @return true if x is larger than y and descending is true or y is larger than x and 125 * descending is false 126 */ 41 127 private static <T extends Comparable<T>> boolean compare(T x, T y, boolean descending) { 42 if( descending ) { 43 return x.compareTo(y)>0; 44 } else { 45 return x.compareTo(y)<0; 128 if (descending) { 129 return x.compareTo(y) > 0; 130 } 131 else { 132 return x.compareTo(y) < 0; 46 133 } 47 134 } 48 135 49 // exchange a[i] and a[j] 50 private static <T extends Comparable<T>> void exch(T[] a, int[] index, int i, int j) { 51 T swap = a[i]; 52 a[i] = a[j]; 53 a[j] = swap; 136 /** 137 * <p> 138 * swaps to elements 139 * </p> 140 * 141 * @param main 142 * the array that is sorted 143 * @param index 144 * the index set for the array 145 * @param i 146 * index of the first element 147 * @param j 148 * index of the second element 149 */ 150 private static <T extends Comparable<T>> void swap(T[] main, int[] index, int i, int j) { 151 T tmp = main[i]; 152 main[i] = main[j]; 153 main[j] = tmp; 54 154 int b = index[i]; 55 155 index[i] = index[j]; -
trunk/CrossPare/src/de/ugoe/cs/cpdp/util/WekaUtils.java
r129 r135 15 15 package de.ugoe.cs.cpdp.util; 16 16 17 // TODO comment18 17 import org.apache.commons.math3.ml.distance.EuclideanDistance; 19 18 … … 21 20 import weka.core.Instances; 22 21 22 /** 23 * <p> 24 * Collections of helper functions to work with Weka. 25 * </p> 26 * 27 * @author Steffen Herbold 28 */ 23 29 public class WekaUtils { 24 30 31 /** 32 * <p> 33 * Data class for distance between instances within a data set based on their distributional 34 * characteristics. 35 * </p> 36 * 37 * @author Steffen Herbold 38 */ 25 39 public static class DistChar { 26 40 public final double mean; … … 29 43 public final double max; 30 44 public final int num; 45 31 46 private DistChar(double mean, double std, double min, double max, int num) { 32 47 this.mean = mean; … … 37 52 } 38 53 } 39 54 40 55 /** 41 56 * Scaling value that moves the decimal point by 5 digets. 42 57 */ 43 58 public final static double SCALER = 10000.0d; 44 59 45 60 /** 46 61 * <p> … … 66 81 return distance; 67 82 } 68 83 84 /** 85 * <p> 86 * Returns a double array of the values without the classification. 87 * </p> 88 * 89 * @param instance 90 * the instance 91 * @return double array 92 */ 69 93 public static double[] instanceValues(Instance instance) { 70 double[] values = new double[instance.numAttributes() -1];71 int k =0;72 for ( int j=0; j<instance.numAttributes() ; j++) {73 if ( j!= instance.classIndex()) {94 double[] values = new double[instance.numAttributes() - 1]; 95 int k = 0; 96 for (int j = 0; j < instance.numAttributes(); j++) { 97 if (j != instance.classIndex()) { 74 98 values[k] = instance.value(j); 75 99 k++; … … 78 102 return values; 79 103 } 80 104 105 /** 106 * <p> 107 * Calculates the distributional characteristics of the distances the instances within a data 108 * set have to each other. 109 * </p> 110 * 111 * @param data 112 * data for which the instances are characterized 113 * @return characteristics 114 */ 81 115 public static DistChar datasetDistance(Instances data) { 82 116 double distance; … … 87 121 int numCmp = 0; 88 122 int l = 0; 89 double[] inst1 = new double[data.numAttributes() -1];90 double[] inst2 = new double[data.numAttributes() -1];123 double[] inst1 = new double[data.numAttributes() - 1]; 124 double[] inst2 = new double[data.numAttributes() - 1]; 91 125 EuclideanDistance euclideanDistance = new EuclideanDistance(); 92 for ( int i=0; i<data.numInstances(); i++) {93 l =0;94 for ( int k=0; k<data.numAttributes(); k++) {95 if ( k!=data.classIndex()) {126 for (int i = 0; i < data.numInstances(); i++) { 127 l = 0; 128 for (int k = 0; k < data.numAttributes(); k++) { 129 if (k != data.classIndex()) { 96 130 inst1[l] = data.instance(i).value(k); 97 131 } 98 132 } 99 for ( int j=0; j<data.numInstances(); j++) {100 if ( j!=i) {101 l =0;102 for ( int k=0; k<data.numAttributes(); k++) {103 if ( k!=data.classIndex()) {133 for (int j = 0; j < data.numInstances(); j++) { 134 if (j != i) { 135 l = 0; 136 for (int k = 0; k < data.numAttributes(); k++) { 137 if (k != data.classIndex()) { 104 138 inst2[l] = data.instance(j).value(k); 105 139 } … … 107 141 distance = euclideanDistance.compute(inst1, inst2); 108 142 sumAll += distance; 109 sumAllQ += distance *distance;143 sumAllQ += distance * distance; 110 144 numCmp++; 111 if ( distance < min) {145 if (distance < min) { 112 146 min = distance; 113 147 } 114 if ( distance > max) {148 if (distance > max) { 115 149 max = distance; 116 150 } … … 119 153 } 120 154 double mean = sumAll / numCmp; 121 double std = Math.sqrt((sumAllQ-(sumAll*sumAll)/numCmp) * 122 (1.0d / (numCmp - 1))); 155 double std = Math.sqrt((sumAllQ - (sumAll * sumAll) / numCmp) * (1.0d / (numCmp - 1))); 123 156 return new DistChar(mean, std, min, max, data.numInstances()); 124 157 } 125 126 // like above, but for single attribute 158 159 /** 160 * <p> 161 * Calculates the distributional characteristics of the distances of a single attribute the 162 * instances within a data set have to each other. 163 * </p> 164 * 165 * @param data 166 * data for which the instances are characterized 167 * @param index 168 * attribute for which the distances are characterized 169 * @return characteristics 170 */ 127 171 public static DistChar attributeDistance(Instances data, int index) { 128 172 double distance; … … 133 177 int numCmp = 0; 134 178 double value1, value2; 135 for ( int i=0; i<data.numInstances(); i++) {179 for (int i = 0; i < data.numInstances(); i++) { 136 180 value1 = data.instance(i).value(index); 137 for ( int j=0; j<data.numInstances(); j++) {138 if ( j!=i) {181 for (int j = 0; j < data.numInstances(); j++) { 182 if (j != i) { 139 183 value2 = data.instance(j).value(index); 140 distance = Math.abs(value1 -value2);184 distance = Math.abs(value1 - value2); 141 185 sumAll += distance; 142 sumAllQ += distance *distance;186 sumAllQ += distance * distance; 143 187 numCmp++; 144 if ( distance < min) {188 if (distance < min) { 145 189 min = distance; 146 190 } 147 if ( distance > max) {191 if (distance > max) { 148 192 max = distance; 149 193 } … … 152 196 } 153 197 double mean = sumAll / numCmp; 154 double std = Math.sqrt((sumAllQ-(sumAll*sumAll)/numCmp) * 155 (1.0d / (numCmp - 1))); 198 double std = Math.sqrt((sumAllQ - (sumAll * sumAll) / numCmp) * (1.0d / (numCmp - 1))); 156 199 return new DistChar(mean, std, min, max, data.numInstances()); 157 200 } 158 201 159 202 /** 160 203 * <p>
Note: See TracChangeset
for help on using the changeset viewer.