交叉验证(Cross Validation)与网格搜索(Grid

2022-05-03  本文已影响0人  万州客

收到了三份代码,我觉得clf = gs.best_estimator_这种用法,应该是比较科学和不重复的,书写也比较规范,又不含糊,求验证~

https://blog.csdn.net/fgg1234567890/article/details/110207142?spm=1001.2101.3001.6650.14&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14.pc_relevant_antiscanv2&utm_relevant_index=17

https://blog.csdn.net/wf592523813/article/details/86309547?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&utm_relevant_index=2

https://blog.csdn.net/wp_python/article/details/80255466

原始数据划分为3份,分别为:训练集、验证集和测试集;其中训练集用来模型训练,验证集用来调整参数,而测试集用来衡量模型表现好坏。

那请问是不是用了GridSearchCV,就可以不用cross_val_score、cross_validate了,就也相当于是交叉验证了,经过训练之后就只需要测试就可以了对吧?---对的

一,clf = gs.best_estimator_写得明白

from sklearn.model_selection import validation_curve
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC

df = pd.read_csv('xxx\\wdbc.data',
                 header=None)
print(df.head())

X = df.loc[:, 2:].values
y = df.loc[:, 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,
                     stratify=y,
                     random_state=1)
print(len(X_train))

pipe_svc = make_pipeline(StandardScaler(),
                         SVC(random_state=1))  # 支持向量机(SVM)

param_range = [0.0001, 0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]

param_grid = [{'svc__C': param_range,
               'svc__kernel': ['linear']},
              {'svc__C': param_range,
               'svc__gamma': param_range,
               'svc__kernel': ['rbf']}]

gs = GridSearchCV(estimator=pipe_svc,
                  param_grid=param_grid,
                  scoring='accuracy',
                  cv=10,
                  n_jobs=-1)
gs = gs.fit(X_train, y_train)
print(gs.best_score_)
print(gs.best_params_)

clf = gs.best_estimator_
clf.fit(X_train, y_train)
print('Test accuracy: %.3f' % clf.score(X_test, y_test))

二,grid_search.fit(X_train, y_train) # 训练,找到最优的参数,同时使用最优的参数实例化一个新的SVC estimator,这个就有些含糊,图省事

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'WF'

from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.model_selection import cross_val_score


def simpleGridSearch(X_train, X_test, y_train, y_test):
    '''
    使用for循环实现网格搜索
    :param X_train:
    :param X_test:
    :param y_train:
    :param y_test:
    :return:
    '''
    # grid search start
    best_score = 0
    for gamma in [0.001, 0.01, 0.1, 1, 10, 100]:
        for C in [0.001, 0.01, 0.1, 1, 10, 100]:
            svm = SVC(gamma=gamma,C=C)#对于每种参数可能的组合,进行一次训练;
            svm.fit(X_train,y_train)
            score = svm.score(X_test,y_test)
            if score > best_score:#找到表现最好的参数
                best_score = score
                best_parameters = {'gamma':gamma,'C':C}

    print("Best score:{:.2f}".format(best_score))
    print("Best parameters:{}".format(best_parameters))


def gridSearchCv(X_train, X_test, y_train, y_test):
    '''
    使用for循环实现网格搜索与交叉验证
    :param X_train:
    :param X_test:
    :param y_train:
    :param y_test:
    :return:
    '''
    best_score = 0.0
    for gamma in [0.001,0.01,0.1,1,10,100]:
        for C in [0.001,0.01,0.1,1,10,100]:
            svm = SVC(gamma=gamma, C=C)
            scores = cross_val_score(svm, X_train, y_train, cv=5) #5折交叉验证
            score = scores.mean() #取平均数
            if score > best_score:
                best_score = score
                best_parameters = {"gamma": gamma, "C": C}
    svm = SVC(**best_parameters)
    svm.fit(X_train, y_train)
    test_score = svm.score(X_test,y_test)
    print("Best score on validation set:{:.2f}".format(best_score))
    print("Best parameters:{}".format(best_parameters))
    print("Score on testing set:{:.2f}".format(test_score))


def skGridSearchCv(X_train, X_test, y_train, y_test):
    '''
    利用sklearn中的GridSearchCV类
    :param X_train:
    :param X_test:
    :param y_train:
    :param y_test:
    :return:
    '''
    #把要调整的参数以及其候选值 列出来;
    param_grid = {"gamma": [0.001,0.01,0.1,1,10,100],
                 "C": [0.001,0.01,0.1,1,10,100]}
    print("Parameters:{}".format(param_grid))

    grid_search = GridSearchCV(SVC(),param_grid,cv=5) # 实例化一个GridSearchCV类
    X_train, X_test, y_train, y_test = train_test_split(iris.data,iris.target, random_state=10)
    grid_search.fit(X_train, y_train)  # 训练,找到最优的参数,同时使用最优的参数实例化一个新的SVC estimator。
    print("Test set score:{:.2f}".format(grid_search.score(X_test, y_test)))
    print("Best parameters:{}".format(grid_search.best_params_))
    print("Best score on train set:{:.2f}".format(grid_search.best_score_))


if __name__ == '__main__':
    iris = load_iris()
    X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=0)
    print("Size of training set:{} size of testing set:{}".format(X_train.shape[0], X_test.shape[0]))
    # simpleGridSearch(X_train, X_test, y_train, y_test)
    # gridSearchCv(X_train, X_test, y_train, y_test)
    skGridSearchCv(X_train, X_test, y_train, y_test)

三,clf = SVC(kernel='rbf', C=1, gamma=1e-3)#最优模型-这个用得也太正规和重复输入了吧

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report
from sklearn.svm import SVC
 
#下载数据
digits = datasets.load_digits()
#print(digits.images)
n_sample = len(digits.images)
#把数据转换为二维数据,x的行数据是不同样本数据,列是样本属性。
x = digits.images.reshape(n_sample, -1)#取数据的所有行第一列数据
y = digits.target
#print(x)
 
#以下方法确定解释变量只能有一个,但是多个解释变量该怎么处理呢,答案是x包含了众多解释变量
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=0)
 
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                     'C': [1, 10, 100, 1000]},
                    {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
scores = ['precision', 'recall']
for score in scores:
    print('Tuning hyper-parameters for %s'%score)
    print()
    #利用网格搜索算法构建评估器模型,并且对数据进行评估
    clf = GridSearchCV(SVC(C=1), tuned_parameters, cv=5, scoring='%s_macro'%score)
    clf.fit(x_train, y_train)
    print('最优参数:',clf.best_params_)
    print()
    means = clf.cv_results_['mean_test_score']
    stds = clf.cv_results_['std_test_score']
    for mean, std, params in zip(means, stds, clf.cv_results_['params']):
        print('网格数据得分:','%0.3f (+/-%0.3f) for %r'%(mean, std, params)) 
    #这个std有的文章乘以2,但个人不知道为什么需要乘以2,如有明白的朋友,求指点。
    print()
    y_true, y_pred = y_test, clf.predict(x_test)
    print(y_true)
    print(classification_report(y_true, y_pred))
 
#在获取最优超参数之后, 用5折交叉验证来评估模型  
clf = SVC(kernel='rbf', C=1, gamma=1e-3)#最优模型
#对模型进行评分
scores = cross_val_score(clf, x, y, cv=5)
print(scores)
    
上一篇下一篇

猜你喜欢

热点阅读