![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
CDA數據分析師 出品
Python的主要功能機器學習庫的最新版本包括許多新功能和錯誤修復。你能夠從Scikit-learn官方0.22 發行要點中找到有關這些更改的完整說明。dom
經過pip完成安裝更新:機器學習
pip install --upgrade scikit-learn學習
或conda:url
conda install scikit-learnspa
最新的Scikit-learn中有5個新功能值得你注意。.net
1.新的繪圖API
新的繪圖API可用,無需從新計算便可正常工做。支持的圖包括一些相關圖,混淆矩陣和ROC曲線。下面是Scikit-learn用戶指南中的示例,對API進行了演示:rest
from sklearn.model_selection import train_test_splitorm
from sklearn.svm import SVCblog
from sklearn.metrics import plot_roc_curveseo
from sklearn.datasets import load_wine
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)
svc_disp = plot_roc_curve(svc, X_test, y_test)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
請注意,繪製是經過最後一行代碼完成的。
2.堆疊歸納
Scikit-learn已經集成了用於減小估計量誤差的總體學習技術。StackingClassifier和StackingRegressor是啓用估算器堆疊的模塊,並使用final_estimator這些堆疊的估算器預測做爲其輸入。請參閱用戶指南中的示例,使用如下定義爲的迴歸估計量estimators和梯度加強迴歸最終估計量:
from sklearn.linear_model import RidgeCV, LassoCV
from sklearn.svm import SVR
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import StackingRegressor
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
estimators = [('ridge', RidgeCV()),
('lasso', LassoCV(random_state=42)),
('svr', SVR(C=1, gamma=1e-6))]
reg = StackingRegressor(
estimators=estimators,
final_estimator=GradientBoostingRegressor(random_state=42))
X, y = load_boston(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
reg.fit(X_train, y_train)
StackingRegressor(...)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
3.任何估計器特徵的重要性
如今,任何適合的Scikit-learn估計器均可以使用基於置換的重要性特徵。從用戶指南中描述如何計算功能的排列重要性:
特徵排列重要性計算方式以下:首先,在X定義的數據集上評估經過評分定義的基線度量。接着,對驗證集中的要素列進行置換,並再次評估度量。排列重要性定義爲基線度量和來自特徵列度量之間的差別。
發行說明中的完整示例:
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
X, y = make_classification(random_state=0, n_features=5, n_informative=3)
rf = RandomForestClassifier(random_state=0).fit(X, y)
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()
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
4.梯度提高缺失價值支持
梯度提高分類器和迴歸器如今都已經具有了處理缺失值的能力,從而消除了手動插補的須要。如下是遺漏的方式:
在訓練過程當中,樹木種植者會根據潛在的收益,在每一個分割點上了解缺失值的樣本應歸子級左仍是右子級。進行預測時,所以將具備缺失值的樣本分配給左或右的子級。若是在訓練過程當中沒有遇到給定特徵的缺失值,則將具備缺失值的樣本映射到樣本最多的那一方。
如下示例演示:
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))
[0 0 1 1]
5.基於KNN的缺失值估算
如今,梯度加強自己就支持缺失值插補,但能夠使用K近鄰插值器在任何數據集上執行顯式插補。只要在訓練集中,n個最近鄰居的平均值就推算出每一個缺失值,只要兩個樣本都不缺失的特徵就近了。歐式距離是使用的默認距離度量。
一個例子:
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))
[[1,2,4]
[3,4,3]
[5.5 ,6,5]
[8,8,7]]
最新版本的Scikit-learn中有更多功能,這裏就不作過多介紹了。你能夠去官網獲取更多的信息!
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
獲取更多優質內容,可前往:疫情當下,宅家也能好好提高本身,爲將來蓄能——蓄勢待發!