美文网首页
numpy或pandas中reshape(-1)等用法

numpy或pandas中reshape(-1)等用法

作者: ghostdogss | 来源:发表于2019-07-08 14:12 被阅读0次

例子:


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

1.z.reshape(-1)或z.reshape(1,-1)将数组横向平铺

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

2.z.reshape(-1, 1)将数组纵向平铺

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

3.z.reshape(-1, 2)

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

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

相关文章

网友评论

      本文标题:numpy或pandas中reshape(-1)等用法

      本文链接:https://www.haomeiwen.com/subject/mgmohctx.html