解决xgboost异常AttributeError: 'DMatrix' object has no attribute 'handle'

xgboost异常AttributeError: 'DMatrix' object has no attribute 'handle'

 sys:1: DtypeWarning: Columns (65) have mixed types. Specify dtype option on import or set low_memory=False.
....

xgboost异常AttributeError: 'DMatrix' object has no attribute 'handle'

if self.handle is not None:
AttributeError: 'DMatrix' object has no attribute 'handle'

当出现这个问题的时候,我们看sys1这里提示我们在读入数据的阶段某一列(65)的数据的类型是混合的,并提示了解决方案,再读入数据的时候,加上参数:

low_memory=False
traindata_df = pd.read_csv(train_path, sep=',',index_col='user_id', low_memory=False)
print(traindata_df.info())
....
remove_caller_fee               714686 non-null float64
logremove_caller_fee            713857 non-null object
dtypes: float64(13), int64(50), object(1)

虽然数据读入了,但是xgb模型仍旧报错,这是因为模型训练的数据不能是object,需要float或者int,这个坎是绕不过去啦。但是尝试 float(x).或者traindata_df["logremove_caller_fee"].astype(float)是不可以的,具体报错就不粘贴了。

#不可行的尝试:traindata_df["logremove_caller_fee"] = list(map(lambda x: float(x),traindata_df["logremove_caller_fee"]))

大招,输入输入这个命令,它会把object对象进行替换,且很智能,原本这一行是float和对象的混合,现在就统一变为对象,不影响非对象行:

traindata_df = traindata_df.convert_objects(convert_numeric=True)
traindata_df = pd.read_csv(train_path, sep=',',index_col='user_id', low_memory=False)
print(traindata_df.info())

...
mean_service_caller_time_fee    714686 non-null float64
remove_caller_fee               714686 non-null float64
logremove_caller_fee            713855 non-null float64
dtypes: float64(14), int64(50)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值