On 24/10/16 19:05, Peter Otten wrote: > duncan smith wrote: > >> Hello, >> I have several arrays that I need to combine elementwise in >> various fashions. They are basically probability tables and there is a >> mapping of axes to variables. I have code for transposing and reshaping >> that aligns the variables / axes so the usual broadcasting rules achieve >> the desired objective. But for a specific application I want to avoid >> the transposing and reshaping. So I've specified arrays that contain the >> full dimensionality (dimensions equal to the total number of variables). >> e.g. >> >> Arrays with shape, >> >> [1,3,3] and [2,3,1] >> >> to represent probability tables with variables >> >> [B,C] and [A,B]. >> >> One operation that I need that is not elementwise is summing over axes, >> but I can use numpy.sum with keepdims=True to retain the appropriate >> shape. >> >> The problem I have is with slicing. This drops dimensions. Does anyone >> know of a solution to this so that I can e.g. take an array with shape >> [2,3,1] and generate a slice with shape [2,1,1]? I'm hoping to avoid >> having to manually reshape it. Thanks. > > Can you clarify your requirement or give an example of what you want? > > Given an array > >>>> a.shape > (2, 3, 1) > > you can get a slice with shape (2,1,1) with (for example) > >>>> a[:,:1,:].shape > (2, 1, 1) > > or even > >>>> newshape = (2, 1, 1) >>>> a[tuple(slice(d) for d in newshape)].shape > (2, 1, 1) > > but that's probably not what you are asking for... >
Thanks. I think that's exactly what I wanted. Duncan -- https://mail.python.org/mailman/listinfo/python-list