[读书笔记] 《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
原创粉丝点击