MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / getIrisData

Function getIrisData

iris-fitDataset/data.js:106–137  ·  view source on GitHub ↗
(testSplit)

Source from the content-addressed store, hash-verified

104 * - Labels as a rank-1 `Tensor` in one-hot format.
105 */
106export async function getIrisData(testSplit) {
107 // TODO(bileschi): Update shuffle etc. to use the tf.data API calls once
108 // it is possible to cache the results for performance and train-test split
109 // stability across epochs. Once caching is available, perform batching first
110 // and then map the preprocessing functions across the batches.
111 // https://github.com/tensorflow/tfjs/issues/1025
112
113 // Shuffle a copy of the raw data.
114 const shuffled = IRIS_RAW_DATA.slice();
115 tf.util.shuffle(shuffled);
116 // Split the data into training and testing portions.
117 const numTestExamples = Math.round(IRIS_RAW_DATA.length * testSplit);
118 const numTrainExamples = IRIS_RAW_DATA.length - numTestExamples;
119 const train = shuffled.slice(0, numTrainExamples);
120 const test = shuffled.slice(numTrainExamples);
121 // Split the data into into X & y and apply feature mapping transformations
122 const trainX = tf.data.array(train.map(r => r.slice(0, 4)));
123 const testX = tf.data.array(test.map(r => r.slice(0, 4)));
124 // TODO(we should be able to just directly use tensors built from oneHot here
125 // instead of converting to tensor and back using datasync & Array.from.
126 // This causes an internal disposal error however.
127 // See https://github.com/tensorflow/tfjs/issues/1071
128 //
129 // const trainY = tf.data.array(train.map(r => tf.oneHot([r[4]], 3)));
130 // const testY = tf.data.array(test.map(r => tf.oneHot([r[4]], 3)));
131 const trainY = tf.data.array(train.map(r => flatOneHot(r[4])));
132 const testY = tf.data.array(test.map(r => flatOneHot(r[4])));
133 // Recombine the X and y portions of the data.
134 const trainDataset = tf.data.zip({xs: trainX, ys: trainY});
135 const testDataset = tf.data.zip({xs: testX, ys: testY});
136 return [trainDataset, testDataset];
137}

Callers

nothing calls this directly

Calls 1

flatOneHotFunction · 0.85

Tested by

no test coverage detected