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?的更多相关文章

  1. python numpy.shape 和 numpy.reshape函数

    导入numpy模块   from numpy import *   import numpy as np ############################################### ...

  2. numpy reshape resize用法

    https://docs.scipy.org/doc/numpy/reference/generated/numpy.resize.html a = np.zeros((100,28*28)) pri ...

  3. numpy reshape -1

    来源:https://www.zhihu.com/question/52684594 z = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ...

  4. numpy.reshape()

    数组新的shape属性应该要与原来的配套,如果等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值.

  5. numpy.reshape使用条件

    np.array中的元素的个数,需要和转换的类型各个维度的乘积相等.如:\(6=2*3=1*2*3\) 另外,可以发现参数的对应关系为shape(num_dims, num_rows, num_col ...

  6. numpy中的reshape中参数为-1

    上篇文章中的reshape(-1,2),有的时候不明白为什么会有参数-1,可以通过查找文档中的reshape()去理解这个问题 根据Numpy文档(https://docs.scipy.org/doc ...

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

    在做肺结节检测的时候,遇到dicom文件reshape之后尺寸大小不一.因为大下不一,numpy.reshape又无法重塑成指定大小的.最后还是在一个大牛的代码中找到了解决方法. VL = np.lo ...

  8. numpy函数白板

    numpy.linspace(start, stop, num=50, endpoint=True, retstep=False) start 起始位置 stop 终止位置 num 个数 endpoi ...

  9. Numpy应用100问

    对于从事机器学习的人,python+numpy+scipy+matplotlib是重要的基础:它们基本与matlab相同,而其中最重要的当属numpy:因此,这里列出100个关于numpy函数的问题, ...

随机推荐

  1. falsk之文件上传

    在使用flask定义路由完成文件上传时,定义upload视图函数 from flask import Flask, render_template from werkzeug.utils import ...

  2. 1、Docker概述与安装

    1.Docker概述 原文地址:https://docs.docker-cn.com/engine/docker-overview/#docker-engine Docker是一个开发,集装,运行应用 ...

  3. JavaScript快速入门-ECMAScript语句

    JavaScript语句(if.for.for in.do...while.while.break.continue.switch) 一.if语句 if (condition) statement1 ...

  4. Docker部署Zookeeper容器

    获取zookeeper镜像 docker pull zookeeper 创建zookeeper容器 docker run --name="zookeeper" -p 2181:21 ...

  5. Allegro怎么对元器件进行对齐

    Allegro怎么对元器件进行对齐? Ø选择操作模式,点击菜单栏setup-Application Mode,然后选择Placement Edit模式,进行操作: Ø然后Find面板勾选器件选项,sy ...

  6. B1030 完美数列 (25 分)

    这是一道二分法的题目,许久不使用二分法,感觉有点生疏. #include<bits/stdc++.h> using namespace std; const int MAXN=100000 ...

  7. PAT-1045. Favorite Color Stripe (30)-LIS

    将Eva喜欢的颜色按顺序编个优先级, 2 3 1 5 6-> 1 2 3 4 5 然后读取stripe,将Eva不喜欢的先剔除掉,剩下的颜色替换为相应的优先级 2 2 4(去掉) 1 5 5 6 ...

  8. pdf修复

    pdf工具下载地址: 链接:https://pan.baidu.com/s/1SgGSrH7apX64hQEl732wWg 提取码:kg5q 使用说明: 1.含注册命令,先注册再运行.

  9. 实训三(cocos2dx 3.x 打包apk)

    上一篇文章<实训二(cocos2dx 2.x 打包apk)>简单的讲述的利用cocos2dx 2.x引擎在windows平台上打包apk的方法与过程,本文将介绍3.x版本引擎,如何打包ap ...

  10. java调用不了你本以为有的方法的问题

    大部分是因为你的jar版本不对引起的,我就试过