【发布时间】:2015-08-19 15:26:22
【问题描述】:
我正在尝试根据帖子的文本和其他特征(一天中的时间、帖子的长度等)对帖子的得分进行建模
我想知道如何将这些不同类型的功能最好地组合到一个模型中。现在,我有类似以下的内容(从here 和here 窃取)。
import pandas as pd
...
def features(p):
terms = vectorizer(p[0])
d = {'feature_1': p[1], 'feature_2': p[2]}
for t in terms:
d[t] = d.get(t, 0) + 1
return d
posts = pd.read_csv('path/to/csv')
# Create vectorizer for function to use
vectorizer = CountVectorizer(binary=True, ngram_range=(1, 2)).build_tokenizer()
y = posts["score"].values.astype(np.float32)
vect = DictVectorizer()
# This is the part I want to fix
temp = zip(list(posts.message), list(posts.feature_1), list(posts.feature_2))
tokenized = map(lambda x: features(x), temp)
X = vect.fit_transform(tokenized)
从 pandas 数据框中提取我想要的所有特征,只是将它们全部压缩在一起,这似乎很愚蠢。有没有更好的方法来完成这一步?
CSV 如下所示:
ID,message,feature_1,feature_2
1,'This is the text',4,7
2,'This is more text',3,2
...
【问题讨论】:
-
你能展示你的 csv 样本吗?
-
@elyase,我刚刚添加了它的玩具版本。
标签: python pandas machine-learning nlp scikit-learn