---
# System prepended metadata

title: Release Highlights for scikit-learn 0.22(翻譯)
tags: [Release Highlights, 翻譯, sklearn, python, machine learning, scikit-learn]

---

# Release Highlights for scikit-learn 0.22(翻譯)
###### tags: `scikit-learn` `sklearn` `python` `machine learning` `Release Highlights` `翻譯`
[原文連結](https://scikit-learn.org/stable/auto_examples/release_highlights/plot_release_highlights_0_22_0.html#sphx-glr-auto-examples-release-highlights-plot-release-highlights-0-22-0-py)

我們很高興宣佈scikit-learn 0.22的發布，其中包含許多bug的修復以及新功能!下面我們詳細說明這版本的一些主要功能。關於完整的修正清單，請參閱發行說明。

使用`pip`安裝最新版本：
```shell
pip install --upgrade scikit-learn
```
或使用`conda`：
```shell
conda install scikit-learn
```
## New plotting API
新的plotting API可用於建立可視化。這個新的API允許在不涉及任何重新計算情況下快速調整繪圖的視覺效果。也可以在同一個圖(figure)上加入不同圖形。下面範例說明[plot_roc_curve](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_roc_curve.html#sklearn.metrics.plot_roc_curve)，但支援其它繪圖工具，像是[plot_partial_dependence](https://scikit-learn.org/stable/modules/generated/sklearn.inspection.plot_partial_dependence.html#sklearn.inspection.plot_partial_dependence)、[plot_precision_recall_curve](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_precision_recall_curve.html#sklearn.metrics.plot_precision_recall_curve)與[plot_confusion_matrix](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_confusion_matrix.html#sklearn.metrics.plot_confusion_matrix)。關於這個API可參閱[使用者指南](https://scikit-learn.org/stable/visualizations.html#visualizations)。

```python
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import plot_roc_curve
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
import matplotlib.pyplot as plt

# 生成測試資料
X, y = make_classification(random_state=0)
# 資料集分為訓練與測試資料集
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

svc = SVC(random_state=42)
svc.fit(X_train, y_train)

rfc = RandomForestClassifier(random_state=42)
rfc.fit(X_train, y_train)

# 利用plot_roc_curve計算roc
svc_disp = plot_roc_curve(svc, X_test, y_test)
rfc_disp = plot_roc_curve(rfc, X_test, y_test, ax=svc_disp.ax_)
rfc_disp.figure_.suptitle("ROC curve comparison")

plt.show()
```
`plot_roc_curve`很快速的幫我們計算出ROC曲線，並且將兩個不同模型的結果繪製在同一張圖(figure)上，這對我們瞭解不同模型的比較非常實用。

## Stacking Classifier and Regressor
[StackingClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.StackingClassifier.html#sklearn.ensemble.StackingClassifier)與[StackingRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.StackingRegressor.html#sklearn.ensemble.StackingRegressor)允許你有一個帶有最終分類器或回歸器的估計器堆疊。堆疊概括包括堆疊各別估計器的輸出，並使用一個分類器來完全最終的預測。堆疊允許使用每個各別估計器的強度，透過使用它們的輸出做為最終估計器的輸入。基礎估計器在完整的X上擬合，而最終估計器的訓練則使用`cross_val_predict`對基礎估計器做交叉驗證的預測。

更多資訊可參閱[使用者指南](https://scikit-learn.org/stable/modules/ensemble.html#stacking)

```python
from sklearn.datasets import load_iris
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import StackingClassifier
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)

# 利用list加入多個分類器，分類器中一樣可以設置pipeline
estimators = [
    ('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
    ('svr', make_pipeline(StandardScaler(),
                          LinearSVC(random_state=42)))
]

# 實作StackingClassifier，指定最終分類器
clf = StackingClassifier(
    estimators=estimators, final_estimator=LogisticRegression()
)
# 分割資料集
X_train, X_test, y_train, y_test = train_test_split(
    X, y, stratify=y, random_state=42
)

clf.fit(X_train, y_train).score(X_test, y_test)
```

## Permutation-based feature importance
對任一個擬合的估計器，[inspection.permutation_importance](https://scikit-learn.org/stable/modules/generated/sklearn.inspection.permutation_importance.html#sklearn.inspection.permutation_importance)可以用來得到每一個特徵的重要性的估計。

```python
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance

# 取得測試資料
X, y = make_classification(random_state=0, n_features=5, n_informative=3)

# 實作estimator
rf = RandomForestClassifier(random_state=0).fit(X, y)
# 實作permutation_importance
result = permutation_importance(rf, X, y, n_repeats=10, random_state=0,
                                n_jobs=-1)

# 定義圖表 
fig, ax = plt.subplots()
# 取得重要性均值排序
sorted_idx = result.importances_mean.argsort()
# 繪製圖表
ax.boxplot(result.importances[sorted_idx].T,
           vert=False, labels=range(X.shape[1]))
# 細部調整圖表
ax.set_title("Permutation Importance of each feature")
ax.set_ylabel("Features")
fig.tight_layout()
plt.show()
```
## Native support for missing values for gradient boosting
[ensemble.HistGradientBoostingClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.HistGradientBoostingClassifier.html#sklearn.ensemble.HistGradientBoostingClassifier)與[ensemble.HistGradientBoostingRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.HistGradientBoostingRegressor.html#sklearn.ensemble.HistGradientBoostingRegressor)現在對缺失值(NaNs)有本機支援。這意味著在訓練或預測的時候不需要再做缺值插補。

```python
from sklearn.experimental import enable_hist_gradient_boosting  # noqa
from sklearn.ensemble import HistGradientBoostingClassifier
import numpy as np

# 故意放一個缺值
X = np.array([0, 1, 2, np.nan]).reshape(-1, 1)
y = [0, 0, 1, 1]

gbdt = HistGradientBoostingClassifier(min_samples_leaf=1).fit(X, y)
print(gbdt.predict(X))
```
## Precomputed sparse nearest neighbors graph

大多基於最近鄰圖的估計器現在接受預先計算的稀疏圖做為輸入，以便重覆使用同一圖進行多個估計器擬合。要在pipeline中使用這個功能，可以使用記憶體參數，以及兩個新的轉換器[neighbors.KNeighborsTransformer](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsTransformer.html#sklearn.neighbors.KNeighborsTransformer)與[neighbors.RadiusNeighborsTransformer](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.RadiusNeighborsTransformer.html#sklearn.neighbors.RadiusNeighborsTransformer)。預先計算也可以透過自定義估計器來執行做為替代的實現，像是近似最近鄰方法。更多資訊可參閱[使用者指南](https://scikit-learn.org/stable/modules/neighbors.html#neighbors-transformer)。

```python
from tempfile import TemporaryDirectory
from sklearn.neighbors import KNeighborsTransformer
from sklearn.manifold import Isomap
from sklearn.pipeline import make_pipeline

X, y = make_classification(random_state=0)

with TemporaryDirectory(prefix="sklearn_cache_") as tmpdir:
    estimator = make_pipeline(
        KNeighborsTransformer(n_neighbors=10, mode='distance'),
        Isomap(n_neighbors=10, metric='precomputed'),
        memory=tmpdir)
    estimator.fit(X)

    # We can decrease the number of neighbors and the graph will not be
    # recomputed.
    estimator.set_params(isomap__n_neighbors=5)
    estimator.fit(X)
```
## KNN Based Imputation
我們現在支援使用KNN來做缺值插補。

每個樣本的缺失值都用訓練集中發現的`n_neighbors`個最近鄰的平均值來插補。如果兩個不缺值的特徵都很接近，則兩個樣本是接近的。預設情況下，使用支援缺失值`nan_euclidean_distances`的歐氏距離度量來找出最近鄰。

更多資訊可參閱[使用者指南](https://scikit-learn.org/stable/modules/impute.html#knnimpute)。

```python
import numpy as np
from sklearn.impute import KNNImputer

# 故意插入空值
X = [[1, 2, np.nan], [3, 4, 3], [np.nan, 6, 5], [8, 8, 7]]
imputer = KNNImputer(n_neighbors=2)
print(imputer.fit_transform(X))
```
## Tree pruning
在樹(tree)建構完成之後，現在可以修剪多數基於樹的估計器。修剪法是基於最小化成本複雜。更多資訊可參閱[使用者指南](https://scikit-learn.org/stable/modules/tree.html#minimal-cost-complexity-pruning)。

```python
X, y = make_classification(random_state=0)

rf = RandomForestClassifier(random_state=0, ccp_alpha=0).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_state=0, ccp_alpha=0.05).fit(X, y)
print("Average number of nodes with pruning {:.1f}".format(
    np.mean([e.tree_.node_count for e in rf.estimators_])))
```
## Retrieve dataframes from OpenML
[`datasets.fetch_openml`](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.fetch_openml.html#sklearn.datasets.fetch_openml)現在可以回傳pandas dataframe，從而正確處理帶有異質資料的資料集。

```python
from sklearn.datasets import fetch_openml

titanic = fetch_openml('titanic', version=1, as_frame=True)
print(titanic.data.head()[['pclass', 'embarked']])
```
## Checking scikit-learn compatibility of an estimator
開發人員可以使用[`check_estimator`](https://scikit-learn.org/stable/modules/generated/sklearn.utils.estimator_checks.check_estimator.html#sklearn.utils.estimator_checks.check_estimator)檢查他們的scikit-learn相容估計器的相容性。例如，`check_estimator(LinearSVC)`通過。

我們現在提供一個`pytest`特定的裝飾器，它允許`pytest`獨立執行所有檢查並報告失敗的檢查。

```python
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.utils.estimator_checks import parametrize_with_checks


@parametrize_with_checks([LogisticRegression, DecisionTreeRegressor])
def test_sklearn_compatible_estimator(estimator, check):
    check(estimator)
```
## ROC AUC now supports multiclass classification
函數`roc_auc_score`也可以應用在多類別的分類。目前支援兩種平均策略：一對一演算法計算成對的ROC AUC分數的平均值，一對多演算法計算每個類別相對於其它類別的ROC AUC分數的平均值。這兩種情況下，多類別的ROC AUC分數都是根據模型從樣本屬於特定類別的機率估計計算而來。OvO與OvR演算法皆支援均勻加權(`average='macro'`)，與依盛行率加權(`average='weighted'`)。

更多資訊可參閱[使用者指南](https://scikit-learn.org/stable/modules/model_evaluation.html#roc-metrics)。

```python
from sklearn.datasets import make_classification
from sklearn.svm import SVC
from sklearn.metrics import roc_auc_score

X, y = make_classification(n_classes=4, n_informative=16)
clf = SVC(decision_function_shape='ovo', probability=True).fit(X, y)
print(roc_auc_score(y, clf.predict_proba(X), multi_class='ovo'))
```





