十、模型选择

作者:Chris Albon

译者:飞龙

协议:CC BY-NC-SA 4.0

在模型选择期间寻找最佳预处理步骤

在进行模型选择时,我们必须小心正确处理预处理。 首先,GridSearchCV使用交叉验证来确定哪个模型表现最好。 然而,在交叉验证中,我们假装作为测试集被留出的一折是不可见的,因此不适合一些预处理步骤(例如缩放或标准化)。 出于这个原因,我们无法预处理数据然后运行GridSearchCV

其次,一些预处理方法有自己的参数,通常必须由用户提供。 通过在搜索空间中包括候选成分值,可以像对待任何想要搜索其他超参数一样对待它们。

# 加载库
import numpy as np
from sklearn import datasets
from sklearn.feature_selection import SelectKBest
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler# 设置随机种子
np.random.seed(0)# 加载数据
iris = datasets.load_iris()
X = iris.data
y = iris.target

我们包括两个不同的预处理步骤:主成分分析和 k 最佳特征选择。

# 创建组合预处理对象
preprocess = FeatureUnion([('pca', PCA()), ("kbest", SelectKBest(k=1))])# 创建流水线
pipe = Pipeline([('preprocess', preprocess), ('classifier', LogisticRegression())])# 创建候选值空间
search_space = [{'preprocess__pca__n_components': [1, 2, 3],'classifier__penalty': ['l1', 'l2'],'classifier__C': np.logspace(0, 4, 10)}]# 创建网格搜索
clf = GridSearchCV(pipe, search_space, cv=5, verbose=0, n_jobs=-1)# 拟合网格搜索
best_model = clf.fit(X, y)# 查看最佳超参数
print('Best Number Of Princpal Components:', best_model.best_estimator_.get_params()['preprocess__pca__n_components'])
print('Best Penalty:', best_model.best_estimator_.get_params()['classifier__penalty'])
print('Best C:', best_model.best_estimator_.get_params()['classifier__C'])'''
Best Number Of Princpal Components: 3
Best Penalty: l1
Best C: 59.9484250319
'''

使用网格搜索的超参数调优

# 加载库
import numpy as np
from sklearn import linear_model, datasets
from sklearn.model_selection import GridSearchCV# 加载数据
iris = datasets.load_iris()
X = iris.data
y = iris.target# 创建逻辑回归
logistic = linear_model.LogisticRegression()# 创建正则化惩罚空间
penalty = ['l1', 'l2']# 创建正则化超参数空间
C = np.logspace(0, 4, 10)# 创建超参数选项
hyperparameters = dict(C=C, penalty=penalty)# 使用 5 折交叉验证创建网格搜索
clf = GridSearchCV(logistic, hyperparameters, cv=5, verbose=0)# 拟合网格搜索
best_model = clf.fit(X, y)# 查看最佳超参数
print('Best Penalty:', best_model.best_estimator_.get_params()['penalty'])
print('Best C:', best_model.best_estimator_.get_params()['C'])
'''
Best Penalty: l1
Best C: 7.74263682681
'''# 预测目标向量
best_model.predict(X)
'''
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
'''

使用随机搜索的超参数调优

# 加载库
from scipy.stats import uniform
from sklearn import linear_model, datasets
from sklearn.model_selection import RandomizedSearchCV# 加载数据
iris = datasets.load_iris()
X = iris.data
y = iris.target# 创建逻辑回归
logistic = linear_model.LogisticRegression()# 创建正则化惩罚空间
penalty = ['l1', 'l2']# 使用均匀分布创建正则化超参数分布
C = uniform(loc=0, scale=4)# 创建超参数选项
hyperparameters = dict(C=C, penalty=penalty)# 使用 5 折交叉验证和 100 个迭代
clf = RandomizedSearchCV(logistic, hyperparameters, random_state=1, n_iter=100, cv=5, verbose=0, n_jobs=-1)# 拟合随机搜索
best_model = clf.fit(X, y)# 查看最佳超参数
print('Best Penalty:', best_model.best_estimator_.get_params()['penalty'])
print('Best C:', best_model.best_estimator_.get_params()['C'])
'''
Best Penalty: l1
Best C: 1.66808801881
'''# 预测目标向量
best_model.predict(X)
'''
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
'''

使用网格搜索的模型选择

# 加载库
import numpy as np
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline# 设置随机种子
np.random.seed(0)# 加载数据
iris = datasets.load_iris()
X = iris.data
y = iris.target

请注意,我们包括需要搜索的多个可能的学习算法和多个可能的超参数值。

# 创建流水线
pipe = Pipeline([('classifier', RandomForestClassifier())])# 创建候选学习算法和它们的超参数的空间
search_space = [{'classifier': [LogisticRegression()],'classifier__penalty': ['l1', 'l2'],'classifier__C': np.logspace(0, 4, 10)},{'classifier': [RandomForestClassifier()],'classifier__n_estimators': [10, 100, 1000],'classifier__max_features': [1, 2, 3]}]# 创建网格搜索
clf = GridSearchCV(pipe, search_space, cv=5, verbose=0)# 拟合网格搜索
best_model = clf.fit(X, y)# 查看最佳模型
best_model.best_estimator_.get_params()['classifier']
'''
LogisticRegression(C=7.7426368268112693, class_weight=None, dual=False,fit_intercept=True, intercept_scaling=1, max_iter=100,multi_class='ovr', n_jobs=1, penalty='l1', random_state=None,solver='liblinear', tol=0.0001, verbose=0, warm_start=False)
'''# 预测目标向量
best_model.predict(X)
'''
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
'''

带有参数选项的流水线

