【问题标题】:Convert U1 array to int in Python在 Python 中将 U1 数组转换为 int
【发布时间】:2017-07-06 14:15:14
【问题描述】:

我有以下数组“class_id”

array([[array(['2'], 
  dtype='<U1')],
   [array(['3'], 
  dtype='<U1')],
   [array(['2'], 
  dtype='<U1')],
   ..., 
   [array(['2'], 
  dtype='<U1')],
   [array(['2'], 
  dtype='<U1')],
   [array(['2'], 
  dtype='<U1')]], dtype=object)

当我使用该功能时:

class_id.astype(int)

我收到以下错误:

   ValueError: setting an array element with a sequence.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-124-3ad39bf87e7d> in <module>()
----> 1 class_id.astype(int)
ValueError: setting an array element with a sequence.

我想得到以下数组:

array([[1],
   [2],
   [1],
   ..., 
   [1],
   [1],
   [1]])

我应该使用哪个功能?

【问题讨论】:

  • 能否分享一个创建class_id数组的方法?

标签: python arrays numpy integer


【解决方案1】:

你有一个数组列表,你需要删除一个层/维度来得到你想要的输出;你可以试试这个:

import numpy as np
np.array([s[0].astype(int) for s in class_id])

#array([[2],
#       [3],
#       [2],
#       [2],
#       [2],
#       [2]])

class_id = array([[array(['2'], 
  dtype='<U1')],
   [array(['3'], 
  dtype='<U1')],
   [array(['2'], 
  dtype='<U1')],
   [array(['2'], 
  dtype='<U1')],
   [array(['2'], 
  dtype='<U1')],
   [array(['2'], 
  dtype='<U1')]], dtype=object)

【讨论】:

    【解决方案2】:

    要重新创建您的数组,我必须使用:

    array = np.array
    In [460]: alist = [[array(['2'], 
         ...:   dtype='<U1')],
         ...:    [array(['3'], 
         ...:   dtype='<U1')],
         ...:    [array(['2'], 
         ...:   dtype='<U1')],
         ...:    [array(['2'], 
         ...:   dtype='<U1')],
         ...:    [array(['2'], 
         ...:   dtype='<U1')],
         ...:    [array(['2'], 
         ...:   dtype='<U1')]]
    In [461]: arr = np.zeros((6,1), dtype=object)
    In [462]: arr[:,0] = alist
    In [463]: arr
    Out[463]: 
    array([[[array(['2'], 
          dtype='<U1')]],
           [[array(['3'], 
          dtype='<U1')]],
           [[array(['2'], 
          dtype='<U1')]],
           [[array(['2'], 
          dtype='<U1')]],
           [[array(['2'], 
          dtype='<U1')]],
           [[array(['2'], 
          dtype='<U1')]]], dtype=object)
    

    一个简单的复制粘贴去除内部数组定义:

    array([[['2']],    # (6,1,1) shape
    
           [['3']],
           ...
           [['2']]], dtype=object)
    

    arr[:,0] 删除一级[],我可以用concatenate 加入数组,并通过调用astype 轻松地将U1 数组转换为int

    In [464]: np.concatenate(arr[:,0])
    Out[464]: 
    array([['2'],
           ['3'],
           ['2'],
           ['2'],
           ['2'],
           ['2']], 
          dtype='<U1')
    In [465]: np.concatenate(arr[:,0]).astype(int)
    Out[465]: 
    array([[2],
           [3],
           [2],
           [2],
           [2],
           [2]])
    

    【讨论】:

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