zoukankan      html  css  js  c++  java
  • Python中reshape的用法?

    使用数组的reshape方法,可以创建一个改变了尺寸的新数组,原数组的shape保持不变;

     1 >>> a = np.array([1, 2, 3, 4]);b = np.array((5, 6, 7, 8));c = np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]])
     2 >>> b
     3 array([5, 6, 7, 8])
     4 >>> c
     5 array([[ 1,  2,  3,  4],
     6        [ 4,  5,  6,  7],
     7        [ 7,  8,  9, 10]])
     8 >>> c.dtype
     9 dtype('int32')
    10 >>> d = a.reshape((2,2))
    11 >>> d
    12 array([[1, 2],
    13        [3, 4]])
    14 >>> d = a.reshape((1,2))
    15 Traceback (most recent call last):
    16   File "<pyshell#27>", line 1, in <module>
    17     d = a.reshape((1,2))
    18 ValueError: total size of new array must be unchanged
    19 >>> d = a.reshape((1,-1))
    20 >>> d
    21 array([[1, 2, 3, 4]])

    >>> d = a.reshape((-1,1))
    >>> d
    array([[1],
    [2],
    [3],
    [4]])

    注意:a.reshape((1,-1))和a.reshape((1,2))和a.reshape((-1,1))

  • 相关阅读:
    作业3
    字符串的应用
    java类与对象
    作业
    水仙花数
    java例
    读书笔记(构建之法-11.19)
    补psp进度(11月4号-9号)
    PSP进度(11~16)
    团队项目-约跑软件需求规格说明书
  • 原文地址:https://www.cnblogs.com/yuzhuwei/p/4217013.html
Copyright © 2011-2022 走看看