网站推广优化趋势,网易企业邮箱和个人邮箱的区别,免费网站建设加盟,室内设计和装修设计十三 发自 凹非寺 量子位 报道 | 公众号 QbitAIScikit-learn#xff0c;这个强大的Python包#xff0c;一直深受机器学习玩家青睐。而近日#xff0c;scikit-learn 官方发布了 0.22 最终版本。此次的更新修复了许多旧版本的bug#xff0c;同时发布了一些新功能。安装最新版…十三 发自 凹非寺 量子位 报道 | 公众号 QbitAIScikit-learn这个强大的Python包一直深受机器学习玩家青睐。而近日scikit-learn 官方发布了 0.22 最终版本。此次的更新修复了许多旧版本的bug同时发布了一些新功能。安装最新版本 scikit-learn 也很简单。使用 pip pip install --upgrade scikit-learn使用 conda conda install scikit-learn接下来就是此次更新的十大亮点。全新 plotting API对于创建可视化任务scikit-learn 推出了一个全新 plotting API。这个新API可以快速调整图形的视觉效果不再需要进行重新计算。也可以在同一个图形中添加不同的图表。例如from sklearn.model_selection import train_test_splitfrom sklearn.svm import SVCfrom sklearn.metrics import plot_roc_curvefrom sklearn.ensemble import RandomForestClassifierfrom sklearn.datasets import make_classificationimport matplotlib.pyplot as pltX, y make_classification(random_state0)X_train, X_test, y_train, y_test train_test_split(X, y, random_state42)svc SVC(random_state42)svc.fit(X_train, y_train)rfc RandomForestClassifier(random_state42)rfc.fit(X_train, y_train)svc_disp plot_roc_curve(svc, X_test, y_test)rfc_disp plot_roc_curve(rfc, X_test, y_test, axsvc_disp.ax_)rfc_disp.figure_.suptitle(ROC curve comparison)plt.show()StackingClassifier和StackingRegressorStackingClassifier 和 StackingRegressor 允许用户拥有一个具有最终分类器/回归器的估计器堆栈(estimator of stack)。堆栈泛化(stacked generalization)是将各个估计器的输出叠加起来然后使用分类器来计算最终的预测。基础估计器拟合在完整的X( full X )上而最终估计器则使用基于cross_val_predict的基础估计器的交叉验证预测进行训练。例如from sklearn.datasets import load_irisfrom sklearn.svm import LinearSVCfrom sklearn.linear_model import LogisticRegressionfrom sklearn.preprocessing import StandardScalerfrom sklearn.pipeline import make_pipelinefrom sklearn.ensemble import StackingClassifierfrom sklearn.model_selection import train_test_splitX, y load_iris(return_X_yTrue)estimators [ (rf, RandomForestClassifier(n_estimators10, random_state42)), (svr, make_pipeline(StandardScaler(), LinearSVC(random_state42)))]clf StackingClassifier( estimatorsestimators, final_estimatorLogisticRegression())X_train, X_test, y_train, y_test train_test_split( X, y, stratifyy, random_state42)clf.fit(X_train, y_train).score(X_test, y_test)输出0.9473684210526315。基于排列(permutation)的特征重要性inspection.permutation_importance可以用来估计每个特征的重要性对于任何拟合的估算器from sklearn.ensemble import RandomForestClassifierfrom sklearn.inspection import permutation_importanceX, y make_classification(random_state0, n_features5, n_informative3)rf RandomForestClassifier(random_state0).fit(X, y)result permutation_importance(rf, X, y, n_repeats10, random_state0, n_jobs-1)fig, ax plt.subplots()sorted_idx result.importances_mean.argsort()ax.boxplot(result.importances[sorted_idx].T, vertFalse, labelsrange(X.shape[1]))ax.set_title(Permutation Importance of each feature)ax.set_ylabel(Features)fig.tight_layout()plt.show()对梯度提升提供缺失值的本地支持ensemble.HistGradientBoostingClassifier 和 ensemble.HistGradientBoostingRegressor 现在对缺失值(NaNs)具有本机支持。这意味着在训练或预测时无需插补数据。from sklearn.experimental import enable_hist_gradient_boosting # noqafrom sklearn.ensemble import HistGradientBoostingClassifierimport numpy as npX np.array([0, 1, 2, np.nan]).reshape(-1, 1)y [0, 0, 1, 1]gbdt HistGradientBoostingClassifier(min_samples_leaf1).fit(X, y)print(gbdt.predict(X))输出[0 0 1 1]。预计算的稀疏近邻图现在大多数基于最近邻图的估算都接受预先计算的稀疏图作为输入以将同一图重用于多个估算量拟合。要在pipeline中使用这个特性可以使用 memory 参数以及neighbors.KNeighborsTransformer和neighbors.RadiusNeighborsTransformer中的一个。预计算还可以由自定义的估算器来执行。from tempfile import TemporaryDirectoryfrom sklearn.neighbors import KNeighborsTransformerfrom sklearn.manifold import Isomapfrom sklearn.pipeline import make_pipelineX, y make_classification(random_state0)with TemporaryDirectory(prefixsklearn_cache_) as tmpdir: estimator make_pipeline( KNeighborsTransformer(n_neighbors10, modedistance), Isomap(n_neighbors10, metricprecomputed), memorytmpdir) estimator.fit(X) # We can decrease the number of neighbors and the graph will not be # recomputed. estimator.set_params(isomap__n_neighbors5) estimator.fit(X)基于Imputation的KNN现在scikit_learn 支持使用k近邻来填充缺失值。from sklearn.impute import KNNImputerX [[1, 2, np.nan], [3, 4, 3], [np.nan, 6, 5], [8, 8, 7]]imputer KNNImputer(n_neighbors2)print(imputer.fit_transform(X))输出[[1. 2. 4. ][3. 4. 3. ][5.5 6. 5. ][8. 8. 7. ]]树剪枝现在在建立一个树之后可以剪枝大部分基于树的估算器。X, y make_classification(random_state0)rf RandomForestClassifier(random_state0, ccp_alpha0).fit(X, y)print(Average number of nodes without pruning {:.1f}.format( np.mean([e.tree_.node_count for e in rf.estimators_])))rf RandomForestClassifier(random_state0, ccp_alpha0.05).fit(X, y)print(Average number of nodes with pruning {:.1f}.format( np.mean([e.tree_.node_count for e in rf.estimators_])))输出Average number of nodes without pruning 22.3Average number of nodes with pruning 6.4从OpenML检索dataframedatasets.fetch_openml现在可以返回pandas dataframe从而正确处理具有异构数据的数据集from sklearn.datasets import fetch_openmltitanic fetch_openml(titanic, version1, as_frameTrue)print(titanic.data.head()[[pclass, embarked]])输出pclass embarked0 1.0 S1 1.0 S2 1.0 S3 1.0 S4 1.0 S检查一个估算器的scikit-learn兼容性开发人员可以使用check_estimator检查其scikit-learn兼容估算器的兼容性。现在scikit-learn 提供了pytest特定的装饰器(decorator)该装饰器允许pytest独立运行所有检查并报告失败的检查。from sklearn.linear_model import LogisticRegressionfrom sklearn.tree import DecisionTreeRegressorfrom sklearn.utils.estimator_checks import parametrize_with_checksparametrize_with_checks([LogisticRegression, DecisionTreeRegressor])def test_sklearn_compatible_estimator(estimator, check): check(estimator)ROC AUC现在支持多类别分类roc_auc_score 函数也可用于多类别分类。目前支持两种平均策略one-vs-one算法计算两两配对的ROC AUC分数的平均值one-vs-rest算法计算每个类别相对于所有其他类别的ROC AUC分数的平均值。在这两种情况下模型都是根据样本属于特定类别的概率估计来计算多类别ROC AUC分数。from sklearn.datasets import make_classificationfrom sklearn.svm import SVCfrom sklearn.metrics import roc_auc_scoreX, y make_classification(n_classes4, n_informative16)clf SVC(decision_function_shapeovo, probabilityTrue).fit(X, y)print(roc_auc_score(y, clf.predict_proba(X), multi_classovo))输出0.9957333333333332传送门Twitterhttps://twitter.com/scikit_learn/status/1201847227561529346博客https://scikit-learn.org/stable/auto_examples/release_highlights/plot_release_highlights_0_22_0.html#new-plotting-api使用指南https://scikit-learn.org/stable/modules/model_evaluation.html#roc-metrics— 完 —量子位 QbitAI · 头条号签约关注我们第一时间获知前沿科技动态