如何标准化特征向量HOW TO NORMALISE FEATURE VECTORS

来源:互联网 发布:js文件在线格式化 编辑:程序博客网 时间:2024/05/18 01:13

HOW TO NORMALISE FEATURE VECTORS

I was trying to create a sample file for training a neural network and ran into a common problem: the feature values are all over the place. In this example I’m working with demographical real-world values for countries. For example, a feature for GDP per person in a country ranges from 551.27 to 88286.0, whereas estimates for corruption range between -1.56 to 2.42. This can be very confusing for machine learning algorithms, as they can end up treating bigger values as more important signals.

To handle this issue, we want to scale all the feature values into roughly the same range. We can do this by taking each feature value, subtracting its mean (thereby shifting the mean to 0), and dividing by the standard deviation (normalising the distribution). This is a piece of code I’ve implemented a number of times for various projects, so it’s time to write a nice reusable script. Hopefully it can be helpful for others as well. I chose to do this in python, as it’s easies to run compared to C++ and Java (doesn’t need to be compiled), but has better support for real-valued numbers compared to bash scripting.

Each line in the input file is assumed to be a feature vector, with values separated by whitespace. The first element is an integer class label that will be left untouched. This is followed by a number of floating point feature values which will be normalised. For example:

1
2
1 0.563 13498174.2 -21.3
0 0.114 42234434.3 15.67

We’re assuming dense vectors, meaning that each line has an equal number of features.

To execute it, simply use

1
python feature-normaliser.py < in.txt > out.txt

The complete script that will normalise feature vectors is here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
importsys;
importfileinput;
importnumpy;
 
data=[]
linecount=0
forline infileinput.input():
  ifline.strip():
    index=0
    forvalue inline.split():
      iflinecount ==0:
        data.append([])
      ifindex ==0:
        data[index].append(int(value))
      else:
        data[index].append(float(value))
      index+=1
    linecount+=1
 
forrow inrange(0, linecount):
  forcol inrange(0, index):
    ifcol ==0:
      sys.stdout.write(str(data[col][row]))
    else:
      val=(data[col][row] -numpy.mean(data[col]))/numpy.std(data[col])
      sys.stdout.write("\t"+ str(val))
  sys.stdout.write("\n")

from: http://www.marekrei.com/blog/normalise-feature-vectors/
0 0
原创粉丝点击