简体   繁体   English

ValueError:所有输入数组必须具有相同的形状

[英]ValueError: all input arrays must have the same shape

I am trying to execute a code but got this Value error. 我正在尝试执行代码,但出现此Value错误。 Source.pkl file contains 100 elements as shown in array.png attached with this. Source.pkl文件包含100个元素,如随附的array.png中所示。

As shown in figure at index 0 there are 16 numpy arrays all with shape(1,59) so, first element has shape (16,1,59). 如图中索引0所示,共有16个numpy数组,它们的形状均为(1,59),因此第一个元素的形状为(16,1,59)。 At index 1 there are 11 numpy arrays all with shape(1,59) so, now shape of second element is (11,1,59) and so on for other elements also. 在索引1处,有11个numpy数组,它们的形状均为(1,59),因此,第二个元素的形状为(11,1,59),对于其他元素,依此类推。

How can I have all the arrays with the same shape? 如何使所有阵列具有相同的形状?

包含100个具有不同数组大小的元素

import numpy as np
from keras.layers import Input, LSTM, Dense
from keras.models import Model

pickle_in = open("source.pkl","rb")
predictions = np.load(pickle_in)[0:100]
in_predictions = np.stack(predictions)
pickle_in.close()

Here's two choices: 这有两个选择:

Append zero arrays to every list, until all of the lists have same size. 将零数组追加到每个列表,直到所有列表的大小都相同。 In your case, assuming 16 is the largest size among all of the lists, you have to append (1, 59) shaped arrays to every list until the size of it is 16. 在您的情况下,假设16个是所有列表中最大的大小,则必须向每个列表追加(1,59)形状的数组,直到其大小为16。

But, it's a waste of space. 但是,这是浪费空间。 Another choice is: 另一个选择是:

Concatenate your lists to a large one, and then convert the large one into numpy array. 将您的列表连接成一个大列表 ,然后将大列表转换为numpy数组。 Use another list to record which array was originally in which list. 使用另一个列表来记录最初在哪个列表中的数组。 Here is a sample code, but I have not tested it: 这是一个示例代码,但我尚未对其进行测试:

pickle_in = open("source.pkl","rb")
predictions = np.load(pickle_in)[0:100]
wrap_list = []
index_map = []
for i in range(len(predictions)):
    pred = predictions[i]
    wrap_list += pred
    index_map += [i for _ in range(len(pred))]
in_predictions = np.array(wrap_list)
pickle_in.close()

I hope this could help you. 希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:所有输入 arrays 必须具有相同的维数 - ValueError: all the input arrays must have same number of dimensions 连接两个NumPy数组将给出“ ValueError:所有输入数组必须具有相同数量的维数” - Concatenating two NumPy arrays gives “ValueError: all the input arrays must have same number of dimensions” Numpy hstack - “ValueError:所有输入数组必须具有相同数量的维度” - 但它们确实如此 - Numpy hstack - “ValueError: all the input arrays must have same number of dimensions” - but they do ValueError:所有输入数组的维数必须相同(numpy错误) - ValueError: all the input arrays must have same number of dimensions, (numpy error) NumPy hstack抛出“ ValueError:所有输入数组必须具有相同数量的维吗?” - NumPy hstack throws “ValueError: all the input arrays must have same number of dimensions?” ValueError:logits 和标签必须具有相同的形状 ((None, 2) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1)) Python:读取 CSV 导致 ValueError:arrays 的长度必须相同 - Python: Reading CSV in results in ValueError: arrays must all be same length “ValueError:条件数组必须与自身形状相同” - "ValueError: Array conditional must be same shape as self" ValueError:对于 arrays,无法将输入数组从形状 (x,y) 广播到形状 (x-1, y),形状使用相同的变量定义 - ValueError: Could not broadcast input array from shape (x,y) into shape (x-1, y) for arrays with shape defined with the same variables 连接之前的输入数组必须具有相同数量的维 - input arrays before concatenate must have the same number of dimensions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM