提问人:Saswat Behera 提问时间:10/31/2023 最后编辑:mkrieger1Saswat Behera 更新时间:10/31/2023 访问量:38
无法向 numpy 文件添加数据
Unable to add data to a numpy file
问:
while (True):
if not paused:
screen = grab_screen(region=(0, 40, 800, 640))
screen = cv2.cvtColor(screen,
cv2.COLOR_BGR2GRAY) # Removing color can reduce the complexity to train the neural network model
screen = cv2.resize(screen, (160, 120)) # resizing so that it would be easy to train in a CNN model
keys = key_check()
output = keys_to_output(keys)
training_data.append([screen, output])
if len(training_data) % 100 == 0: # to save training data after every 1000 records
print(len(training_data))
np.save(file_name, training_data)
错误:
File "create_training_data.py", line 63, in main
np.save(file_name, training_data) # saving 1000 records of training data to a file
File "<__array_function__ internals>", line 200, in save
File "C:\Users\Administrator\PycharmProjects\GameAutomation\venv\lib\site-packages\numpy\lib\npyio.py", line 521, in save
arr = np.asanyarray(arr)
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (100, 2) + inhomogeneous part.
我正在尝试获取屏幕数据以及记录的键并添加到列表中,在 100 个此类数据记录之后,我尝试将其保存到文件中。但是我遇到了某种错误。
答:
0赞
Klops
10/31/2023
#1
您正在尝试保存具有不同形状的numpy数组列表(?),因此连接数据将不起作用。[np.array([1,2,4]), np.array([1,2,3])]
我建议您简单地将两个值保存到不同的文件中,这应该使形状均匀,并将它们保存在类似数组的结构中将起作用。
PS:你可以用实际数据重新创建一个小问题,这样我们就可以给你更具体的建议。
0赞
Sauron
10/31/2023
#2
- Numpy 数组要求所有元素具有相同的形状。您遇到的错误是因为 ,它包含具有不同形状的元素,在将其保存到文件之前training_data转换为 numpy 数组。
training_data
if len(training_data) % 100 == 0:
print(len(training_data))
# Convert training_data to a numpy array
training_data_array = np.array(training_data)
np.save(file_name, training_data_array)
评论
1赞
Saswat Behera
11/2/2023
我尝试这样做,但失败了。所以我用一个泡菜文件来保存我的训练数据,它起作用了。
评论
training_data
key_check
keys_to_output
np.save
保存一个 numpy 数组。它在将您的输入(列表?)转换为数组时遇到了问题。