Python Normalization results in 1s insted of range 0-1

Viewed 82

I am trying to normalize my data using Python sklearn preprocessing.normalize, but almast all the results end as 1 insted of in range [0-1] . I guess it is simple mistake, but I am new to Python. The biggest difference is visible in average, which clearly shows something is way off!

Here is example code to reproduce the problem

import numpy as np
import pandas as pd
from sklearn import preprocessing

tmp = np.random.randint(0, 100, 1000) 

tmp_st = preprocessing.normalize(tmp.reshape(-1, 1))
print('min: ' + str(min(tmp_st)) + 
      ' | max: ' + str(max(tmp_st)) + 
      ' | avg: ' + str(sum(tmp_st) / len(tmp_st)) + 
      ' - min org: ' + str(min(tmp)) + 
      ' | max org: ' + str(max(tmp)) + 
      ' | avg org: ' + str(sum(tmp) / len(tmp)))
# min: 0.0 | max: 1.0 | avg: 0.99 - min org: 0 | max org: 99 | avg org: 50.156

Also I have tried to do it in Data Frame

df_tmp = pd.DataFrame({'tmp': tmp})
df_tmp['tmp_st'] = preprocessing.normalize(df_tmp[['tmp']])
print('min: ' + str(min(df_tmp['tmp_st'])) + 
      ' | max: ' + str(max(df_tmp['tmp_st'])) + 
      ' | avg: ' + str(sum(df_tmp['tmp_st']) / len(df_tmp['tmp_st'])) + 
      ' - min org: ' + str(min(df_tmp['tmp'])) + 
      ' | max org: ' + str(max(df_tmp['tmp'])) + 
      ' | avg org: ' + str(sum(df_tmp['tmp']) / len(df_tmp['tmp'])))
# min: 0.0 | max: 1.0 | avg: 0.99 - min org: 0 | max org: 99 | avg org: 50.156
1 Answers

normalize by default normalizes each row independently. But you're giving it a column vector — each row only has one value! Try adding the axis=0 keyword arg to normalize by column instead.

Related