ValueError: could not broadcast input array from shape (2,2) into shape (1,2)

I’m working with inhomogeneous higher dimensions matrix, but I could solve all of the inhomogeneous problems by using:

self.matrix = np.array([np.empty((1, 5, 5, 5), dtype=object), np.empty((1, 5, 5, 5), dtype=object)])

This is a first simplification, where I just trying to work with 2 dimensions that “grow” independently, I can and will colapse some dimensions in the future.

The following code is a direct representation of my problem:

a1 = np.arange(1, 3).reshape(1, 2)
a2 = np.arange(1, 5).reshape(2, 2)
a1 = a2

a3 = np.arange(1, 3).reshape(1, 1, 2)
a4 = np.arange(1, 5).reshape(1, 2, 2)
a3[0] = a4[0]

I just don’t get why the first part works fine and the second one doesn’t, they just seem to be the same thing, the only difference is that I couldn’t work with inhomogeneous arrays without using it like the second example.

I’ll try to clarify it in 2D, since there are only 2 dimensions that need to be different, while the others can be treated as a function of either of the first 2.

I have [[1]] at the beginning, and I need it to be possible to become [[1 2]] or [[1][2]] or the most likely scenario [[1 2][1][1 2 3][ 2 3]], where the numbers represent different states (for rows, not columns). I need each row to preserve the past states for my calculations, that’s why [[2][1][3][3]] doesn’t work.

One thing that may be misrepresented is that [0][1] is not the same as [2][1], but [2][1] is the same as [3][1] (notice how [3][0] is empty), this logic applies to [2][2] != [3][2].

The amount of workaround I had to do already shows me that numpy doesn’t like to do this, I just couldn’t find any other way of working with this.

New contributor

user27452820 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Your question isn’t real clear, but I think you want a ‘general’ way of constructing ‘inhomogenous’ arrays. Keep in mind that that is something of a step-child in numpy. Numpy is really intended for working with large ‘rectangular’ arrays with numeric values.

Your first example makes:

In [142]: arr = np.array([np.empty((1, 5, 5, 5), dtype=object), np.empty((1, 5, 5, 5), dtype=object)])

In [143]: arr.shape
Out[143]: (2, 1, 5, 5, 5)

In [144]: arr.dtype
Out[144]: dtype('O')
    
In [146]: arr[0,0,0,0,:]
Out[146]: array([None, None, None, None, None], dtype=object)

It’s a 5d array filled with None objects. You can reshape it, but can’t add or remove elements, or change slices to other sizes.

Let’s instead make an object dtype array with just 2 elements:

In [147]: arr = np.empty(2, object); arr
Out[147]: array([None, None], dtype=object)

and your 2 arrays with different shapes:

In [148]: a3 = np.arange(1, 3).reshape(1, 1, 2)
     ...: a4 = np.arange(1, 5).reshape(1, 2, 2)

In [149]: a3
Out[149]: array([[[1, 2]]])

In [150]: a4
Out[150]: 
array([[[1, 2],
        [3, 4]]])

We can assign those to the elements of arr:

In [151]: arr[0] = a3; arr[1] = a4; arr
Out[151]: 
array([array([[[1, 2]]]), array([[[1, 2],
                                  [3, 4]]])], dtype=object)

We can, at least when arr is 1d, assign all elements from a list:

In [152]: arr[:] = [a3,a4]

In [153]: arr
Out[153]: 
array([array([[[1, 2]]]), array([[[1, 2],
                                  [3, 4]]])], dtype=object)

In many ways such a object array is little different from a list.

Note we cannot make an object array directly from the list:

In [154]: np.array([a3,a4], object)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[154], line 1
----> 1 np.array([a3,a4], object)

ValueError: could not broadcast input array from shape (2,) into shape (1,)

There are cases where np.array(alist, object) works, but in others it makes a higher dimensional array as in your first example. In others you get this kind of error. The create and fill approach works in all cases.

But keep in mind that computations, and most other processing, of such array are not as fast or convenient as the equivalent on a homegeneous numeric array.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật