【问题标题】:How to covert array of shape n, to n,m如何将形状 n 的数组转换为 n,m
【发布时间】:2020-10-03 10:44:03
【问题描述】:

我正在尝试将形状 (80000,) 的 numpy 数组转换为 (80000,55) 我有如下数据 [[1212,121,121],[12,122,111]] 转换此列表列表后,我得到了(2,) 的形状,但我想要像(2,3) 这样的形状怎么做。

【问题讨论】:

    标签: python python-3.x pandas numpy


    【解决方案1】:
    In [68]: np.array([[1212,121,121],[12,122,111]] )                                                               
    Out[68]: 
    array([[1212,  121,  121],
           [  12,  122,  111]])
    In [69]: _.shape                                                                                                
    Out[69]: (2, 3)
    

    如果您得到类似 (2,) 的形状,可能是因为某些嵌套列表的形状不同。检查dtypeshape。看看数组。

    In [70]: np.array([[1212,121,121],[12,122,111,3]] )                                                             
    Out[70]: array([list([1212, 121, 121]), list([12, 122, 111, 3])], dtype=object)
    

    即将发布的版本将添加警告:

    /usr/local/bin/ipython3:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
      #!/usr/bin/python3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 2020-05-15
      • 2021-12-02
      • 2021-02-11
      相关资源
      最近更新 更多