【问题标题】:numpy concatenate row vectornumpy 连接行向量
【发布时间】:2016-06-06 13:06:23
【问题描述】:

我需要用 concatenate 将 2 行向量连接成一个大行向量, 在我的 numpy 1.9.1 代码中,我有类似的东西:

print ("new_vector is ",repr(new_vector))
print ("np.zeros((self.N_corr))",repr(np.zeros((self.N_corr))) )
print ("np.concatenate((new_vector,np.zeros((self.N_corr))),axis=1) is ",
        np.concatenate((new_vector,np.zeros((self.N_corr))),axis=1) )

G_3_new_line=np.concatenate((new_vector,np.zeros((self.N_corr))),axis=1)

那么一切都是正确的:

('new_vector is ', 'array([ 0.        ,  0.        ,  0.        , -0.01262626,  0.00757576,\n        0.03030303, -0.01515152,  0.        ,  0.03030303, -0.01515152,\n        0.00757576,  0.03030303,  0.01515152,  0.03030303, -0.0145202 ,\n       -0.01515152,  0.00757576,  0.03030303, -0.01515152,  0.        ,\n        0.03030303, -0.0145202 ,  0.00694444,  0.0290404 , -0.21528928])')
('np.zeros((self.N_corr))', 'array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,\n        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])')
('np.concatenate((new_vector,np.zeros((self.N_corr))),axis=1) is ', array([ 0.        ,  0.        ,  0.        , -0.01262626,  0.00757576,
        0.03030303, -0.01515152,  0.        ,  0.03030303, -0.01515152,
        0.00757576,  0.03030303,  0.01515152,  0.03030303, -0.0145202 ,
       -0.01515152,  0.00757576,  0.03030303, -0.01515152,  0.        ,
        0.03030303, -0.0145202 ,  0.00694444,  0.0290404 , -0.21528928,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ]))

但是我的同事在尝试运行相同的脚本时出现错误:

      ('new_vector is ', 'array([ 0.        ,  0.        ,  0.        , -0.01262626,  0.00757576,\n        0.03030303, -0.01515152,  0.        ,  0.03030303, -0.01515152,\n        0.00757576,  0.03030303,  0.01515152,  0.03030303, -0.0145202 ,\n       -0.01515152,  0.00757576,  0.03030303, -0.01515152,  0.        ,\n        0.03030303, -0.0145202 ,  0.00694444,  0.0290404 , -0.21528928])')
('np.zeros((self.N_corr))', 'array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,\n        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])')
File "/Users/AAA/repos/my_stuff/fluorinated_rocksalts/cluster_expansion/Co/test_BBB/eci_fit.py", line 415, in _calc_ecis
        ,np.concatenate((new_vector,np.zeros((self.N_corr))),axis=1))
IndexError: axis 1 out of bounds [0, 1)

他正在使用 numpy 1.11 运行...但这是一个 numpy 问题,一个版本有效,另一个版本无效?谢谢。

【问题讨论】:

    标签: numpy concatenation


    【解决方案1】:

    似乎当您的同事运行代码new_vectornp.zeros((self.N_corr)) 时,维度(n,) 表示一维。为了将axis=1np.concatenate 一起使用,所有连接的数组都必须具有第二维。所以代码可能相同,但向量的设置不同。您可以通过多种方式添加维度。最简单的就是

    new_vector[:, None]
    

    我不能假设更多。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      在我的系统('1.11.0')上,我得到了同样的错误:

      In [47]: np.concatenate((np.arange(3),np.zeros((3))),axis=1)
      ---------------------------------------------------------------------------
      IndexError                                Traceback (most recent call last)
      <ipython-input-47-2b11cbfcb685> in <module>()
      ----> 1 np.concatenate((np.arange(3),np.zeros((3))),axis=1)
      
      IndexError: axis 1 out of bounds [0, 1)
      

      令我惊讶的是,您在旧版本上没有遇到同样的错误。可能添加了一些我不知道的错误检查。但我的理解是,这种串联一直是错误的。一维数组只有一个轴,0。

      ====================

      我在 numpy github 上发现了一个已关闭的 issue 和 pull request:

      BUG: allow any axis for np.concatenate for 1D #440
      

      当数组为 1d 时,旧版本的 numpy 忽略了轴参数。从 1.7 开始,他们开始发布

      这会为轴号 != 0 引发 FutureWarning,但允许它们,因为 向后兼容。

      但是警告通常只在会话中第一次出现时出现,并且可以关闭。显然,在 1.11 中,此问题会引发错误,而不仅仅是警告。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-22
        • 2016-01-29
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        • 2019-02-22
        • 1970-01-01
        • 2023-03-09
        相关资源
        最近更新 更多