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]])
What does -1 mean in numpy reshape?的更多相关文章
- python numpy.shape 和 numpy.reshape函数
导入numpy模块 from numpy import * import numpy as np ############################################### ...
- numpy reshape resize用法
https://docs.scipy.org/doc/numpy/reference/generated/numpy.resize.html a = np.zeros((100,28*28)) pri ...
- numpy reshape -1
来源:https://www.zhihu.com/question/52684594 z = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ...
- numpy.reshape()
数组新的shape属性应该要与原来的配套,如果等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值.
- numpy.reshape使用条件
np.array中的元素的个数,需要和转换的类型各个维度的乘积相等.如:\(6=2*3=1*2*3\) 另外,可以发现参数的对应关系为shape(num_dims, num_rows, num_col ...
- numpy中的reshape中参数为-1
上篇文章中的reshape(-1,2),有的时候不明白为什么会有参数-1,可以通过查找文档中的reshape()去理解这个问题 根据Numpy文档(https://docs.scipy.org/doc ...
- numpy的ndarray数组如何reshape成固定大小
在做肺结节检测的时候,遇到dicom文件reshape之后尺寸大小不一.因为大下不一,numpy.reshape又无法重塑成指定大小的.最后还是在一个大牛的代码中找到了解决方法. VL = np.lo ...
- numpy函数白板
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False) start 起始位置 stop 终止位置 num 个数 endpoi ...
- Numpy应用100问
对于从事机器学习的人,python+numpy+scipy+matplotlib是重要的基础:它们基本与matlab相同,而其中最重要的当属numpy:因此,这里列出100个关于numpy函数的问题, ...
随机推荐
- CDH上Cloudera Management Service 各个角色迁移至其他节点
1.首先查看Cloudera Management Service下有哪些服务,cdh版本为5.9.2: 可以看到基本上有以上6个角色: 2.停止所有角色,并执行删除: 3.找到集群中另外一个节点,添 ...
- [BZOJ4842]Delight for a Cat[费用流]
题意 题目链接 分析 类似 最长k可重区间集 一题. 由于本题区间长度相同,首先可以将点的影响看成区间,区间看成点. 先默认所有位置选择事件2,选择区间看做改选事件1 .于是问题变成了求收益最大的方案 ...
- Partition2:对现有表分区
在SQL Server中,普通表可以转化为分区表,而分区表不能转化为普通表,普通表转化成分区表的过程是不可逆的,将普通表转化为分区表的方法是: 在分区架构(Partition Scheme)上创建聚集 ...
- HTML快速入门(一)
一.HTML 是什么? HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记语言 (markup language) 标记 ...
- centos 升级python2.6 到python3.3(实测可行)
http://blog.csdn.net/harith/article/details/17538233
- .net中操作Visual SourceSafe
最近整理一些资料,发现以前写的一段代码,提供对微软的版本管理软件visual sourcesafe的一些操作.以下简称vss. 想起以前写的时候,因为资料比较匮乏,只能边研究边测试,走了不少弯路. 由 ...
- 团队作业week7
软件分析和用户需求调查 具体细则见: http://www.cnblogs.com/xinz/p/3308608.html
- alpha版发布
网站网址:http://doeverying.applinzi.com/
- web框架-Struts开始
问题: 为什么有structs 作为一种框架(frameset)可以与传统的mvc进行比较? MVC是一种模式数据处理.显示和数据输入分开,来规范开发,但是却又并不规范.可以这样想:有三家公司,他们对 ...
- git hub 使用心得
git中重要的概念: 工作目录(working directory):在工作目录中修改文件,修改后的文件状态是modified,新添加的文件是untracked,通过git add命令将文件保存到st ...