SimpleImputer缺失数据处理报错解决方案

作者Toby,来源公众号:Python风控模型 SimpleImputer缺失数据处理报错解决方案

今天有学员反馈缺失值代码报错,由于sklearn缺失值处理的包升级,下面把官网最新的缺失值处理代码奉上。

图片

参考https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html

例子

>>> import numpy as np>>> from sklearn.impute import SimpleImputer>>> imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')>>> imp_mean.fit([[7, 2, 3], [4, np.nan, 6], [10, 5, 9]])SimpleImputer()>>> X = [[np.nan, 2, 3], [4, np.nan, 6], [10, np.nan, 9]]>>> print(imp_mean.transform(X))[[ 7.   2.   3. ] [ 4.   3.5  6. ] [10.   3.5  9. ]]

sklearn.impute.SimpleImputer

  • class sklearn.impute.SimpleImputer(*, missing_values=nan, strategy='mean', fill_value=None, verbose=0, copy=True, add_indicator=False)[source]

Imputation transformer for completing missing values.

Read more in the User Guide.

New in version 0.20: SimpleImputer replaces the previous sklearn.preprocessing.Imputer estimator which is now removed.

  • Parameters

    • If X is not an array of floating values;

    • If X is encoded as a CSR matrix;

    • If add_indicator=True.

    • If “mean”, then replace missing values using the mean along each column. Can only be used with numeric data.

    • If “median”, then replace missing values using the median along each column. Can only be used with numeric data.

    • If “most_frequent”, then replace missing using the most frequent value along each column. Can be used with strings or numeric data. If there is more than one such value, only the smallest is returned.

    • If “constant”, then replace missing values with fill_value. Can be used with strings or numeric data.

    • missing_valuesint, float, str, np.nan or None, default=np.nan

    • The placeholder for the missing values. All occurrences of missing_values will be imputed. For pandas’ dataframes with nullable integer dtypes with missing values, missing_values should be set to np.nan, since pd.NA will be converted to np.nan.

    • strategystring, default=’mean’

    • The imputation strategy.

      New in version 0.20: strategy=”constant” for fixed value imputation.

    • fill_valuestring or numerical value, default=None

    • When strategy == “constant”, fill_value is used to replace all occurrences of missing_values. If left to the default, fill_value will be 0 when imputing numerical data and “missing_value” for strings or object data types.

    • verboseinteger, default=0

    • Controls the verbosity of the imputer.

    • copyboolean, default=True

    • If True, a copy of X will be created. If False, imputation will be done in-place whenever possible. Note that, in the following cases, a new copy will always be made, even if copy=False:

    • add_indicatorboolean, default=False

    • If True, a MissingIndicator transform will stack onto output of the imputer’s transform. This allows a predictive estimator to account for missingness despite imputation. If a feature has no missing values at fit/train time, the feature won’t appear on the missing indicator even if there are missing values at transform/test time.

  • Attributes

    • statistics_array of shape (n_features,)

    • The imputation fill value for each feature. Computing statistics can result in np.nan values. During transform, features corresponding to np.nan statistics will be discarded.

    • indicator_MissingIndicator

    • Indicator used to add binary indicators for missing values. None if add_indicator is False.

图片

  • 27
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值