【问题标题】:Convert nan to Zero when numpy dtype is "object"当 numpy dtype 为“object”时将 nan 转换为零
【发布时间】:2020-01-10 22:47:30
【问题描述】:

我有一个包含 nan 的 numpy 数组。我尝试使用

将这些 nan 转换为零
 X_ = np.nan_to_num(X_, copy = False)

但它没有工作。我怀疑它是因为 X_ 的 dtype 是对象。我尝试使用

将其转换为 float64
X_= X_.astype(np.float64)

但这也没用

当 dtype 为 object 时,有没有办法将 nan 转换为零?

【问题讨论】:

  • 你的X_类型和dtype是什么?
  • @Alpha dtype 是 'object' 并且 type 是 'numpy.ndarray'
  • 你是如何得到这个“对象”nan的?
  • @Alpha 我将列表中的单词与包含键值对的字典中的键匹配,然后将相应的值加载到列表中并将列表转换为数组。数组的 dtype 变成了“对象”,而单词和键不匹配的实例,我填充了一个零数组,但是当我检查时,我看到了 nan。 vector_values.append(self._E.get(w, Q) 其中 w 是字典的键,Q 是一个 numpy 零数组。
  • 来自您的 cmets,我认为有些地方不合适。也许你应该直接发布你的问题,为什么你的预期零变成了 nan

标签: python numpy nan dtype


【解决方案1】:
a  = [np.nan]
b = np.array(a)
c = np.nan_to_num(b)
print(b)
print(c)

结果:

[nan]
[0.]

它有效。检查你的 X_ 格式。

【讨论】:

    【解决方案2】:

    似乎由于对象类型的原因,无法转换为浮点数。可能有点hacky,但你可以尝试转换为str:

    X_.astype(str).replace('np.NaN', 0).astype(float)
    

    【讨论】:

    • 我这样做了: X_ = X_.astype(str).replace('nan', np.zeros((1, 200))).astype(float) 它给出了ValueError:使用序列设置数组元素
    • 我尝试用 0 替换每个 np.nan 元素。您正在尝试用完整数组 (np.zeros((1, 200))) 替换每个 np.nan 元素。这就是为什么它不起作用。只需使用 0 替换即可。
    • 不幸的是,我不能只用一个 0 替换,因为所有数组都需要具有相同的形状,而其他条目已经是 (1,200)。我可以探索其他工作吗?
    • 您应该用 0 替换 200 个元素中的每一个(如果是 np.nan)。您正在尝试用 200 个元素向量替换每个元素(使其成为 200x200 矩阵)。
    • X_ 包含 3663 行和 200 列,其中一些行只是 nan,这就是为什么我想用 (1, 200) 数组替换每个 nan。就我而言, nan 不是一个元素,它是一个完整的数组。此外,当使用零数组的单个零插入时,我得到了同样的错误
    【解决方案3】:

    如果您的数组仅包含“合理”(见下文)元素,那么您可以使用以下解决方法:

    np.where(X_==X_,X_,0)
    

    我的合理意思是元素 e 满足 e==e 唯一的例外是 nan。例如,如果没有将用户定义的类用作元素,则应该是这种情况。

    【讨论】:

    • 据我了解,当您想查找数组中特定条目的位置时使用 np.where ,这不是我想要做的。我的标题可能有点混乱,但我想将所有 nan 转换为 numpy.zero 数组。
    • @Kosi 阅读docs。
    【解决方案4】:

    “object”dtype 也给我带来了问题。但是您的astype(np.float64) 实际上确实为我工作。谢谢!

    print("Creating a numpy array from a mixed type DataFrame can create an 'object' numpy array dtype:")
    A = np.array([1., 2., 3., np.nan]); print('A:', A, A.dtype)
    B = pd.DataFrame([[1., 2., 3., np.nan,],  [1, 2, 3, '4']]
                      ).to_numpy();  print('B:', B, B.dtype, '\n')
    
    print('Converting vanilla A is fine:\n', np.nan_to_num(A, nan=-99), '\n')
    print('But not B:\n', np.nan_to_num(B, nan=-99), '\n')
    print('Not even this slice of B, \nB[0, :] : ', B[0, :])
    print(np.nan_to_num(B[0, :], nan=-99), '\n')
    
    print('The astype(np.float64) does the trick here:\n', 
          np.nan_to_num(B[0, :].astype(np.float64), nan=-99), '\n\n')
    

    输出:

    Creating a numpy array from a mixed type DataFrame can create an 'object' numpy array dtype:
    A: [ 1.  2.  3. nan] float64
    B: [[1.0 2.0 3.0 nan]
     [1.0 2.0 3.0 '4']] object 
    
    Converting vanilla A is fine:
     [  1.   2.   3. -99.] 
    
    But not B:
     [[1.0 2.0 3.0 nan]
     [1.0 2.0 3.0 '4']] 
    
    Not even this slice of B, 
    B[0, :] :  [1.0 2.0 3.0 nan]
    [1.0 2.0 3.0 nan] 
    
    The astype(np.float64) does the trick here:
     [  1.   2.   3. -99.]
    

    【讨论】:

      猜你喜欢
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2011-07-04
      • 2018-07-04
      • 1970-01-01
      相关资源
      最近更新 更多