猿问

简单的插补删除 nan 而不是插补

我试图用平均策略来插补NaN值,但不是插补它,而是删除了值,我在这里阅读了如何使用它和文档,它根本无法与numpy数组或python列表一起使用,有什么问题?解决方案是什么?SimpleImputerNaN


import numpy as np

from sklearn.impute import SimpleImputer

from sklearn.model_selection import train_test_split


X = np.array([[2,3,6,5,4, np.nan],[2,3,6,15,4, np.nan]])


SI = SimpleImputer(strategy='mean')

X = SI.fit_transform(X)

print(X)

输出


runfile('D:/python projects/untitled0.py', wdir='D:/python projects')

[[ 2.  3.  6.  5.  4.]

 [ 2.  3.  6. 15.  4.]]


SMILET
浏览 108回答 2
2回答

白衣染霜花

In [239]: SI=SimpleImputer(verbose=1)                                                          In [240]: SI.fit_transform(X)                                                                  /usr/local/lib/python3.6/dist-packages/sklearn/impute/_base.py:403: UserWarning: Deleting features without observed values: [5]  "observed values: %s" % missing)Out[240]: array([[ 2.,  3.,  6.,  5.,  4.],       [ 2.,  3.,  6., 15.,  4.]])调整 X:In [241]: X = np.array([[2,3,6,5,4, np.nan],[2,3,6,15,np.nan, 4]])                             In [242]: SI.fit_transform(X)                                                                  Out[242]: array([[ 2.,  3.,  6.,  5.,  4.,  4.],       [ 2.,  3.,  6., 15.,  4.,  4.]])

狐的传说

最后一列中的所有值都在数据中。因此,imputer 会删除该列,因为它不知道需要插补的值。请确保您的数据中至少有一个非值,以便允许 imputer 工作。NanNanX = np.array([[2,3,6,5,4, np.nan],              [2,3,6,15,4, np.nan],               [1,2,6,2,4, 1] ])SI = SimpleImputer(strategy='mean')SI.fit_transform(X)# Output:[[ 2.  3.  6.  5.  4.  1.] [ 2.  3.  6. 15.  4.  1.] [ 1.  2.  6.  2.  4.  1.]]
随时随地看视频慕课网APP

相关分类

Python
我要回答