一维阵列的形状(长度,)与(长度,1)与(长度) [英] one-dimensional array shapes (length,) vs. (length,1) vs. (length)

查看:216
本文介绍了一维阵列的形状(长度,)与(长度,1)与(长度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我检查数组的使用形态 numpy.shape(),我有时会收到(长度,1)有时(长度)。它看起来像不同的是与行向量列...但它似乎并不像改变有关阵列本身[除了一些功能,当我通过与形状一个数组(长度抱怨什么, 1)

When I check the shape of an array using numpy.shape(), I sometimes get (length,1) and sometimes (length,). It looks like the difference is a column vs. row vector... but It doesn't seem like that changes anything about the array itself [except some functions complain when I pass an array with shape (length,1)].

什么是这两者之间有什么区别?结果
为什么不是只是外形,(长度)

What is the difference between these two?
Why isn't the shape just, (length)?

推荐答案

的要点是,说的载体可以看出无论是作为

The point is that say a vector can be seen either as


  • 一个vector

  • 只有一列的矩阵

  • 3维数组,其中第二和第三层面有一个长度

  • ...

您可以使用 [:, np.newaxis] 语法添加尺寸或使用尺寸下降 np.squeeze

You can add dimensions using [:, np.newaxis] syntax or drop dimensions using np.squeeze:

>>> xs = np.array([1, 2, 3, 4, 5])
>>> xs.shape
(5,)
>>> xs[:, np.newaxis].shape  # a matrix with only one column
(5, 1)
>>> xs[np.newaxis, :].shape  # a matrix with only one row
(1, 5)
>>> xs[:, np.newaxis, np.newaxis].shape  # a 3 dimensional array
(5, 1, 1)
>>> np.squeeze(xs[:, np.newaxis, np.newaxis]).shape
(5,)

这篇关于一维阵列的形状(长度,)与(长度,1)与(长度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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