用sklearn做一个完整的机器学习工程——以波士顿房价预测为例(三、调参,找最优参数)

机器学习实战二:波士顿房价预测 Boston Housing 波士顿房价预测 Boston housing 这是一个波士顿房价预测的一个实战,上一次 阅读详情

前言

本来是打算介绍一下:GBDT、XGboost的原理的,看着看着发现网络上的博客资料都不是很全,讲的云里雾里。

建议直接阅读一下Friedman的论文和陈天奇的论文,链接: https://pan.baidu.com/s/14TmsZTorZmOAHEwZU5fiNA 密码: g2g1

下面我要开始介绍sklearn中的调参步骤啦

如果你现在有了一个列表,列表里有几个有希望的模型。你现在需要对它们进行微调。让我们来看sklearn中提供的俩种微调的方法

网格化调整参数和随机参数

由于这里训练时间的关系我只展示 Xgboost的调参和随机森林的调参,GBDT训练的速度有点慢了

GridSearch

网格化调整超参数所需要做的是告诉GridSearchCV要试验有哪些超参数,要试验什么值,GridSearchCV就能用交叉验证试验所有可能超参数值的组合

比如下面对随机森林的网格化调参

from sklearn.model_selection import GridSearchCV

param_grid = [
    
    {'n_estimators': [3, 10, 30], 'max_features': [2, 4, 6, 8]},
    
    {'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]},
  ]

forest_reg = RandomForestRegressor(random_state=42)
# train across 5 folds, that's a total of (12+6)*5=90 rounds of training 
grid_search = GridSearchCV(forest_reg, param_grid, cv=5,
                           scoring='neg
机器学习学习笔记——以波士顿房价问题为例 说起入坑机器学习,还是因为在学校一个偶然的机会:那时候在我们教三楼验收数据库课设,然后去学办逛了逛,就看到了有一个百度菁英俱乐部的创立请求书,然后觉得百度这个头衔听牛批的,并且主席刚好是我的一个朋友,然后就找到了他,当天中午去参加了面试(没有走后门嗷),还是正经的写了简历和做了题目的,然后一个研究生学长面试我们(我也比较熟),由于想学技术,就选了技术部,刚好他问的问题我 阅读详情

相关推荐

python-机器学习-波士顿房价回归分析

以波士顿房价数据集为对象,理解数据和认识数据,掌握和的初步方法,掌握的一般方法,对回归分析的结果解读。

weixin_59938092的博客 6628

机器学习线性回归(七):sklearn之模型保存和加载(附房价预测案例源代码)

模型的保存和加载 文章源代码下载地址:波士顿房价岭回归正则化预测代码实现 文章目录模型的保存和加载1.sklearn模型的保存和加载API2.线性回归的模型保存加载案例2.1保存模型2.2加载模型2.3结果 1.sklearn模型的保存和加载API from sklearn.externals import joblib - 保存:joblib.dump(estimator, 'test.pkl...

汪雯琦的博客 2965

波士顿房价数据集——随机森林

波士顿房价数据链接:https://pan.baidu.com/s/1JPrcNl1AgNCKEHCjOGyHvQ 提取码:1234 import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn import metrics #评价函数库 import pandas as pd import numpy as np from sklearn.ensemble impo.

m0_57210162的博客 2817

【sklearn】GridSearchCV辅助Pipeline调参

实例 波士顿房价的预测。数据集是统计的 20 世纪 70 年代中期波士顿郊区房价的中位数,来源即sklearn.dataset.load_boston()。该例分析流程: 生成特征多项式、数据归一化、构建岭回归模型,并将这三步封装到Pipeline中 用 GridSearchCV,对 Pipeline中 PolynomialFeatures的 degree参数和岭回归模型的 alpha参数进行调优 用训练集进行模型拟合 用训练好的模型对测试集的预测准确度进行评估,评估指标采用R2_score 通过上面对

weixin_43298886的博客 1169

决策树回归实战:波士顿房价预测与模型调优

决策树是机器学习中常用的算法,既能处理分类问题也能解决回归任务。在回归场景中,决策树通过均方误差(MSE)或平均绝对误差(MAE)等指标进行节点划分,最终叶节点输出样本标签均值。这种结构使其特别适合处理非线性关系的数据,如房价预测等实际问题。评估回归模型时,R²分数是最核心的指标,它反映了模型解释目标变量变化的能力。通过波士顿房价数据集实战,可以学习到决策树回归的完整实现流程,包括数据准备、基础模型训练、学习曲线分析和网格搜索调参等关键步骤。掌握这些技术能够有效解决模型过拟合问题,提升预测准确率。

weixin_33695082的博客 363

【机器学习小实验4】支持向量机回归的波士顿房价预测

实验结果显示不同核函数对支持向量机的性能产生了显著影响。在这个特定的房价预测问题上,线性核函数表现最好,这可能是因为数据集的线性关系较为明显。因此,在使用支持向量机时,选择合适的核函数非常关键。MSE、MAE 和 R-squared 是评估回归模型性能的重要指标。MSE 和 MAE 衡量了预测误差的大小,而 R-squared 则描述了模型对目标变量的解释能力。综合考虑这些指标有助于更好地评估模型的表现。实验中未对数据进行标准化处理。

m0_69310669的博客 1899

波士顿房价预测实战与线性回归全栈代码精讲

设我们有 $ n $ 个样本,每个样本包含 $ p $ 个特征,则可以将多元线性回归模型写成向量形式:其中:- $ \mathbf{y} \in \mathbb{R}^n $:目标变量向量;- $ \mathbf{X} \in \mathbb{R}^{n \times (p+1)} $:设计矩阵,首列为全1(对应截距项 $ \beta_0 $);- $ \boldsymbol{\beta} \in \mathbb{R}^{p+1} $:未知参数向量;

weixin_42579969的博客 1182

波士顿房价预测数据及代码

本资源包含数据,代码,解释,相应的文件。代码是练习用的,文章中的代码都可以运行出来,是很好的一个练手项目。

机器学习作业-波士顿房价预测作业+源代码+文档说明

1、资源内容:机器学习作业-波士顿房价预测作业+源代码+文档说明 2、代码特点:内含运行结果,不会运行可私信,参数化编程、参数可方便更改、代码编程思路清晰、注释明细,都经过测试运行成功,功能ok的情况下才上传的。 3、适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。 4、作者介绍:某大厂资深算法工程师,从事Matlab、Python、C/C++、Java、YOLO算法仿真工作10年;擅长计算机视觉、 目标检测模型、智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、智能控制、路径规划、无人机等多种领域的算法仿真实验,更多源码,请上博主主页搜索。 -------------------------------------------------------------------------- -

ESP32搭配FT6236触摸屏:从芯片手册到完整I2C驱动代码的保姆级解析

本文详细解析了ESP32搭配FT6236触摸屏的I2C驱动开发过程,从芯片手册解读到完整代码实现。通过硬件架构分析、寄存器配置、I2C通信封装及触摸数据处理等步骤,提供了一套完整的驱动解决方案,帮助开发者快速掌握ESP32与触摸屏的交互技术。

weixin_29174385的博客 296

【codex教程】Codex 注册和使用教程(2026最新版)

Codex 是 OpenAI 推出的 AI 编程助手,可以帮助开发者阅读代码、修改代码、运行命令、生成测试、排查 bug、做代码审查,也可以在终端、IDE、Codex App 或网页端使用。根据 OpenAI 帮助中心,截至 2026 年 6 月,Codex 已包含在 Free、Go、Plus、Pro、Business、Edu 和 Enterprise 等 ChatGPT 方案中,但不同套餐的使用额度会有所不同。适合开发者在终端中使用。如果你想在命令行里使用 Codex,可以安装 Codex CLI。

与时俱进,开放共享 2万+

Zemax API 与 Python 的独立应用程序

Zemax 提供了多个 python 示例代码,位于 Documents\Zemax\ZOS-API Sample Code\Python。使用以下代码,它们保存在 ZOS 文件目录中:/Documents\Zemax\Samples\API\Python。此代码块定位已安装的 OpticStudio 版本,确定根目录,创建命名空间和类,加载局部变量。如果定义的镜头需要一些特殊的编辑,例如反转,一种快速方法是从位于编程选项卡的 ZOS-API 语法帮助中找到相应的命令。重新运行整个代码块,镜头方向会改变。

jkh920184196的博客 1979

LTspice introduction - 2 LTspice的界面

LTspice的界面主要有两个:Schematic Capture和Waveform Viewer Schematic Capture 它主要是用来画电路图的,画完电路图之后,添加一些dot 命令同时选择一些仿真的参数,点击执行(Run)Waveform Viewer 如果你的仿真不是op point的话,一个都会打开它,在它里面你可以画图相应的图形。 有两种方式可以在Viewer

贾亮的专栏 3223

可疑物品遗留检测系统:基于YOLOv8的智能安防解决方案

数据采集与标注模块:负责收集和标注可疑物品图像数据模型训练模块:基于YOLOv8算法训练检测模型推理检测模块:加载训练好的模型进行实时检测用户界面模块:提供友好的操作界面和可视化结果展示报警模块:当检测到可疑物品时触发报警机制:包含多种暴力场景和可疑物品SUAVe:监控场景下的异常行为与物品数据集:包含多种可疑物品自建数据集:针对特定场景收集的数据背包(backpack)手提箱(suitcase)无人看管的包裹(package)可疑箱子(box)

YOLO 435

基于BQ25895RTWR的锂电池充电管理硬件设计

BQ25895 是一款适用于单节锂离子电池和锂聚合物电池的高度集成型 5A 开关模式电池充电管理和系统电源路径管理器件。此类器件支持高输入电压快速充电。低阻抗电源路径对开关模式运行效率进行了优化、缩短了电池充电时间并延长了放电阶段的电池使用寿命。• 移动电源、移动 Wi-Fi 热点。• 便携式互联网器件。

corlin6688的博客 1089

30分钟上手YOLO26 nano:INT8量化部署到智能相机,推理速度提升43%

微调代码(仅需5行,数据集遵循COCO格式)data="custom_data.yaml", # 数据集配置文件epochs=20, # 少量轮数即可收敛batch=8,imgsz=640model.save("yolov26n_custom.pt") # 保存微调后的模型本文在30分钟内完成了YOLO26 nano的INT8量化与智能相机部署,核心步骤为环境准备→模型获取→ONNX导出→INT8量化→部署推理。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 989

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

上一篇: 损失函数的研究,二元分类和多分类(LR 和softmax)
下一篇: SVM 的俩种视角
PythonstartL
博客等级 码龄9年 31粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值