[读书笔记] 《Python 机器学习》- 使用嵌套交叉验证进行模型选择
2017-07-26 20:14
706 查看
摘要
通过嵌套交叉验证选择算法(外部循环通过k-折等进行参数优化,内部循环使用交叉验证),我们可以对特定数据集进行模型选择代码
# 6.4.2: 嵌套交叉验证选择算法,用于在不同的机器学习算法中进行选择 import matplotlib.pyplot as plt import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.model_selection import cross_val_score from sklearn.model_selection import GridSearchCV from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import LabelEncoder from sklearn.linear_model import LogisticRegression from sklearn.svm import SVC from sklearn.tree import DecisionTreeClassifier from sklearn.pipeline import Pipeline # 导入数据 df = pd.read_csv('./Data/UCI/wdbc.data.txt') # Slicing X, y = df.iloc[:, 2:].values, df.iloc[:, < 4000 span class="hljs-number">1].values le = 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)))
相关文章推荐
- 机器学习:交叉验证和模型选择与Python代码实现
- machine learning in coding(python):使用交叉验证【选择模型超参数】
- [读书笔记] 《Python 机器学习》 - 使用RandomParameterOpt与GridSearch进行超参调整
- xgboost调用sklearn的交叉验证,并且使用自定义的训练集、验证集进行模型的调参
- Spark2.0机器学习系列之2:基于Pipeline、交叉验证、ParamMap的模型选择和超参数调优
- 开发一个应用程序,使用 Python、NLTK 和机器学习对 RSS 提要进行分类
- 机器学习-CrossValidation交叉验证Python实现
- asp.net mvc 3.0详细笔记__17__使用 DataAnnotations 进行模型验证
- 服务器控件使用js进行验证(选择地调用后台事件)
- ASP.NET MVC 音乐商店 - 6. 使用 DataAnnotations 进行模型验证
- 第1章 统计学习方法概论——第4~6节 模型选择、正则化和交叉验证、泛化能力
- 【scikit-learn】交叉验证及其用于参数选择、模型选择、特征选择的例子
- 探索 Python、机器学习和 NLTK 库 开发一个应用程序,使用 Python、NLTK 和机器学习对 RSS 提要进行分类
- Asp.Net MVC 使用 DataAnnotations 进行模型验证
- machine learning in coding(python):使用贪心搜索【进行特征选择】
- 使用svm机器学习模型对抓取异常进行挖掘
- ASP.NET MVC 音乐商店 - 6. 使用 DataAnnotations 进行模型验证
- 《机器学习与R语言》读书笔记2:使用R进行机器学习
- 交叉验证--模型参数选择
- 模型选择之交叉验证