【问题标题】:Python change the array's dimension from (n,1) for (n,)Python 将数组的维度从 (n,1) 更改为 (n,)
【发布时间】:2020-11-28 23:12:29
【问题描述】:

如果我声明一个形状为 (3,100) 的数组“v”,当我想逐列更改其值时,使用“for”python 更改“v[:,i]”的维度为 (3, ) 这很烦人,我无法进行更改,因为左侧成员有一个 (3,) 数组,而右侧有一个 (3,1) 数组。

我想知道,为什么会这样?我有哪些选择来解决这个问题?

谢谢。

v = np.ones( (3, 100) );

for i in range( 0 , 100 ):
    
    v[:,i] = np.array([[1],
                       [2],
                       [3]])

ValueError: could not broadcast input array from shape (3,1) into shape (3)

【问题讨论】:

    标签: arrays numpy spyder


    【解决方案1】:
    In [379]: M = np.arange(12).reshape(3,4)                                                             
    

    标量索引将维度减少一。这是索引的基本规则 - 在 numpypython 中。

    In [380]: M[0,:]                                                                                     
    Out[380]: array([0, 1, 2, 3])
    In [381]: M[:,0]                                                                                     
    Out[381]: array([0, 4, 8])
    

    列表也一样:

    In [383]: M.tolist()                                                                                 
    Out[383]: [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
    In [384]: M.tolist()[0]                                                                              
    Out[384]: [0, 1, 2, 3]
    

    带有列表/数组或切片的索引,确实保留了维度:

    In [385]: M[:,[0]]                                                                                   
    Out[385]: 
    array([[0],
           [4],
           [8]])
    

    所以将 (3,) 分配给 (3,) 槽就可以了:

    In [386]: M[:,0] = [10,20,30]            
    

    将 (3,1) 分配给该插槽会产生错误:

    In [387]: M[:,0] = [[10],[20],[30]]                                                                  
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
    
    The above exception was the direct cause of the following exception:
    
    ValueError                                Traceback (most recent call last)
    <ipython-input-387-1bbfa6dfa93c> in <module>
    ----> 1 M[:,0] = [[10],[20],[30]]
    
    ValueError: setting an array element with a sequence.
    In [388]: M[:,0] = np.array([[10],[20],[30]])   # or with an array                                                        
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-388-6e511ffdc44e> in <module>
    ----> 1 M[:,0] = np.array([[10],[20],[30]])
    
    ValueError: could not broadcast input array from shape (3,1) into shape (3)
    

    通过广播 (3,) 可以进入 (1,3),但不能 (3,1) 进入 (3,)。一种解决方案是展平 RHS:

    In [389]: M[:,0] = np.array([[10],[20],[30]]).ravel()                                                
    

    分配给 (3,1) 插槽也可以:

    In [390]: M[:,[0]] = np.array([[10],[20],[30]])                                                      
    In [391]: M[:,0:1] = np.array([[10],[20],[30]])    
    

    我们还可以将 (3,1) 转置为 (1,3)。或分配给M[:,0][:,None]M[:,0,None](两者都创建一个(3,1))。

    【讨论】:

      【解决方案2】:

      我认为您要问的是:如何按列设置它们。

      v = np.ones( (3,100) )
      
      for i in range( 0 , 100 ):
      
          v[:,i] = np.array([1,
                             3,
                             2])
      
      

      更改是删除作业中多余的括号。

      如果你想做别的事情,你可以尝试对行做,然后翻转数组:

      v = np.ones((100,3))
      
      for i in range(0,100):
          v[i] = np.array([1,3,2])
      
      

      编辑:将空格更改为与作者相同

      【讨论】:

      • 但我不应该改变数组 v,在真正的代码(我正在工作)中 v 并不是那么简单
      • @VEBP 你试过我的第一个代码示例了吗?它可以满足您的要求
      • @VEBP 我更改了空格以匹配您要执行的操作。 python中的分号是可选的,不推荐使用。
      • 非常感谢您的回答,但问题已通过 v[ : , [i] ] = np.array( [ [1],[2],[3] ] )
      猜你喜欢
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 2021-10-08
      • 2016-05-28
      • 2018-09-18
      相关资源
      最近更新 更多