尝试对数组进行切片会导致 "Too many indices for array"。我可以填充阵列来解决这个问题吗?
trying to slice array results in "Too many indices for array". Can I pad the array to fix this?
我已经看到关于这个特定错误的大量问题。我相信我的问题是不同的足以保证它自己 post。
我的 objective: 我正在构建一个生成新闻标题的循环神经网络。它会根据之前出现的单词预测下一个单词。此代码来自 example,我正在努力使其适应我的情况。我正在尝试将数组分割成 X
和 y
.
问题:
我知道出现错误是因为数组被索引为二维数组,但它实际上是一维数组。在将 sequences
转换为数组之前,它是一个列表列表,但并非所有嵌套列表的长度都相同,因此 numPy 将其转换为一维数组。
我的问题: 是否有一种简单或优雅的方法来填充 sequences
以便所有列表的长度相同?我可以使用空格在较短的标题中保持相同的含义吗?为什么我需要将列表的列表更改为数组?正如我之前所说,这是来自一个例子,我试图理解他们做了什么以及为什么。
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Pretreat Data Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# integer encode sequences of words
# create the tokenizer
t = Tokenizer()
# fit the tokenizer on the headlines
t.fit_on_texts(headlines)
sequences = t.texts_to_sequences(headlines)
# vocabulary size
vocab_size = len(t.word_index) + 1
#separate into input and output
sequences = np.array(sequences)
X, y = sequences[:,:-1], sequences[:,-1] # fix this
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-87-eb7aab0c3a22> in <module>
18 #separate into input and output
19 sequences = np.array(sequences)
---> 20 X, y = sequences[:,:-1], sequences[:,-1] # fix this
21 y = to_categorical(y, num_classes=vocab_size)
22 seq_length = X.shape[1]
IndexError: too many indices for array
问题是本教程在一页上只有几个部分,每个部分都有自己的 "Complete Example"
首先 "Complete Example"
从 republic_clean.txt
中读取文本,将其清除并保存在 republic_sequences.txt
中 - 它会创建具有相同字数的序列。
第二个 "Complete Example"
从 republic_sequences.txt
读取文本并将其与
一起使用
sequences = np.array(sequences)
X, y = sequences[:,:-1], sequences[:,-1]
因为第一部分创建了具有相同数量单词的序列,所以这段代码可以正常工作。
您似乎跳过了第一部分。您必须返回第一部分以了解如何清除文本以及如何创建可以在第二部分中使用的正确文件。
编辑: 如果您不能用相同数量的单词制作序列,那么您可以在较短的序列中添加空格。代码可以工作,但我不知道它是否会创建更好的模型。
sequences = [['a'], ['b','c'], ['d','e','f']]
max_len = max(map(len, sequences))
sequences = [x + [""]*(max_len-len(x)) for x in sequences]
print(sequences)
结果
[['a', '', ''], ['b', 'c', ''], ['d', 'e', 'f']]
我已经看到关于这个特定错误的大量问题。我相信我的问题是不同的足以保证它自己 post。
我的 objective: 我正在构建一个生成新闻标题的循环神经网络。它会根据之前出现的单词预测下一个单词。此代码来自 example,我正在努力使其适应我的情况。我正在尝试将数组分割成 X
和 y
.
问题:
我知道出现错误是因为数组被索引为二维数组,但它实际上是一维数组。在将 sequences
转换为数组之前,它是一个列表列表,但并非所有嵌套列表的长度都相同,因此 numPy 将其转换为一维数组。
我的问题: 是否有一种简单或优雅的方法来填充 sequences
以便所有列表的长度相同?我可以使用空格在较短的标题中保持相同的含义吗?为什么我需要将列表的列表更改为数组?正如我之前所说,这是来自一个例子,我试图理解他们做了什么以及为什么。
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Pretreat Data Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# integer encode sequences of words
# create the tokenizer
t = Tokenizer()
# fit the tokenizer on the headlines
t.fit_on_texts(headlines)
sequences = t.texts_to_sequences(headlines)
# vocabulary size
vocab_size = len(t.word_index) + 1
#separate into input and output
sequences = np.array(sequences)
X, y = sequences[:,:-1], sequences[:,-1] # fix this
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-87-eb7aab0c3a22> in <module>
18 #separate into input and output
19 sequences = np.array(sequences)
---> 20 X, y = sequences[:,:-1], sequences[:,-1] # fix this
21 y = to_categorical(y, num_classes=vocab_size)
22 seq_length = X.shape[1]
IndexError: too many indices for array
问题是本教程在一页上只有几个部分,每个部分都有自己的 "Complete Example"
首先 "Complete Example"
从 republic_clean.txt
中读取文本,将其清除并保存在 republic_sequences.txt
中 - 它会创建具有相同字数的序列。
第二个 "Complete Example"
从 republic_sequences.txt
读取文本并将其与
sequences = np.array(sequences)
X, y = sequences[:,:-1], sequences[:,-1]
因为第一部分创建了具有相同数量单词的序列,所以这段代码可以正常工作。
您似乎跳过了第一部分。您必须返回第一部分以了解如何清除文本以及如何创建可以在第二部分中使用的正确文件。
编辑: 如果您不能用相同数量的单词制作序列,那么您可以在较短的序列中添加空格。代码可以工作,但我不知道它是否会创建更好的模型。
sequences = [['a'], ['b','c'], ['d','e','f']]
max_len = max(map(len, sequences))
sequences = [x + [""]*(max_len-len(x)) for x in sequences]
print(sequences)
结果
[['a', '', ''], ['b', 'c', ''], ['d', 'e', 'f']]