# 如何在 Python 中開發可重復使用的抽樣檢查算法框架
> 譯文: [https://machinelearningmastery.com/spot-check-machine-learning-algorithms-in-python/](https://machinelearningmastery.com/spot-check-machine-learning-algorithms-in-python/)
[抽樣檢查算法](https://machinelearningmastery.com/spot-check-classification-machine-learning-algorithms-python-scikit-learn/)是一種應用機器學習技術,旨在快速客觀地為新的預測建模問題提供第一組結果。
與尋找算法的最佳算法或最佳配置的網格搜索和其他類型的算法調整不同,點檢查旨在快速評估各種算法并提供粗略的第一切結果。如果問題或問題表示確實是可預測的,則可以使用該第一剪切結果,如果是,則可能值得進一步研究該問題的算法類型。
現場檢查是一種幫助克服應用機器學習的“[難題](https://machinelearningmastery.com/applied-machine-learning-is-hard/)”的方法,并鼓勵您清楚地考慮在任何機器學習項目中執行的[高階搜索問題](https://machinelearningmastery.com/applied-machine-learning-as-a-search-problem/) 。
在本教程中,您將發現現場檢查算法對新預測建模問題的有用性,以及如何在 python 中為分類和回歸問題開發用于抽樣檢查算法的標準框架。
完成本教程后,您將了解:
* 抽樣檢查提供了一種快速發現在預測建模問題上表現良好的算法類型的方法。
* 如何開發用于加載數據,定義模型,評估模型和總結結果的通用框架。
* 如何應用框架進行分類和回歸問題。
讓我們開始吧。

如何在 Python 中開發可重復使用的抽樣檢查算法框架
[Jeff Turner](https://www.flickr.com/photos/respres/16216077206/) 的照片,保留一些權利。
## 教程概述
本教程分為五個部分;他們是:
1. 抽樣檢查算法
2. Python 中的 Spot-Checking 框架
3. 現場檢查分類
4. 現場檢查回歸
5. 框架擴展
## 1.抽樣檢查算法
我們事先無法知道哪些算法在給定的預測建模問題上表現良好。
這是應用機器學習的[難點部分,只能通過系統實驗來解決。](https://machinelearningmastery.com/applied-machine-learning-is-hard/)
[抽查](https://machinelearningmastery.com/why-you-should-be-spot-checking-algorithms-on-your-machine-learning-problems/)是解決這個問題的方法。
它涉及針對問題快速測試大量不同的機器學習算法,以便快速發現哪些算法可能起作用以及在哪里集中注意力。
* **速度很快**;它繞過準備和分析的幾天或幾周,并使用可能不會導致結果的算法。
* **它是客觀的**,允許您發現什么可能適用于問題,而不是使用您上次使用的。
* **得到結果**;您將實際擬合模型,進行預測并了解您的問題是否可以預測以及基線技能可能是什么樣子。
抽樣檢查可能需要您使用數據集的一小部分樣本才能快速轉換結果。
最后,現場檢查的結果是一個起點。一個起點。他們建議將注意力集中在問題上,而不是最佳算法。該過程旨在讓您擺脫典型的思考和分析,轉而關注結果。
您可以在帖子中了解有關抽查的更多信息:
* [為什么你應該在機器學習問題上進行抽樣檢查算法](https://machinelearningmastery.com/why-you-should-be-spot-checking-algorithms-on-your-machine-learning-problems/)
現在我們知道了什么是現場檢查,讓我們看看如何在 Python 中系統地執行抽樣檢查。
## 2\. Python 中的 Spot-Checking 框架
在本節中,我們將構建一個腳本框架,該框架可用于在分類或回歸問題上對機器學習算法進行抽樣檢查。
我們需要開發框架有四個部分;他們是:
* 加載數據集
* 定義模型
* 評估模型
* 總結結果
讓我們依次看看每一個。
### 加載數據集
框架的第一步是加載數據。
必須針對給定問題實現該功能,并專門針對該問題。它可能涉及從一個或多個 CSV 文件加載數據。
我們將調用此函數 _load_data()_;它不需要參數并返回輸入( _X_ )和輸出( _y_ )用于預測問題。
```
# load the dataset, returns X and y elements
def load_dataset():
X, y = None, None
return X, y
```
### 定義模型
下一步是定義模型以評估預測建模問題。
定義的模型將特定于類型預測建模問題,例如,分類或回歸。
定義的模型應該是多樣的,包括以下的混合:
* 線性模型。
* 非線性模型。
* 合奏模型。
每個模型應該是一個很好的機會,可以很好地解決問題。這可能意味著提供模型的一些變體,使用不同的常見或眾所周知的配置,平均表現良好。
我們將調用此函數 _define_models()_。它將返回映射到 scikit-learn 模型對象的模型名稱字典。名稱應該很短,例如' _svm_ ',并且可以包括配置細節,例如“KNN-7”。
該函數還將字典作為可選參數;如果未提供,則創建并填充新字典。如果提供了字典,則會向其添加模型。
如果您希望使用多個函數來定義模型,或者添加具有不同配置的特定類型的大量模型,則可以增加靈活性。
```
# create a dict of standard models to evaluate {name:object}
def define_models(models=dict()):
# ...
return models
```
我們的想法不是網格搜索模型參數;那可以晚點來。
相反,每個模型應該有機會表現良好(即不是最佳)。這可能意味著在某些情況下嘗試許多參數組合,例如在梯度增強的情況下。
### 評估模型
下一步是評估已加載數據集上的已定義模型。
scikit-learn 庫提供了在評估期間管道模型的能力。這允許在用于擬合模型之前變換數據,并且這以正確的方式完成,使得變換在訓練數據上準備并應用于測試數據。
我們可以定義一個函數,在評估之前準備給定的模型,以允許在抽樣檢查過程中使用特定的變換。它們將以一攬子方式對所有模型進行。這對于執行標準化,規范化和特征選擇等操作非常有用。
我們將定義一個名為 _make_pipeline()_ 的函數,它接受一個已定義的模型并返回一個管道。下面是準備管道的示例,該管道將首先標準化輸入數據,然后在擬合模型之前對其進行標準化。
```
# create a feature preparation pipeline for a model
def make_pipeline(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
```
此函數可以擴展為添加其他變換,或者簡化為返回提供的模型而不進行變換。
現在我們需要評估準備好的模型。
我們將使用 k-fold 交叉驗證評估模型的標準。對每個定義的模型的評估將產生結果列表。這是因為該模型的 10 個不同版本將被擬合和評估,從而得到 k 分數列表。
我們將定義一個名為 _evaluate_model()_ 的函數,該函數將獲取數據,定義的模型,多個折疊以及用于評估結果的表現指標。它將返回分數列表。
該函數調用 _make_pipeline()_ 為定義的模型準備所需的任何數據變換,然后調用 [cross_val_score()](http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.cross_val_score.html) scikit-learn 函數。重要的是, _n_jobs_ 參數設置為-1,以允許模型評估并行發生,從而利用硬件上可用的核心數量。
```
# evaluate a single model
def evaluate_model(X, y, model, folds, metric):
# create the pipeline
pipeline = make_pipeline(model)
# evaluate model
scores = cross_val_score(pipeline, X, y, scoring=metric, cv=folds, n_jobs=-1)
return scores
```
模型的評估可能會因異常而失敗。我已經看到了這一點,特別是在 statsmodels 庫中的一些模型的情況下。
評估模型也可能產生大量警告信息。我已經看到了這一點,特別是在使用 XGBoost 模型的情況下。
在抽查時我們不關心異常或警告。我們只想知道哪些有效,哪些有效。因此,我們可以在評估每個模型時捕獲異常并忽略所有警告。
名為 _robust_evaluate_model()_ 的函數實現了此行為。 _evaluate_model()_ 的調用方式是捕獲異常并忽略警告。如果發生異常并且給定模型無法得到結果,則返回 _ 無 _ 結果。
```
# evaluate a model and try to trap errors and and hide warnings
def robust_evaluate_model(X, y, model, folds, metric):
scores = None
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
scores = evaluate_model(X, y, model, folds, metric)
except:
scores = None
return scores
```
最后,我們可以定義頂級函數來評估已定義模型的列表。
我們將定義一個名為 _evaluate_models()_ 的函數,它將模型字典作為參數,并將模型名稱字典返回到結果列表。
交叉驗證過程中的折疊數可以由默認為 10 的可選參數指定。根據模型的預測計算的度量也可以由可選參數指定,默認為分類精度。
有關支持的指標的完整列表,請參閱此列表:
* [評分參數:定義模型評估規則,scikit-learn](http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter) 。
跳過任何無結果,不會將其添加到結果字典中。
重要的是,我們提供了一些詳細的輸出,總結了每個模型評估后的平均值和標準差。如果數據集上的抽樣檢查過程需要幾分鐘到幾小時,這將非常有用。
```
# evaluate a dict of models {name:object}, returns {name:score}
def evaluate_models(X, y, models, folds=10, metric='accuracy'):
results = dict()
for name, model in models.items():
# evaluate the model
scores = robust_evaluate_model(X, y, model, folds, metric)
# show process
if scores is not None:
# store a result
results[name] = scores
mean_score, std_score = mean(scores), std(scores)
print('>%s: %.3f (+/-%.3f)' % (name, mean_score, std_score))
else:
print('>%s: error' % name)
return results
```
請注意,如果由于某種原因您想要查看警告和錯誤,您可以更新 _evaluate_models()_ 以直接調用 _evaluate_model()_ 函數,繞過強大的錯誤處理。在測試靜默失敗的新方法或方法配置時,我發現這很有用。
### 總結結果
最后,我們可以評估結果。
真的,我們只想知道哪些算法表現良好。
總結結果的兩種有用方法是:
1. 排名前 10 位的算法的平均值和標準差的線摘要。
2. 前 10 名執行算法的框和胡須圖。
線條摘要快速而精確,但假設表現良好的高斯分布,這可能不合理。
盒子和須狀圖假設沒有分布,并提供了一種直觀的方法,可以直接比較模型的分數在中位數表現和分數差異方面的分布。
我們將定義一個名為 _summarize_results()_ 的函數,該函數獲取結果字典,打印結果摘要,并創建保存到文件的 boxplot 圖像。該函數接受一個參數來指定評估得分是否最大化,默認情況下為 _True_ 。要匯總的結果數也可以作為可選參數提供,默認為 10。
該功能首先在打印摘要和創建框和須圖之前對得分進行排序。
```
# print and plot the top n results
def summarize_results(results, maximize=True, top_n=10):
# check for no results
if len(results) == 0:
print('no results')
return
# determine how many results to summarize
n = min(top_n, len(results))
# create a list of (name, mean(scores)) tuples
mean_scores = [(k,mean(v)) for k,v in results.items()]
# sort tuples by mean score
mean_scores = sorted(mean_scores, key=lambda x: x[1])
# reverse for descending order (e.g. for accuracy)
if maximize:
mean_scores = list(reversed(mean_scores))
# retrieve the top n for summarization
names = [x[0] for x in mean_scores[:n]]
scores = [results[x[0]] for x in mean_scores[:n]]
# print the top n
print()
for i in range(n):
name = names[i]
mean_score, std_score = mean(results[name]), std(results[name])
print('Rank=%d, Name=%s, Score=%.3f (+/- %.3f)' % (i+1, name, mean_score, std_score))
# boxplot for the top n
pyplot.boxplot(scores, labels=names)
_, labels = pyplot.xticks()
pyplot.setp(labels, rotation=90)
pyplot.savefig('spotcheck.png')
```
現在我們已經專門設計了一個用于 Python 中的抽樣算法的框架,讓我們看一下如何將它應用于分類問題。
## 3.現場檢查分類
我們將使用 [make_classification()函數](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_classification.html)生成二元分類問題。
該函數將生成 1,000 個樣本,包含 20 個變量,一些冗余變量和兩個類。
```
# load the dataset, returns X and y elements
def load_dataset():
return make_classification(n_samples=1000, n_classes=2, random_state=1)
```
作為分類問題,我們將嘗試一套分類算法,具體來說:
### 線性算法
* Logistic 回歸
* 嶺回歸
* 隨機梯度下降分類器
* 被動攻擊性分類器
我嘗試了 LDA 和 QDA,但他們遺憾地在某處的 C 代碼中崩潰了。
### 非線性算法
* k-最近鄰居
* 分類和回歸樹
* 額外的樹
* 支持向量機
* 樸素貝葉斯
### 集合算法
* AdaBoost 的
* 袋裝決策樹
* 隨機森林
* 額外的樹木
* 梯度增壓機
此外,我為一些算法添加了多種配置,如 Ridge,kNN 和 SVM,以便為他們提供很好的解決問題的機會。
下面列出了完整的 _define_models()_ 函數。
```
# create a dict of standard models to evaluate {name:object}
def define_models(models=dict()):
# linear models
models['logistic'] = LogisticRegression()
alpha = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for a in alpha:
models['ridge-'+str(a)] = RidgeClassifier(alpha=a)
models['sgd'] = SGDClassifier(max_iter=1000, tol=1e-3)
models['pa'] = PassiveAggressiveClassifier(max_iter=1000, tol=1e-3)
# non-linear models
n_neighbors = range(1, 21)
for k in n_neighbors:
models['knn-'+str(k)] = KNeighborsClassifier(n_neighbors=k)
models['cart'] = DecisionTreeClassifier()
models['extra'] = ExtraTreeClassifier()
models['svml'] = SVC(kernel='linear')
models['svmp'] = SVC(kernel='poly')
c_values = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for c in c_values:
models['svmr'+str(c)] = SVC(C=c)
models['bayes'] = GaussianNB()
# ensemble models
n_trees = 100
models['ada'] = AdaBoostClassifier(n_estimators=n_trees)
models['bag'] = BaggingClassifier(n_estimators=n_trees)
models['rf'] = RandomForestClassifier(n_estimators=n_trees)
models['et'] = ExtraTreesClassifier(n_estimators=n_trees)
models['gbm'] = GradientBoostingClassifier(n_estimators=n_trees)
print('Defined %d models' % len(models))
return models
```
而已;我們現在準備好檢查問題的算法。
下面列出了完整的示例。
```
# binary classification spot check script
import warnings
from numpy import mean
from numpy import std
from matplotlib import pyplot
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import RidgeClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import ExtraTreeClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import GradientBoostingClassifier
# load the dataset, returns X and y elements
def load_dataset():
return make_classification(n_samples=1000, n_classes=2, random_state=1)
# create a dict of standard models to evaluate {name:object}
def define_models(models=dict()):
# linear models
models['logistic'] = LogisticRegression()
alpha = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for a in alpha:
models['ridge-'+str(a)] = RidgeClassifier(alpha=a)
models['sgd'] = SGDClassifier(max_iter=1000, tol=1e-3)
models['pa'] = PassiveAggressiveClassifier(max_iter=1000, tol=1e-3)
# non-linear models
n_neighbors = range(1, 21)
for k in n_neighbors:
models['knn-'+str(k)] = KNeighborsClassifier(n_neighbors=k)
models['cart'] = DecisionTreeClassifier()
models['extra'] = ExtraTreeClassifier()
models['svml'] = SVC(kernel='linear')
models['svmp'] = SVC(kernel='poly')
c_values = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for c in c_values:
models['svmr'+str(c)] = SVC(C=c)
models['bayes'] = GaussianNB()
# ensemble models
n_trees = 100
models['ada'] = AdaBoostClassifier(n_estimators=n_trees)
models['bag'] = BaggingClassifier(n_estimators=n_trees)
models['rf'] = RandomForestClassifier(n_estimators=n_trees)
models['et'] = ExtraTreesClassifier(n_estimators=n_trees)
models['gbm'] = GradientBoostingClassifier(n_estimators=n_trees)
print('Defined %d models' % len(models))
return models
# create a feature preparation pipeline for a model
def make_pipeline(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# evaluate a single model
def evaluate_model(X, y, model, folds, metric):
# create the pipeline
pipeline = make_pipeline(model)
# evaluate model
scores = cross_val_score(pipeline, X, y, scoring=metric, cv=folds, n_jobs=-1)
return scores
# evaluate a model and try to trap errors and and hide warnings
def robust_evaluate_model(X, y, model, folds, metric):
scores = None
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
scores = evaluate_model(X, y, model, folds, metric)
except:
scores = None
return scores
# evaluate a dict of models {name:object}, returns {name:score}
def evaluate_models(X, y, models, folds=10, metric='accuracy'):
results = dict()
for name, model in models.items():
# evaluate the model
scores = robust_evaluate_model(X, y, model, folds, metric)
# show process
if scores is not None:
# store a result
results[name] = scores
mean_score, std_score = mean(scores), std(scores)
print('>%s: %.3f (+/-%.3f)' % (name, mean_score, std_score))
else:
print('>%s: error' % name)
return results
# print and plot the top n results
def summarize_results(results, maximize=True, top_n=10):
# check for no results
if len(results) == 0:
print('no results')
return
# determine how many results to summarize
n = min(top_n, len(results))
# create a list of (name, mean(scores)) tuples
mean_scores = [(k,mean(v)) for k,v in results.items()]
# sort tuples by mean score
mean_scores = sorted(mean_scores, key=lambda x: x[1])
# reverse for descending order (e.g. for accuracy)
if maximize:
mean_scores = list(reversed(mean_scores))
# retrieve the top n for summarization
names = [x[0] for x in mean_scores[:n]]
scores = [results[x[0]] for x in mean_scores[:n]]
# print the top n
print()
for i in range(n):
name = names[i]
mean_score, std_score = mean(results[name]), std(results[name])
print('Rank=%d, Name=%s, Score=%.3f (+/- %.3f)' % (i+1, name, mean_score, std_score))
# boxplot for the top n
pyplot.boxplot(scores, labels=names)
_, labels = pyplot.xticks()
pyplot.setp(labels, rotation=90)
pyplot.savefig('spotcheck.png')
# load dataset
X, y = load_dataset()
# get model list
models = define_models()
# evaluate models
results = evaluate_models(X, y, models)
# summarize results
summarize_results(results)
```
運行該示例為每個評估模型打印一行,結束對問題的前 10 個執行算法的摘要。
我們可以看到決策樹的集合對這個問題表現最好。這表明了一些事情:
* 決策樹的集合可能是集中注意力的好地方。
* 如果進一步調整,梯度提升可能會很好。
* 該問題的“良好”表現是準確度約為 86%。
* 嶺回歸的相對較高的表現表明需要進行特征選擇。
```
...
>bag: 0.862 (+/-0.034)
>rf: 0.865 (+/-0.033)
>et: 0.858 (+/-0.035)
>gbm: 0.867 (+/-0.044)
Rank=1, Name=gbm, Score=0.867 (+/- 0.044)
Rank=2, Name=rf, Score=0.865 (+/- 0.033)
Rank=3, Name=bag, Score=0.862 (+/- 0.034)
Rank=4, Name=et, Score=0.858 (+/- 0.035)
Rank=5, Name=ada, Score=0.850 (+/- 0.035)
Rank=6, Name=ridge-0.9, Score=0.848 (+/- 0.038)
Rank=7, Name=ridge-0.8, Score=0.848 (+/- 0.038)
Rank=8, Name=ridge-0.7, Score=0.848 (+/- 0.038)
Rank=9, Name=ridge-0.6, Score=0.848 (+/- 0.038)
Rank=10, Name=ridge-0.5, Score=0.848 (+/- 0.038)
```
還創建了一個盒子和胡須圖,以總結前 10 個表現良好的算法的結果。
該圖顯示了由決策樹集合組成的方法的高程。該情節強調了進一步關注這些方法將是個好主意的觀念。

分類問題前 10 個抽樣檢驗算法的箱線圖
如果這是一個真正的分類問題,我會跟進進一步的抽查,例如:
* 使用各種不同的特征選擇方法進行抽查。
* 無需數據縮放方法的抽查。
* 使用 sklearn 或 XGBoost 中的梯度增強配置的課程網格進行抽查。
接下來,我們將看到如何將框架應用于回歸問題。
## 4.現場檢查回歸
我們可以通過非常小的變化來探索回歸預測建模問題的相同框架。
我們可以使用 [make_regression()函數](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_regression.html#sklearn.datasets.make_regression)來生成一個人為的回歸問題,包括 1,000 個示例和 50 個特征,其中一些是冗余的。
定義的 _load_dataset()_ 功能如下所示。
```
# load the dataset, returns X and y elements
def load_dataset():
return make_regression(n_samples=1000, n_features=50, noise=0.1, random_state=1)
```
然后我們可以指定一個 _get_models()_ 函數來定義一套回歸方法。
Scikit-learn 提供了廣泛的線性回歸方法,非常出色。并非所有這些都可能是您的問題所必需的。我建議使用最小的線性回歸和彈性網,后者有一套很好的 alpha 和 lambda 參數。
不過,我們將測試有關此問題的全套方法,包括:
### Linear Algorithms
* 線性回歸
* 套索回歸
* 嶺回歸
* 彈性網絡回歸
* 胡貝爾回歸
* LARS 回歸
* Lasso LARS 回歸
* 被動攻擊性回歸
* RANSAC 回歸量
* 隨機梯度下降回歸
* Theil 回歸
### Nonlinear Algorithms
* k-最近鄰居
* 分類和回歸樹
* 額外的樹
* 支持向量回歸
### Ensemble Algorithms
* AdaBoost 的
* 袋裝決策樹
* 隨機森林
* 額外的樹木
* 梯度增壓機
完整的 _get_models()_ 功能如下所示。
```
# create a dict of standard models to evaluate {name:object}
def get_models(models=dict()):
# linear models
models['lr'] = LinearRegression()
alpha = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for a in alpha:
models['lasso-'+str(a)] = Lasso(alpha=a)
for a in alpha:
models['ridge-'+str(a)] = Ridge(alpha=a)
for a1 in alpha:
for a2 in alpha:
name = 'en-' + str(a1) + '-' + str(a2)
models[name] = ElasticNet(a1, a2)
models['huber'] = HuberRegressor()
models['lars'] = Lars()
models['llars'] = LassoLars()
models['pa'] = PassiveAggressiveRegressor(max_iter=1000, tol=1e-3)
models['ranscac'] = RANSACRegressor()
models['sgd'] = SGDRegressor(max_iter=1000, tol=1e-3)
models['theil'] = TheilSenRegressor()
# non-linear models
n_neighbors = range(1, 21)
for k in n_neighbors:
models['knn-'+str(k)] = KNeighborsRegressor(n_neighbors=k)
models['cart'] = DecisionTreeRegressor()
models['extra'] = ExtraTreeRegressor()
models['svml'] = SVR(kernel='linear')
models['svmp'] = SVR(kernel='poly')
c_values = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for c in c_values:
models['svmr'+str(c)] = SVR(C=c)
# ensemble models
n_trees = 100
models['ada'] = AdaBoostRegressor(n_estimators=n_trees)
models['bag'] = BaggingRegressor(n_estimators=n_trees)
models['rf'] = RandomForestRegressor(n_estimators=n_trees)
models['et'] = ExtraTreesRegressor(n_estimators=n_trees)
models['gbm'] = GradientBoostingRegressor(n_estimators=n_trees)
print('Defined %d models' % len(models))
return models
```
默認情況下,框架使用分類準確性作為評估模型預測的方法。
這對回歸沒有意義,我們可以改變這對回歸更有意義的東西,例如均方誤差。我們可以通過在調用 _evaluate_models()_ 函數時傳遞 _metric ='neg_mean_squared_error'_ 參數來做到這一點。
```
# evaluate models
results = evaluate_models(models, metric='neg_mean_squared_error')
```
請注意,默認情況下,scikit-learn 會反轉錯誤分數,以便最大化而不是最小化。這就是為什么均方誤差為負,并在匯總時會有負號。因為分數被反轉,我們可以繼續假設我們在 _summarize_results()_ 函數中最大化分數,并且不需要像我們在使用時所預期的那樣指定 _maximize = False_ 。錯誤指標。
完整的代碼示例如下所示。
```
# regression spot check script
import warnings
from numpy import mean
from numpy import std
from matplotlib import pyplot
from sklearn.datasets import make_regression
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import HuberRegressor
from sklearn.linear_model import Lars
from sklearn.linear_model import LassoLars
from sklearn.linear_model import PassiveAggressiveRegressor
from sklearn.linear_model import RANSACRegressor
from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import TheilSenRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import ExtraTreeRegressor
from sklearn.svm import SVR
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import GradientBoostingRegressor
# load the dataset, returns X and y elements
def load_dataset():
return make_regression(n_samples=1000, n_features=50, noise=0.1, random_state=1)
# create a dict of standard models to evaluate {name:object}
def get_models(models=dict()):
# linear models
models['lr'] = LinearRegression()
alpha = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for a in alpha:
models['lasso-'+str(a)] = Lasso(alpha=a)
for a in alpha:
models['ridge-'+str(a)] = Ridge(alpha=a)
for a1 in alpha:
for a2 in alpha:
name = 'en-' + str(a1) + '-' + str(a2)
models[name] = ElasticNet(a1, a2)
models['huber'] = HuberRegressor()
models['lars'] = Lars()
models['llars'] = LassoLars()
models['pa'] = PassiveAggressiveRegressor(max_iter=1000, tol=1e-3)
models['ranscac'] = RANSACRegressor()
models['sgd'] = SGDRegressor(max_iter=1000, tol=1e-3)
models['theil'] = TheilSenRegressor()
# non-linear models
n_neighbors = range(1, 21)
for k in n_neighbors:
models['knn-'+str(k)] = KNeighborsRegressor(n_neighbors=k)
models['cart'] = DecisionTreeRegressor()
models['extra'] = ExtraTreeRegressor()
models['svml'] = SVR(kernel='linear')
models['svmp'] = SVR(kernel='poly')
c_values = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for c in c_values:
models['svmr'+str(c)] = SVR(C=c)
# ensemble models
n_trees = 100
models['ada'] = AdaBoostRegressor(n_estimators=n_trees)
models['bag'] = BaggingRegressor(n_estimators=n_trees)
models['rf'] = RandomForestRegressor(n_estimators=n_trees)
models['et'] = ExtraTreesRegressor(n_estimators=n_trees)
models['gbm'] = GradientBoostingRegressor(n_estimators=n_trees)
print('Defined %d models' % len(models))
return models
# create a feature preparation pipeline for a model
def make_pipeline(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# evaluate a single model
def evaluate_model(X, y, model, folds, metric):
# create the pipeline
pipeline = make_pipeline(model)
# evaluate model
scores = cross_val_score(pipeline, X, y, scoring=metric, cv=folds, n_jobs=-1)
return scores
# evaluate a model and try to trap errors and and hide warnings
def robust_evaluate_model(X, y, model, folds, metric):
scores = None
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
scores = evaluate_model(X, y, model, folds, metric)
except:
scores = None
return scores
# evaluate a dict of models {name:object}, returns {name:score}
def evaluate_models(X, y, models, folds=10, metric='accuracy'):
results = dict()
for name, model in models.items():
# evaluate the model
scores = robust_evaluate_model(X, y, model, folds, metric)
# show process
if scores is not None:
# store a result
results[name] = scores
mean_score, std_score = mean(scores), std(scores)
print('>%s: %.3f (+/-%.3f)' % (name, mean_score, std_score))
else:
print('>%s: error' % name)
return results
# print and plot the top n results
def summarize_results(results, maximize=True, top_n=10):
# check for no results
if len(results) == 0:
print('no results')
return
# determine how many results to summarize
n = min(top_n, len(results))
# create a list of (name, mean(scores)) tuples
mean_scores = [(k,mean(v)) for k,v in results.items()]
# sort tuples by mean score
mean_scores = sorted(mean_scores, key=lambda x: x[1])
# reverse for descending order (e.g. for accuracy)
if maximize:
mean_scores = list(reversed(mean_scores))
# retrieve the top n for summarization
names = [x[0] for x in mean_scores[:n]]
scores = [results[x[0]] for x in mean_scores[:n]]
# print the top n
print()
for i in range(n):
name = names[i]
mean_score, std_score = mean(results[name]), std(results[name])
print('Rank=%d, Name=%s, Score=%.3f (+/- %.3f)' % (i+1, name, mean_score, std_score))
# boxplot for the top n
pyplot.boxplot(scores, labels=names)
_, labels = pyplot.xticks()
pyplot.setp(labels, rotation=90)
pyplot.savefig('spotcheck.png')
# load dataset
X, y = load_dataset()
# get model list
models = get_models()
# evaluate models
results = evaluate_models(X, y, models, metric='neg_mean_squared_error')
# summarize results
summarize_results(results)
```
運行該示例總結了所評估的每個模型的表現,然后打印出前 10 個表現良好的算法的表現。
我們可以看到許多線性算法可能在這個問題上找到了相同的最優解。值得注意的是,那些表現良好的方法使用正則化作為一種??特征選擇,允許他們放大最佳解決方案。
這將表明在對此問題進行建模時特征選擇的重要性,并且線性方法將成為關注的領域,至少目前是這樣。
查看評估模型的打印分數還顯示了對此問題執行的非線性和集合算法的差異程度。
```
...
>bag: -6118.084 (+/-1558.433)
>rf: -6127.169 (+/-1594.392)
>et: -5017.062 (+/-1037.673)
>gbm: -2347.807 (+/-500.364)
Rank=1, Name=lars, Score=-0.011 (+/- 0.001)
Rank=2, Name=ranscac, Score=-0.011 (+/- 0.001)
Rank=3, Name=lr, Score=-0.011 (+/- 0.001)
Rank=4, Name=ridge-0.0, Score=-0.011 (+/- 0.001)
Rank=5, Name=en-0.0-0.1, Score=-0.011 (+/- 0.001)
Rank=6, Name=en-0.0-0.8, Score=-0.011 (+/- 0.001)
Rank=7, Name=en-0.0-0.2, Score=-0.011 (+/- 0.001)
Rank=8, Name=en-0.0-0.7, Score=-0.011 (+/- 0.001)
Rank=9, Name=en-0.0-0.0, Score=-0.011 (+/- 0.001)
Rank=10, Name=en-0.0-0.3, Score=-0.011 (+/- 0.001)
```
在這種情況下,會創建一個盒子和胡須圖,而不是真正為結果分析增加價值。

回歸問題前 10 個點檢算法的箱形圖
## 5.框架擴展
在本節中,我們將探討抽樣檢查框架的一些方便擴展。
### 課程網格搜索梯度提升
我發現自己使用 XGBoost 和梯度提升很多直接分類和回歸問題。
因此,我喜歡在抽樣檢查時使用方法的標準配置參數的課程網格。
下面是一個可以直接在現場檢查框架中使用的功能。
```
# define gradient boosting models
def define_gbm_models(models=dict(), use_xgb=True):
# define config ranges
rates = [0.001, 0.01, 0.1]
trees = [50, 100]
ss = [0.5, 0.7, 1.0]
depth = [3, 7, 9]
# add configurations
for l in rates:
for e in trees:
for s in ss:
for d in depth:
cfg = [l, e, s, d]
if use_xgb:
name = 'xgb-' + str(cfg)
models[name] = XGBClassifier(learning_rate=l, n_estimators=e, subsample=s, max_depth=d)
else:
name = 'gbm-' + str(cfg)
models[name] = GradientBoostingClassifier(learning_rate=l, n_estimators=e, subsample=s, max_depth=d)
print('Defined %d models' % len(models))
return models
```
默認情況下,該函數將使用 XGBoost 模型,但如果函數的 _use_xgb_ 參數設置為 _False_ ,則可以使用 sklearn 梯度增強模型。
同樣,我們并沒有嘗試在問題上優化調整 GBM,只是很快找到配置空間中可能值得進一步調查的區域。
此功能可以直接用于分類和回歸問題,只需稍微改變“ _XGBClassifier_ ”到“ _XGBRegressor_ ”和“ _GradientBoostingClassifier_ ”到“[HTG6” ] GradientBoostingRegressor “。例如:
```
# define gradient boosting models
def get_gbm_models(models=dict(), use_xgb=True):
# define config ranges
rates = [0.001, 0.01, 0.1]
trees = [50, 100]
ss = [0.5, 0.7, 1.0]
depth = [3, 7, 9]
# add configurations
for l in rates:
for e in trees:
for s in ss:
for d in depth:
cfg = [l, e, s, d]
if use_xgb:
name = 'xgb-' + str(cfg)
models[name] = XGBRegressor(learning_rate=l, n_estimators=e, subsample=s, max_depth=d)
else:
name = 'gbm-' + str(cfg)
models[name] = GradientBoostingXGBRegressor(learning_rate=l, n_estimators=e, subsample=s, max_depth=d)
print('Defined %d models' % len(models))
return models
```
為了具體化,下面是更新的二元分類示例,以定義 XGBoost 模型。
```
# binary classification spot check script
import warnings
from numpy import mean
from numpy import std
from matplotlib import pyplot
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import RidgeClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import ExtraTreeClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import GradientBoostingClassifier
from xgboost import XGBClassifier
# load the dataset, returns X and y elements
def load_dataset():
return make_classification(n_samples=1000, n_classes=2, random_state=1)
# create a dict of standard models to evaluate {name:object}
def define_models(models=dict()):
# linear models
models['logistic'] = LogisticRegression()
alpha = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for a in alpha:
models['ridge-'+str(a)] = RidgeClassifier(alpha=a)
models['sgd'] = SGDClassifier(max_iter=1000, tol=1e-3)
models['pa'] = PassiveAggressiveClassifier(max_iter=1000, tol=1e-3)
# non-linear models
n_neighbors = range(1, 21)
for k in n_neighbors:
models['knn-'+str(k)] = KNeighborsClassifier(n_neighbors=k)
models['cart'] = DecisionTreeClassifier()
models['extra'] = ExtraTreeClassifier()
models['svml'] = SVC(kernel='linear')
models['svmp'] = SVC(kernel='poly')
c_values = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for c in c_values:
models['svmr'+str(c)] = SVC(C=c)
models['bayes'] = GaussianNB()
# ensemble models
n_trees = 100
models['ada'] = AdaBoostClassifier(n_estimators=n_trees)
models['bag'] = BaggingClassifier(n_estimators=n_trees)
models['rf'] = RandomForestClassifier(n_estimators=n_trees)
models['et'] = ExtraTreesClassifier(n_estimators=n_trees)
models['gbm'] = GradientBoostingClassifier(n_estimators=n_trees)
print('Defined %d models' % len(models))
return models
# define gradient boosting models
def define_gbm_models(models=dict(), use_xgb=True):
# define config ranges
rates = [0.001, 0.01, 0.1]
trees = [50, 100]
ss = [0.5, 0.7, 1.0]
depth = [3, 7, 9]
# add configurations
for l in rates:
for e in trees:
for s in ss:
for d in depth:
cfg = [l, e, s, d]
if use_xgb:
name = 'xgb-' + str(cfg)
models[name] = XGBClassifier(learning_rate=l, n_estimators=e, subsample=s, max_depth=d)
else:
name = 'gbm-' + str(cfg)
models[name] = GradientBoostingClassifier(learning_rate=l, n_estimators=e, subsample=s, max_depth=d)
print('Defined %d models' % len(models))
return models
# create a feature preparation pipeline for a model
def make_pipeline(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# evaluate a single model
def evaluate_model(X, y, model, folds, metric):
# create the pipeline
pipeline = make_pipeline(model)
# evaluate model
scores = cross_val_score(pipeline, X, y, scoring=metric, cv=folds, n_jobs=-1)
return scores
# evaluate a model and try to trap errors and and hide warnings
def robust_evaluate_model(X, y, model, folds, metric):
scores = None
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
scores = evaluate_model(X, y, model, folds, metric)
except:
scores = None
return scores
# evaluate a dict of models {name:object}, returns {name:score}
def evaluate_models(X, y, models, folds=10, metric='accuracy'):
results = dict()
for name, model in models.items():
# evaluate the model
scores = robust_evaluate_model(X, y, model, folds, metric)
# show process
if scores is not None:
# store a result
results[name] = scores
mean_score, std_score = mean(scores), std(scores)
print('>%s: %.3f (+/-%.3f)' % (name, mean_score, std_score))
else:
print('>%s: error' % name)
return results
# print and plot the top n results
def summarize_results(results, maximize=True, top_n=10):
# check for no results
if len(results) == 0:
print('no results')
return
# determine how many results to summarize
n = min(top_n, len(results))
# create a list of (name, mean(scores)) tuples
mean_scores = [(k,mean(v)) for k,v in results.items()]
# sort tuples by mean score
mean_scores = sorted(mean_scores, key=lambda x: x[1])
# reverse for descending order (e.g. for accuracy)
if maximize:
mean_scores = list(reversed(mean_scores))
# retrieve the top n for summarization
names = [x[0] for x in mean_scores[:n]]
scores = [results[x[0]] for x in mean_scores[:n]]
# print the top n
print()
for i in range(n):
name = names[i]
mean_score, std_score = mean(results[name]), std(results[name])
print('Rank=%d, Name=%s, Score=%.3f (+/- %.3f)' % (i+1, name, mean_score, std_score))
# boxplot for the top n
pyplot.boxplot(scores, labels=names)
_, labels = pyplot.xticks()
pyplot.setp(labels, rotation=90)
pyplot.savefig('spotcheck.png')
# load dataset
X, y = load_dataset()
# get model list
models = define_models()
# add gbm models
models = define_gbm_models(models)
# evaluate models
results = evaluate_models(X, y, models)
# summarize results
summarize_results(results)
```
運行該示例表明,確實有些 XGBoost 模型在問題上表現良好。
```
...
>xgb-[0.1, 100, 1.0, 3]: 0.864 (+/-0.044)
>xgb-[0.1, 100, 1.0, 7]: 0.865 (+/-0.036)
>xgb-[0.1, 100, 1.0, 9]: 0.867 (+/-0.039)
Rank=1, Name=xgb-[0.1, 50, 1.0, 3], Score=0.872 (+/- 0.039)
Rank=2, Name=et, Score=0.869 (+/- 0.033)
Rank=3, Name=xgb-[0.1, 50, 1.0, 9], Score=0.868 (+/- 0.038)
Rank=4, Name=xgb-[0.1, 100, 1.0, 9], Score=0.867 (+/- 0.039)
Rank=5, Name=xgb-[0.01, 50, 1.0, 3], Score=0.867 (+/- 0.035)
Rank=6, Name=xgb-[0.1, 50, 1.0, 7], Score=0.867 (+/- 0.037)
Rank=7, Name=xgb-[0.001, 100, 0.7, 9], Score=0.866 (+/- 0.040)
Rank=8, Name=xgb-[0.01, 100, 1.0, 3], Score=0.866 (+/- 0.037)
Rank=9, Name=xgb-[0.001, 100, 0.7, 3], Score=0.866 (+/- 0.034)
Rank=10, Name=xgb-[0.01, 50, 0.7, 3], Score=0.866 (+/- 0.034)
```

XGBoost 分類問題前 10 個抽樣檢驗算法的箱線圖
### 重復評估
上述結果也突出了評估的嘈雜性,例如:此次運行中額外樹木的結果與上面的運行不同(0.858 對 0.869)。
我們使用 k 折交叉驗證來產生一個分數,但人口很少,計算的平均值會很吵。
只要我們將抽樣檢查結果作為起點而不是算法對問題的明確結果,這就沒問題了。這很難做到;它需要從業者的紀律。
或者,您可能希望調整框架,使模型評估方案更好地匹配您打算用于特定問題的模型評估方案。
例如,在評估諸如袋裝或增強決策樹之類的隨機算法時,最好在相同的訓練/測試集(稱為重復)上多次運行每個實驗,以便考慮學習算法的隨機性質。
我們可以更新 _evaluate_model()_ 函數來重復給定模型的 n 次評估,每次都有不同的數據分割,然后返回所有分數。例如,10 次交叉驗證的三次重復將導致每次 30 分,以計算模型的平均表現。
```
# evaluate a single model
def evaluate_model(X, y, model, folds, repeats, metric):
# create the pipeline
pipeline = make_pipeline(model)
# evaluate model
scores = list()
# repeat model evaluation n times
for _ in range(repeats):
# perform run
scores_r = cross_val_score(pipeline, X, y, scoring=metric, cv=folds, n_jobs=-1)
# add scores to list
scores += scores_r.tolist()
return scores
```
或者,您可能更喜歡從每個 k 倍交叉驗證運行計算平均分數,然后計算所有運行的平均值,如下所述:
* [如何評估深度學習模型的技巧](https://machinelearningmastery.com/evaluate-skill-deep-learning-models/)
然后我們可以更新 _robust_evaluate_model()_ 函數來傳遞重復參數和 _evaluate_models()_ 函數來定義默認值,例如 3。
下面列出了具有三次重復模型評估的二元分類示例的完整示例。
```
# binary classification spot check script
import warnings
from numpy import mean
from numpy import std
from matplotlib import pyplot
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import RidgeClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import ExtraTreeClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import GradientBoostingClassifier
# load the dataset, returns X and y elements
def load_dataset():
return make_classification(n_samples=1000, n_classes=2, random_state=1)
# create a dict of standard models to evaluate {name:object}
def define_models(models=dict()):
# linear models
models['logistic'] = LogisticRegression()
alpha = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for a in alpha:
models['ridge-'+str(a)] = RidgeClassifier(alpha=a)
models['sgd'] = SGDClassifier(max_iter=1000, tol=1e-3)
models['pa'] = PassiveAggressiveClassifier(max_iter=1000, tol=1e-3)
# non-linear models
n_neighbors = range(1, 21)
for k in n_neighbors:
models['knn-'+str(k)] = KNeighborsClassifier(n_neighbors=k)
models['cart'] = DecisionTreeClassifier()
models['extra'] = ExtraTreeClassifier()
models['svml'] = SVC(kernel='linear')
models['svmp'] = SVC(kernel='poly')
c_values = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for c in c_values:
models['svmr'+str(c)] = SVC(C=c)
models['bayes'] = GaussianNB()
# ensemble models
n_trees = 100
models['ada'] = AdaBoostClassifier(n_estimators=n_trees)
models['bag'] = BaggingClassifier(n_estimators=n_trees)
models['rf'] = RandomForestClassifier(n_estimators=n_trees)
models['et'] = ExtraTreesClassifier(n_estimators=n_trees)
models['gbm'] = GradientBoostingClassifier(n_estimators=n_trees)
print('Defined %d models' % len(models))
return models
# create a feature preparation pipeline for a model
def make_pipeline(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# evaluate a single model
def evaluate_model(X, y, model, folds, repeats, metric):
# create the pipeline
pipeline = make_pipeline(model)
# evaluate model
scores = list()
# repeat model evaluation n times
for _ in range(repeats):
# perform run
scores_r = cross_val_score(pipeline, X, y, scoring=metric, cv=folds, n_jobs=-1)
# add scores to list
scores += scores_r.tolist()
return scores
# evaluate a model and try to trap errors and hide warnings
def robust_evaluate_model(X, y, model, folds, repeats, metric):
scores = None
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
scores = evaluate_model(X, y, model, folds, repeats, metric)
except:
scores = None
return scores
# evaluate a dict of models {name:object}, returns {name:score}
def evaluate_models(X, y, models, folds=10, repeats=3, metric='accuracy'):
results = dict()
for name, model in models.items():
# evaluate the model
scores = robust_evaluate_model(X, y, model, folds, repeats, metric)
# show process
if scores is not None:
# store a result
results[name] = scores
mean_score, std_score = mean(scores), std(scores)
print('>%s: %.3f (+/-%.3f)' % (name, mean_score, std_score))
else:
print('>%s: error' % name)
return results
# print and plot the top n results
def summarize_results(results, maximize=True, top_n=10):
# check for no results
if len(results) == 0:
print('no results')
return
# determine how many results to summarize
n = min(top_n, len(results))
# create a list of (name, mean(scores)) tuples
mean_scores = [(k,mean(v)) for k,v in results.items()]
# sort tuples by mean score
mean_scores = sorted(mean_scores, key=lambda x: x[1])
# reverse for descending order (e.g. for accuracy)
if maximize:
mean_scores = list(reversed(mean_scores))
# retrieve the top n for summarization
names = [x[0] for x in mean_scores[:n]]
scores = [results[x[0]] for x in mean_scores[:n]]
# print the top n
print()
for i in range(n):
name = names[i]
mean_score, std_score = mean(results[name]), std(results[name])
print('Rank=%d, Name=%s, Score=%.3f (+/- %.3f)' % (i+1, name, mean_score, std_score))
# boxplot for the top n
pyplot.boxplot(scores, labels=names)
_, labels = pyplot.xticks()
pyplot.setp(labels, rotation=90)
pyplot.savefig('spotcheck.png')
# load dataset
X, y = load_dataset()
# get model list
models = define_models()
# evaluate models
results = evaluate_models(X, y, models)
# summarize results
summarize_results(results)
```
運行該示例可以更準確地估計分數。
```
...
>bag: 0.861 (+/-0.037)
>rf: 0.859 (+/-0.036)
>et: 0.869 (+/-0.035)
>gbm: 0.867 (+/-0.044)
Rank=1, Name=et, Score=0.869 (+/- 0.035)
Rank=2, Name=gbm, Score=0.867 (+/- 0.044)
Rank=3, Name=bag, Score=0.861 (+/- 0.037)
Rank=4, Name=rf, Score=0.859 (+/- 0.036)
Rank=5, Name=ada, Score=0.850 (+/- 0.035)
Rank=6, Name=ridge-0.9, Score=0.848 (+/- 0.038)
Rank=7, Name=ridge-0.8, Score=0.848 (+/- 0.038)
Rank=8, Name=ridge-0.7, Score=0.848 (+/- 0.038)
Rank=9, Name=ridge-0.6, Score=0.848 (+/- 0.038)
Rank=10, Name=ridge-0.5, Score=0.848 (+/- 0.038)
```
報告的方法仍然存在一些差異,但不到一次 k-fold 交叉驗證。
可以增加重復次數以進一步減少這種變化,代價是運行時間較長,并且可能違背抽樣檢查的意圖。
### 各種輸入表示
在擬合模型之前,我非常喜歡避免對數據表示的假設和建議。
相反,我也想檢查輸入數據的多個表示和變換,我將其稱為視圖。我在帖子中解釋了這個:
* [如何充分利用機器學習數據](https://machinelearningmastery.com/how-to-get-the-most-from-your-machine-learning-data/)
我們可以更新框架,以便對每個模型的多個不同表示進行抽查。
一種方法是更新 _evaluate_models()_ 函數,以便我們可以提供可用于每個已定義模型的 _make_pipeline()_ 函數列表。
```
# evaluate a dict of models {name:object}, returns {name:score}
def evaluate_models(X, y, models, pipe_funcs, folds=10, metric='accuracy'):
results = dict()
for name, model in models.items():
# evaluate model under each preparation function
for i in range(len(pipe_funcs)):
# evaluate the model
scores = robust_evaluate_model(X, y, model, folds, metric, pipe_funcs[i])
# update name
run_name = str(i) + name
# show process
if scores is not None:
# store a result
results[run_name] = scores
mean_score, std_score = mean(scores), std(scores)
print('>%s: %.3f (+/-%.3f)' % (run_name, mean_score, std_score))
else:
print('>%s: error' % run_name)
return results
```
然后,可以將所選擇的流水線函數向下傳遞給 _robust_evaluate_model()_ 函數以及可以使用它的 _evaluate_model()_ 函數。
然后我們可以定義一堆不同的管道函數;例如:
```
# no transforms pipeline
def pipeline_none(model):
return model
# standardize transform pipeline
def pipeline_standardize(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# normalize transform pipeline
def pipeline_normalize(model):
steps = list()
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# standardize and normalize pipeline
def pipeline_std_norm(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
```
然后創建一個可以提供給 _evaluate_models()_ 函數的函數名列表。
```
# define transform pipelines
pipelines = [pipeline_none, pipeline_standardize, pipeline_normalize, pipeline_std_norm]
```
下面列出了更新為檢查管道變換的分類案例的完整示例。
```
# binary classification spot check script
import warnings
from numpy import mean
from numpy import std
from matplotlib import pyplot
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import RidgeClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import ExtraTreeClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import GradientBoostingClassifier
# load the dataset, returns X and y elements
def load_dataset():
return make_classification(n_samples=1000, n_classes=2, random_state=1)
# create a dict of standard models to evaluate {name:object}
def define_models(models=dict()):
# linear models
models['logistic'] = LogisticRegression()
alpha = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for a in alpha:
models['ridge-'+str(a)] = RidgeClassifier(alpha=a)
models['sgd'] = SGDClassifier(max_iter=1000, tol=1e-3)
models['pa'] = PassiveAggressiveClassifier(max_iter=1000, tol=1e-3)
# non-linear models
n_neighbors = range(1, 21)
for k in n_neighbors:
models['knn-'+str(k)] = KNeighborsClassifier(n_neighbors=k)
models['cart'] = DecisionTreeClassifier()
models['extra'] = ExtraTreeClassifier()
models['svml'] = SVC(kernel='linear')
models['svmp'] = SVC(kernel='poly')
c_values = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
for c in c_values:
models['svmr'+str(c)] = SVC(C=c)
models['bayes'] = GaussianNB()
# ensemble models
n_trees = 100
models['ada'] = AdaBoostClassifier(n_estimators=n_trees)
models['bag'] = BaggingClassifier(n_estimators=n_trees)
models['rf'] = RandomForestClassifier(n_estimators=n_trees)
models['et'] = ExtraTreesClassifier(n_estimators=n_trees)
models['gbm'] = GradientBoostingClassifier(n_estimators=n_trees)
print('Defined %d models' % len(models))
return models
# no transforms pipeline
def pipeline_none(model):
return model
# standardize transform pipeline
def pipeline_standardize(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# normalize transform pipeline
def pipeline_normalize(model):
steps = list()
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# standardize and normalize pipeline
def pipeline_std_norm(model):
steps = list()
# standardization
steps.append(('standardize', StandardScaler()))
# normalization
steps.append(('normalize', MinMaxScaler()))
# the model
steps.append(('model', model))
# create pipeline
pipeline = Pipeline(steps=steps)
return pipeline
# evaluate a single model
def evaluate_model(X, y, model, folds, metric, pipe_func):
# create the pipeline
pipeline = pipe_func(model)
# evaluate model
scores = cross_val_score(pipeline, X, y, scoring=metric, cv=folds, n_jobs=-1)
return scores
# evaluate a model and try to trap errors and and hide warnings
def robust_evaluate_model(X, y, model, folds, metric, pipe_func):
scores = None
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
scores = evaluate_model(X, y, model, folds, metric, pipe_func)
except:
scores = None
return scores
# evaluate a dict of models {name:object}, returns {name:score}
def evaluate_models(X, y, models, pipe_funcs, folds=10, metric='accuracy'):
results = dict()
for name, model in models.items():
# evaluate model under each preparation function
for i in range(len(pipe_funcs)):
# evaluate the model
scores = robust_evaluate_model(X, y, model, folds, metric, pipe_funcs[i])
# update name
run_name = str(i) + name
# show process
if scores is not None:
# store a result
results[run_name] = scores
mean_score, std_score = mean(scores), std(scores)
print('>%s: %.3f (+/-%.3f)' % (run_name, mean_score, std_score))
else:
print('>%s: error' % run_name)
return results
# print and plot the top n results
def summarize_results(results, maximize=True, top_n=10):
# check for no results
if len(results) == 0:
print('no results')
return
# determine how many results to summarize
n = min(top_n, len(results))
# create a list of (name, mean(scores)) tuples
mean_scores = [(k,mean(v)) for k,v in results.items()]
# sort tuples by mean score
mean_scores = sorted(mean_scores, key=lambda x: x[1])
# reverse for descending order (e.g. for accuracy)
if maximize:
mean_scores = list(reversed(mean_scores))
# retrieve the top n for summarization
names = [x[0] for x in mean_scores[:n]]
scores = [results[x[0]] for x in mean_scores[:n]]
# print the top n
print()
for i in range(n):
name = names[i]
mean_score, std_score = mean(results[name]), std(results[name])
print('Rank=%d, Name=%s, Score=%.3f (+/- %.3f)' % (i+1, name, mean_score, std_score))
# boxplot for the top n
pyplot.boxplot(scores, labels=names)
_, labels = pyplot.xticks()
pyplot.setp(labels, rotation=90)
pyplot.savefig('spotcheck.png')
# load dataset
X, y = load_dataset()
# get model list
models = define_models()
# define transform pipelines
pipelines = [pipeline_none, pipeline_standardize, pipeline_normalize, pipeline_std_norm]
# evaluate models
results = evaluate_models(X, y, models, pipelines)
# summarize results
summarize_results(results)
```
運行該示例表明,我們通過將管道號添加到算法描述名稱的開頭來區分每個管道的結果,例如, ' _0rf_ '表示第一個管道的 RF,沒有變換。
樹算法的集合在這個問題上表現良好,并且這些算法對于數據縮放是不變的。這意味著它們在每個管道上的結果將是相似的(或相同的),反過來它們將擠出前 10 個列表中的其他算法。
```
...
>0gbm: 0.865 (+/-0.044)
>1gbm: 0.865 (+/-0.044)
>2gbm: 0.865 (+/-0.044)
>3gbm: 0.865 (+/-0.044)
Rank=1, Name=3rf, Score=0.870 (+/- 0.034)
Rank=2, Name=2rf, Score=0.870 (+/- 0.034)
Rank=3, Name=1rf, Score=0.870 (+/- 0.034)
Rank=4, Name=0rf, Score=0.870 (+/- 0.034)
Rank=5, Name=3bag, Score=0.866 (+/- 0.039)
Rank=6, Name=2bag, Score=0.866 (+/- 0.039)
Rank=7, Name=1bag, Score=0.866 (+/- 0.039)
Rank=8, Name=0bag, Score=0.866 (+/- 0.039)
Rank=9, Name=3gbm, Score=0.865 (+/- 0.044)
Rank=10, Name=2gbm, Score=0.865 (+/- 0.044)
```
## 進一步閱讀
如果您希望深入了解,本節將提供有關該主題的更多資源。
* [為什么你應該在機器學習問題上進行抽樣檢查算法](https://machinelearningmastery.com/why-you-should-be-spot-checking-algorithms-on-your-machine-learning-problems/)
* [使用 scikit-learn](https://machinelearningmastery.com/spot-check-classification-machine-learning-algorithms-python-scikit-learn/) 在 Python 中進行 Spot-Check 分類機器學習算法
* [使用 scikit-learn](https://machinelearningmastery.com/spot-check-regression-machine-learning-algorithms-python-scikit-learn/) 在 Python 中使用 Spot 檢查回歸機器學習算法
* [如何評估深度學習模型的技巧](https://machinelearningmastery.com/evaluate-skill-deep-learning-models/)
* [為什么應用機器學習很難](https://machinelearningmastery.com/applied-machine-learning-is-hard/)
* [應用機器學習作為搜索問題的溫和介紹](https://machinelearningmastery.com/applied-machine-learning-as-a-search-problem/)
* [如何充分利用機器學習數據](https://machinelearningmastery.com/how-to-get-the-most-from-your-machine-learning-data/)
## 摘要
在本教程中,您發現了點檢查算法對新預測建模問題的有用性,以及如何為 python 中的點檢查算法開發標準框架以用于分類和回歸問題。
具體來說,你學到了:
* 抽樣檢查提供了一種快速發現在預測建模問題上表現良好的算法類型的方法。
* 如何開發用于加載數據,定義模型,評估模型和總結結果的通用框架。
* 如何應用框架進行分類和回歸問題。
您是否使用過這個框架,或者您是否有進一步的建議來改進它?
請在評論中告訴我。
你有任何問題嗎?
在下面的評論中提出您的問題,我會盡力回答。
- Machine Learning Mastery 應用機器學習教程
- 5競爭機器學習的好處
- 過度擬合的簡單直覺,或者為什么測試訓練數據是一個壞主意
- 特征選擇簡介
- 應用機器學習作為一個搜索問題的溫和介紹
- 為什么應用機器學習很難
- 為什么我的結果不如我想的那么好?你可能過度擬合了
- 用ROC曲線評估和比較分類器表現
- BigML評論:發現本機學習即服務平臺的聰明功能
- BigML教程:開發您的第一個決策樹并進行預測
- 構建生產機器學習基礎設施
- 分類準確性不夠:可以使用更多表現測量
- 一種預測模型的巧妙應用
- 機器學習項目中常見的陷阱
- 數據清理:將凌亂的數據轉換為整潔的數據
- 機器學習中的數據泄漏
- 數據,學習和建模
- 數據管理至關重要以及為什么需要認真對待它
- 將預測模型部署到生產中
- 參數和超參數之間有什么區別?
- 測試和驗證數據集之間有什么區別?
- 發現特征工程,如何設計特征以及如何獲得它
- 如何開始使用Kaggle
- 超越預測
- 如何在評估機器學習算法時選擇正確的測試選項
- 如何定義機器學習問題
- 如何評估機器學習算法
- 如何獲得基線結果及其重要性
- 如何充分利用機器學習數據
- 如何識別數據中的異常值
- 如何提高機器學習效果
- 如何在競爭機器學習中踢屁股
- 如何知道您的機器學習模型是否具有良好的表現
- 如何布局和管理您的機器學習項目
- 如何為機器學習準備數據
- 如何減少最終機器學習模型中的方差
- 如何使用機器學習結果
- 如何解決像數據科學家這樣的問題
- 通過數據預處理提高模型精度
- 處理機器學習的大數據文件的7種方法
- 建立機器學習系統的經驗教訓
- 如何使用機器學習清單可靠地獲得準確的預測(即使您是初學者)
- 機器學習模型運行期間要做什么
- 機器學習表現改進備忘單
- 來自世界級從業者的機器學習技巧:Phil Brierley
- 模型預測精度與機器學習中的解釋
- 競爭機器學習的模型選擇技巧
- 機器學習需要多少訓練數據?
- 如何系統地規劃和運行機器學習實驗
- 應用機器學習過程
- 默認情況下可重現的機器學習結果
- 10個實踐應用機器學習的標準數據集
- 簡單的三步法到最佳機器學習算法
- 打擊機器學習數據集中不平衡類的8種策略
- 模型表現不匹配問題(以及如何處理)
- 黑箱機器學習的誘惑陷阱
- 如何培養最終的機器學習模型
- 使用探索性數據分析了解您的問題并獲得更好的結果
- 什么是數據挖掘和KDD
- 為什么One-Hot在機器學習中編碼數據?
- 為什么你應該在你的機器學習問題上進行抽樣檢查算法
- 所以,你正在研究機器學習問題......
- Machine Learning Mastery Keras 深度學習教程
- Keras 中神經網絡模型的 5 步生命周期
- 在 Python 迷你課程中應用深度學習
- Keras 深度學習庫的二元分類教程
- 如何用 Keras 構建多層感知器神經網絡模型
- 如何在 Keras 中檢查深度學習模型
- 10 個用于 Amazon Web Services 深度學習的命令行秘籍
- 機器學習卷積神經網絡的速成課程
- 如何在 Python 中使用 Keras 進行深度學習的度量
- 深度學習書籍
- 深度學習課程
- 你所知道的深度學習是一種謊言
- 如何設置 Amazon AWS EC2 GPU 以訓練 Keras 深度學習模型(分步)
- 神經網絡中批量和迭代之間的區別是什么?
- 在 Keras 展示深度學習模型訓練歷史
- 基于 Keras 的深度學習模型中的dropout正則化
- 評估 Keras 中深度學習模型的表現
- 如何評價深度學習模型的技巧
- 小批量梯度下降的簡要介紹以及如何配置批量大小
- 在 Keras 中獲得深度學習幫助的 9 種方法
- 如何使用 Keras 在 Python 中網格搜索深度學習模型的超參數
- 用 Keras 在 Python 中使用卷積神經網絡進行手寫數字識別
- 如何用 Keras 進行預測
- 用 Keras 進行深度學習的圖像增強
- 8 個深度學習的鼓舞人心的應用
- Python 深度學習庫 Keras 簡介
- Python 深度學習庫 TensorFlow 簡介
- Python 深度學習庫 Theano 簡介
- 如何使用 Keras 函數式 API 進行深度學習
- Keras 深度學習庫的多類分類教程
- 多層感知器神經網絡速成課程
- 基于卷積神經網絡的 Keras 深度學習庫中的目標識別
- 流行的深度學習庫
- 用深度學習預測電影評論的情感
- Python 中的 Keras 深度學習庫的回歸教程
- 如何使用 Keras 獲得可重現的結果
- 如何在 Linux 服務器上運行深度學習實驗
- 保存并加載您的 Keras 深度學習模型
- 用 Keras 逐步開發 Python 中的第一個神經網絡
- 用 Keras 理解 Python 中的有狀態 LSTM 循環神經網絡
- 在 Python 中使用 Keras 深度學習模型和 Scikit-Learn
- 如何使用預訓練的 VGG 模型對照片中的物體進行分類
- 在 Python 和 Keras 中對深度學習模型使用學習率調度
- 如何在 Keras 中可視化深度學習神經網絡模型
- 什么是深度學習?
- 何時使用 MLP,CNN 和 RNN 神經網絡
- 為什么用隨機權重初始化神經網絡?
- Machine Learning Mastery 深度學習 NLP 教程
- 深度學習在自然語言處理中的 7 個應用
- 如何實現自然語言處理的波束搜索解碼器
- 深度學習文檔分類的最佳實踐
- 關于自然語言處理的熱門書籍
- 在 Python 中計算文本 BLEU 分數的溫和介紹
- 使用編碼器 - 解碼器模型的用于字幕生成的注入和合并架構
- 如何用 Python 清理機器學習的文本
- 如何配置神經機器翻譯的編碼器 - 解碼器模型
- 如何開始深度學習自然語言處理(7 天迷你課程)
- 自然語言處理的數據集
- 如何開發一種深度學習的詞袋模型來預測電影評論情感
- 深度學習字幕生成模型的溫和介紹
- 如何在 Keras 中定義神經機器翻譯的編碼器 - 解碼器序列 - 序列模型
- 如何利用小實驗在 Keras 中開發字幕生成模型
- 如何從頭開發深度學習圖片標題生成器
- 如何在 Keras 中開發基于字符的神經語言模型
- 如何開發用于情感分析的 N-gram 多通道卷積神經網絡
- 如何從零開始開發神經機器翻譯系統
- 如何在 Python 中用 Keras 開發基于單詞的神經語言模型
- 如何開發一種預測電影評論情感的詞嵌入模型
- 如何使用 Gensim 在 Python 中開發詞嵌入
- 用于文本摘要的編碼器 - 解碼器深度學習模型
- Keras 中文本摘要的編碼器 - 解碼器模型
- 用于神經機器翻譯的編碼器 - 解碼器循環神經網絡模型
- 淺談詞袋模型
- 文本摘要的溫和介紹
- 編碼器 - 解碼器循環神經網絡中的注意力如何工作
- 如何利用深度學習自動生成照片的文本描述
- 如何開發一個單詞級神經語言模型并用它來生成文本
- 淺談神經機器翻譯
- 什么是自然語言處理?
- 牛津自然語言處理深度學習課程
- 如何為機器翻譯準備法語到英語的數據集
- 如何為情感分析準備電影評論數據
- 如何為文本摘要準備新聞文章
- 如何準備照片標題數據集以訓練深度學習模型
- 如何使用 Keras 為深度學習準備文本數據
- 如何使用 scikit-learn 為機器學習準備文本數據
- 自然語言處理神經網絡模型入門
- 對自然語言處理的深度學習的承諾
- 在 Python 中用 Keras 進行 LSTM 循環神經網絡的序列分類
- 斯坦福自然語言處理深度學習課程評價
- 統計語言建模和神經語言模型的簡要介紹
- 使用 Keras 在 Python 中進行 LSTM 循環神經網絡的文本生成
- 淺談機器學習中的轉換
- 如何使用 Keras 將詞嵌入層用于深度學習
- 什么是用于文本的詞嵌入
- Machine Learning Mastery 深度學習時間序列教程
- 如何開發人類活動識別的一維卷積神經網絡模型
- 人類活動識別的深度學習模型
- 如何評估人類活動識別的機器學習算法
- 時間序列預測的多層感知器網絡探索性配置
- 比較經典和機器學習方法進行時間序列預測的結果
- 如何通過深度學習快速獲得時間序列預測的結果
- 如何利用 Python 處理序列預測問題中的缺失時間步長
- 如何建立預測大氣污染日的概率預測模型
- 如何開發一種熟練的機器學習時間序列預測模型
- 如何構建家庭用電自回歸預測模型
- 如何開發多步空氣污染時間序列預測的自回歸預測模型
- 如何制定多站點多元空氣污染時間序列預測的基線預測
- 如何開發時間序列預測的卷積神經網絡模型
- 如何開發卷積神經網絡用于多步時間序列預測
- 如何開發單變量時間序列預測的深度學習模型
- 如何開發 LSTM 模型用于家庭用電的多步時間序列預測
- 如何開發 LSTM 模型進行時間序列預測
- 如何開發多元多步空氣污染時間序列預測的機器學習模型
- 如何開發多層感知器模型進行時間序列預測
- 如何開發人類活動識別時間序列分類的 RNN 模型
- 如何開始深度學習的時間序列預測(7 天迷你課程)
- 如何網格搜索深度學習模型進行時間序列預測
- 如何對單變量時間序列預測的網格搜索樸素方法
- 如何在 Python 中搜索 SARIMA 模型超參數用于時間序列預測
- 如何在 Python 中進行時間序列預測的網格搜索三次指數平滑
- 一個標準的人類活動識別問題的溫和介紹
- 如何加載和探索家庭用電數據
- 如何加載,可視化和探索復雜的多變量多步時間序列預測數據集
- 如何從智能手機數據模擬人類活動
- 如何根據環境因素預測房間占用率
- 如何使用腦波預測人眼是開放還是閉合
- 如何在 Python 中擴展長短期內存網絡的數據
- 如何使用 TimeseriesGenerator 進行 Keras 中的時間序列預測
- 基于機器學習算法的室內運動時間序列分類
- 用于時間序列預測的狀態 LSTM 在線學習的不穩定性
- 用于罕見事件時間序列預測的 LSTM 模型體系結構
- 用于時間序列預測的 4 種通用機器學習數據變換
- Python 中長短期記憶網絡的多步時間序列預測
- 家庭用電機器學習的多步時間序列預測
- Keras 中 LSTM 的多變量時間序列預測
- 如何開發和評估樸素的家庭用電量預測方法
- 如何為長短期記憶網絡準備單變量時間序列數據
- 循環神經網絡在時間序列預測中的應用
- 如何在 Python 中使用差異變換刪除趨勢和季節性
- 如何在 LSTM 中種子狀態用于 Python 中的時間序列預測
- 使用 Python 進行時間序列預測的有狀態和無狀態 LSTM
- 長短時記憶網絡在時間序列預測中的適用性
- 時間序列預測問題的分類
- Python 中長短期記憶網絡的時間序列預測
- 基于 Keras 的 Python 中 LSTM 循環神經網絡的時間序列預測
- Keras 中深度學習的時間序列預測
- 如何用 Keras 調整 LSTM 超參數進行時間序列預測
- 如何在時間序列預測訓練期間更新 LSTM 網絡
- 如何使用 LSTM 網絡的 Dropout 進行時間序列預測
- 如何使用 LSTM 網絡中的特征進行時間序列預測
- 如何在 LSTM 網絡中使用時間序列進行時間序列預測
- 如何利用 LSTM 網絡進行權重正則化進行時間序列預測
- Machine Learning Mastery 線性代數教程
- 機器學習數學符號的基礎知識
- 用 NumPy 陣列輕松介紹廣播
- 如何從 Python 中的 Scratch 計算主成分分析(PCA)
- 用于編碼器審查的計算線性代數
- 10 機器學習中的線性代數示例
- 線性代數的溫和介紹
- 用 NumPy 輕松介紹 Python 中的 N 維數組
- 機器學習向量的溫和介紹
- 如何在 Python 中為機器學習索引,切片和重塑 NumPy 數組
- 機器學習的矩陣和矩陣算法簡介
- 溫和地介紹機器學習的特征分解,特征值和特征向量
- NumPy 對預期價值,方差和協方差的簡要介紹
- 機器學習矩陣分解的溫和介紹
- 用 NumPy 輕松介紹機器學習的張量
- 用于機器學習的線性代數中的矩陣類型簡介
- 用于機器學習的線性代數備忘單
- 線性代數的深度學習
- 用于機器學習的線性代數(7 天迷你課程)
- 機器學習的線性代數
- 機器學習矩陣運算的溫和介紹
- 線性代數評論沒有廢話指南
- 學習機器學習線性代數的主要資源
- 淺談機器學習的奇異值分解
- 如何用線性代數求解線性回歸
- 用于機器學習的稀疏矩陣的溫和介紹
- 機器學習中向量規范的溫和介紹
- 學習線性代數用于機器學習的 5 個理由
- Machine Learning Mastery LSTM 教程
- Keras中長短期記憶模型的5步生命周期
- 長短時記憶循環神經網絡的注意事項
- CNN長短期記憶網絡
- 逆向神經網絡中的深度學習速成課程
- 可變長度輸入序列的數據準備
- 如何用Keras開發用于Python序列分類的雙向LSTM
- 如何開發Keras序列到序列預測的編碼器 - 解碼器模型
- 如何診斷LSTM模型的過度擬合和欠擬合
- 如何開發一種編碼器 - 解碼器模型,注重Keras中的序列到序列預測
- 編碼器 - 解碼器長短期存儲器網絡
- 神經網絡中爆炸梯度的溫和介紹
- 對時間反向傳播的溫和介紹
- 生成長短期記憶網絡的溫和介紹
- 專家對長短期記憶網絡的簡要介紹
- 在序列預測問題上充分利用LSTM
- 編輯器 - 解碼器循環神經網絡全局注意的溫和介紹
- 如何利用長短時記憶循環神經網絡處理很長的序列
- 如何在Python中對一個熱編碼序列數據
- 如何使用編碼器 - 解碼器LSTM來回顯隨機整數序列
- 具有注意力的編碼器 - 解碼器RNN體系結構的實現模式
- 學習使用編碼器解碼器LSTM循環神經網絡添加數字
- 如何學習長短時記憶循環神經網絡回聲隨機整數
- 具有Keras的長短期記憶循環神經網絡的迷你課程
- LSTM自動編碼器的溫和介紹
- 如何用Keras中的長短期記憶模型進行預測
- 用Python中的長短期內存網絡演示內存
- 基于循環神經網絡的序列預測模型的簡要介紹
- 深度學習的循環神經網絡算法之旅
- 如何重塑Keras中長短期存儲網絡的輸入數據
- 了解Keras中LSTM的返回序列和返回狀態之間的差異
- RNN展開的溫和介紹
- 5學習LSTM循環神經網絡的簡單序列預測問題的例子
- 使用序列進行預測
- 堆疊長短期內存網絡
- 什么是教師強制循環神經網絡?
- 如何在Python中使用TimeDistributed Layer for Long Short-Term Memory Networks
- 如何準備Keras中截斷反向傳播的序列預測
- 如何在使用LSTM進行訓練和預測時使用不同的批量大小
- Machine Learning Mastery 機器學習算法教程
- 機器學習算法之旅
- 用于機器學習的裝袋和隨機森林集合算法
- 從頭開始實施機器學習算法的好處
- 更好的樸素貝葉斯:從樸素貝葉斯算法中獲取最多的12個技巧
- 機器學習的提升和AdaBoost
- 選擇機器學習算法:Microsoft Azure的經驗教訓
- 機器學習的分類和回歸樹
- 什么是機器學習中的混淆矩陣
- 如何使用Python從頭開始創建算法測試工具
- 通過創建機器學習算法的目標列表來控制
- 從頭開始停止編碼機器學習算法
- 在實現機器學習算法時,不要從開源代碼開始
- 不要使用隨機猜測作為基線分類器
- 淺談機器學習中的概念漂移
- 溫和介紹機器學習中的偏差 - 方差權衡
- 機器學習的梯度下降
- 機器學習算法如何工作(他們學習輸入到輸出的映射)
- 如何建立機器學習算法的直覺
- 如何實現機器學習算法
- 如何研究機器學習算法行為
- 如何學習機器學習算法
- 如何研究機器學習算法
- 如何研究機器學習算法
- 如何在Python中從頭開始實現反向傳播算法
- 如何用Python從頭開始實現Bagging
- 如何用Python從頭開始實現基線機器學習算法
- 如何在Python中從頭開始實現決策樹算法
- 如何用Python從頭開始實現學習向量量化
- 如何利用Python從頭開始隨機梯度下降實現線性回歸
- 如何利用Python從頭開始隨機梯度下降實現Logistic回歸
- 如何用Python從頭開始實現機器學習算法表現指標
- 如何在Python中從頭開始實現感知器算法
- 如何在Python中從零開始實現隨機森林
- 如何在Python中從頭開始實現重采樣方法
- 如何用Python從頭開始實現簡單線性回歸
- 如何用Python從頭開始實現堆棧泛化(Stacking)
- K-Nearest Neighbors for Machine Learning
- 學習機器學習的向量量化
- 機器學習的線性判別分析
- 機器學習的線性回歸
- 使用梯度下降進行機器學習的線性回歸教程
- 如何在Python中從頭開始加載機器學習數據
- 機器學習的Logistic回歸
- 機器學習的Logistic回歸教程
- 機器學習算法迷你課程
- 如何在Python中從頭開始實現樸素貝葉斯
- 樸素貝葉斯機器學習
- 樸素貝葉斯機器學習教程
- 機器學習算法的過擬合和欠擬合
- 參數化和非參數機器學習算法
- 理解任何機器學習算法的6個問題
- 在機器學習中擁抱隨機性
- 如何使用Python從頭開始擴展機器學習數據
- 機器學習的簡單線性回歸教程
- 有監督和無監督的機器學習算法
- 用于機器學習的支持向量機
- 在沒有數學背景的情況下理解機器學習算法的5種技術
- 最好的機器學習算法
- 教程從頭開始在Python中實現k-Nearest Neighbors
- 通過從零開始實現它們來理解機器學習算法(以及繞過壞代碼的策略)
- 使用隨機森林:在121個數據集上測試179個分類器
- 為什么從零開始實現機器學習算法
- Machine Learning Mastery 機器學習入門教程
- 機器學習入門的四個步驟:初學者入門與實踐的自上而下策略
- 你應該培養的 5 個機器學習領域
- 一種選擇機器學習算法的數據驅動方法
- 機器學習中的分析與數值解
- 應用機器學習是一種精英政治
- 機器學習的基本概念
- 如何成為數據科學家
- 初學者如何在機器學習中弄錯
- 機器學習的最佳編程語言
- 構建機器學習組合
- 機器學習中分類與回歸的區別
- 評估自己作為數據科學家并利用結果建立驚人的數據科學團隊
- 探索 Kaggle 大師的方法論和心態:對 Diogo Ferreira 的采訪
- 擴展機器學習工具并展示掌握
- 通過尋找地標開始機器學習
- 溫和地介紹預測建模
- 通過提供結果在機器學習中獲得夢想的工作
- 如何開始機器學習:自學藍圖
- 開始并在機器學習方面取得進展
- 應用機器學習的 Hello World
- 初學者如何使用小型項目開始機器學習并在 Kaggle 上進行競爭
- 我如何開始機器學習? (簡短版)
- 我是如何開始機器學習的
- 如何在機器學習中取得更好的成績
- 如何從在銀行工作到擔任 Target 的高級數據科學家
- 如何學習任何機器學習工具
- 使用小型目標項目深入了解機器學習工具
- 獲得付費申請機器學習
- 映射機器學習工具的景觀
- 機器學習開發環境
- 機器學習金錢
- 程序員的機器學習
- 機器學習很有意思
- 機器學習是 Kaggle 比賽
- 機器學習現在很受歡迎
- 機器學習掌握方法
- 機器學習很重要
- 機器學習 Q& A:概念漂移,更好的結果和學習更快
- 缺乏自學機器學習的路線圖
- 機器學習很重要
- 快速了解任何機器學習工具(即使您是初學者)
- 機器學習工具
- 找到你的機器學習部落
- 機器學習在一年
- 通過競爭一致的大師 Kaggle
- 5 程序員在機器學習中開始犯錯誤
- 哲學畢業生到機器學習從業者(Brian Thomas 采訪)
- 機器學習入門的實用建議
- 實用機器學習問題
- 使用來自 UCI 機器學習庫的數據集練習機器學習
- 使用秘籍的任何機器學習工具快速啟動
- 程序員可以進入機器學習
- 程序員應該進入機器學習
- 項目焦點:Shashank Singh 的人臉識別
- 項目焦點:使用 Mahout 和 Konstantin Slisenko 進行堆棧交換群集
- 機器學習自學指南
- 4 個自學機器學習項目
- álvaroLemos 如何在數據科學團隊中獲得機器學習實習
- 如何思考機器學習
- 現實世界機器學習問題之旅
- 有關機器學習的有用知識
- 如果我沒有學位怎么辦?
- 如果我不是一個優秀的程序員怎么辦?
- 如果我不擅長數學怎么辦?
- 為什么機器學習算法會處理以前從未見過的數據?
- 是什么阻礙了你的機器學習目標?
- 什么是機器學習?
- 機器學習適合哪里?
- 為什么要進入機器學習?
- 研究對您來說很重要的機器學習問題
- 你這樣做是錯的。為什么機器學習不必如此困難
- Machine Learning Mastery Sklearn 教程
- Scikit-Learn 的溫和介紹:Python 機器學習庫
- 使用 Python 管道和 scikit-learn 自動化機器學習工作流程
- 如何以及何時使用帶有 scikit-learn 的校準分類模型
- 如何比較 Python 中的機器學習算法與 scikit-learn
- 用于機器學習開發人員的 Python 崩潰課程
- 用 scikit-learn 在 Python 中集成機器學習算法
- 使用重采樣評估 Python 中機器學習算法的表現
- 使用 Scikit-Learn 在 Python 中進行特征選擇
- Python 中機器學習的特征選擇
- 如何使用 scikit-learn 在 Python 中生成測試數據集
- scikit-learn 中的機器學習算法秘籍
- 如何使用 Python 處理丟失的數據
- 如何開始使用 Python 進行機器學習
- 如何使用 Scikit-Learn 在 Python 中加載數據
- Python 中概率評分方法的簡要介紹
- 如何用 Scikit-Learn 調整算法參數
- 如何在 Mac OS X 上安裝 Python 3 環境以進行機器學習和深度學習
- 使用 scikit-learn 進行機器學習簡介
- 從 shell 到一本帶有 Fernando Perez 單一工具的書的 IPython
- 如何使用 Python 3 為機器學習開發創建 Linux 虛擬機
- 如何在 Python 中加載機器學習數據
- 您在 Python 中的第一個機器學習項目循序漸進
- 如何使用 scikit-learn 進行預測
- 用于評估 Python 中機器學習算法的度量標準
- 使用 Pandas 為 Python 中的機器學習準備數據
- 如何使用 Scikit-Learn 為 Python 機器學習準備數據
- 項目焦點:使用 Artem Yankov 在 Python 中進行事件推薦
- 用于機器學習的 Python 生態系統
- Python 是應用機器學習的成長平臺
- Python 機器學習書籍
- Python 機器學習迷你課程
- 使用 Pandas 快速和骯臟的數據分析
- 使用 Scikit-Learn 重新調整 Python 中的機器學習數據
- 如何以及何時使用 ROC 曲線和精確調用曲線進行 Python 分類
- 使用 scikit-learn 在 Python 中保存和加載機器學習模型
- scikit-learn Cookbook 書評
- 如何使用 Anaconda 為機器學習和深度學習設置 Python 環境
- 使用 scikit-learn 在 Python 中進行 Spot-Check 分類機器學習算法
- 如何在 Python 中開發可重復使用的抽樣檢查算法框架
- 使用 scikit-learn 在 Python 中進行 Spot-Check 回歸機器學習算法
- 使用 Python 中的描述性統計來了解您的機器學習數據
- 使用 OpenCV,Python 和模板匹配來播放“哪里是 Waldo?”
- 使用 Pandas 在 Python 中可視化機器學習數據
- Machine Learning Mastery 統計學教程
- 淺談計算正態匯總統計量
- 非參數統計的溫和介紹
- Python中常態測試的溫和介紹
- 淺談Bootstrap方法
- 淺談機器學習的中心極限定理
- 淺談機器學習中的大數定律
- 機器學習的所有統計數據
- 如何計算Python中機器學習結果的Bootstrap置信區間
- 淺談機器學習的Chi-Squared測試
- 機器學習的置信區間
- 隨機化在機器學習中解決混雜變量的作用
- 機器學習中的受控實驗
- 機器學習統計學速成班
- 統計假設檢驗的關鍵值以及如何在Python中計算它們
- 如何在機器學習中談論數據(統計學和計算機科學術語)
- Python中數據可視化方法的簡要介紹
- Python中效果大小度量的溫和介紹
- 估計隨機機器學習算法的實驗重復次數
- 機器學習評估統計的溫和介紹
- 如何計算Python中的非參數秩相關性
- 如何在Python中計算數據的5位數摘要
- 如何在Python中從頭開始編寫學生t檢驗
- 如何在Python中生成隨機數
- 如何轉換數據以更好地擬合正態分布
- 如何使用相關來理解變量之間的關系
- 如何使用統計信息識別數據中的異常值
- 用于Python機器學習的隨機數生成器簡介
- k-fold交叉驗證的溫和介紹
- 如何計算McNemar的比較兩種機器學習量詞的測試
- Python中非參數統計顯著性測試簡介
- 如何在Python中使用參數統計顯著性測試
- 機器學習的預測間隔
- 應用統計學與機器學習的密切關系
- 如何使用置信區間報告分類器表現
- 統計數據分布的簡要介紹
- 15 Python中的統計假設檢驗(備忘單)
- 統計假設檢驗的溫和介紹
- 10如何在機器學習項目中使用統計方法的示例
- Python中統計功效和功耗分析的簡要介紹
- 統計抽樣和重新抽樣的簡要介紹
- 比較機器學習算法的統計顯著性檢驗
- 機器學習中統計容差區間的溫和介紹
- 機器學習統計書籍
- 評估機器學習模型的統計數據
- 機器學習統計(7天迷你課程)
- 用于機器學習的簡明英語統計
- 如何使用統計顯著性檢驗來解釋機器學習結果
- 什么是統計(為什么它在機器學習中很重要)?
- Machine Learning Mastery 時間序列入門教程
- 如何在 Python 中為時間序列預測創建 ARIMA 模型
- 用 Python 進行時間序列預測的自回歸模型
- 如何回溯機器學習模型的時間序列預測
- Python 中基于時間序列數據的基本特征工程
- R 的時間序列預測熱門書籍
- 10 挑戰機器學習時間序列預測問題
- 如何將時間序列轉換為 Python 中的監督學習問題
- 如何將時間序列數據分解為趨勢和季節性
- 如何用 ARCH 和 GARCH 模擬波動率進行時間序列預測
- 如何將時間序列數據集與 Python 區分開來
- Python 中時間序列預測的指數平滑的溫和介紹
- 用 Python 進行時間序列預測的特征選擇
- 淺談自相關和部分自相關
- 時間序列預測的 Box-Jenkins 方法簡介
- 用 Python 簡要介紹時間序列的時間序列預測
- 如何使用 Python 網格搜索 ARIMA 模型超參數
- 如何在 Python 中加載和探索時間序列數據
- 如何使用 Python 對 ARIMA 模型進行手動預測
- 如何用 Python 進行時間序列預測的預測
- 如何使用 Python 中的 ARIMA 進行樣本外預測
- 如何利用 Python 模擬殘差錯誤來糾正時間序列預測
- 使用 Python 進行數據準備,特征工程和時間序列預測的移動平均平滑
- 多步時間序列預測的 4 種策略
- 如何在 Python 中規范化和標準化時間序列數據
- 如何利用 Python 進行時間序列預測的基線預測
- 如何使用 Python 對時間序列預測數據進行功率變換
- 用于時間序列預測的 Python 環境
- 如何重構時間序列預測問題
- 如何使用 Python 重新采樣和插值您的時間序列數據
- 用 Python 編寫 SARIMA 時間序列預測
- 如何在 Python 中保存 ARIMA 時間序列預測模型
- 使用 Python 進行季節性持久性預測
- 基于 ARIMA 的 Python 歷史規模敏感性預測技巧分析
- 簡單的時間序列預測模型進行測試,這樣你就不會欺騙自己
- 標準多變量,多步驟和多站點時間序列預測問題
- 如何使用 Python 檢查時間序列數據是否是固定的
- 使用 Python 進行時間序列數據可視化
- 7 個機器學習的時間序列數據集
- 時間序列預測案例研究與 Python:波士頓每月武裝搶劫案
- Python 的時間序列預測案例研究:巴爾的摩的年度用水量
- 使用 Python 進行時間序列預測研究:法國香檳的月銷售額
- 使用 Python 的置信區間理解時間序列預測不確定性
- 11 Python 中的經典時間序列預測方法(備忘單)
- 使用 Python 進行時間序列預測表現測量
- 使用 Python 7 天迷你課程進行時間序列預測
- 時間序列預測作為監督學習
- 什么是時間序列預測?
- 如何使用 Python 識別和刪除時間序列數據的季節性
- 如何在 Python 中使用和刪除時間序列數據中的趨勢信息
- 如何在 Python 中調整 ARIMA 參數
- 如何用 Python 可視化時間序列殘差預測錯誤
- 白噪聲時間序列與 Python
- 如何通過時間序列預測項目
- Machine Learning Mastery XGBoost 教程
- 通過在 Python 中使用 XGBoost 提前停止來避免過度擬合
- 如何在 Python 中調優 XGBoost 的多線程支持
- 如何配置梯度提升算法
- 在 Python 中使用 XGBoost 進行梯度提升的數據準備
- 如何使用 scikit-learn 在 Python 中開發您的第一個 XGBoost 模型
- 如何在 Python 中使用 XGBoost 評估梯度提升模型
- 在 Python 中使用 XGBoost 的特征重要性和特征選擇
- 淺談機器學習的梯度提升算法
- 應用機器學習的 XGBoost 簡介
- 如何在 macOS 上為 Python 安裝 XGBoost
- 如何在 Python 中使用 XGBoost 保存梯度提升模型
- 從梯度提升開始,比較 165 個數據集上的 13 種算法
- 在 Python 中使用 XGBoost 和 scikit-learn 進行隨機梯度提升
- 如何使用 Amazon Web Services 在云中訓練 XGBoost 模型
- 在 Python 中使用 XGBoost 調整梯度提升的學習率
- 如何在 Python 中使用 XGBoost 調整決策樹的數量和大小
- 如何在 Python 中使用 XGBoost 可視化梯度提升決策樹
- 在 Python 中開始使用 XGBoost 的 7 步迷你課程