-1 在 numpy reshape 中是什么意思? [英] What does -1 mean in numpy reshape?

查看:34
本文介绍了-1 在 numpy reshape 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用参数为 -1 的 reshape 函数将 numpy 矩阵重新整形为向量.但我不知道这里的 -1 是什么意思.

A numpy matrix can be reshaped into a vector using reshape function with parameter -1. But I don't know what -1 means here.

例如:

a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
b = numpy.reshape(a, -1)

b 的结果是:matrix([[[1, 2, 3, 4, 5, 6, 7, 8]])

有谁知道这里的 -1 是什么意思?而且似乎python给-1赋值了几个意思,比如: array[-1] 表示最后一个元素.能解释一下吗?

Does anyone know what -1 means here? And it seems python assign -1 several meanings, such as: array[-1] means the last element. Can you give an explanation?

推荐答案

提供新形状的条件是'新形状应该与原始形状兼容'

numpy 允许我们将新的形状参数之一指定为 -1(例如:(2,-1) 或 (-1,3) 但不是 (-1, -1)).它只是意味着它是一个未知的维度,我们希望 numpy 来解决它.numpy 将通过查看 '数组的长度和剩余维度' 并确保它满足上述标准来计算这一点

numpy allow us to give one of new shape parameter as -1 (eg: (2,-1) or (-1,3) but not (-1, -1)). It simply means that it is an unknown dimension and we want numpy to figure it out. And numpy will figure this by looking at the 'length of the array and remaining dimensions' and making sure it satisfies the above mentioned criteria

现在看例子.

z = np.array([[1, 2, 3, 4],
         [5, 6, 7, 8],
         [9, 10, 11, 12]])
z.shape
(3, 4)

现在尝试用 (-1) 重塑.结果新形状是 (12,) 并且与原始形状 (3,4) 兼容

Now trying to reshape with (-1) . Result new shape is (12,) and is compatible with original shape (3,4)

z.reshape(-1)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

现在尝试用 (-1, 1) 重塑.我们提供 column 作为 1 但行作为 unknown .所以我们得到结果新形状为 (12, 1). 再次兼容原始形状 (3,4)

Now trying to reshape with (-1, 1) . We have provided column as 1 but rows as unknown . So we get result new shape as (12, 1).again compatible with original shape(3,4)

z.reshape(-1,1)
array([[ 1],
   [ 2],
   [ 3],
   [ 4],
   [ 5],
   [ 6],
   [ 7],
   [ 8],
   [ 9],
   [10],
   [11],
   [12]])

以上与numpy建议/错误信息一致,对单个特征使用reshape(-1,1);即单列

The above is consistent with numpy advice/error message, to use reshape(-1,1) for a single feature; i.e. single column

如果您的数据具有单一特征

新形状为 (-1, 2).行未知,第 2 列.我们得到结果新形状为 (6, 2)

New shape as (-1, 2). row unknown, column 2. we get result new shape as (6, 2)

z.reshape(-1, 2)
array([[ 1,  2],
   [ 3,  4],
   [ 5,  6],
   [ 7,  8],
   [ 9, 10],
   [11, 12]])

现在试图将列保持为未知.新形状为 (1,-1).即,行为 1,列未知.我们得到结果新形状为 (1, 12)

Now trying to keep column as unknown. New shape as (1,-1). i.e, row is 1, column unknown. we get result new shape as (1, 12)

z.reshape(1,-1)
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]])

以上与numpy建议/错误信息一致,对单个样本使用reshape(1,-1);即单行

The above is consistent with numpy advice/error message, to use reshape(1,-1) for a single sample; i.e. single row

如果数据包含单个样本

新形状 (2, -1).第 2 行,列未知.我们得到结果新形状为 (2,6)

New shape (2, -1). Row 2, column unknown. we get result new shape as (2,6)

z.reshape(2, -1)
array([[ 1,  2,  3,  4,  5,  6],
   [ 7,  8,  9, 10, 11, 12]])

新形状为 (3, -1).第 3 行,列未知.我们得到结果新形状为 (3,4)

New shape as (3, -1). Row 3, column unknown. we get result new shape as (3,4)

z.reshape(3, -1)
array([[ 1,  2,  3,  4],
   [ 5,  6,  7,  8],
   [ 9, 10, 11, 12]])

最后,如果我们尝试将两个维度都提供为未知,即新形状为 (-1,-1).会报错

And finally, if we try to provide both dimension as unknown i.e new shape as (-1,-1). It will throw an error

z.reshape(-1, -1)
ValueError: can only specify one unknown dimension

这篇关于-1 在 numpy reshape 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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