# Denormalization of numpy matrix is not correct

I have a 4D array of shape (1948, 60, 2, 3) Which I normalized to a range of [0,1]

a sample of how it looks is below:

original_mat = array([[[  3.93048840e-05,   7.70215296e-04,   1.13865805e-03],
[  1.11679799e-04,  -7.04810066e-04,   1.83552688e-04]])


After normalization (x - x_min)/ (x_max - x_min)

 predicted =   array([[ 0.19302673, -0.03372632, -0.23808828],
[ 0.30002626, -0.71888705,  0.71468331]])


I fed this input to a neural network to predict a similar output, after convergence my resultant matrix looked the same and to denormalize it ,I did,

denormed_matrix = predicted*(xmax - xmin) + xmin
denormed_matrix = [[-0.62747524, -0.72737077,  0.70058271],
[-0.39488326, -0.18533665, -1.48910199]],


I expected it to have same order of magnitude values ( e-03 to e-05), but the matrix didn't scale down in magnitude, it had similar values like the normalized one.

1. Am I missing any point here?
2. Are my calculation correct?

EDIT: CODE for Normalization

### Get min, max value aming all elements for each column
x = np.asarray(poseList)
x_min = np.min(x, axis=tuple(range(x.ndim-1)), keepdims=1)
x_max = np.max(x, axis=tuple(range(x.ndim-1)), keepdims=1)
#
### Normalize with those min, max values leveraging broadcasting
normalized = (x - x_min)/ (x_max - x_min)
normalized = 2.0*normalized - 1.0    # noralizing in the range [-1,1]
#
print "final_save"

In : norm.shape
Out: (309, 60, 2, 3)

In : x_max
Out: array([[[[ 0.10778677,  0.16254221,  0.1198302 ]]]])

In : x_min
Out: array([[[[-0.56810854, -0.21604319, -0.37091526]]]])


Code for Denormalization Following this formula(@Marco D.G.): enter image description here

normalized = np.load('/home/normalized.npy')
normalized = normalized+ 1  #[a,b] = [-1,1]
diff = x_max - x_min
numerator = diff * normalized
denormalized = (numerator/2.0 ) + x_min

• Are the values of xmax and xmin the same in the two uses? – Fabio Somenzi Nov 9 '17 at 20:44
• Yes. I have saved the values. Is the problem the way I am doing the calculation, like, should I be inverting the denormalized matrix? – deeplearning Nov 9 '17 at 20:50
• Just to clarify, does the resultant matrix look like the normalized input? Also, is a parenthesis missing from your normalization expression? – Fabio Somenzi Nov 9 '17 at 20:56
• @FabioSomenzi Yeah! Thanks..I have edited the formula. Yes, my denormed output looks like predicted output. I have included a sample of how it looks above. – deeplearning Nov 9 '17 at 20:59
• But your predicted matrix is not in the range $[0,1]$ as you say. If you wanted that range, you'd do (x-xmin)/(xmax-xmin), wouldn't you? – Fabio Somenzi Nov 9 '17 at 21:02

## The denormalization formula is wrong.

Normalization:

$X' = a + \frac{\left(X-X_{\min}\right)\left(b-a\right)}{X_{\max} - X_{\min}}$

Denormalization (inverse formula):

$X = \frac{(X_{\max}-X_{\min})(X'-a)}{b-a} + X_{\min}$

Example with original_mat(0,0):

$X' = 2\frac{(3.93048840 + 7.04810066)}{(7.70215296 + 7.04810066)}-1$

$X' = 2\frac{10.97858906}{14.75025362}-1$

$X' = 0.48859665$

denormalization:

$X = \frac{14.75025362(0.48859665+1)}{2} - 7.04810066$

$X = \frac{21.95717813}{2} - 7.04810066$

$X =3.93048840$

I hope I have been of help, best regards,

Marco.

• Solid answer. About as good an answer as one could hope for concerning programming issues on a mathematics site! – Brevan Ellefsen Nov 13 '17 at 7:32
• Hey! Thanks for correcting me..I am still; not getting the right answer, I normalize and renormalize, but it doesn't work. I have updated the code in the question details above. – deeplearning Nov 13 '17 at 20:39
• sorry, maybe I did not understand the problem. I'm not very good at python, could you just show the input matrix, the minimum, the maximum, and the output obtained? rather, why do you have more than a maximum and a minimum? – Marco D.G. Nov 14 '17 at 8:21
• @MarcoD.G. Hey, the issue ios resolved, it was a python code error. Thanks for your effort – deeplearning Nov 21 '17 at 15:44