scikit learn - converting features stored as strings into numbers
Question: I'm currently dipping my toes into machine learning using the scikit-learn
python library and am trying to use some .CSV data in the format

    
    
    Date        Name                    Average_Price_SA
    1995-01-01  Barking And Dagenham    70885.331285935
    1995-01-01  Barnet                  99567.4268042005
    1995-01-01  Barnsley                49608.33494746
    ....
    ....
    ....
    2005-01-01  Barking And Dagenham    13294.12321312
    

I have read them in using panda using the line

    
    
    data = pd.read_csv('data.csv')
    

From what I have learned so far, I think I'm supposed to convert those 'Name'
category strings into floats so that they can be accepted into a model.

I'm not sure how to go about this. Any help would be greatly appreciated.

Thanks


Answer: You can use scikit's `LabelBinarizer` to convert the strings to one hot
vectors. These have N zeros (where N is the number of unique strings) with a
one at a single component.

    
    
    from __future__ import print_function
    from sklearn import preprocessing
    
    names = ["Barking And Dagenham", "Barnet", "Barnsley"]
    lb = preprocessing.LabelBinarizer()
    vectors = lb.fit_transform(names)
    for name, vector in zip(names, vectors):
        print("%s => %s" % (name, str(vector)))
    

Output:

    
    
    Barking And Dagenham => [1 0 0]
    Barnet => [0 1 0]
    Barnsley => [0 0 1]
    

