summaryrefslogtreecommitdiff
path: root/code/bostonset.js
diff options
context:
space:
mode:
Diffstat (limited to 'code/bostonset.js')
-rw-r--r--code/bostonset.js100
1 files changed, 100 insertions, 0 deletions
diff --git a/code/bostonset.js b/code/bostonset.js
new file mode 100644
index 0000000..50b9d38
--- /dev/null
+++ b/code/bostonset.js
@@ -0,0 +1,100 @@
+var Bostonset = (function() {
+ var attrs =
+ ["crim", "zn", "indus", "chas", "nox", "rm", "age", "dis", "rad", "tax", "ptratio", "black", "lstat", "medv"];
+
+ function Bostonset() {
+ this.data = [];
+ // fixed constants exported from training data
+ this.dataScaler = new MinMaxScaler();
+ this.dataScaler.setScales([[0.00632, 0.0, 0.46, 0.0, 0.385, 3.561, 2.9, 1.1296, 1.0, 187.0, 12.6, 0.32, 1.73], [88.9762, 100.0, 27.74, 1.0, 0.871, 8.78, 100.0, 12.1265, 24.0, 711.0, 22.0, 396.9, 37.97]]);
+ this.targetScaler = new MinMaxScaler();
+ this.targetScaler.setScales([5.0, 50.0]);
+ }
+
+ Bostonset.prototype.load = function(url) {
+ var self = this;
+ d3.csv(url).get(function(e, data) {
+ if (e) console.log(e);
+ data.forEach(function (el) {
+ var arr = [];
+ for (c=0; c<attrs.length; c++) {
+ arr.push(parseFloat(el[attrs[c]]));
+ }
+ self.data.push(arr);
+ });
+ });
+ };
+
+ Bostonset.prototype.length = function() {
+ return this.data.length;
+ };
+
+ Bostonset.prototype.rawInputs = function(row) {
+ return this.data[row].slice(0, -1); // leave out MEDV
+ };
+
+ Bostonset.prototype.inputs = function(row) {
+ return this.dataScaler.scale(this.rawInputs(row));
+ };
+
+ Bostonset.prototype.asOutput = function(num) {
+ return this.targetScaler.unscale(num);
+ };
+
+ Bostonset.prototype.expectedOutput = function(row) {
+ return this.data[row][attrs.length-1];
+ };
+
+ return Bostonset;
+})();
+
+// TODO abstract for generic shapes?
+var MinMaxScaler = (function() {
+ function MinMaxScaler() {
+ }
+
+ MinMaxScaler.prototype.fit = function(arr) {
+ this.setScales(d3.extent(arr));
+ };
+
+ MinMaxScaler.prototype.setScales = function(fits) {
+ this.min = fits[0];
+ this.max = fits[1];
+ };
+
+ MinMaxScaler.prototype.scale = function(data) {
+ data = this._arrayDivide(this._arraySubtract(data, this.min), this._arraySubtract(this.max, this.min));
+ return data;
+ };
+
+ MinMaxScaler.prototype.unscale = function(data) {
+ data = this._arrayAdd(this._arrayMultiply(data, this._arraySubtract(this.max, this.min)), this.min);
+ return data;
+ };
+
+ MinMaxScaler.prototype._arraySubtract = function(one, two) {
+ if (one.constructor !== Array) return one - two;
+ for (i=0; i<one.length; one[i]-=two[i++]);
+ return one;
+ };
+
+ MinMaxScaler.prototype._arrayAdd = function(one, two) {
+ if (one.constructor !== Array) return one + two;
+ for (i=0; i<one.length; one[i]+=two[i++]);
+ return one;
+ };
+
+ MinMaxScaler.prototype._arrayMultiply = function(one, two) {
+ if (one.constructor !== Array) return one * two;
+ for (i=0; i<one.length; one[i]*=two[i++]);
+ return one;
+ };
+
+ MinMaxScaler.prototype._arrayDivide = function(one, two) {
+ if (one.constructor !== Array) return one / two;
+ for (i=0; i<one.length; one[i]/=two[i++]);
+ return one;
+ };
+
+ return MinMaxScaler;
+})();