[读书笔记] 《Python 机器学习》- 使用嵌套交叉验证进行模型选择
来源:互联网 发布:大连网络教育 编辑:程序博客网 时间:2024/06/08 05:49
摘要
通过嵌套交叉验证选择算法(外部循环通过k-折等进行参数优化,内部循环使用交叉验证),我们可以对特定数据集进行模型选择
代码
# 6.4.2: 嵌套交叉验证选择算法,用于在不同的机器学习算法中进行选择import matplotlib.pyplot as pltimport numpy as npimport pandas as pdfrom sklearn.model_selection import train_test_splitfrom sklearn.model_selection import cross_val_scorefrom sklearn.model_selection import GridSearchCVfrom sklearn.preprocessing import StandardScalerfrom sklearn.preprocessing import LabelEncoderfrom sklearn.linear_model import LogisticRegressionfrom sklearn.svm import SVCfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.pipeline import Pipeline# 导入数据df = pd.read_csv('./Data/UCI/wdbc.data.txt')# SlicingX, y = df.iloc[:, 2:].values, df.iloc[:, 1].valuesle = LabelEncoder()y = le.fit_transform(y)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=1)# 构造模型pipe_svc = Pipeline([('scl', StandardScaler()), ('clf', SVC(random_state=1))])param_range = [10**c for c in range(-4, 4)]# param_range = np.linspace(0.0001, 1, 10)param_grid = [ {'clf__C': param_range, 'clf__kernel': ['linear']}, # 对于线性SVM只需要调优正则化参数C {'clf__C': param_range, 'clf__gamma': param_range, 'clf__kernel': ['rbf']} # 对于核SVM则需要同时调优C和gamma值]gs = GridSearchCV(estimator=pipe_svc, param_grid=param_grid, scoring='accuracy', cv=10, n_jobs=-1)# gs.fit(X_train, y_train)# clf = gs.best_estimator_scores = cross_val_score(gs, X_train, y_train, scoring='accuracy', cv=5)print('CV accuracy: %.3f +/- %.3f' % (np.mean(scores), np.std(scores)))gs = GridSearchCV(estimator=DecisionTreeClassifier(random_state=0), param_grid=[{'max_depth':[1,2,3,4,5,6,7,None]}], scoring='accuracy', cv=5)scores = cross_val_score(gs, X_train, y_train, scoring='accuracy', cv=5)print('CV accuracy: %.3f +/- %.3f' % (np.mean(scores), np.std(scores)))
阅读全文
0 0
- [读书笔记] 《Python 机器学习》- 使用嵌套交叉验证进行模型选择
- 机器学习:交叉验证和模型选择与Python代码实现
- 机器学习之模型选择(交叉验证)
- [读书笔记] 《Python 机器学习》- 各种模型选择方法的封装
- 机器学习-CrossValidation交叉验证Python实现
- 机器学习-CrossValidation交叉验证Python实现
- 机器学习-CrossValidation交叉验证Python实现
- 机器学习 python 交叉验证实例
- machine learning in coding(python):使用交叉验证【选择模型超参数】
- 机器学习--交叉验证
- 机器学习--交叉验证
- 机器学习-交叉验证
- 模型选择之交叉验证
- 交叉验证--模型参数选择
- Spark2.0机器学习系列之1:基于Pipeline、交叉验证、ParamMap的模型选择和超参数调优
- 机器学习——交叉验证与特征选择
- [交叉验证]机器学习中分类器的选择
- R机器学习之交叉验证改善模型
- HDU
- spring+springMvc+mybatis配置事务管理
- 注解_通用BaseDao优化
- commom fileupload简介
- POJ 2411 Mondriaan's Dream (状压DP)
- [读书笔记] 《Python 机器学习》- 使用嵌套交叉验证进行模型选择
- POJ 1273 Drainage Ditches(网络流入门)
- string容易忽略的一些问题
- HDU-2604(递推+矩阵快速幂)
- spark 源代码分析 (一)准备阶段: idea 导入spark 源代码
- 嵌入式系统学习——S3C2451之2.4寸LCD函数封装
- ZooKeeper教程(二)----zookeeper单机版伪集群
- shell脚本学习第四天——bash内部变量,变量主串,变量替换
- 数据结构基本概念总结