【问题标题】:Bag of Words encoding for Python with vocabulary带词汇的 Python 词袋编码
【发布时间】:2019-12-23 04:13:13
【问题描述】:

我正在尝试在我的 ML 模型中实现新列。如果在抓取数据的文本中找到特定字词,则应创建一个数字列。为此,我创建了一个用于测试的虚拟脚本。

import pandas as pd

bagOfWords = ["cool", "place"]
wordsFound = ""

mystring = "This is a cool new place"
mystring = mystring.lower()

for word in bagOfWords:
    if word in mystring: 
        wordsFound = wordsFound + word + " "

print(wordsFound)
pd.get_dummies(wordsFound)

输出是

    cool place
0   1

这意味着有一个句子“0”和一个“酷地方”条目。这是不正确的。期望是这样的:

    cool place
0   1    1

【问题讨论】:

    标签: python machine-learning nlp


    【解决方案1】:

    找到了一个不同的解决方案,因为我找不到任何前进的方法。它是一个简单的直接热编码。为此,我为每个需要在数据框中输入一个新列的单词输入并直接创建编码。

    vocabulary = ["achtung", "suchen"]
    
    for word in vocabulary:
        df2[word] = 0
    
        for index, row in df2.iterrows():
            if word in row["title"].lower():
                df2.set_value(index, word, 1)
    

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 2016-02-16
      • 2016-01-11
      • 2016-10-07
      • 1970-01-01
      • 2019-03-15
      • 2018-07-26
      • 2019-02-28
      相关资源
      最近更新 更多