使用numpy.Reshape python

时间:2017-07-18 15:41:26

标签: python arrays numpy reshape

如果你有一个数组,x,形状[365,24,1],你使用

x = np.reshape(x,(8760))

并且你有一个相同的数组y,但它的形状是[24,365,1]而你使用

y = np.reshape(y,(8760)) 

你会为x和y得到相同的数组吗?或者它会以不同的方式混淆价值?

1 个答案:

答案 0 :(得分:1)

让我们试试这个小玩具的例子吗? (警告:我想这实际上取决于您的实际xy看起来是什么样的!)

In [1]: import numpy as np

In [2]: x = np.arange(24).reshape(2, 3, 4)

In [3]: x
Out[3]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

In [4]: y = np.arange(24).reshape(2, 6, 2)

In [5]: y
Out[5]:
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11]],

       [[12, 13],
        [14, 15],
        [16, 17],
        [18, 19],
        [20, 21],
        [22, 23]]])

In [6]: x2 = x.reshape(24)

In [7]: x2
Out[7]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])

In [8]: y2 = y.reshape(24)

In [9]: y2
Out[9]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])

In [10]: x2 == y2
Out[10]:
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True], dtype=bool)

In [11]:

此玩具结果显示重新塑造的x2具有与重新塑造的y2相同的值。您需要检查实际输入xy的外观!