| 37 | * labeStddev {number} The standard deviation of the albel column. |
| 38 | */ |
| 39 | export async function getDatasetStats() { |
| 40 | const featureValues = {}; |
| 41 | featureColumns.forEach(feature => { |
| 42 | featureValues[feature] = []; |
| 43 | }); |
| 44 | const labelValues = []; |
| 45 | |
| 46 | const dataset = tf.data.csv(HOUSING_CSV_URL, { |
| 47 | columnConfigs: { |
| 48 | [labelColumn]: { |
| 49 | isLabel: true |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | const iterator = await dataset.iterator(); |
| 54 | let count = 0; |
| 55 | while (true) { |
| 56 | const item = await iterator.next(); |
| 57 | if (item.done) { |
| 58 | break; |
| 59 | } |
| 60 | featureColumns.forEach(feature => { |
| 61 | if (item.value.xs[feature] == null) { |
| 62 | throw new Error(`item #{count} lacks feature ${feature}`); |
| 63 | } |
| 64 | featureValues[feature].push(item.value.xs[feature]); |
| 65 | }); |
| 66 | labelValues.push(item.value.ys[labelColumn]); |
| 67 | count++; |
| 68 | } |
| 69 | |
| 70 | return tf.tidy(() => { |
| 71 | const featureMeans = {}; |
| 72 | const featureStddevs = {}; |
| 73 | featureColumns.forEach(feature => { |
| 74 | const {mean, variance} = tf.moments(featureValues[feature]); |
| 75 | featureMeans[feature] = mean.arraySync(); |
| 76 | featureStddevs[feature] = tf.sqrt(variance).arraySync(); |
| 77 | }); |
| 78 | |
| 79 | const moments = tf.moments(labelValues); |
| 80 | const labelMean = moments.mean.arraySync(); |
| 81 | const labelStddev = tf.sqrt(moments.variance).arraySync(); |
| 82 | return { |
| 83 | count, |
| 84 | featureMeans, |
| 85 | featureStddevs, |
| 86 | labelMean, |
| 87 | labelStddev |
| 88 | }; |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get a dataset with the features and label z-normalized, |