本文翻译自:What does -1 mean in numpy reshape?

A numpy matrix can be reshaped into a vector using reshape function with parameter -1. 可以使用参数为-1的整形函数将numpy矩阵整形为向量。 But I don't know what -1 means here. 但我不知道-1在这里意味着什么。

For example: 例如:

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

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

Does anyone know what -1 means here? 有人知道-1在这里意味着什么吗? And it seems python assign -1 several meanings, such as: array[-1] means the last element. 似乎python给-1赋予了多种含义,例如: array[-1]表示最后一个元素。 Can you give an explanation? 你能解释一下吗?


#1楼

参考:https://stackoom.com/question/1GQP6/在numpy重塑中是什么意思


#2楼

According to the documentation : 根据the documentation

newshape : int or tuple of ints newshape:int或int的元组

The new shape should be compatible with the original shape. 新形状应与原始形状兼容。 If an integer, then the result will be a 1-D array of that length. 如果是整数,则结果将是该长度的一维数组。 One shape dimension can be -1. 一个形状尺寸可以为-1。 In this case, the value is inferred from the length of the array and remaining dimensions. 在这种情况下,该值是根据数组的长度和其余维来推断的。


#3楼

numpy.reshape(a,newshape,order{}) check the below link for more info. numpy.reshape(a,newshape,order {})检查以下链接以了解更多信息。 https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html https://docs.scipy.org/doc/numpy/reference/generation/numpy.reshape.html

for the below example you mentioned the output explains the resultant vector to be a single row.(-1) indicates the number of rows to be 1. if the 对于下面提到的示例,输出将结果向量解释为单行。(-1)表示行数为1。

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

output: 输出:

matrix([[1, 2, 3, 4, 5, 6, 7, 8]]) 矩阵([[1、2、3、4、5、6、7、8]])

this can be explained more precisely with another example: 这可以用另一个示例更精确地解释:

b = np.arange(10).reshape((-1,1))

output:(is a 1 dimensional columnar array) 输出:(是一维列式数组)

