numpy 连接维度 [英] numpy concatenate over dimension

查看:82
本文介绍了numpy 连接维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己经常执行以下操作,并且想知道是否有一种规范"的方法可以做到.

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

我有一个 ndarray 说 shape = (100, 4, 6) 我想通过将长度为 6 的 4 个向量连接成一个向量来减少到 (100, 24)

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

我可以使用 reshape 来做到这一点,但我一直在手动计算新的形状

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

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

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)

np.concatenate(x,dim=-1)

但是 np.concatenate 对 ndarray 的可枚举进行操作.我想知道是否可以在 ndarray 轴上提供迭代器,但没有进一步研究.这里通常的模式是什么?

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)

这篇关于numpy 连接维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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