From a list of feature vectors, convert category data to use multiple binary dummy variables using a DictVectorizer. Return the DictVectorizer and new list of feature vectors. The DictVectorizer must be used to tranform future testing feature vectors for prediction purposes.
(features)
| 100 | |
| 101 | |
| 102 | def convert_categorical_data(features): |
| 103 | """ |
| 104 | From a list of feature vectors, convert category data to use |
| 105 | multiple binary dummy variables using a DictVectorizer. |
| 106 | Return the DictVectorizer and new list of feature vectors. |
| 107 | The DictVectorizer must be used to tranform future testing |
| 108 | feature vectors for prediction purposes. |
| 109 | """ |
| 110 | asdict = todict(features) |
| 111 | vec = DictVectorizer(sort=False) |
| 112 | features_with_dummy_vars = vec.fit_transform(asdict).toarray() |
| 113 | print "number new vars", len(vec.get_feature_names()) |
| 114 | #print vec.get_feature_names() |
| 115 | return vec, features_with_dummy_vars |
| 116 | |
| 117 | |
| 118 | def todict(features): |