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

相关推荐
Vince的修炼之路5 分钟前
用Python将JSON格式文件数据导入到Elasticsearch上
python
数据的世界016 分钟前
重构智慧书-第7条:恰当彰显价值,勿越职场分寸
人工智能
xwill*7 分钟前
VOTE: Vision-Language-Action Optimization with Trajectory Ensemble Voting
人工智能·pytorch·深度学习
wxdlfkj7 分钟前
光谱共焦传感器 LTC2400/LTC4000F 对手机镜头镜片的圆角倒角厚度测量检测
人工智能
不会吉他的肌肉男不是好的挨踢男8 分钟前
LLaMA Factory 训练模型未检测到CUDA环境解决
python·ai·llama
mys551811 分钟前
从SEO到GEO:AI搜索如何重塑企业流量新路径?
人工智能·aigc·geo·ai搜索优化·ai引擎优化
用户17178327988113 分钟前
AI大模型爆火Agent(打造专属LLM智能体)
人工智能
Justinyh18 分钟前
Notion同步到CSDN + 构建Obsidian本地博客系统指南
python·csdn·图床·notion·obsidian·文档同步·piclist
大千AI助手21 分钟前
多维空间的高效导航者:KD树算法深度解析
数据结构·人工智能·算法·机器学习·大千ai助手·kd tree·kd树
Coding茶水间21 分钟前
基于深度学习的西红柿成熟度检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