【问题标题】:Deleting a word in column based on frequencies根据频率删除列中的单词
【发布时间】:2020-04-13 13:10:37
【问题描述】:

我有一个 NLP 项目,我想删除在关键字中只出现一次的单词。也就是说,对于每一行,我都有一个关键字列表及其频率。

我想要类似的东西

if the frequency for the word in the whole column ['keywords'] ==1 then replace by "". 

我无法逐字测试。所以我的想法是创建一个包含所有单词的列表并删除重复项,然后对于此列表中的每个单词 count.sum 然后删除。但我不知道该怎么做。 有任何想法吗?谢谢!

我的数据如下所示:

sample.head(4)

    ID  keywords                                            age sex
0   1   fibre:16;quoi:1;dangers:1;combien:1;hightech:1...   62  F
1   2   restaurant:1;marrakech.shtml:1  35  M
2   3   payer:1;faq:1;taxe:1;habitation:1;macron:1;qui...   45  F
3   4   rigaud:3;laurent:3;photo:11;profile:8;photopro...   46  F

【问题讨论】:

  • 请添加语言标签
  • 谢谢,已添加
  • 一个编程语言标签!您的项目使用什么语言?
  • 对对对,python 3

标签: python-3.x dataframe nlp data-cleaning french


【解决方案1】:

要补充@jpl 与 scikit-learn 的 CountVectorizer 提到的内容,有一个选项 min_df 可以完全满足您的需求,前提是您可以以正确的格式获取数据。这是一个例子:

from sklearn.feature_extraction.text import CountVectorizer
# assuming you want the token to appear in >= 2 documents
vectorizer = CountVectorizer(min_df=2)
documents = ['hello there', 'hello']
X = vectorizer.fit_transform(documents)

这给了你:

# Notice the dimensions of our array – 2 documents by 1 token
>>> X.shape
(2, 1)
# Here is a count of how many times the tokens meeting the inclusion
# criteria are observed in each document (as you see, "hello" is seen once
# in each document
>>> X.toarray()
array([[1],
       [1]])
# this is the entire vocabulary our vectorizer knows – see how "there" is excluded?
>>> vectorizer.vocabulary_
{'hello': 0}

【讨论】:

  • 是的!无论如何,我确实使用矢量化器来训练我的分类模型,但我不知道 min_df。感谢 vectorizer.vocabulary_ 也允许进一步探索令牌!
  • @Me.Ch 如果答案解决了您的问题,请接受 - 请参阅What should I do when someone answers my question?
【解决方案2】:

您的代理使这变得困难。 您应该构建一个数据框,其中每列都是一个单词;那么你可以轻松地使用像 sum 这样的 pandas 操作来做任何你想做的事情。

但是,这会导致数据帧非常稀疏,这绝不是好事。

许多库,例如scikit learn's CountVectorizer 让你高效地做你想做的事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多