array([[0], 数组([[0],

   [1],[2],[3],[4],[5],[6],[7],[8],[9]])

b = np.arange(10).reshape((1,-1)) b = np.arange(10).reshape((1,-1))

output:(is a 1 dimensional row array) 输出:(是一维行数组)

array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) array([[0,1,2,3,4,5,6,7,8,9]])


#4楼

It is fairly easy to understand. 这很容易理解。 The "-1" stands for "unknown dimension" which can should be infered from another dimension. “ -1”代表“未知尺寸”,可以从另一个尺寸推断出来。 In this case, if you set your matrix like this: 在这种情况下,如果您这样设置矩阵:

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

Modify your matrix like this: 像这样修改矩阵:

b = numpy.reshape(a, -1)

It will call some deafult operations to the matrix a, which will return a 1-d numpy array/martrix. 它将对矩阵a调用一些默认操作,这将返回1-d numpy数组/矩阵。

However, I don't think it is a good idea to use code like this. 但是,我认为使用这样的代码不是一个好主意。 Why not try: 为什么不尝试:

b = a.reshape(1,-1)

It will give you the same result and it's more clear for readers to understand: Set b as another shape of a. 它将给您相同的结果,并使读者更清楚地理解:将b设置为a的另一种形状。 For a, we don't how much columns it should have(set it to -1!), but we want a 1-dimension array(set the first parameter to 1!). 对于a,我们不应该有多少列(将其设置为-1!),但是我们需要一维数组(将第一个参数设置为1!)。


#5楼

The criterion to satisfy for providing the new shape is that 'The new shape should be compatible with the original shape' 提供新形状所需满足的标准是“新形状应与原始形状兼容”

numpy allow us to give one of new shape parameter as -1 (eg: (2,-1) or (-1,3) but not (-1, -1)). numpy允许我们将新形状参数之一设为-1(例如:(2,-1)或(-1,3),但不提供(-1,-1))。 It simply means that it is an unknown dimension and we want numpy to figure it out. 它只是意味着它是一个未知的维,我们希望numpy弄清楚。 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 numpy将通过查看“数组的长度和剩余维数”并确保满足上述条件来解决这个问题

Now see the example. 现在看示例。

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

Now trying to reshape with (-1) . 现在尝试用(-1)重塑形状。 Result new shape is (12,) and is compatible with original shape (3,4) 结果新形状为(12,)并与原始形状(3,4)兼容

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

Now trying to reshape with (-1, 1) . 现在尝试用(-1,1)重塑形状。 We have provided column as 1 but rows as unknown . 我们将列设置为1,将行设置为unknown。 So we get result new shape as (12, 1).again compatible with original shape(3,4) 因此我们得到的新形状为(12,1)。又与原始形状(3,4)兼容

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

The above is consistent with numpy advice/error message, to use reshape(-1,1) for a single feature; 上面的内容与numpy通知/错误消息一致, numpy单个功能使用reshape(-1,1) ie single column 即单列

Reshape your data using array.reshape(-1, 1) if your data has a single feature 如果数据具有单个功能 array.reshape(-1, 1)请使用array.reshape(-1, 1)重塑数据

New shape as (-1, 2). 新形状为(-1,2)。 row unknown, column 2. we get result new shape as (6, 2) 未知行,第2列。我们得到的新形状为(6,2)

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

Now trying to keep column as unknown. 现在尝试使列为未知。 New shape as (1,-1). 新形状为(1,-1)。 ie, row is 1, column unknown. 即,行为1,列未知。 we get result new shape as (1, 12) 我们得到的结果新形状为(1,12)

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

The above is consistent with numpy advice/error message, to use reshape(1,-1) for a single sample; 以上与numpy通知/错误消息一致,对于单个样本使用reshape(1,-1) ie single row 即单排

Reshape your data using array.reshape(1, -1) if it contains a single sample 如果数据包含单个样本 array.reshape(1, -1)则使用array.reshape(1, -1)重塑数据

New shape (2, -1). 新形状(2,-1)。 Row 2, column unknown. 第2行,列不明。 we get result new shape as (2,6) 我们得到的结果新形状为(2,6)

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

New shape as (3, -1). 新形状为(3,-1)。 Row 3, column unknown. 第3行,列不明。 we get result new shape as (3,4) 我们得到的结果新形状为(3,4)

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

And finally, if we try to provide both dimension as unknown ie new shape as (-1,-1). 最后,如果我们尝试提供两个未知尺寸,即新形状为(-1,-1)。 It will throw an error 会抛出错误

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

#6楼

Used to reshape an array. 用于整形数组。

Say we have a 3 dimensional array of dimensions 2 x 10 x 10: 假设我们有一个尺寸为2 x 10 x 10的3维数组:

r = numpy.random.rand(2, 10, 10)

Now we want to reshape to 5 X 5 x 8: 现在我们要重塑为5 X 5 x 8:

numpy.reshape(r, shape=(5, 5, 8))

will do the job. 会做的工作。

Note that, once you fix first dim = 5 and second dim = 5, you don't need to determine third dimension. 请注意,一旦固定了第一个dim = 5和第二个dim = 5,就不需要确定第三维。 To assist your laziness, python gives the option of -1: 为了帮助您懒惰,python提供了-1选项:

numpy.reshape(r, shape=(5, 5, -1))

will give you an array of shape = (5, 5, 8). 将为您提供形状=(5,5,8)的数组。

Likewise, 同样

numpy.reshape(r, shape=(50, -1))

will give you an array of shape = (50, 4) 将为您提供形状=(50,4)的数组

You can read more at http://anie.me/numpy-reshape-transpose-theano-dimshuffle/ 您可以在http://anie.me/numpy-reshape-transpose-theano-dimshuffle/了解更多信息

-1在numpy重塑中是什么意思?相关推荐

  1. numpy使用[]语法索引二维numpy数组中指定行列位置的数值内容(access value at certain row and column in numpy array)

    numpy使用[]语法索引二维numpy数组中指定行列位置的数值内容(access value at certain row and column in numpy array) 目录

  2. numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row)

    numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row) 目录

  3. numpy使用[]语法索引二维numpy数组中指定数据行的数值内容(accessing the specific row in numpy array)

    numpy使用[]语法索引二维numpy数组中指定数据行的数值内容(accessing the specific row in numpy array) 目录 numpy使用[]语法索引二维numpy ...

  4. numpy使用[]语法索引二维numpy数组中指定范围数据行的数值内容(accessing rows in numpy array with specific range)

    numpy使用[]语法索引二维numpy数组中指定范围数据行的数值内容(accessing rows in numpy array with specific range) 目录

  5. numpy使用[]语法索引二维numpy数组中指定指定行之前所有数据行的数值内容(accessing rows in numpy array before specifc row)

    numpy使用[]语法索引二维numpy数组中指定指定行之前所有数据行的数值内容(accessing rows in numpy array before specifc row) 目录

  6. numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column)

    numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column) 目录

  7. numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array)

    numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array) 目录 numpy使用[]语

  8. python编写自定义函数计算一维numpy数组中与指定目标数值最接近(距离最近)的数值(find closest value in numpy array to a certain value)

    python编写自定义函数计算一维numpy数组中与指定目标数值最接近(距离最近)的数值(find closest value in numpy array to a certain value) 目 ...

  9. python使用numpy的np.float_power函数计算numpy数组中每个数值的指定幂次(例如平方、立方)、np.power函数默认返回整数格式、np.float_power函数返回浮点数

    python使用numpy的np.float_power函数计算numpy数组中每个数值的指定幂次(例如平方.立方).np.power函数默认返回整数格式.np.float_power函数默认返回浮点 ...

