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

特征维中词矩阵

时间:2018-04-28 19:34:13      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:learn   count   drop   import   extract   csv   join   cto   index   

在处理数据的时候偶尔会遇到特征维如下情况:

技术分享图片

可以将other维中的以分号分隔的词转化为词向量的形式:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer

df = pd.read_csv("data.txt",index_col=0)
df[other] = df[other].apply(lambda x:  .join(x.split(;)))
cv = CountVectorizer()
other_list = cv.fit_transform(df[other])
df1 = pd.DataFrame(other_list.toarray(), columns = cv.get_feature_names())
df = df.reset_index(drop = True)
df_tmp = df.drop(other, axis=1)
df_final = df_tmp.join(df1)

最终得到结果:

技术分享图片

 

特征维中词矩阵

标签:learn   count   drop   import   extract   csv   join   cto   index   

原文地址:https://www.cnblogs.com/hbwxcw/p/8968881.html

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