Skip to main content
 首页 » 编程设计

python之删除数组中长度为 1 的维度

2025年05月04日35cyq1162

我是 Python 的真正初学者,我的 ndarrays 经常出现问题。 我对括号很困惑(有没有任何地方在 Python 中使用括号的示意图合成?)。我总是最终得到许多维度的数组。 现在我有这个:

>>> values 
Out[1]:  
array([[[ array([[ 4.23156519, -0.93539198], 
       [ 3.50074853, -1.67043386], 
       [ 4.64192393, -1.03918172], 
       [ 4.52056725,  0.2561267 ], 
       [ 3.36400016,  0.26435125], 
       [ 3.82025672,  1.16503286]])]]], dtype=object) 

从这里开始,我该如何减小尺寸?我只想要一个 6x2 数组。我尝试了 np.reshape 但由于 values 的当前形状是 (1,1,1) 我无法直接将数组 reshape 为 6x2 的数组。

对于这个愚蠢的问题,我深表歉意,我正在寻找一个通用的、示意性的答案,它可以解释我如何从更高的维度传递到更低的维度,反之亦然。

这是我创建数组的方法。 values 是 clustered_points

indices=[] # initialize indices 
clustered_points=[] # initialize array containing points in different sub-arrays=clusters 
for k in range(len(mu)): 
    a=r[:,k] 
    index=[t for t in range(len(a)) if a[t] == 1] 
    indices.append(index) 
    clustered_points.append(data[indices[k]]) 
clustered_points=np.reshape(clustered_points,(len(clustered_points),1,1)) 

请您参考如下方法:

要制作一个与您的初始显示相匹配的数组,我必须特别注意将一个数组嵌入另一个数组:

In [402]: x=np.array([[ 4.23156519, -0.93539198], 
       [ 3.50074853, -1.67043386], 
       [ 4.64192393, -1.03918172], 
       [ 4.52056725,  0.2561267 ], 
       [ 3.36400016,  0.26435125], 
       [ 3.82025672,  1.16503286]]) 
In [403]: a=array([[[None]]],dtype=object) 
In [404]: a[0,0,0]=x 
In [405]: a 
Out[405]:  
array([[[ array([[ 4.23156519, -0.93539198], 
       [ 3.50074853, -1.67043386], 
       [ 4.64192393, -1.03918172], 
       [ 4.52056725,  0.2561267 ], 
       [ 3.36400016,  0.26435125], 
       [ 3.82025672,  1.16503286]])]]], dtype=object) 
 
In [406]: a.shape 
Out[406]: (1, 1, 1) 
In [407]: a[0,0,0].shape 
Out[407]: (6, 2) 

只需从显示屏上进行剪切粘贴即可生成形状为 (1,1,1,6,2) 的不同数组。那没有内部 array 标记。无论哪种方式,a[0,0,0] 都会给出内部 (6,2) 数组。

reshape 和 squeeze 适用于 (1,1,1,6,2) 数组,但不适用于 (6,2) 嵌套在 (1,1,1) 中。您需要了解其中的区别。


(编辑)

要运行您的“我是怎么做到的”剪辑,我必须对输入进行一些猜测(这几乎值得投反对票)。

我会猜测一些输入:

In [420]: mu=np.arange(3); r=np.ones((4,3));data=np.ones(5) 
In [421]: %paste 
indices=[] # initialize indices 
clustered_points=[] # initialize array containing points in different sub-arrays=clusters 
for k in range(len(mu)): 
    a=r[:,k] 
    index=[t for t in range(len(a)) if a[t] == 1] 
    indices.append(index) 
    clustered_points.append(data[indices[k]]) 
 
## -- End pasted text -- 
In [422]: clustered_points 
Out[422]:  
[array([ 1.,  1.,  1.,  1.]), 
 array([ 1.,  1.,  1.,  1.]), 
 array([ 1.,  1.,  1.,  1.])] 

cluster_points 是一个包含多个一维数组的列表。

我可以

np.reshape(clustered_points,(12,1,1)) 
np.reshape(clustered_points,(3,4,1,1)) 

虽然我认为先执行 np.array(clustered_points) 会更好,甚至可以检查它的形状。

自

np.reshape(clustered_points,(len(clustered_points),1,1)) 

应该有效然后 clustered_points 必须是 n 单个元素数组的列表。但是这个 reshape 应该产生一个 (n,1,1) 数组,而不是你的 (1,1,1,...) 数组。

因此该编辑没有帮助。

=========================

I'm seeking a general and schematic answer that would explain me how to pass from a higher dimension to a lower one and vice versa.

第一步是向您自己和其他人清楚,您的数组结构是什么。这包括了解 shape 和 dtype。如果 dtype 不是简单的数字,请注意元素的结构(例如数组中的对象)。

可以使用索引、[0] 或 squeeze 删除奇异维度(值 1)。 reshape 也会删除维度(或添加维度),但您必须注意元素的总数。如果旧形状有 12 个元素,新形状也必须有 12 个。但 reshape 不会跨越 dtype 边界进行操作。