创建numpy数组问题(无法将输入数组从形状(2)广播到形状(1)) [英] Creating numpy array problem ( could not broadcast input array from shape (2) into shape (1) )

查看:139
本文介绍了创建numpy数组问题(无法将输入数组从形状(2)广播到形状(1))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建此numpy数组有什么问题

what's the problem with creating this numpy array

np.array( [np.array([1]), np.array([ [1,2] ])] )

# Error: could not broadcast input array from shape (2) into shape (1)

但这没问题

np.array( [np.array([1]), np.array([ [1,2], [1,2] ])] )

为具有不同形状的不同物体创建容器数组有什么问题?

what's the problem with creating a container array for different things with different shapes?

推荐答案

如果最终输出可以是一维矢量,则np.append可能会成功:

If the final output can be a 1D vector, np.append might do the trick:

np.append(np.array([1]),np.array([[1,2]]))

如果最终所需数组的每个元素都是一个不同的维,是否需要将其作为一个numpy对象?列表应该final = [np.array([1]), np.array([ [1,2], [1,2] ])]

If each element of the final desired array is a different dimension, do you need it to be a numpy object? a list should work final = [np.array([1]), np.array([ [1,2], [1,2] ])]

这篇关于创建numpy数组问题(无法将输入数组从形状(2)广播到形状(1))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