js/partition.js
/**
	 * Partitions the values of field into bins according to Freedmen-Diaconis method.
	 * If minBins/maxBins are defined the number of bins will be restricted to within this range.
	 **/
Partitions the values of field into bins according to Freedmen-Diaconis method.
If minBins/maxBins are defined the number of bins will be restricted to within this range.
Partitions the values of field into bins according to Freedmen - Diaconis method . If minBins / maxBins are defined the number of bins will be restricted to within this range .
partitionToBins(field, minBins, maxBins) {
		if(typeof minBins == 'undefined') {
			minBins = 0;
			maxBins = 1000;
		}
		else if(typeof maxBins == 'undefined')
			maxBins = 1000;
		let binWidth = 0;
		let minValue = this.data[0][field];
		let maxValue = minValue;
		for(let i = 1; i < this.data.length; i++) {
			let v = this.data[i][field];
			if(v > maxValue) maxValue = v;
			if(v < minValue) minValue = v;
		}
		if(minBins != maxBins) {
			let vs = [];
			for(let i = 0; i < this.data.length; i++)
				vs.push(this.data[i][field]);
			vs.sort(function(a,b) { return a-b; });
			let iqr = vs[Math.floor(vs.length-1-vs.length/4)] - vs[Math.floor(vs.length/4)];
			binWidth = iqr*2 / Math.cbrt(vs.length);
			minValue = vs[0];
			maxValue = vs[vs.length-1];
		}
		else
			binWidth = (maxValue - minValue) / (numBins-0.1); 
		let numBins = Math.ceil( (maxValue-minValue)/binWidth );
		if(numBins > maxBins || numBins < minBins) {
			numBins = Math.min(maxBins, Math.max(minBins,numBins));
			binWidth = (maxValue - minValue) / (numBins-0.1);
		}
		let splits = [];
		for(let i = 0; i < numBins; i++) splits.push([]);
		for(let i = 0; i < this.data.length; i++) {
			let v = this.data[i][field];
			let bin = Math.ceil( (v-minValue) / binWidth );
			if(bin >= splits.length) bin = splits.length-1;
			splits[bin].push(this.data[i]);
		}
		let partitions = [];
		for(let i = 0; i < numBins; i++) {
			let partition = new Partition(minValue+(i*binWidth),splits[i],[]);
			partition[field] = partition.label;
			partition.id = i;
			partitions.push(partition);
		}
		let partitioning = new Partitioning(field, this);
		partitioning.data = partitions;
		partitioning.count = partitions.length;
		this.children.push(partitioning);
		return partitioning;
	}
partitionToBins ( field , minBins , maxBins ) { if ( typeof minBins == 'undefined' ) { minBins = 0 ; maxBins = 1000 ; } else if ( typeof maxBins == 'undefined' ) maxBins = 1000 ; let binWidth = 0 ; let minValue = this . data [ 0 ] [ field ] ; let maxValue = minValue ; for ( let i = 1 ; i < this . data . length ; i ++ ) { let v = this . data [ i ] [ field ] ; if ( v > maxValue ) maxValue = v ; if ( v < minValue ) minValue = v ; } if ( minBins != maxBins ) { let vs = [ ] ; for ( let i = 0 ; i < this . data . length ; i ++ ) vs . push ( this . data [ i ] [ field ] ) ; vs . sort ( function ( a , b ) { return a - b ; } ) ; let iqr = vs [ Math . floor ( vs . length - 1 - vs . length / 4 ) ] - vs [ Math . floor ( vs . length / 4 ) ] ; binWidth = iqr * 2 / Math . cbrt ( vs . length ) ; minValue = vs [ 0 ] ; maxValue = vs [ vs . length - 1 ] ; } else binWidth = ( maxValue - minValue ) / ( numBins - 0.1 ) ; let numBins = Math . ceil ( ( maxValue - minValue ) / binWidth ) ; if ( numBins > maxBins || numBins < minBins ) { numBins = Math . min ( maxBins , Math . max ( minBins , numBins ) ) ; binWidth = ( maxValue - minValue ) / ( numBins - 0.1 ) ; } let splits = [ ] ; for ( let i = 0 ; i < numBins ; i ++ ) splits . push ( [ ] ) ; for ( let i = 0 ; i < this . data . length ; i ++ ) { let v = this . data [ i ] [ field ] ; let bin = Math . ceil ( ( v - minValue ) / binWidth ) ; if ( bin >= splits . length ) bin = splits . length - 1 ; splits [ bin ] . push ( this . data [ i ] ) ; } let partitions = [ ] ; for ( let i = 0 ; i < numBins ; i ++ ) { let partition = new Partition ( minValue + ( i * binWidth ) , splits [ i ] , [ ] ) ; partition [ field ] = partition . label ; partition . id = i ; partitions . push ( partition ) ; } let partitioning = new Partitioning ( field , this ) ; partitioning . data = partitions ; partitioning . count = partitions . length ; this . children . push ( partitioning ) ; return partitioning ; }
Partitions the values of field into bins according to Freedmen-Diaconis method.
Partitions the values of field into bins according to Freedmen - Diaconis method .
//Freedman-Diaconis //ensure rounding doesn't break things //now perform the partitioning //label is the lower bin value