【问题标题】:Cleaning sentences through Tf-idf values and retaining only high tf-idf score words in the sentence in Python在 Python 中通过 Tf-idf 值清洗句子,只保留句子中 tf-idf 得分高的词
【发布时间】:2021-02-24 13:54:35
【问题描述】:

我有一个数据集,格式为我有一个句子,对于每个句子,我都有该句子中每个相关单词的 tf-idf 值。

Sample dataset:
                                            heel syrup word3 word4 word5
    So what is a better exercise            0     0     0      0    0.34 
    how many days hv to take syrup          0   0.95    0      0     0      
    Can I take this solution ?              0   0   0   0      0.23     
      

数据集非常庞大,大约有 10K 行是句子,5K 列是单词。 从这里我想创建一个新列,并为每个句子保留 tf-idf 值大于 0.6 的单词。 实现的代码是:

dataset = pd.read_csv(r'Desktop/tfidf_val.csv')

dataset.apply(lambda x: x.index[x.astype(bool)].tolist(), 1)

但我遇到了内存错误。知道如何解决这个问题或者代码是否存在问题

【问题讨论】:

    标签: python pandas nlp out-of-memory tf-idf


    【解决方案1】:

    我曾经遇到过这个问题,我的解决方案是更换:

    df = pd.read_csv(r'filename.csv')
    
    

    df = pd.read_csv(r'filename.csv',sep=';',low_memory=False)
    

    编辑: 由于您的列包含逗号并且文件以逗号分隔,因此您需要知道您有多少列。当你这样做时,它是 M,添加这个:

    n = M
    df = pd.read_csv(r'filename.csv', 
                     usecols=range(M),
                     lineterminator='\n',       header=None,low_memory=False))
    

    【讨论】:

    • 我收到此错误“ParserError: Error tokenizing data.C error: Expected 1 fields in line 128, saw 3”
    • 这意味着您在第一列中有逗号。你原来的 Df 是用 , 还是 ; 分隔的?
    • 逗号分隔
    • n = M 有什么作用?尝试过但收到此错误“ParserError: Error tokenizing data. C error: out of memory”
    • M 是数据框中的列数。 Sp,如果你知道有 10 列,它们 M = 10。
    猜你喜欢
    • 2018-03-23
    • 2016-10-13
    • 2019-04-17
    • 2015-05-07
    • 2018-08-05
    • 2017-07-01
    • 2018-08-22
    • 1970-01-01
    • 2015-04-17
    相关资源
    最近更新 更多