码迷,mamicode.com
首页 > 其他好文 > 详细

字典特征抽取

时间:2018-12-22 22:04:58      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:color   from   分享   main   life   inf   ima   code   name   

#特征抽取 feature_extraction
#导包
# from sklearn.feature_extraction.text import CountVectorizer
#
# vector = CountVectorizer()
#
# res = vector.fit_transform(["life is short,i like python ", "life is too long ,i dislike python"])
#
# print(vector.get_feature_names())
#
# print(res.toarray())

#导包 字典特征抽取
from sklearn.feature_extraction import DictVectorizer
#字典数据抽取:把字典中一些类别数据,分别进行转换成特征
def dictvec():
    #实例化
    dict = DictVectorizer(sparse=False) #sparse=False 取消稀疏矩阵
    data = dict.fit_transform([{city: 北京, temp: 100}, {city: 上海, temp: 60}, {city: 江西, temp: 30}])
    print(data)#sparse格式 节约内存 便于读取处理
    # [[0.   1.   0. 100.]
    #  [1.   0.   0.  60.]
    #  [0.   0.   1.  30.]]
    print(dict.get_feature_names()) #读取特征值
    # [‘city=上海‘, ‘city=北京‘, ‘city=江西‘, ‘temp‘]
    return None

if __name__ == "__main__":
    dictvec()

运行结果:

技术分享图片

 

技术分享图片

 

字典特征抽取

标签:color   from   分享   main   life   inf   ima   code   name   

原文地址:https://www.cnblogs.com/shixinzei/p/10162370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!