|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +const Papa = require('papaparse'); |
| 19 | + |
| 20 | + |
| 21 | +// Boston Housing data constants: |
| 22 | +const BASE_URL = |
| 23 | + 'https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/'; |
| 24 | + |
| 25 | +const TRAIN_FEATURES_FN = 'train-data.csv'; |
| 26 | +const TRAIN_TARGET_FN = 'train-target.csv'; |
| 27 | +const TEST_FEATURES_FN = 'test-data.csv'; |
| 28 | +const TEST_TARGET_FN = 'test-target.csv'; |
| 29 | + |
| 30 | +/** |
| 31 | + * Given CSV data returns an array of arrays of numbers. |
| 32 | + * |
| 33 | + * @param {Array<Object>} data Downloaded data. |
| 34 | + * |
| 35 | + * @returns {Promise.Array<number[]>} Resolves to data with values parsed as floats. |
| 36 | + */ |
| 37 | +const parseCsv = async (data) => { |
| 38 | + return new Promise(resolve => { |
| 39 | + data = data.map((row) => { |
| 40 | + return Object.keys(row).map(key => parseFloat(row[key])); |
| 41 | + }); |
| 42 | + resolve(data); |
| 43 | + }); |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Downloads and returns the csv. |
| 48 | + * |
| 49 | + * @param {string} filename Name of file to be loaded. |
| 50 | + * |
| 51 | + * @returns {Promise.Array<number[]>} Resolves to parsed csv data. |
| 52 | + */ |
| 53 | +export const loadCsv = async (filename) => { |
| 54 | + return new Promise(resolve => { |
| 55 | + const url = `${BASE_URL}${filename}`; |
| 56 | + |
| 57 | + console.log(` * Downloading data from: ${url}`); |
| 58 | + Papa.parse(url, { |
| 59 | + download: true, |
| 60 | + header: true, |
| 61 | + complete: (results) => { |
| 62 | + resolve(parseCsv(results['data'])); |
| 63 | + } |
| 64 | + }) |
| 65 | + }); |
| 66 | +}; |
| 67 | + |
| 68 | +/** Helper class to handle loading training and test data. */ |
| 69 | +export class BostonHousingDataset { |
| 70 | + constructor() { |
| 71 | + // Arrays to hold the data. |
| 72 | + this.trainFeatures = null; |
| 73 | + this.trainTarget = null; |
| 74 | + this.testFeatures = null; |
| 75 | + this.testTarget = null; |
| 76 | + } |
| 77 | + |
| 78 | + get numFeatures() { |
| 79 | + // If numFetures is accessed before the data is loaded, raise an error. |
| 80 | + if (this.trainFeatures == null) { |
| 81 | + throw new Error('\'loadData()\' must be called before numFeatures') |
| 82 | + } |
| 83 | + return this.trainFeatures[0].length; |
| 84 | + } |
| 85 | + |
| 86 | + /** Loads training and test data. */ |
| 87 | + async loadData() { |
| 88 | + [this.trainFeatures, this.trainTarget, this.testFeatures, this.testTarget] = |
| 89 | + await Promise.all([ |
| 90 | + loadCsv(TRAIN_FEATURES_FN), loadCsv(TRAIN_TARGET_FN), |
| 91 | + loadCsv(TEST_FEATURES_FN), loadCsv(TEST_TARGET_FN) |
| 92 | + ]); |
| 93 | + |
| 94 | + shuffle(this.trainFeatures, this.trainTarget); |
| 95 | + shuffle(this.testFeatures, this.testTarget); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export const featureDescriptions = [ |
| 100 | + 'Crime rate', 'Land zone size', 'Industrial proportion', 'Next to river', |
| 101 | + 'Nitric oxide concentration', 'Number of rooms per house', 'Age of housing', |
| 102 | + 'Distance to commute', 'Distance to highway', 'Tax rate', 'School class size', |
| 103 | + 'School drop-out rate' |
| 104 | +]; |
| 105 | + |
| 106 | +/** |
| 107 | + * Shuffles data and target (maintaining alignment) using Fisher-Yates |
| 108 | + * algorithm.flab |
| 109 | + */ |
| 110 | +function shuffle(data, target) { |
| 111 | + let counter = data.length; |
| 112 | + let temp = 0; |
| 113 | + let index = 0; |
| 114 | + while (counter > 0) { |
| 115 | + index = (Math.random() * counter) | 0; |
| 116 | + counter--; |
| 117 | + // data: |
| 118 | + temp = data[counter]; |
| 119 | + data[counter] = data[index]; |
| 120 | + data[index] = temp; |
| 121 | + // target: |
| 122 | + temp = target[counter]; |
| 123 | + target[counter] = target[index]; |
| 124 | + target[index] = temp; |
| 125 | + } |
| 126 | +}; |
0 commit comments