我们处理feature的时候往往先要normalize encoding,使用python可以很容易做:
from sklearn import preprocessingfrom scipy.stats import rankdatax = [[1], [3], [34], [21], [10], [12]]std_x = preprocessing.StandardScaler().fit_transform(x)norm_x= preprocessing.MinMaxScaler().fit_transform(x)norm_x2= preprocessing.LabelEncoder().fit_transform(x)print('std_x=\n', std_x)print('norm_x=\n', norm_x)print('norm_2=\n', norm_x2)print('oringial order =', rankdata(x))print('stand order =', rankdata(std_x))print('normalize order=', rankdata(norm_x))
其中preprocessing.LabelEncoder().fit_transform(x)就是做normalize encoding,上面的程序输入如下:
std_x= [[-1.1124854 ] [-0.93448773] [ 1.82447605] [ 0.66749124] [-0.31149591] [-0.13349825]]norm_x= [[0. ] [0.06060606] [1. ] [0.60606061] [0.27272727] [0.33333333]]norm_2= [0 1 5 4 2 3]oringial order = [1. 2. 6. 5. 3. 4.]stand order = [1. 2. 6. 5. 3. 4.]normalize order= [1. 2. 6. 5. 3. 4.]
可以看到normailize之后的结果是 [0 1 5 4 2 3]。这样做的好处是什么呢?
下面图片转自知乎()