最新文章

  1. 智源社区票选2021 AI十大进展出炉!
  2. 2019年春季学期第七周作业
  3. 【Numpy】学习笔记1
  4. 笔记本win10玩红警黑屏_【买笔记本电脑差评真的有参考意义?】
  5. 前端学习(3082):vue+element今日头条管理-页面布局
  6. vue用阿里云oss上传图片使用分片上传只能上传100kb以内的解决办法
  7. 华为服务器存储系列,华为 服务器 存储相关资料(示例代码)
  8. Virtual DOM(虚拟dom-1)
  9. eclipse怎么显示代码行数
  10. 手动实现 NSTabViewController 的 Rect Transition 及 Propagate Title-b
  11. 中间键 csrf跨站请求伪造 装饰器相关 auth模块
  12. DB2 JDBC Driver 必需的字符转换器不可用
  13. 为磁盘更换好看的ico图标
  14. 姚锦云:再论庄子传播思想与接受主体性:回应尹连根教授
  15. Mysql 启动命令详解
  16. 破解老程序员的迷茫病——JUST DO IT
  17. Redis 集群可用性测试
  18. get请求图片出现403 防盗链解决方式 no-referrer
  19. 智能车浅谈——方向控制篇
  20. 关于Windows命令提示符中的 xxx > nul 2 > nul

热门文章

  1. 跟屌丝大哥学DB2-第四课 数据类型 ,表 ,视图,索引,模式,约束(一)
  2. 冰城环保进入智慧时代
  3. django定义Model中的方法和属性
  4. 正确配置Linux系统ulimit/nproc值的方法
  5. css滤镜使文字变3D效果
  6. hdu1711(kmp纯模板)
  7. 第一章:1.3:了解编译系统如何工作的好处
  8. 题解报告:hdu 1754 I Hate It(线段树)
  9. photoshop改变图片大小,不改变像素
  10. 读《那些年,那些事 一个程序猿的奋斗史》 一点自己的感触