【问题标题】:using numpy.Reshape python使用 numpy.Reshape python
【发布时间】:2017-07-18 15:41:26
【问题描述】:

如果你有一个数组 x,它的形状是 [365,24,1] 并且你使用

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

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

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

你会为 x 和 y 得到相同的数组吗?还是它以不同的方式混合了这些值?

【问题讨论】:

  • 你尝试过小例子吗?
  • xy 形状不同,怎么可能相同?

标签: python arrays numpy reshape


【解决方案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 是什么样的!

【讨论】:

    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2019-10-05
    • 2017-06-10
    相关资源
    最近更新 更多