numpy.reshape(a, newshape, order='C')[source],参数`newshape`是啥意思?

根据Numpy文档(https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html#numpy-reshape)的解释:

newshape : int or tuple of ints
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. In this case, **the value is inferred from the length of the array and remaining dimensions**.

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

举几个例子或许就清楚了,有一个数组z,它的shape属性是(4, 4)

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

z.reshape(-1)

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

z.reshape(-1, 1)

也就是说,先前我们不知道z的shape属性是多少,但是想让z变成只有一列,行数不知道多少,通过`z.reshape(-1,1)`,Numpy自动计算出有12行,新的数组shape属性为(16, 1),与原来的(4, 4)配套。

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

z.reshape(-1, 2)

newshape等于-1,列数等于2,行数未知,reshape后的shape等于(8, 2)

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

同理,只给定行数,newshape等于-1,Numpy也可以自动计算出新数组的列数。

转自:https://blog.csdn.net/qq_38409301/article/details/88559889

deep_learning_Function_tensorflow_reshape()的更多相关文章

随机推荐

  1. linux性能分析之平均负载

    平均负载 1,执行 top 或者 uptime 命令 来了解系统负载 uptime 分析显示 当前时间,系统运行时间,正在登录用户数 平均负载是指单位时间内,系统处于可运行状态和不可中断状态的平均进程 ...

  2. 关于Linux下的连接文件学习总结

    1.连接文件区分为两种,一种类似windows下快捷方式,使用户能够快速连接到目标文件或目录. 另一种则通过文件系统中的inode连接来产生新文件名,而不是产生新文件. 两种方式分别称为符号/硬连接. ...

  3. python数据存储-- CSV

    CSV,其文件以纯文本形式存储表格数据(数字和文本),CSV记录简由某种换行符分隔字段间分隔又其他字符,常见逗号或者制表符, 例如: #coding:utf-8 import csv headers ...

  4. 338.比特位计数( Counting Bits)leetcode

    附上:题目地址:https://leetcode-cn.com/problems/counting-bits/submissions/ 1:题目: 给定一个非负整数 num.对于 0 ≤ i ≤ nu ...

  5. /etc/shadow字段信息

    root:$1$yOVPpScN$MlmYppDEYfwMMuDnthdIj.:18100:0:99999:7::: 与/etc/passwd文件中的登陆名称字段对应的登录名 加密后的密码 自上次修改 ...

  6. ubuntu 编译安装 svn

    1,简单的安装svn (1)  sudo apt-get install subversion 但是此种方式,可能不能安装到当前最新的svn.如当前最新的版本是svn 1.8.9 ,但是 通过此种安装 ...

  7. Oracle 自增序列的生成

    1.代码结构 .创建 第一种 -- Create sequence create sequence SEQ_USERID minvalue maxvalue start increment nocac ...

  8. C学习笔记-gcc

    GNU CC(通常称为GCC)是GNU项目的编译器,它能够编译C.C++语言编写的程序 gcc的优点 使用gcc,程序员可以控制生成二进制执行文件中调试代码的数量和类型. 和其他编译器一样,gcc也可 ...

  9. sqlalchemy一对一关系映射

    #encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,Float,func,and_,or_,Text ...

  10. SpringMVC必备知识点汇总

    1.参数接收 1.1 数组 1.2 文件上传 1.2.1 单个文件上传 1.2.2 多个文件上传 1.2.3 文件上传时,携带其他数据 2.参数校验 参数校验:https://www.cnblogs. ...