备注
Go to the end 下载完整的示例代码。或者通过浏览器中的MysterLite或Binder运行此示例
scikit-learn 1.6的发布亮点#
我们很高兴宣布scikit-learn 1.6的发布!添加了许多错误修复和改进,以及一些关键的新功能。下面我们详细介绍了该版本的亮点。 For an exhaustive list of all the changes ,请参阅 release notes .
安装最新版本(使用pip):
pip install --upgrade scikit-learn
或带有conda::
conda install -c conda-forge scikit-learn
FrozenEstimator:冻结估算者#
此元估计器允许您采用估计器并冻结其匹配方法,这意味着调用 fit
不执行任何操作;此外, fit_predict
和 fit_transform
呼叫 predict
和 transform
分别不打电话 fit
.原始估计器的其他方法和属性保持不变。一个有趣的用例是使用预拟合模型作为管道中的Transformer步骤,或者将预拟合模型传递给一些元估计器。这是一个简短的例子:
import time
from sklearn.datasets import make_classification
from sklearn.frozen import FrozenEstimator
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import FixedThresholdClassifier
X, y = make_classification(n_samples=1000, random_state=0)
start = time.time()
classifier = SGDClassifier().fit(X, y)
print(f"Fitting the classifier took {(time.time() - start) * 1_000:.2f} milliseconds")
start = time.time()
threshold_classifier = FixedThresholdClassifier(
estimator=FrozenEstimator(classifier), threshold=0.9
).fit(X, y)
print(
f"Fitting the threshold classifier took {(time.time() - start) * 1_000:.2f} "
"milliseconds"
)
Fitting the classifier took 2.23 milliseconds
Fitting the threshold classifier took 0.44 milliseconds
匹配阈值分类器跳过了匹配内部分类器 SGDClassifier
.有关更多详细信息,请参阅示例 例子利用 FrozenEstimator .
转换管道中X以外的数据#
的 Pipeline
现在支持转换传递的数据 X
必要时这可以通过设置新的 transform_input
参数.当通过管道传递验证集时,这特别有用。
作为一个例子,想象一下 EstimatorWithValidationSet
是接受验证集的估计器。我们现在可以有一个管道,它将转换验证集并将其传递给估计器::
with sklearn.config_context(enable_metadata_routing=True):
est_gs = GridSearchCV(
Pipeline(
(
StandardScaler(),
EstimatorWithValidationSet(...).set_fit_request(X_val=True, y_val=True),
),
# telling pipeline to transform these inputs up to the step which is
# requesting them.
transform_input=["X_val"],
),
param_grid={"estimatorwithvalidationset__param_to_optimize": list(range(5))},
cv=5,
).fit(X, y, X_val=X_val, y_val=y_val)
在上面的代码中,关键部分是调用 set_fit_request
以指定 X_val
和 y_val
所要求 EstimatorWithValidationSet.fit
方法和 transform_input
参数来告诉管道进行转换 X_val
然后将其传递给 EstimatorWithValidationSet.fit
.
请注意,目前scikit-learn估计器尚未扩展为接受用户指定的验证集。该功能提前发布,以收集可能从中受益的第三方图书馆的反馈。
多类支持 LogisticRegression(solver="newton-cholesky")
#
的 "newton-cholesky"
求解器(最初在scikit-learn版本1.2中引入)以前仅限于二进制 LogisticRegression
以及一些其他广义线性回归估计量(即 PoissonRegressor
, GammaRegressor
和 TweedieRegressor
).
此新版本包括对多类(多项)的支持 LogisticRegression
.
当特征数量从小到中等时,此求解器特别有用。经验证明,在一些具有单一热编码分类特征的中等大小数据集上,它比其他求解器更可靠、更快地收敛,正如在 benchmark results of the pull-request .
Extra Trees缺少值支持#
的类 ensemble.ExtraTreesClassifier
和 ensemble.ExtraTreesRegressor
现在支持缺失的值。更多详情请参阅 User Guide .
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
X = np.array([0, 1, 6, np.nan]).reshape(-1, 1)
y = [0, 0, 1, 1]
forest = ExtraTreesClassifier(random_state=0).fit(X, y)
forest.predict(X)
array([0, 0, 1, 1])
从网络下载任何数据集#
功能 datasets.fetch_file
允许从任何给定的URL下载文件。这个方便的功能提供了内置的本地磁盘缓存,sha256摘要完整性检查和网络错误的自动重试机制。
目标是提供与数据集获取器相同的便利性和可靠性,同时提供处理来自任意在线源和文件格式的数据的灵活性。
下载的文件然后可以加载通用或特定领域的功能,例如 pandas.read_csv
, pandas.read_parquet
等。
阵列API支持#
自1.5版本以来,更多的估计器和函数已更新,以支持数组API兼容的输入,特别是来自 sklearn.model_selection
模块和来自 sklearn.metrics
module.
请参阅 array API support 页面,了解将scikit-learn与数组API兼容库(例如PyTorch或CuPy)一起使用的说明。
几乎完整的元数据路由支持#
除AdaBoost之外的所有剩余估计器和功能都添加了对路由元数据的支持。看到 Metadata Routing User Guide 了解更多详细信息。
自由线程CPython 3.13支持#
scikit-learn对自由线程CPython提供了初步支持,特别是自由线程轮可用于我们所有支持的平台。
自由线程(也称为nogil)CPython 3.13是CPython 3.13的实验版本,旨在通过删除全局解释器锁(GIL)来实现高效的多线程用例。
有关自由线程CPython的更多详细信息,请参阅 py-free-threading doc ,特别是 how to install a free-threaded CPython 和 Ecosystem compatibility tracking .
请随时在您的用例上尝试自由线程的CPython并报告任何问题!
改进第三方库的开发人员API#
我们一直在努力改进第三方库的开发人员API。这项工作仍在进行中,但在此版本中已经完成了相当多的工作。此版本包括:
sklearn.utils.validation.validate_data
引入并取代之前的私人BaseEstimator._validate_data
法该功能扩展check_array
并添加了对记住输入功能计数和名称的支持。Estimator tags are now revamped and a part of the public API via
sklearn.utils.Tags
. Estimators should now override theBaseEstimator.__sklearn_tags__
method instead of implementing a_more_tags
method. If you'd like to support multiple scikit-learn versions, you can implement both methods in your class.由于开发了一个公共标签API,我们删除了
_xfail_checks
预计失败的标签和测试将直接传递给check_estimator
和parametrize_with_checks
.有关更多详细信息,请参阅相应的API文档。通用测试套件中的许多测试都会更新并引发更有用的错误消息。我们还添加了一些新的测试,这应该可以帮助您更轻松地修复估计器的潜在问题。
我们的更新版本 开发scikit-learn估计器 也可用,我们建议您查看。
Total running time of the script: (0分0.068秒)
相关实例
Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>
_