【Python】处理 scikit-learn 中的 FutureWarning


那年夏天我和你躲在 这一大片宁静的海

直到后来我们都还在 对这个世界充满期待

今年冬天你已经不在 我的心空出了一块

很高兴遇见你 让我终究明白

回忆比真实精彩

🎵 王心凌《那年夏天宁静的海》


在数据科学和机器学习领域,scikit-learn 是一个非常流行的库,用于构建和评估各种机器学习模型。然而,随着版本的更新,库中的某些模块和功能可能会被弃用(deprecated),并在未来的版本中移除。最近在使用 scikit-learn 时,我们遇到了如下的 FutureWarning:

vbnet 复制代码
/usr/local/anaconda3/lib/python3.6/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.neighbors.base module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes/functions should instead be imported from sklearn.neighbors. Anything that cannot be imported from sklearn.neighbors is now part of the private API.
  warnings.warn(message, FutureWarning)
/usr/local/anaconda3/lib/python3.6/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.ensemble.bagging module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes/functions should instead be imported from sklearn.ensemble. Anything that cannot be imported from sklearn.ensemble is now part of the private API.
  warnings.warn(message, FutureWarning)

什么是 FutureWarning?

FutureWarning 是 Python 用来通知用户某些功能将在未来版本中被弃用的方式。虽然这些功能在当前版本中仍然可用,但开发者建议开始使用新的替代方案,以确保代码在未来版本中的兼容性。

在我们的例子中,警告告诉我们:

sklearn.neighbors.base 模块在 0.22 版本中被弃用,并将在 0.24 版本中移除。相应的类/函数应从 sklearn.neighbors 中导入。

sklearn.ensemble.bagging 模块在 0.22 版本中被弃用,并将在 0.24 版本中移除。相应的类/函数应从 sklearn.ensemble 中导入。

如何处理这些警告?

最好的处理方式是按照警告中的建议,更新代码以使用推荐的导入方式。例如:

旧的导入方式
python 复制代码
from sklearn.neighbors.base import KNeighborsClassifier
from sklearn.ensemble.bagging import BaggingClassifier
新的导入方式
python 复制代码
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import BaggingClassifier

通过这种方式,我们确保代码在未来的 scikit-learn 版本中依然可用,同时也提高了代码的可读性和可维护性。

示例代码修正

假设我们有一段代码需要处理空值并使用随机森林分类器进行训练,旧的代码可能如下所示:

python 复制代码
from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
import pandas as pd

# 示例数据
# df = pd.read_csv('your_data.csv')

# 使用均值填充空值
imputer = SimpleImputer(strategy='mean')
X = df.drop('target', axis=1)
X_imputed = imputer.fit_transform(X)
y = df['target']

# 创建和训练随机森林分类器
clf = RandomForestClassifier(class_weight={0: 0.1667, 1: 0.8333})
clf.fit(X_imputed, y)

如果我们遇到 FutureWarning,需要更新导入方式,并可以选择暂时忽略这些警告:

python 复制代码
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)

from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
import pandas as pd

# 示例数据
# df = pd.read_csv('your_data.csv')

# 使用均值填充空值
imputer = SimpleImputer(strategy='mean')
X = df.drop('target', axis=1)
X_imputed = imputer.fit_transform(X)
y = df['target']

# 创建和训练随机森林分类器
clf = RandomForestClassifier(class_weight={0: 0.1667, 1: 0.8333})
clf.fit(X_imputed, y)

总结

通过关注并处理 FutureWarning,我们可以确保代码的前瞻性和兼容性,避免未来版本更新带来的潜在问题。最好是定期检查项目中的所有警告,并根据建议进行相应的代码更新。这不仅有助于保持代码的健康状态,还能提高代码的可维护性和性能。

相关推荐
货拉拉技术20 分钟前
货拉拉大数据智能运维AI Agent探索实践
大数据·人工智能
xsd2024111829 分钟前
Eye-to-Hand 手眼标定实操:固定相机怎么标定出机械臂基座到相机的外参
人工智能
zc.z1 小时前
提示词工程(十一):深入理解Planning Prompt底层原理,从任务拆解到执行计划
人工智能·prompt·提示词工程·prompt planning
上弦外贸新观察2 小时前
WordPress外贸网站建设与Google SEO、海外GEO优化技术指南:网站速度、内容架构和AI搜索可见度布局
人工智能·架构
硅谷秋水2 小时前
逆矩阵的Physis-v1.0
人工智能·机器学习·计算机视觉·语言模型·机器人
2601_967980112 小时前
Python闭包(Closure)详解
python·作用域·装饰器·函数式编程·闭包
feasibility.2 小时前
智元发布 LIFE 框架:让机器人从“完成任务“到“学会生活“,24 小时真机视频验证具身智能
人工智能·机器人·生活·视频·具身智能·智元机器人·智元
AI创界者2 小时前
ComfyUI 进阶实战:MiniMax-H3 视频人物脸部替换工作流全解析(对比 Face 5.5 实测与避坑指南)
人工智能·python·aigc
ndz3 小时前
用一把 Key 接入多家大模型,再查 AI 会不会推荐你的品牌
人工智能
承渊政道3 小时前
【Python编程快速上手—让繁琐工作自动化】(模式匹配与正则表达式:从找到一个号码到批量提取信息)
python·正则表达式·pycharm·自动化·模式匹配