xCategorical = {
function hotEncodeField(field) {
let tensor = tf.tensor(dataset.map(el => parseFloat(el[field.key])), null, 'int32');
if (!field.zeroIndex) {
tensor = tensor.sub(tf.tensor(1, null, 'int32'));
}
const tensorOneHotEncoded = tf.oneHot(tensor, field.categories)
return tensorOneHotEncoded;
}
const hotEncodedTensors = [];
const selectedFields = [
{key: 'season', categories: 4, zeroIndex: false},
{key: 'yr', categories: 2, zeroIndex: true},
{key: 'mnth', categories: 12, zeroIndex: false},
{key: 'holiday', categories: 2, zeroIndex: true},
{key: 'weekday', categories: 7, zeroIndex: false},
{key: 'workingday', categories: 2, zeroIndex: true},
{key: 'weathersit', categories: 4, zeroIndex: false}
]
for (let field of selectedFields) {
hotEncodedTensors.push(hotEncodeField(field))
}
const xCategoricalHotEncodedTensor = tf.concat(hotEncodedTensors, -1);
return xCategoricalHotEncodedTensor.array();
}