python imblearn self._validate_data()方法报错

当使用imblearn库遇到持续报错且无法通过常规手段解决时,一种有效的应急办法是在Python的site-packages目录下,找到imblearn库的相关py文件,注释掉引发错误的方法。例如,注释掉`_validate_data()`方法。此方法适用于处理Python库冲突或不兼容问题。

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

参考链接:https://blog.csdn.net/lilidaren/article/details/107149576

调用imblearn库中的方法时,疯狂报错,无法通过pip uninstall和pip install解决,于是乎上网查一下怎么处理。想不到,最有效的方法居然是将imblearn里面的py文件,涉及该方法的sentences注释掉。今日份之震惊.jpg

  1. 进入Python的第三方库安装路径D:\Python\Python37\Lib\site-packages,进入Imblearn文件夹,根据提示注释该方法。
    在这里插入图片描述

  2. \imblearn\under_sampling_prototype_selection_random_under_sampler.py
    涉及self._validate_data()方法的整句注释掉。
    在这里插入图片描述

  3. \imblearn\base.py
    涉及self._validate_data()方法的整句注释掉。
    在这里插入图片描述

Traceback (most recent call last): File "C:/Users/DELL/Desktop/python_flask-dc/python_flask/bpnn_tf.py", line 106, in <module> last_three = column.str[-3:] File "E:\python-3.7.0\lib\site-packages\pandas\core\generic.py", line 5137, in __getattr__ return object.__getattribute__(self, name) File "E:\python-3.7.0\lib\site-packages\pandas\core\accessor.py", line 187, in __get__ accessor_obj = self._accessor(obj) File "E:\python-3.7.0\lib\site-packages\pandas\core\strings.py", line 2100, in __init__ self._inferred_dtype = self._validate(data) File "E:\python-3.7.0\lib\site-packages\pandas\core\strings.py", line 2157, in _validate raise AttributeError("Can only use .str accessor with string values!") AttributeError: Can only use .str accessor with string values!Traceback (most recent call last): File "C:/Users/DELL/Desktop/python_flask-dc/python_flask/bpnn_tf.py", line 106, in <module> last_three = column.str[-3:] File "E:\python-3.7.0\lib\site-packages\pandas\core\generic.py", line 5137, in __getattr__ return object.__getattribute__(self, name) File "E:\python-3.7.0\lib\site-packages\pandas\core\accessor.py", line 187, in __get__ accessor_obj = self._accessor(obj) File "E:\python-3.7.0\lib\site-packages\pandas\core\strings.py", line 2100, in __init__ self._inferred_dtype = self._validate(data) File "E:\python-3.7.0\lib\site-packages\pandas\core\strings.py", line 2157, in _validate raise AttributeError("Can only use .str accessor with string values!") AttributeError: Can only use .str accessor with string values!
05-27
、KeyError Traceback (most recent call last) File D:\anaconda\Lib\site-packages\pandas\core\indexes\base.py:3805, in Index.get_loc(self, key) 3804 try: -> 3805 return self._engine.get_loc(casted_key) 3806 except KeyError as err: File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc() File pandas\\_libs\\hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas\\_libs\\hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: '有无污染' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[19], line 8 6 data = pd.read_excel(r'C:\Users\冯雪玲\Desktop\北京市空气质量数据.xlsx') 7 X = data.loc[:, ['PM2.5', 'PM10', 'SO2', 'CO', 'NO2', 'O3']] ----> 8 Y = data.loc[:, '有无污染'] 9 modelLR = LogisticRegression() 10 modelLR.fit(X, Y) File D:\anaconda\Lib\site-packages\pandas\core\indexing.py:1184, in _LocationIndexer.__getitem__(self, key) 1182 if self._is_scalar_access(key): 1183 return self.obj._get_value(*key, takeable=self._takeable) -> 1184 return self._getitem_tuple(key) 1185 else: 1186 # we by definition only have the 0th axis 1187 axis = self.axis or 0File D:\anaconda\Lib\site-packages\pandas\core\indexing.py:1368, in _LocIndexer._getitem_tuple(self, tup) 1366 with suppress(IndexingError): 1367 tup = self._expand_ellipsis(tup) -> 1368 return self._getitem_lowerdim(tup) 1370 # no multi-index, so validate all of the indexers 1371 tup = self._validate_tuple_indexer(tup) File D:\anaconda\Lib\site-packages\pandas\core\indexing.py:1065, in _LocationIndexer._getitem_lowerdim(self, tup) 1061 for i, key in enumerate(tup): 1062 if is_label_like(key): 1063 # We don't need to check for tuples here because those are 1064 # caught by the
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值