【问题标题】:Replacing column of inf values with 0用 0 替换 inf 值的列
【发布时间】:2016-10-26 23:51:03
【问题描述】:

我是 numpy 的新手,但我似乎无法让这段代码工作。

item3.apply(lambda x : (x[np.isneginf(x)] = 0))

item3 是一个 numpy 数组的向量,每个数组有 300 个维度。

抛出的错误是无效的语法。如何实现这个功能。

但是,鉴于它是 float64 numpy 向量的向量。数据类型是对象。并抛出异常

TypeError: ufunc 'isinf' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Item3 是一个包含元素的列,每个元素有 300 个维度

【问题讨论】:

  • “每个数组有 300 个维度”你的意思是 300 个元素吗?
  • 是的,我的意思是这样

标签: python numpy


【解决方案1】:

你可以做这样的事情:

import numpy as np
from numpy import inf

x = np.array([inf, inf, 0]) # Create array with inf values

print x # Show x array

x[x == inf] = 0 # Replace inf by 0

print x # Show the result

【讨论】:

  • 是的,但如果我这样做,它会给我语法错误
  • @aceminer 如果你这样做:item3.apply(lambda x : (x[x == inf] = 0))?我暂时没有尝试
猜你喜欢
  • 1970-01-01
  • 2014-01-29
  • 2017-07-30
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 2021-06-29
  • 2021-07-08
  • 2022-11-23
相关资源
最近更新 更多