Owen
Owen

Reputation: 76

How to normalise data for a multi-dimensional 2d array using min-max scaler

I've been using the example here for predicting the Miles Per Gallon of a car based off its Horsepower to help guide me in using this for my own project.

The input dimension for my project is 1 by 3.

For this, I have done:

const inputs = data.map(d => [d.valueOne, d.valueTwo, d.valueThree]);
const labels = data.map(d => d.valueFour);

const inputTensor = tf.tensor2d(inputs);
const labelTensor = tf.tensor2d(labels);

So inputs will return an array of arrays that contain 3 input values.

The next step is to normalise each value in these arrays which is where I become lost. I'm not familiar with .sub and .div like it shows in the example here.

Also, how would one calculate the .min and .max of the three values from a list?

EDIT: According to the link given above, it states:

You can normalize your data before turning it into tensors. We do it afterwards because we can take advantage of vectorization in TensorFlow.js to do the min-max scaling operations without writing any explicit for loops.

Just to clarify, does this mean .min() and .max() calculates the minimum and maximum for me in the 2d array without me having to do it myself?

Upvotes: 1

Views: 1663

Answers (1)

edkeveked
edkeveked

Reputation: 18371

The min-max formula is the following

(x - min(x)) / (max(x) - min(x))

This computation can be done by using the following given t the tensor of interest containing all the data

min = t.min(0)
max = t.max(0)
t.sub(min).div(max.sub(min)) // here is the normalized data

Upvotes: 1

Related Questions