grid = GridSearchCV(xgb_model, param_grid, cv=cv_split, scoring=‘neg_mean_squared_error’)#neg代表是负数
scoring=‘neg_mean_squared_error’,表示用MSE作为评价指标,MSE是非负的,且越接近于0越好。但在GridSearchCV中会选择得分最大的模型,因此在MSE前面一个负号,就会选出离0最近的模型
2.在交叉验证
kf = KFold(n_splits=5, shuffle=True)
score_ndarray = cross_val_score(decisionTree_model, X, y, cv=kf)中,要设置shuffle=True,否则在做回归时可能会出错。
score_ndarray = cross_val_score(decisionTree_model, X, y, cv=5),会因为shuffle=False出错,并且,cross_val_score()函数只能出验证集的评价指标的得分。
3.网格搜索,可以与交叉验证混合,即网格搜索中的每一种组合进行交叉验证,再输出结果,使模型选择将更加客观
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import learning_curve
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
import seaborn as sns
import time
from xgboost import XGBRegressor
from sklearn.model_selection import cross_validate
from sklearn.model_selection import ShuffleSplit
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import LinearSVR
from sklearn.svm import NuSVR
from sklearn.svm import SVR
def plot_learning_curve(plt, estimator, title, X, y, ylim=None, cv=None,
n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
"""
Generate a simple plot of the test and training learning curve.
Parameters
----------
estimator : object type that implements the "fit" and "predict" methods
An object of that type which is cloned for each validation.
title : string
Title for the chart.
X : array-like, shape (n_samples, n_features)
Training vector, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape (n_samples) or (n_samples, n_features), optional