| 1 |
package ml; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Random; |
| 5 |
|
| 6 |
import shared.Shared; |
| 7 |
import shared.Tools; |
| 8 |
|
| 9 |
public class SampleSet implements Cloneable { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
SampleSet(Matrix m){matrix=m;} |
| 16 |
|
| 17 |
void makeSamples() {makeSamples(Integer.MAX_VALUE);} |
| 18 |
void makeSamples(final int max) { |
| 19 |
assert(samples==null); |
| 20 |
numPositive=numNegative=0; |
| 21 |
samples=new Sample[Tools.min(max, matrix.inputs.length)]; |
| 22 |
|
| 23 |
for(int i=0; i<samples.length; i++) { |
| 24 |
Sample s=new Sample(matrix.inputs[i], matrix.outputs[i], i); |
| 25 |
samples[i]=s; |
| 26 |
} |
| 27 |
|
| 28 |
final float midpoint=matrix.outputMidpoint(); |
| 29 |
for(Sample s : samples){ |
| 30 |
if(s.goal[0]>=midpoint) {numPositive++;} |
| 31 |
else {numNegative++;} |
| 32 |
} |
| 33 |
|
| 34 |
samplesSortedByResult=samples.clone(); |
| 35 |
shuffle(); |
| 36 |
} |
| 37 |
|
| 38 |
void makeSubsets(int numSets) { |
| 39 |
subsets=new Subset[numSets]; |
| 40 |
@SuppressWarnings("unchecked") |
| 41 |
ArrayList<Sample>[] lists=new ArrayList[numSets]; |
| 42 |
for(int i=0; i<lists.length; i++) { |
| 43 |
lists[i]=new ArrayList<Sample>(); |
| 44 |
} |
| 45 |
for(int i=0, j=0; i<samples.length; i++, j++) { |
| 46 |
j=j%numSets; |
| 47 |
lists[j].add(samples[i]); |
| 48 |
} |
| 49 |
for(int i=0; i<lists.length; i++) { |
| 50 |
subsets[i]=new Subset(lists[i]); |
| 51 |
} |
| 52 |
currentSubset=0; |
| 53 |
} |
| 54 |
|
| 55 |
Subset currentSubset(int epoch) { |
| 56 |
if(epoch>=nextSubsetEpoch){ |
| 57 |
advanceSubset(); |
| 58 |
nextSubsetEpoch=epoch+subsetInterval; |
| 59 |
} |
| 60 |
return subsets[currentSubset]; |
| 61 |
} |
| 62 |
|
| 63 |
private void advanceSubset() { |
| 64 |
currentSubset=(currentSubset+1)%subsets.length; |
| 65 |
assert(currentSubset>=0); |
| 66 |
if(currentSubset==0 && shuffle && subsets.length>1) { |
| 67 |
shuffle(); |
| 68 |
makeSubsets(subsets.length); |
| 69 |
|
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
private void shuffle() { |
| 74 |
final long seed=numShuffles^Long.rotateLeft(shuffleSeed, 17); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
Random randy=new Random(seed); |
| 79 |
for(int i=0; i<samples.length; i++) { |
| 80 |
int idx=randy.nextInt(samples.length); |
| 81 |
Sample s=samples[idx]; |
| 82 |
samples[idx]=samples[i]; |
| 83 |
samples[i]=s; |
| 84 |
} |
| 85 |
|
| 86 |
numShuffles++; |
| 87 |
} |
| 88 |
|
| 89 |
public void sortByValue() { |
| 90 |
Shared.sort(samplesSortedByResult, SampleValueComparator.COMPARATOR); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
public boolean checkSort() { |
| 95 |
for(int i=1; i<samplesSortedByResult.length; i++) { |
| 96 |
if(samplesSortedByResult[i-1].result[0]>samplesSortedByResult[i].result[0]) { |
| 97 |
assert(false) : i+"\n"+samplesSortedByResult[0].result[0] |
| 98 |
+"\n"+samplesSortedByResult[1].result[0] |
| 99 |
+"\n"+samplesSortedByResult[2].result[0] |
| 100 |
+"\n"+samplesSortedByResult[3].result[0]; |
| 101 |
return false; |
| 102 |
} |
| 103 |
} |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
public double calcFNRFromCutoff(final double cutoff) { |
| 108 |
|
| 109 |
int fn=0, tn=0; |
| 110 |
final double invSamples=1.0/samplesSortedByResult.length; |
| 111 |
for(int i=0; i<samplesSortedByResult.length; i++) { |
| 112 |
Sample s=samplesSortedByResult[i]; |
| 113 |
if(s.result[0]>=cutoff) { |
| 114 |
|
| 115 |
break; |
| 116 |
} |
| 117 |
if(s.positive) {fn++;} |
| 118 |
else {tn++;} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
return fn*invSamples; |
| 123 |
} |
| 124 |
|
| 125 |
public float calcCutoffFromCrossover(double fpMult) { |
| 126 |
|
| 127 |
int pos=0, neg=numNegative, i=0; |
| 128 |
for(; i<samplesSortedByResult.length; i++) { |
| 129 |
Sample s=samplesSortedByResult[i]; |
| 130 |
if(s.positive) { |
| 131 |
pos++; |
| 132 |
}else{ |
| 133 |
neg--; |
| 134 |
} |
| 135 |
if(pos*fpMult>=neg) {break;} |
| 136 |
} |
| 137 |
if(i==0) {return samplesSortedByResult[i].result[0];} |
| 138 |
if(pos*fpMult==neg) {return samplesSortedByResult[i].result[0]-0.00001f;} |
| 139 |
Sample a=samplesSortedByResult[i-1]; |
| 140 |
Sample b=samplesSortedByResult[i]; |
| 141 |
return 0.5f*(a.result[0]+b.result[0]); |
| 142 |
} |
| 143 |
|
| 144 |
public double calcFPRFromCutoff(final double cutoff) { |
| 145 |
|
| 146 |
int fp=0, tp=0; |
| 147 |
final double invSamples=1.0/samplesSortedByResult.length; |
| 148 |
for(int i=samplesSortedByResult.length-1; i>=0; i--) { |
| 149 |
Sample s=samplesSortedByResult[i]; |
| 150 |
if(s.result[0]<cutoff) { |
| 151 |
|
| 152 |
break; |
| 153 |
} |
| 154 |
if(s.positive) {tp++;} |
| 155 |
else {fp++;} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
return fp*invSamples; |
| 160 |
} |
| 161 |
|
| 162 |
public double calcCutoffFromFPR(final double fpr) { |
| 163 |
|
| 164 |
int fp=0, tp=0; |
| 165 |
|
| 166 |
final int target=(int)Math.floor(fpr*samplesSortedByResult.length); |
| 167 |
float lastCutoff=1.0f, prev=1.0f; |
| 168 |
for(int i=samplesSortedByResult.length-1; i>=0 && fp<=target; i--) { |
| 169 |
Sample s=samplesSortedByResult[i]; |
| 170 |
if(s.positive) {tp++;} |
| 171 |
else {fp++;} |
| 172 |
prev=lastCutoff; |
| 173 |
lastCutoff=s.result[0]; |
| 174 |
} |
| 175 |
|
| 176 |
return (lastCutoff+prev)*0.5; |
| 177 |
} |
| 178 |
|
| 179 |
public double calcCutoffFromFNR(final double fnr) { |
| 180 |
|
| 181 |
int fn=0, tn=0; |
| 182 |
|
| 183 |
final int target=(int)Math.floor(fnr*samplesSortedByResult.length); |
| 184 |
float lastCutoff=0.0f, prev=0.0f; |
| 185 |
for(int i=0; i<samplesSortedByResult.length && fn<=target; i++) { |
| 186 |
Sample s=samplesSortedByResult[i]; |
| 187 |
if(s.positive) {fn++;} |
| 188 |
else {tn++;} |
| 189 |
prev=lastCutoff; |
| 190 |
lastCutoff=s.result[0]; |
| 191 |
} |
| 192 |
|
| 193 |
return (lastCutoff+prev)*0.5; |
| 194 |
} |
| 195 |
|
| 196 |
public double calcFNRFromFPR(final double fpr) { |
| 197 |
|
| 198 |
int fp=0, tp=0; |
| 199 |
final double invSamples=1.0/samplesSortedByResult.length; |
| 200 |
final int target=(int)Math.floor(fpr*samplesSortedByResult.length); |
| 201 |
for(int i=samplesSortedByResult.length-1; i>=0 && fp<=target; i--) { |
| 202 |
Sample s=samplesSortedByResult[i]; |
| 203 |
if(s.positive) {tp++;} |
| 204 |
else {fp++;} |
| 205 |
} |
| 206 |
|
| 207 |
int tn=numNegative-fp; |
| 208 |
int fn=numPositive-tp; |
| 209 |
return fn*invSamples; |
| 210 |
} |
| 211 |
|
| 212 |
public double calcFPRFromFNR(final double fnr) { |
| 213 |
|
| 214 |
int fn=0, tn=0; |
| 215 |
final double invSamples=1.0/samplesSortedByResult.length; |
| 216 |
final int target=(int)Math.floor(fnr*samplesSortedByResult.length); |
| 217 |
for(int i=0; i<samplesSortedByResult.length && fn<=target; i++) { |
| 218 |
Sample s=samplesSortedByResult[i]; |
| 219 |
if(s.positive) {fn++;} |
| 220 |
else {tn++;} |
| 221 |
} |
| 222 |
|
| 223 |
int fp=numNegative-tn; |
| 224 |
int tp=numPositive-fn; |
| 225 |
return fp*invSamples; |
| 226 |
} |
| 227 |
|
| 228 |
public float[] calcROC(int bins) { |
| 229 |
bins=Tools.max(bins, 2); |
| 230 |
|
| 231 |
final double invPositive=1.0/numPositive; |
| 232 |
double binlen=(numNegative-1)/(double)(bins-1); |
| 233 |
float[] roc=new float[bins]; |
| 234 |
|
| 235 |
|
| 236 |
int pcount=0, ncount=0; |
| 237 |
int bin=1; |
| 238 |
int target=(int)Math.round(binlen*bin); |
| 239 |
for(int i=0; i<samplesSortedByResult.length; i++) { |
| 240 |
Sample s=samplesSortedByResult[i]; |
| 241 |
if(s.positive) {pcount++;} |
| 242 |
else {ncount++;} |
| 243 |
while(ncount>=target) { |
| 244 |
roc[bin]=(float)(1-pcount*invPositive); |
| 245 |
|
| 246 |
bin++; |
| 247 |
target=(int)Math.round(binlen*bin); |
| 248 |
} |
| 249 |
} |
| 250 |
roc[0]=1; |
| 251 |
|
| 252 |
Tools.reverseInPlace(roc); |
| 253 |
return roc; |
| 254 |
} |
| 255 |
|
| 256 |
SampleSet copy() {return copy(Integer.MAX_VALUE, 1f);} |
| 257 |
SampleSet copy(int maxSamples, float subsetSizeFraction) { |
| 258 |
SampleSet copy=null; |
| 259 |
try { |
| 260 |
copy = (SampleSet) this.clone(); |
| 261 |
} catch (CloneNotSupportedException e) { |
| 262 |
|
| 263 |
e.printStackTrace(); |
| 264 |
} |
| 265 |
copy.samples=null; |
| 266 |
copy.samplesSortedByResult=null; |
| 267 |
copy.subsets=null; |
| 268 |
copy.numShuffles=0; |
| 269 |
copy.currentSubset=0; |
| 270 |
copy.reset(); |
| 271 |
if(samples!=null) { |
| 272 |
copy.makeSamples(maxSamples); |
| 273 |
|
| 274 |
if(subsets!=null && subsetSizeFraction>0) { |
| 275 |
int numSubsets=subsets.length; |
| 276 |
if(maxSamples<0.5f*samples.length) { |
| 277 |
numSubsets=Tools.max(1, (int)Math.ceil(numSubsets*(maxSamples/(subsetSizeFraction*samples.length)))); |
| 278 |
} |
| 279 |
copy.makeSubsets(numSubsets); |
| 280 |
} |
| 281 |
} |
| 282 |
return copy; |
| 283 |
} |
| 284 |
|
| 285 |
void reset() { |
| 286 |
currentSubset=0; |
| 287 |
numShuffles=0; |
| 288 |
nextSubsetEpoch=subsetInterval; |
| 289 |
if(subsets!=null) { |
| 290 |
for(Subset ss : subsets) { |
| 291 |
ss.reset(); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
public int maxSubsetSize() { |
| 297 |
return subsets==null || subsets.length<1 || subsets[0].samples==null ? 0 : subsets[0].samples.length; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
final int numInputs() {return matrix.numInputs();} |
| 305 |
final int numOutputs() {return matrix.numOutputs();} |
| 306 |
final float outputMidpoint() {return matrix.outputMidpoint();} |
| 307 |
|
| 308 |
final Matrix matrix; |
| 309 |
int numPositive=0, numNegative=0; |
| 310 |
Sample[] samples; |
| 311 |
Sample[] samplesSortedByResult; |
| 312 |
private Subset[] subsets; |
| 313 |
|
| 314 |
private int currentSubset=0; |
| 315 |
private int numShuffles=0; |
| 316 |
static long shuffleSeed=0; |
| 317 |
static boolean shuffle=true; |
| 318 |
|
| 319 |
int nextSubsetEpoch=subsetInterval; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static int subsetInterval=64; |
| 326 |
|
| 327 |
} |