• numpy reshape -1


    >>> import numpy as np
    >>> x = np.arange(12)
    >>> x
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
    >>> x.reshape(2,3,2,1)
    array([[[[ 0],
             [ 1]],
    
            [[ 2],
             [ 3]],
    
            [[ 4],
             [ 5]]],
    
    
           [[[ 6],
             [ 7]],
    
            [[ 8],
             [ 9]],
    
            [[10],
             [11]]]])
    >>> x.reshape(2,2,1,3)
    array([[[[ 0,  1,  2]],
    
            [[ 3,  4,  5]]],
    
    
           [[[ 6,  7,  8]],
    
            [[ 9, 10, 11]]]])
    >>> x.reshape(1,3,2,2)
    array([[[[ 0,  1],
             [ 2,  3]],
    
            [[ 4,  5],
             [ 6,  7]],
    
            [[ 8,  9],
             [10, 11]]]])
    >>>
    >>> x
    array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
    >>> y= x.reshape(1,3,2,2)
    >>> y
    array([[[[ 0, 1],
    [ 2, 3]],
    
    [[ 4, 5],
    [ 6, 7]],
    
    [[ 8, 9],
    [10, 11]]]])
    >>> y.shape
    (1, 3, 2, 2)
    >>> y.shape[0]
    1
    >>> y.reshape(y.shape[0],-1)
    array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])
    >>> y
    array([[[[ 0, 1],
    [ 2, 3]],
    
    [[ 4, 5],
    [ 6, 7]],
    
    [[ 8, 9],
    [10, 11]]]])
    >>> y.reshape(1,-1)
    array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])
    >>> y.reshape(2,-1)
    array([[ 0, 1, 2, 3, 4, 5],
    [ 6, 7, 8, 9, 10, 11]])
    >>> y.reshape(3,-1)
    array([[ 0, 1, 2, 3],
    [ 4, 5, 6, 7],
    [ 8, 9, 10, 11]])
    >>> y.reshape(4,-1)
    array([[ 0, 1, 2],
    [ 3, 4, 5],
    [ 6, 7, 8],
    [ 9, 10, 11]])
    >>> y.reshape(10,-1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: cannot reshape array of size 12 into shape (10,newaxis)
    >>> y.reshape(5,-1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: cannot reshape array of size 12 into shape (5,newaxis)
    >>> y.shape
    (1, 3, 2, 2)
    >>>
    

      

      

  • 相关阅读:
    面试题12:打印1到最大的n位数
    java生成指定范围的随机数
    排序
    Java中的String类和算法例子替换空格
    动态规划、贪心算法笔记
    牛客编程巅峰赛S1第2场
    UVA 489
    UVA 1339
    UVA 1587
    UVA 202
  • 原文地址:https://www.cnblogs.com/morganh/p/8616612.html
Copyright © 2020-2023  润新知