# How to normalize test data according to the training data if the normalization on the training data is performed row wise?

I read on several places about the normalization of features in the machine learning method. But I normalize my training data row-wise as shown in the following code. I showed only two samples of training data. My question is that while performing the normalization on test data, should I choose the minimum and maximum value of each test sample to normalize each test data or should I uses minimum and maximum values from the training data? As an explanation in first row -3 is one feature, -2 is second 0 is third and 3 is fifth feature. And second row is second sample comprising of 5 features from -4 to 2. Similar to all other machine learning algorithms each row correspons to one sample consisting of 5 features.

data = np.array([[-3,-2,0, 2,3],[-4,-1,0,3,2]])
print(data)

print(data.shape)
for i in range(len(data)):
print("i: ",i)
old_range = np.amax(data[i]) - np.amin(data[i])
new_range = 2
new_min = -1
data_norm = ((data[i] - np.amin(data[i])) / old_range)*new_range +
new_min
print(data_norm)


Result

[-1.         -0.66666667  0.          0.66666667  1.        ]
[-1.         -0.14285714  0.14285714  1.          0.71428571]

• Is there anything unclear in my question? – jerry Jun 21 '19 at 2:59
• This might be helpful: stats.stackexchange.com/questions/175463/… – Fatemeh Asgarinejad Jun 21 '19 at 6:19
• I don't understand your question: if you normalize row-wise, it means that each normalized instance depends only on the original instance and nothing else. Therefore each test instance should be normalized the same way, independently from the training set or the other instances in the test set. However if your min/max values are computed across the training set then it's not row-wise normalization and the answer is different. – Erwan Jun 21 '19 at 13:35

## 1 Answer

You usually want to normalize features as you also pointed out. In case of tabular data almost every machine learning implementation will expect you to provide the features as columns and observations as rows. In your case if you have a feature in a row you may want to transpose it or if they are not the same features than you may apply a different transformation.

If you do normalization on features you have to use the same transformation on test data that you used on train data. (if you use i.e. sklearn implementations they will take care of it for you)

• Actually i'm not normalizing a single feature. I'm normalizing all the features in one sample in the range [-1,1]. – jerry Jun 23 '19 at 3:42