- Timestamp:
- 07/18/16 12:26:03 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/training/WekaLASERTraining.java
r91 r135 24 24 import weka.core.Instances; 25 25 26 27 26 /** 28 27 * <p> 29 * TODO comment28 * Implements training following the LASER classification scheme. 30 29 * </p> 31 30 * … … 34 33 public class WekaLASERTraining extends WekaBaseTraining implements ITrainingStrategy { 35 34 35 /** 36 * Internal classifier used for LASER. 37 */ 36 38 private final LASERClassifier internalClassifier = new LASERClassifier(); 37 39 40 /* 41 * (non-Javadoc) 42 * 43 * @see de.ugoe.cs.cpdp.training.WekaBaseTraining#getClassifier() 44 */ 38 45 @Override 39 46 public Classifier getClassifier() { … … 41 48 } 42 49 50 /* 51 * (non-Javadoc) 52 * 53 * @see de.ugoe.cs.cpdp.training.ITrainingStrategy#apply(weka.core.Instances) 54 */ 43 55 @Override 44 56 public void apply(Instances traindata) { … … 51 63 } 52 64 65 /** 66 * <p> 67 * Internal helper class that defines the laser classifier. 68 * </p> 69 * 70 * @author Steffen Herbold 71 */ 53 72 public class LASERClassifier extends AbstractClassifier { 54 73 74 /** 75 * Default serial ID. 76 */ 55 77 private static final long serialVersionUID = 1L; 56 78 79 /** 80 * Internal reference to the classifier. 81 */ 57 82 private Classifier laserClassifier = null; 83 84 /** 85 * Internal storage of the training data required for NN analysis. 86 */ 58 87 private Instances traindata = null; 59 88 89 /* 90 * (non-Javadoc) 91 * 92 * @see weka.classifiers.AbstractClassifier#classifyInstance(weka.core.Instance) 93 */ 60 94 @Override 61 95 public double classifyInstance(Instance instance) throws Exception { 62 96 List<Integer> closestInstances = new LinkedList<>(); 63 97 double minDistance = Double.MAX_VALUE; 64 for ( int i=0; i<traindata.size(); i++) {98 for (int i = 0; i < traindata.size(); i++) { 65 99 double distance = WekaUtils.hammingDistance(instance, traindata.get(i)); 66 if ( distance<minDistance) {100 if (distance < minDistance) { 67 101 minDistance = distance; 68 102 } 69 103 } 70 for ( int i=0; i<traindata.size(); i++) {104 for (int i = 0; i < traindata.size(); i++) { 71 105 double distance = WekaUtils.hammingDistance(instance, traindata.get(i)); 72 if ( distance<=minDistance) {106 if (distance <= minDistance) { 73 107 closestInstances.add(i); 74 108 } 75 109 } 76 if ( closestInstances.size()==1) {110 if (closestInstances.size() == 1) { 77 111 int closestIndex = closestInstances.get(0); 78 112 Instance closestTrainingInstance = traindata.get(closestIndex); 79 113 List<Integer> closestToTrainingInstance = new LinkedList<>(); 80 114 double minTrainingDistance = Double.MAX_VALUE; 81 for( int i=0; i<traindata.size(); i++ ) { 82 if( closestIndex!=i ) { 83 double distance = WekaUtils.hammingDistance(closestTrainingInstance, traindata.get(i)); 84 if( distance<minTrainingDistance ) { 115 for (int i = 0; i < traindata.size(); i++) { 116 if (closestIndex != i) { 117 double distance = 118 WekaUtils.hammingDistance(closestTrainingInstance, traindata.get(i)); 119 if (distance < minTrainingDistance) { 85 120 minTrainingDistance = distance; 86 121 } 87 122 } 88 123 } 89 for( int i=0; i<traindata.size(); i++ ) { 90 if( closestIndex!=i ) { 91 double distance = WekaUtils.hammingDistance(closestTrainingInstance, traindata.get(i)); 92 if( distance<=minTrainingDistance ) { 124 for (int i = 0; i < traindata.size(); i++) { 125 if (closestIndex != i) { 126 double distance = 127 WekaUtils.hammingDistance(closestTrainingInstance, traindata.get(i)); 128 if (distance <= minTrainingDistance) { 93 129 closestToTrainingInstance.add(i); 94 130 } 95 131 } 96 132 } 97 if ( closestToTrainingInstance.size()==1) {133 if (closestToTrainingInstance.size() == 1) { 98 134 return laserClassifier.classifyInstance(instance); 99 135 } … … 101 137 double label = Double.NaN; 102 138 boolean allEqual = true; 103 for ( Integer index : closestToTrainingInstance) {104 if ( Double.isNaN(label)) {139 for (Integer index : closestToTrainingInstance) { 140 if (Double.isNaN(label)) { 105 141 label = traindata.get(index).classValue(); 106 142 } 107 else if ( label!=traindata.get(index).classValue()) {143 else if (label != traindata.get(index).classValue()) { 108 144 allEqual = false; 109 145 break; 110 146 } 111 147 } 112 if ( allEqual) {148 if (allEqual) { 113 149 return label; 114 150 } … … 117 153 } 118 154 } 119 } else { 155 } 156 else { 120 157 double label = Double.NaN; 121 158 boolean allEqual = true; 122 for ( Integer index : closestInstances) {123 if ( Double.isNaN(label)) {159 for (Integer index : closestInstances) { 160 if (Double.isNaN(label)) { 124 161 label = traindata.get(index).classValue(); 125 162 } 126 else if ( label!=traindata.get(index).classValue()) {163 else if (label != traindata.get(index).classValue()) { 127 164 allEqual = false; 128 165 break; 129 166 } 130 167 } 131 if ( allEqual) {168 if (allEqual) { 132 169 return label; 133 170 } … … 138 175 } 139 176 177 /* 178 * (non-Javadoc) 179 * 180 * @see weka.classifiers.Classifier#buildClassifier(weka.core.Instances) 181 */ 140 182 @Override 141 183 public void buildClassifier(Instances traindata) throws Exception {
Note: See TracChangeset
for help on using the changeset viewer.