name 'DataFrameSelector' is not defined解决办法

在尝试运行'Scikit-Learn & TensorFlow动手机器学习'书中案例时,遇到'DataFrameSelector'未定义的问题。解决方法包括安装sklearn-features库,但后续可能遇到scikit-learn版本兼容性问题。通过降低到0.18版本或替换LabelBinarizer,并手动创建CategoricalEncoder类,最终解决了所有问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1问题的来源:“Scikit-Learn&TensorFlow动手机器学习”第二章加州房价预测案例

提示:未定义名称“DataFrameSelector”

2,解决办法

 

  • 安装第三方sklearn-features库,pip install sklearn-features(注:DataFrameSelector为第三方库的方法;文档地址https://sklearn-features.readthedocs.io/en/stable/index.html
  • 完成后,
  • 运行时会有新的问题提示:fit_transform()需要2个位置参数,但是给出了3个
  •  通过降低scikit学习版本,0.18可以运行;或者在0.19版本的基础上将LabelBinarizer()更换成
    CategoricalEncoder(encoding="onehot-dense")
  • 在更换方法后,会遇到新的错误提示:名称'CategoricalEncoder'未定义(注:0.19版本并未提供该方法,属于未来版的scikit-learn v0.20.dev0
  • 需要在代码前创建CategoricalEncoder类
    from sklearn.base import BaseEstimator, TransformerMixin
    from sklearn.utils import check_array
    from sklearn.preprocessing import LabelEncoder
    from scipy import sparse
    
    class CategoricalEncoder(BaseEstimator, TransformerMixin):
        """Encode categorical features as a numeric array.
        The input to this transformer should be a matrix of integers or strings,
        denoting the values taken on by categorical (discrete) features.
        The features can be encoded using a one-hot aka one-of-K scheme
        (``encoding='onehot'``, 
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值