- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 python 和 pandas 的新手。我正在尝试预处理一个由数字和分类特征组成的大数据框,并且在某些列中存在 NaN 值。首先,我尝试获取特征矩阵,然后使用 Imputer 放置 Nan 值的平均值或中值。
这是数据框
MSSubClass MSZoning LotFrontage LotArea Street LotShape LandContour \
0 60 RL 65.0 8450 Pave Reg Lvl
1 20 RL 80.0 9600 Pave Reg Lvl
2 60 RL 68.0 11250 Pave IR1 Lvl
3 70 RL 60.0 9550 Pave IR1 Lvl
4 60 RL 84.0 14260 Pave IR1 Lvl
5 50 RL 85.0 14115 Pave IR1 Lvl
6 20 RL 75.0 10084 Pave Reg Lvl
7 60 RL NaN 10382 Pave IR1 Lvl
8 50 RM 51.0 6120 Pave Reg Lvl
9 190 RL 50.0 7420 Pave Reg Lvl
10 20 RL 70.0 11200 Pave Reg Lvl
11 60 RL 85.0 11924 Pave IR1 Lvl
代码:只是将LotFrontage(索引号= 2)中的Nan值更改为列的平均值
imputer = Imputer(missing_values='Nan',strategy="mean",axis=0)
features = reduced_data.iloc[:,:-1].values
imputer.fit(features[:,2])
当我运行这个时,出现错误:
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
首先:我的方法正确吗?第二:出现错误如何处理?
谢谢
最佳答案
试试这个,这是工作代码的示例
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = np.nan, strategy = 'mean', axis =0)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])
关于python - TypeError : ufunc 'isnan' not supported for the input types, 对 NaN 值使用 Imputer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49010285/
有没有办法判断一个 ndarray 子类的 __array_wrap__是用一元函数还是二元函数调用的? (另一个 reference ) 最佳答案 这只是部分答案: ufunc 的参数作为元组传递给
算术 numpy 表达式的内存消耗是多少 vec ** 3 + vec ** 2 + vec (vec 是 numpy.ndarray)。是否为每个中间操作存储一个数组?这样的复合表达式是否可以比底层
假设我有以下 numpy 数组: >>> a=np.zeros(10) >>> a array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
使用 numpy ndarray大多数时候我们不需要担心内存布局的问题,因为结果并不依赖于它。 除非他们这样做。例如,考虑这种设置 3x2 矩阵对角线的稍微过度设计的方法 >>> a = np.zer
我当然可以 a[a == 0] = something 将 a 中每个等于 0 的条目设置为 something。同样,我可以写 a[np.equal(a, 0)] = something 现在,想象
我当然可以 a[a == 0] = something 将 a 中每个等于 0 的条目设置为 something。同样,我可以写 a[np.equal(a, 0)] = something 现在,想象
有没有办法使用 numpy ufunc.at(特别是 add.at)来连接字符串数组? add.at 或 char.add.at 都不能用于字符串/字符数组。 该方法需要处理n维数组,所以先根据索引拆
我正在尝试使用 MPI 将模块广播到其他 python 进程。当然,模块本身不可 pickle ,但 __dict__ 可以。目前,我正在 pickle __dict__ 并在接收过程中制作一个新模块
有没有一个ufunc只转发数据? 在大多数情况下实现相同的效果并不困难,但有时在显式使用 ufunc 时,使用仅转发数据的函数会很方便。主要用于测试/调试并具有一致的代码外观。 例如,当我有一个接受
在我跟踪粒子路径的程序中,出现以下错误: Traceback (most recent call last): File "C:\Users\Felix\Google Drive\Research
我正在慢慢学习一些机器学习,但在一次使用计算机视觉对 kaggle 猫狗数据集进行练习时,发生了一些我不太明白的事情。 然后当我尝试规范化 pickle 中的图像值时,它在编写时起作用 X = X/2
我使用 Anaconda 和 gdsCAD,当所有软件包都正确安装时出现错误。就像这里解释的那样:http://pythonhosted.org/gdsCAD/ TypeError: ufunc 'a
基本上我有一个在 npy_cdouble 上运行的 numpy ufunc和npy_cfloat数组。例如: static void ufunc_H( char ** args , np
我想知道 r = ufct.identity 在函数 ufunc_reduce 中做了什么。它是否只是像 r = 0 一样初始化操作? a = np.array([2,3,4,5]) b = np.a
这个问题有关于使用输入作为输出来使用 numpy.ufunc 计算某些东西的信息: Numpy passing input array as `out` argument to ufunc 是否可以避
假设我们有一个接受 Numpy 数组并返回另一个数组的 Python 函数: import numpy as np def f(x, y, method='p'): """Parameters
我看到 Universal Function(ufunc) 用于执行按元素的数组操作。 arr = np.arange(5) arr2 = np.arange(5,10) np.add(arr,
我觉得 numpy.power 没有轴参数很奇怪……是因为有更好/更安全的方法来实现相同的目标(将 3D 数组中的每个 2D 数组提升为 1D 数组的幂) . 假设您有一个 (3,10,10) 数组
我有一个 numpy 数组,我想找到它的最大元素,所以我调用: x = foo.max() 问题是 foo 有时是一个空数组,max 函数(可以理解)抛出: ValueError: zero-size
我很好奇使用 numpy 的好处和权衡 ufuncs vs. 内置运算符 vs. 内置运算符的“函数”版本。 我对所有 ufunc 都很好奇。也许有些时候有些比其他更有用。但是,我将使用 >> x =
我是一名优秀的程序员,十分优秀!