波士顿房价问题

本文介绍了在解决波士顿房价问题时如何利用GridSearchCV进行参数调优。通过设置scoring参数为'neg_mean_squared_error'确保最小化MSE,并在交叉验证中使用KFold,强调了在训练决策树模型时设置'shuffle=True'的重要性,以避免回归错误。同时,指出cross_val_score函数仅输出验证集得分,且默认不进行洗牌可能造成问题。
摘要由CSDN通过智能技术生成

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
  
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值