# 导入所需的包
import numpy as np
from sklearn import linear_model, decomposition, datasets
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler# 加载乳腺癌数据集
dataset = datasets.load_breast_cancer()# 从数据集特征中创建 X
X = dataset.data# 从数据集目标中创建 y
y = dataset.target# 创建缩放器对象
sc = StandardScaler()# 创建 PCA 对象
pca = decomposition.PCA()# 创建逻辑回归对象,带有 L2 惩罚
logistic = linear_model.LogisticRegression()# 创建三步流水线。首先,标准化数据。
# 其次,使用 PCA 转换数据。
# 然后在数据上训练逻辑回归。
pipe = Pipeline(steps=[('sc', sc), ('pca', pca), ('logistic', logistic)])# 创建 1 到 30 的一列整数(X + 1,特征序号)
n_components = list(range(1,X.shape[1]+1,1))# 创建正则化参数的一列值
C = np.logspace(-4, 4, 50)# 为正则化乘法创建一列选项
penalty = ['l1', 'l2']# 为所有参数选项创建字典
# 注意,你可以使用 '__' 来访问流水线的步骤的参数
parameters = dict(pca__n_components=n_components, logistic__C=C,logistic__penalty=penalty)# 创建网格搜索对象
clf = GridSearchCV(pipe, parameters)# 拟合网格搜索
clf.fit(X, y)# 查看超参数
print('Best Penalty:', clf.best_estimator_.get_params()['logistic__penalty'])
print('Best C:', clf.best_estimator_.get_params()['logistic__C'])
print('Best Number Of Components:', clf.best_estimator_.get_params()['pca__n_components'])# 使用 3 折交叉验证拟合网格搜索
cross_val_score(clf, X, y)

数据科学和人工智能技术笔记 十、模型选择相关推荐

  1. 数据科学和人工智能技术笔记 十二、逻辑回归

    十二.逻辑回归 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 C 超参数快速调优 有时,学习算法的特征使我们能够比蛮力或随机模型搜索方法更快地搜索最佳超参数. sci ...

  2. 数据科学和人工智能技术笔记 十五、支持向量机

    十五.支持向量机 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 校准 SVC 中的预测概率 SVC 使用超平面来创建决策区域,不会自然输出观察是某一类成员的概率估计. ...

  3. 数据科学和人工智能技术笔记 十四、K 最近邻

    十四.K 最近邻 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 确定 K 的最佳值 # 加载库 from sklearn.neighbors import KNeig ...

  4. 数据科学和人工智能技术笔记 十六、朴素贝叶斯

    十六.朴素贝叶斯 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 伯努利朴素贝叶斯 伯努利朴素贝叶斯分类器假设我们的所有特征都是二元的,它们仅有两个值(例如,已经是独热 ...

  5. 数据科学和人工智能技术笔记 十九、数据整理(上)

    十九.数据整理(上) 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 在 Pandas 中通过分组应用函数 import pandas as pd# 创建示例数据帧 d ...

  6. 数据科学和人工智能技术笔记 十三、树和森林

    十三.树和森林 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 Adaboost 分类器 # 加载库 from sklearn.ensemble import AdaB ...

  7. 数据科学和人工智能技术笔记 十一、线性回归

    十一.线性回归 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 添加交互项 # 加载库 from sklearn.linear_model import LinearR ...

  8. 数据科学和人工智能技术笔记 七、特征工程

    七.特征工程 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 稀疏特征矩阵上的降维 # 加载库 from sklearn.preprocessing import St ...

  9. 数据科学和人工智能技术笔记 九、模型验证

    九.模型验证 作者:Chris Albon 译者:飞龙 协议:CC BY-NC-SA 4.0 准确率 # 加载库 from sklearn.model_selection import cross_v ...

最新文章

  1. 安卓开发之使用viewpager+fragment实现滚动tab页
  2. Redis中的执行命令的过程
  3. 从 +new Date 说起,Javascript的一元操作符
  4. [转载]当代中国建筑设计百家名院名单
  5. HDU - 3518 Boring counting(后缀数组)
  6. 以前的某个程序安装计算机上创建挂起_教研拓进王立辉老师计算机专业教学心得...
  7. ASP.NET Core 源码学习之 Options[2]:IOptions
  8. 依赖注入与对象间关系
  9. laravel 向模板中添加公共变量
  10. linux 打包大文件,tar打包处理大文件的解压缩方法
  11. 在线诺基亚短信图片生成器工具
  12. 计算机mod函数,MOD函数的公式语法及使用方法实例
  13. JarvisOJ 逆向Writeup
  14. python推箱子小游戏_python写的推箱子小游戏
  15. 聚合支付平台需要哪方面的技术?主要有哪些功能结构?
  16. 花三分钟,一起了解一下消费全返模式是怎么样的?消费者又消费又赚钱
  17. Tableau地图/分组数据集联系
  18. salesforce的前世今生
  19. 802.11协议精读10:节能模式(PSM)
  20. 【数据库】事务管理概念

热门文章

  1. 在linux下做源码免杀,Cobaltstrike免杀从源码级到落地思维转变
  2. git merge分支不合并_合并分支使用Merge还是Rebase?
  3. h5跳转小程序_微信小程序吞掉H5?
  4. java file list listfiles,Java File listFiles()用法及代码示例
  5. python算法应用(八)——优化
  6. linux input输入子系统分析《三》:S3C2440的触摸屏驱动实例
  7. Linux内核开发之异步通知与异步I/O《来自linux设备开发详解》
  8. [计算机网络] - 调节参数提高 TCP 性能
  9. mysql 高性能架构_高性能MySQL之架构与历史(1)
  10. c语言 tcl.exe 自动登录,Tcl命令操作实验-----(3)---字符串