【问题标题】:Keras Tokenizer only tokenizing the first row of CSV fileKeras Tokenizer 仅对 CSV 文件的第一行进行标记
【发布时间】:2021-11-28 19:04:51
【问题描述】:

我真的是 keras API 的新手,我可能会被困在一个非常简单的任务上。我有一个 4 列的 csv 文件。目前我只想使用这些列中的 1 个。我正在使用 pandas 库来读取 csv 并选择仅使用列 'host'

这工作正常,但是当我通过 keras 标记器函数对数据进行标记时,它只读取 csv 文件中的第一行。

我需要分词器读取 csv 并在字符级别对其进行分词,它似乎正在这样做,但仅针对第一行。请参阅下面的代码,非常感谢任何帮助。

fields=['host']
test_dataset = pd.read_csv('dga_data.csv',usecols=fields)

test_dataset_tok= Tokenizer(split=',',char_level=True, oov_token=True)
print(test_dataset_tok)

test_dataset_tok.fit_on_texts(test_dataset)
print(test_dataset_tok)

test_dataset_sequences=test_dataset_tok.texts_to_sequences(test_dataset)
print(test_dataset_sequences)
print(test_dataset_tok.word_index)

【问题讨论】:

    标签: python pandas csv keras


    【解决方案1】:

    您将 Dataframe 传递给 fit_on_texts 并且您需要传递一个列表。来自documentation

    texts : 可以是字符串列表、字符串生成器(用于提高内存效率)或字符串列表。

    因此,您需要传递一个列表,或者至少是一个 Pandas Series,因此当 fit_on_texts 执行 this for 循环时,它会遍历 CSV 的每一行文件,而不仅仅是数据框轴标签。

    In [22]: type(test_dataset)
    Out[22]: pandas.core.frame.DataFrame
    
    In [23]: type(test_dataset['host'])
    Out[23]: pandas.core.series.Series
    
    import pandas as pd
    from tensorflow.keras.preprocessing.text import Tokenizer
    
    test_dataset = pd.DataFrame({'host': [
        'Aspire to inspire before we expire.',
        'Let the beauty of what you love be what you do.',
        'The meaning of life is to give life meaning.',
        'I have nothing to lose but something to gain.',
    ]})
    
    # pandas.core.series.Series
    test_dataset = test_dataset['host']
    
    test_dataset_tok= Tokenizer(split=',',char_level=True, oov_token=True)
    print(test_dataset_tok)
    
    test_dataset_tok.fit_on_texts(test_dataset)
    print(test_dataset_tok)
    
    test_dataset_sequences=test_dataset_tok.texts_to_sequences(test_dataset)
    print(test_dataset_sequences)
    print(test_dataset_tok.word_index)
    

    输出:

    <keras_preprocessing.text.Tokenizer object at 0x0000019AFFA65CD0>
    <keras_preprocessing.text.Tokenizer object at 0x0000019AFFA65CD0>
    [
        [8, 11, 18, 4, 14, 3, 2, 6, 5, 2, 4, 7, 11, 18, 4, 14, 3, 2, 15, 3, 12, 5, 14, 3, 2, 19, 3, 2, 3, 23, 18, 4, 14, 3, 16],
        [13, 3, 6, 2, 6, 9, 3, 2, 15, 3, 8, 17, 6, 20, 2, 5, 12, 2, 19, 9, 8, 6, 2, 20, 5, 17, 2, 13, 5, 21, 3, 2, 15, 3, 2, 19, 9, 8, 6, 2, 20, 5, 17, 2, 24, 5, 16],
        [6, 9, 3, 2, 22, 3, 8, 7, 4, 7, 10, 2, 5, 12, 2, 13, 4, 12, 3, 2, 4, 11, 2, 6, 5, 2, 10, 4, 21, 3, 2, 13, 4, 12, 3, 2, 22, 3, 8, 7, 4, 7, 10, 16], 
        [4, 2, 9, 8, 21, 3, 2, 7, 5, 6, 9, 4, 7, 10, 2, 6, 5, 2, 13, 5, 11, 3, 2, 15, 17, 6, 2, 11, 5, 22, 3, 6, 9, 4, 7, 10, 2, 6, 5, 2, 10, 8, 4, 7, 16]
    ]
    {
        True: 1, ' ': 2, 'e': 3, 'i': 4, 'o': 5, 't': 6, 'n': 7, 'a': 8,
        'h': 9, 'g': 10, 's': 11, 'f': 12, 'l': 13, 'r': 14, 'b': 15, '.': 16,
         'u': 17, 'p': 18, 'w': 19, 'y': 20, 'v': 21, 'm': 22, 'x': 23, 'd': 24
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-23
      • 2018-06-01
      • 1970-01-01
      • 2018-08-01
      • 2017-11-29
      • 2014-12-02
      • 1970-01-01
      相关资源
      最近更新 更多