初赛
from sklearn.feature_extraction import DictVectorizer
创建字典
staff = [{'name': 'Steve Miller', 'age': 33.},
{'name': 'Lyndon Jones', 'age': 12.},
{'name': 'Baxter Morth', 'age': 18.}]
将字典转换为特征矩阵
# Create an object for our dictionary vectorizer
vec = DictVectorizer()
# Fit then transform the staff dictionary with vec, then output an array
vec.fit_transform(staff).toarray()
array([[ 33., 0., 0., 1.],
[ 12., 0., 1., 0.],
[ 18., 1., 0., 0.]])
查看功能名称
# Get Feature Names
vec.get_feature_names()
['age', 'name=Baxter Morth', 'name=Lyndon Jones', 'name=Steve Miller']
转载请注明:XAMPP中文组官网 » python学习:从字典加载特征