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

相关推荐
文火冰糖的硅基工坊3 小时前
[人工智能-大模型-19]:GitHub Copilot:程序员的 AI 编程副驾驶
人工智能·github·copilot
shuououo5 小时前
YOLOv4 核心内容笔记
人工智能·计算机视觉·目标跟踪
DO_Community8 小时前
普通服务器都能跑:深入了解 Qwen3-Next-80B-A3B-Instruct
人工智能·开源·llm·大语言模型·qwen
WWZZ20258 小时前
快速上手大模型:机器学习3(多元线性回归及梯度、向量化、正规方程)
人工智能·算法·机器学习·机器人·slam·具身感知
deephub8 小时前
深入BERT内核:用数学解密掩码语言模型的工作原理
人工智能·深度学习·语言模型·bert·transformer
PKNLP8 小时前
BERT系列模型
人工智能·深度学习·bert
应用市场9 小时前
构建自定义命令行工具 - 打造专属指令体
开发语言·windows·python
兰亭妙微9 小时前
ui设计公司审美积累 | 金融人工智能与用户体验 用户界面仪表盘设计
人工智能·金融·ux
东方佑9 小时前
从字符串中提取重复子串的Python算法解析
windows·python·算法
AKAMAI10 小时前
安全风暴的绝地反击 :从告警地狱到智能防护
运维·人工智能·云计算