numpy concatenate over dimension

☆樱花仙子☆ 提交于 2021-02-11 12:21:30

问题


I find myself doing the following quite frequently and am wondering if there's a "canonical" way of doing it.

I have an ndarray say shape = (100, 4, 6) and I want to reduce to (100, 24) by concatenating the 4 vectors of length 6 into one vector

I can use reshape to do this but I've been manually computing the new shape

i.e.

np.reshape(x,shape=(a.shape[0],a.shape[1]*a.shape[2]))

ideally I'd simply supply the dimension I want to reduce on

np.concatenate(x,dim=-1)

but np.concatenate operates on an enumerable of ndarray. I've wondered if it's possible to supply an iterator over an ndarray axis but haven't looked further. What is the usual pattern here?


回答1:


You can avoid calculating one dimension by using -1 like:

x.reshape(a.shape[0], -1)


来源:https://stackoverflow.com/questions/61316456/numpy-concatenate-over-dimension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!