What does -1 mean in numpy reshape?

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)). 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

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) . 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])

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]])

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]])
时间: 2024-10-13 14:09:48

What does -1 mean in numpy reshape?的相关文章

数组:numpy.shape 与numpy.reshape函数

倒入numpy模块 import numpy as np Array(数组) a = np.array([1,2,3])#a #array([1,2,3]) type(a)#nympy.ndarray a.shape#(3,) #一纬数据 看大小 a= a.reshape((1,-1) ) #明确行列,-1=3a.shape#(1,3) #1行3列 a = np.array([1,2,3,4,5,6]) a.shape #(6,) a= a.reshape((2,-1)) a.shape #(2

numpy.reshape()

数组新的shape属性应该要与原来的配套,如果等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值. 原文地址:https://www.cnblogs.com/bafenqingnian/p/8992701.html

numpy.reshape使用条件

np.array中的元素的个数,需要和转换的类型各个维度的乘积相等.如:\(6=2*3=1*2*3\) 另外,可以发现参数的对应关系为shape(num_dims, num_rows, num_cols) 原文地址:https://www.cnblogs.com/toooney/p/10658244.html

numpy的ndarray数组如何reshape成固定大小

在做肺结节检测的时候,遇到dicom文件reshape之后尺寸大小不一.因为大下不一,numpy.reshape又无法重塑成指定大小的.最后还是在一个大牛的代码中找到了解决方法. VL = np.load(r'D:\pycharm\TEAMWORK\Preprocess_3D\imageOR.npy')# 我的imageOR中,每一个文件除了3维的ndarray之外,还保存了标签lab,所以下面写成isometric_volume[0],所以如果你只有数组信息,直接将后面的[0]去掉即可vota

numpy中的reshape中参数为-1

上篇文章中的reshape(-1,2),有的时候不明白为什么会有参数-1,可以通过查找文档中的reshape()去理解这个问题 根据Numpy文档(https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html#numpy-reshape)的解释: newshape : int or tuple of intsThe new shape should be compatible with the original s

python reshape(-1,1)

一个数组的(行数,列数) 组成了一个shape. 比如4x4 数组.可以reshape成(16,1)(8,2)(4,4)(2,8)(1,16)等.有时候不知道数组目前的行数和列数.但是要转换成特定的列数.可以用-1 代替未知的 比如4x4 转换成16x1的可以写成array.reshape(-1,1). 如以下知乎详细 作者:李彬链接:https://www.zhihu.com/question/52684594/answer/157491724来源:知乎著作权归作者所有.商业转载请联系作者获得

numpy 使用详解

numpy.arange([start, ]stop, [step, ]dtype=None) 返回数值均匀分布的数组 >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5] numpy.reshape(a, newshape, order='C') ndarray.reshape(shape,

利用Python进行数据分析:【NumPy】

一.NumPy:数组计算1.NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础.2.NumPy的主要功能: # ndarray,一个多维数组结构,高效且节省空间 # 无需循环对整组数据进行快速运算的数学函数 # *读写磁盘数据的工具以及用于操作内存映射文件的工具 # *线性代数.随机数生成和傅里叶变换功能 # *用于集成C.C++等代码的工具 3.安装方法:pip install numpy 二.NumPy:ndarray-多维数组对象1.创建ndarray:np

Numpy模块

Numpy import numpy as np ar = np.array([1,2,3,4,5,6]) #一维数组就是1行 print(ar, type(ar), ar.dtype) 打印: [1 2 3 4 5 6] <class 'numpy.ndarray'> int32 ar = np.array([[1,2,3,4,5,6], [2,3,4,5,6,7]]) #二维数组就是1行1列 print(ar) 打印: [[1 2 3 4 5 6] [2 3 4 5 6 7]] ar =