# What is the best way to normalize histogram vectors to get distribution?

l have the following sample of 4 vectors of dimension 5 . They are sparse vectors, in a way that each value in a vector represent the frequency (number of occurrence of values). For instance

v_1=[0,4,0,0,1]

4 at index 1 means we have four values of 1 and 1 at index 4 it means we have one value of 4.

The purpose of normalizing these histograms is to get a distribution.

Here is a sample of my data.

Vectors=[[0,4,0,0,1],[5,0,0,3,1],[1,0,0,0,0],[0,0,6,0,0]]

What l tried ?

vectors=(vectors-np.mean(vectors,axis=0)) / np.std(vectors,axis=0)


and l tried :

vectors=sklearn.preprocessing.scale(vectors)


A)Is it correct what l have tried ?

B)l have question to normalization by subtracting means and dividing by standard deviation: through all the vectors, through each dimension, means( of a dimension) is computed. Than each data point of this dimension is subtracted from its mean. Is it correct what l'm saying ? in other way for each dimension, we have it's own mean.

For instance, if we have dim(vectors)=(400,2000) such that 400 is the number of examples.
we compute 2000 means and 2000 std since we have 2000 dimensions. And then each data point is subtracted from its means and divided by its std (mean and std of that dimension)

Thank you for correcting me.

It's okay, what you are saying, except the

each data point is subtracted from its means

part because it's in the opposite way, you have to subtract the means from their data points.

I guess your problem is coming from that you put your vectors into the matrix as row vectors, but np.mean(..., axis = 0) calculates the column means.

If you want to use the above formula for normalizing then you should transpose your matrix.

vectors = np.array([[0,4,0,0,1],[5,0,0,3,1],[1,0,0,0,0],[0,0,6,0,0]])
vectors.T
array([[0, 5, 1, 0],
[4, 0, 0, 0],
[0, 0, 0, 6],
[0, 3, 0, 0],
[1, 1, 0, 0]])


And now the np.mean(vectors.T, axis=0) gives the correct means for your vectors:

array([ 1. ,  1.8,  0.2,  1.2])


Substract the means from the transposed matrix like vectors.T - np.mean(vectors.T, axis=0)

array([[-1. ,  3.2,  0.8, -1.2],
[ 3. , -1.8, -0.2, -1.2],
[-1. , -1.8, -0.2,  4.8],
[-1. ,  1.2, -0.2, -1.2],
[ 0. , -0.8, -0.2, -1.2]])


And finally divide it by the standard deviation of the transposed matrix

t_vectors = (vectors.T-np.mean(vectors.T,axis=0)) / np.std(vectors.T,axis=0)
t_vectors
array([[-0.64549722,  1.65027399,  2.        , -0.5       ],
[ 1.93649167, -0.92827912, -0.5       , -0.5       ],
[-0.64549722, -0.92827912, -0.5       ,  2.        ],
[-0.64549722,  0.61885275, -0.5       , -0.5       ],
[ 0.        , -0.4125685 , -0.5       , -0.5       ]])


Sklearn gives the same sklearn.preprocessing.scale(vectors.T).