集成学习Day6 sklearn分类模型的评估、优化与人脸分类实例
1. 模型评估与优化
(1)超参数选择
A 使用网格搜索选择超参数
from sklearn import datasets
import pandas as pd
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
import time
iris = datasets.load_iris()
X = iris.data
y = iris.target
feature = iris.feature_names
start_time = time.time()
pipe_svc = make_pipeline(StandardScaler(), SVC(random_state=1))
param_range = [0.0001, 0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]
param_grid = [{
'svc__C':param_range, 'svc__kernel':['linear']}, \
{
'svc__C':param_range,'svc__gamma':param_range,'svc__kernel':['rbf']}]
gs = GridSearchCV(estimator=pipe_svc, param_grid=param_grid, scoring='accuracy', cv=10, n_jobs=-1)
gs.fit(X, y)
end_time = time.time()
print("网格搜索经历的时间为: %.3f S" %float(end_time - start_time))
print(gs.best_score_)
print(gs.best_params_)

B 使用随机搜索选择超参数
from sklearn import datasets
import pandas as pd
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import RandomizedSearchCV
from sklearn.svm import SVC
import time
iris = datasets.load_iris()
X = iris.data
y = iris.target
feature = iris.feature_names
start_time = time.time()
pipe_svc = make_pipeline(StandardScaler(), SVC(random_state=1))
param_range = [0.0001, 0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]
param_grid = [{
'svc__C':param_range,'svc__kernel':['linear']},\
{
'svc__C':param_range,'svc__gamma':param_range,'svc__kernel':['rbf']}]
gs = RandomizedSearchCV(estimator=pipe_svc, param_distributions=param_grid, scoring='accuracy',cv=10, n_jobs=-1)
gs = gs.fit(X,y)
end_time = time.time()