您的位置:首页 > 编程语言 > Python开发

[读书笔记] 《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)))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