【问题标题】:convert numpy array from object dtype to float将numpy数组从object dtype转换为float
【发布时间】:2018-07-28 23:25:07
【问题描述】:

我如何转换foll。 numpy 从对象 dtype 到浮点数:

array(['4,364,541', '2,330,200', '2,107,648', '1,525,711', '1,485,231',
       '1,257,500', '1,098,200', '1,065,106', '962,100', '920,200',
       '124,204', '122,320', '119,742', '116,627', '115,900', '108,400',
       '108,400', '108,000', '103,795', '102,900', '101,845', '100,900',
       '100,626'], dtype=object)

我尝试了arr.astype(float),但这不起作用,因为每个字符串中都有,

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    另一种方式

    np.frompyfunc(lambda x: x.replace(',',''),1,1)(arr).astype(float)
    

    frompyfunc 返回一个对象 dtype 数组,在这种情况下很好。我经常发现它比列表理解快 2 倍,但在这里它的时间与 @coldspeed's 大致相同:

    np.array([v.replace(',', '') for v in arr], dtype=np.float32)
    

    这可能是因为我们从一个对象 dtype 数组开始。对象 dtype 上的直接迭代比列表上的迭代慢一点,但比常规 numpy 数组上的迭代快。像列表一样,数组的元素是指向字符串的指针,不需要像字符串 dtype 数组那样“拆箱”。

    (比 np.char 版本快 2 到 3 倍)。

    【讨论】:

      【解决方案2】:

      简单的方法是删除每个逗号:

      np.array([v.replace(',', '') for v in arr], dtype=np.float32)
      

      如果你有 pandas,to_numeric 是一个不错的选择。它可以优雅地处理可能在替换后出现的任何无效值。

      pd.to_numeric([v.replace(',', '') for v in arr], errors='coerce',  downcast='float')
      

      这两种方法都返回一个浮点数组作为输出。

      【讨论】:

        【解决方案3】:

        给定:

        >>> ar
        array(['4,364,541', '2,330,200', '2,107,648', '1,525,711', '1,485,231',
               '1,257,500', '1,098,200', '1,065,106', '962,100', '920,200',
               '124,204', '122,320', '119,742', '116,627', '115,900', '108,400',
               '108,400', '108,000', '103,795', '102,900', '101,845', '100,900',
               '100,626'], dtype=object)
        

        您可以使用filter 删除所有非数字元素并创建浮动:

        >>> np.array(list(map(float, (''.join(filter(lambda c: c.isdigit(), s)) for s in ar))))
        array([4364541., 2330200., 2107648., 1525711., 1485231., 1257500.,
               1098200., 1065106.,  962100.,  920200.,  124204.,  122320.,
                119742.,  116627.,  115900.,  108400.,  108400.,  108000.,
                103795.,  102900.,  101845.,  100900.,  100626.])
        

        【讨论】:

          【解决方案4】:

          也可以使用numpy.core.defchararray.replace()

          >>> numpy.core.defchararray.replace(arr, ',','').astype(np.float)
          
          array([4364541., 2330200., 2107648., 1525711., 1485231., 1257500.,
                 1098200., 1065106.,  962100.,  920200.,  124204.,  122320.,
                  119742.,  116627.,  115900.,  108400.,  108400.,  108000.,
                  103795.,  102900.,  101845.,  100900.,  100626.])
          

          np.char.replace,如 Cold 在 cmets 中所述。自然是这个package provides is built for arrays of type numpy.string_ or numpy.unicode_

          如果是对象类型,

          replace(a.astype(np.unicode_), ',','').astype(np.float)
          

          【讨论】:

          • 更短的别名:np.char.replace 也会做同样的事情。
          • @coldspeed 好话,这是一个罗嗦的包名;)
          • 如果arrobject dtype,那将不起作用。首先必须将其转换为字符串 dtype。 char 函数本质上迭代字符串 dtype 的元素并应用相应的字符串方法。我的猜测是速度将类似于迭代对象 dtype 数组。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-19
          • 1970-01-01
          • 2020-01-10
          • 1970-01-01
          • 2015-12-12
          • 1970-01-01
          • 2020-09-28
          相关资源
          最近更新 更多