楼主: CDA网校
187 0

基于 MLflow 实现实验追踪、模型日志记录与结果复现实操指南 [推广有奖]

管理员

已卖:189份资源

泰斗

16%

还不是VIP/贵宾

-

威望
3 级
论坛币
163129 个
通用积分
19656.1649
学术水平
373 点
热心指数
385 点
信用等级
348 点
经验
250378 点
帖子
8054
精华
23
在线时间
4612 小时
注册时间
2019-9-13
最后登录
2026-11-27

初级热心勋章

楼主
CDA网校 学生认证  发表于 2026-8-4 15:05:23 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
<!-- markdown css tag --><section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: black; padding: 0 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: PingFangSC-Light;"><figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/8a3910db-2138-43f7-ac6a-322271a28cab.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">试想你正在搭建机器学习模型:用简单模型得到了不错的基线效果后,你尝试用更复杂的模型提升性能。开发过程中,产品团队新增了一批可用数据,于是你又退回使用简易模型。完成大规模超参数调优后,团队建议你更换优化指标。当你终于训练完成模型,业务方却调整了项目方向,要求你整体重构模型。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">这是数据科学家的日常常态,尤其是在跨团队协作、需求方多变、项目优先级频繁调整的工作场景中。模型开发很容易陷入混乱:反复梳理需求、核对数据源、调整评估指标,耗时费力却毫无章法。在这种混乱的开发模式下,团队必须能够完整追踪模型全生命周期、快速复现实验结果,这就需要<strong style="font-weight: border; color: rgb(248,57,41);">模型治理</strong>体系的支撑。</p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">模型治理</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">模型治理是一套团队用于规范模型使用的完整框架,核心包含实验追踪、版本管理、结果可复现等能力。绝大多数团队都是在模型上线生产后,才意识到这项能力的重要性。搭建完善的模型治理流程,能够有效记录模型开发过程、线上运行模型、所用训练数据,帮助企业保障模型质量、满足合规要求、监控机器学习业务的落地效果。</p>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/94721d2f-a1bb-4ed5-95e7-2d7def9aa25a.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">想要确保上线生产的都是最优模型,就需要一套工具来统一管理模型及版本、追踪性能指标、复现实验结果。<strong style="font-weight: border; color: rgb(248,57,41);">MLflow</strong> 就此应运而生。MLflow 是一款开源的机器学习运维(ML Ops)平台,通过标准化的模型、指标、日志记录机制,实现灵活的模型开发、部署与全生命周期管理。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">本文将手把手讲解 MLflow 的安装部署、实验创建、模型日志记录等核心实操流程。</p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">MLflow 实操教程</span><span class="suffix"></span></h2>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">模型开发</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">本次教程将搭建一个基础线性回归模型,基于职位名称、公司规模等特征,预测数据岗位的年薪水平。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">数据集地址:<a href="https://www.kaggle.com/datasets/ruchi798/data-science-job-salaries" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">https://www.kaggle.com/datasets/ruchi798/data-science-job-salaries</a>(公共领域 CC0 协议)。笔者对数据集做了小幅修改,精简了部分特征的取值类型。本文模型仅用于演示 MLflow 功能,模型本身拟合效果较差,不具备实际落地价值。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;定义自变量与因变量</span><br>X&nbsp;=&nbsp;salary_data.drop(columns&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'salary_in_usd'</span>)<br>y&nbsp;=&nbsp;salary_data[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'salary_in_usd'</span>]<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;划分训练集与测试集</span><br>X_train,&nbsp;X_test,&nbsp;y_train,&nbsp;y_test&nbsp;=&nbsp;train_test_split(<br>&nbsp;&nbsp;X,&nbsp;y,&nbsp;random_state&nbsp;=&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">104</span>,&nbsp;test_size&nbsp;=&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.2</span>,&nbsp;shuffle&nbsp;=&nbsp;<span class="hljs-literal" style="color: #56b6c2; line-height: 26px;">True</span>)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;单独定义模型超参数,用于后续日志记录</span><br>params&nbsp;=&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">"fit_intercept"</span>:&nbsp;<span class="hljs-literal" style="color: #56b6c2; line-height: 26px;">True</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"positive"</span>:&nbsp;<span class="hljs-literal" style="color: #56b6c2; line-height: 26px;">False</span>}<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;训练线性回归模型</span><br>regr&nbsp;=&nbsp;linear_model.LinearRegression(**params)<br>regr.fit(X_train,&nbsp;y_train)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;模型预测</span><br>y_pred&nbsp;=&nbsp;regr.predict(X_test)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">需要注意:我们单独拆分定义模型超参数,是适配 MLflow 日志记录的关键操作。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">基于 MLflow 记录模型日志</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">首先在终端安装 MLflow 依赖,再启动本地服务,搭建可视化操作界面。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">pip&nbsp;install&nbsp;mlflow<br>mlflow&nbsp;server&nbsp;--host&nbsp;127.0.0.1&nbsp;--port&nbsp;8080<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">完成安装并启动本地服务后,即可开始记录模型日志。在记录日志前,需要先创建<strong style="font-weight: border; color: rgb(248,57,41);">实验(Experiment)</strong>,可将其理解为项目文件夹,用于统一收纳存储所有相关模型、指标、数据及日志。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">一个实验包含多次<strong style="font-weight: border; color: rgb(248,57,41);">运行(Run)</strong>,每次运行会独立记录所用模型、超参数、评估指标、产出文件等信息。多次运行日志可横向对比模型迭代效果与性能差异。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">下方代码可实现完整流程:创建实验、启动单次运行、记录调整后 R² 指标、保存模型超参数、添加自定义标签、归档训练模型。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span>&nbsp;mlflow<br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">from</span>&nbsp;mlflow.models&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span>&nbsp;infer_signature<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;绑定本地&nbsp;MLflow&nbsp;服务地址</span><br>mlflow.set_tracking_uri(uri&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"http://127.0.0.1:8080"</span>)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;创建全新&nbsp;MLflow&nbsp;实验</span><br>mlflow.set_experiment(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Logging&nbsp;Example"</span>)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;启动单次模型运行任务</span><br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span>&nbsp;mlflow.start_run(run_name&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"salary_baseline_regression"</span>)&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span>&nbsp;run:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;记录模型评估指标:调整后&nbsp;R²</span><br>&nbsp;&nbsp;&nbsp;&nbsp;mlflow.log_metric(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Adjusted&nbsp;R2"</span>,&nbsp;r2_score(y_test,&nbsp;y_pred))<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;记录模型超参数</span><br>&nbsp;&nbsp;&nbsp;&nbsp;mlflow.log_params(params)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;添加自定义标签,标注运行任务用途</span><br>&nbsp;&nbsp;&nbsp;&nbsp;mlflow.set_tag(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Model&nbsp;Type"</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Baseline"</span>)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;归档训练模型</span><br>&nbsp;&nbsp;&nbsp;&nbsp;model_info&nbsp;=&nbsp;mlflow.sklearn.log_model(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sk_model&nbsp;=&nbsp;regr,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;artifact_path&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'model'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;signature&nbsp;=&nbsp;infer_signature(X_train,&nbsp;regr.predict(X_train)),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input_example&nbsp;=&nbsp;X_train,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;registered_model_name&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"salary_baseline_regression"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">代码运行完成后,可通过链接访问 MLflow 可视化界面:<a href="http://localhost:8080/" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">http://localhost:8080/</a></p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">MLflow 可视化界面操作指南</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">打开页面后默认进入「实验」标签页,可查看已创建的实验、基线模型的运行记录,以及所有已记录的模型指标与参数。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">若页面未展示指标和参数,可通过顶部「列(Columns)」下拉菜单手动添加对应展示项。</p>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/3d08cd98-1f22-4afc-b53f-ce98f0921857.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">点击运行任务名称,可进入任务详情页,在概览标签页中清晰查看所有归档内容:模型文件、R² 指标、超参数等。</p>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/ebb85652-5d36-447b-b901-4f859bf43893.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">点击模型名称进入模型管理页面,可查看模型详情、补充描述、新增标签、追踪版本迭代。每一次新的模型运行,都会自动生成新版本号。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">自定义标签与描述可用于规范化管理模型,区分开发中、审核中、已上线等不同状态的模型,完善项目文档。</p>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/2602a89f-7cc4-49d8-9602-836e185bd174.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">以上为 MLflow 基础用法:创建实验与运行任务、记录模型、指标与参数。下文将介绍高级日志功能,实现流程自动化与精细化管理。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">高级日志功能</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">模型迭代过程中,所需追踪的指标会随模型结构、训练策略变化,手动增减指标日志十分繁琐。通过传入测试数据与预测结果,可实现 MLflow 指标自动记录,大幅简化流程。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">首先整合测试集数据与模型预测结果,生成评估数据集:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;整合特征数据与预测结果,用于模型评估</span><br>eval_data&nbsp;=&nbsp;X_test.copy()<br>eval_data[<span class="hljs-string" style="color: #98c379; line-height: 26px;">"salary"</span>]&nbsp;=&nbsp;y_test<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">将评估数据集传入 MLflow 并指定目标变量(薪资),即可实现三大核心能力:记录训练数据源、通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgb(271,93,108);">mlflow.evaluate()</code> 自动生成并记录全套评估指标、归档模型可视化结果等产出文件。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;绑定本地&nbsp;MLflow&nbsp;服务地址</span><br>mlflow.set_tracking_uri(uri&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"http://127.0.0.1:8080"</span>)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;基于&nbsp;Pandas&nbsp;数据集创建&nbsp;MLflow&nbsp;可识别数据对象</span><br>dataset&nbsp;=&nbsp;mlflow.data.from_pandas(<br>&nbsp;&nbsp;&nbsp;&nbsp;eval_data,&nbsp;source&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'kaggle'</span>,&nbsp;name&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"ds_salaries"</span>,&nbsp;targets&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"salary"</span>)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;创建专属实验</span><br>mlflow.set_experiment(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Salary&nbsp;Prediction"</span>)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;启动模型运行任务</span><br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">with</span>&nbsp;mlflow.start_run(run_name&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"salary_baseline_regression"</span>)&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span>&nbsp;run:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;记录训练所用数据集</span><br>&nbsp;&nbsp;&nbsp;&nbsp;mlflow.log_input(dataset,&nbsp;context&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"training"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;记录模型超参数</span><br>&nbsp;&nbsp;&nbsp;&nbsp;mlflow.log_params(params)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;归档模型可视化结果图片</span><br>&nbsp;&nbsp;&nbsp;&nbsp;mlflow.log_artifact(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Result.png'</span>)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;自定义任务标签</span><br>&nbsp;&nbsp;&nbsp;&nbsp;mlflow.set_tag(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Model&nbsp;Type"</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Baseline"</span>)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;归档训练模型</span><br>&nbsp;&nbsp;&nbsp;&nbsp;model_info&nbsp;=&nbsp;mlflow.sklearn.log_model(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sk_model&nbsp;=&nbsp;regr,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;artifact_path&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'model'</span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;signature&nbsp;=&nbsp;infer_signature(X_train,&nbsp;regr.predict(X_train)),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input_example&nbsp;=&nbsp;X_train,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;registered_model_name&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"salary_baseline_regression"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;model_uri&nbsp;=&nbsp;mlflow.get_artifact_uri(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"model"</span>)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;自动评估模型并记录全套指标</span><br>&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;mlflow.evaluate(model&nbsp;=&nbsp;model_uri,&nbsp;data&nbsp;=&nbsp;dataset,&nbsp;model_type&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"regressor"</span>,&nbsp;evaluators=[<span class="hljs-string" style="color: #98c379; line-height: 26px;">"default"</span>])<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">重新进入 MLflow 界面并打开本次运行任务,可查看系统自动生成的多项评估指标。同时支持多模型横向对比,通过自动化可视化图表直观展示性能差异。所有归档的可视化文件,均可在「产出文件(Artifacts)」标签页查看。此外,界面会完整展示输入数据的结构与类型,方便开发者在增减特征、更换数据源时,完整追溯数据迭代记录。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">MLflow 模型加载与复现</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">通过模型名称与版本号,可随时从 MLflow 模型仓库中加载归档模型,快速复现实验结果,也可用于线上模型的重新训练与迭代优化。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">model_name&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"salary_baseline_regression"</span><br>model_version&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"latest"</span><br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;从模型仓库加载指定模型版本</span><br>model_uri&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"models:/<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{model_name}</span>/<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{model_version}</span>"</span><br>model&nbsp;=&nbsp;mlflow.sklearn.load_model(model_uri)<br></code></pre>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">Databricks 平台集成</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">MLflow 已与 Databricks 平台深度原生集成,无需本地部署启动服务。Databricks 直接将 MLflow 可视化界面嵌入平台「实验」模块,同时拓展了更多高阶能力,支持生成式 AI 应用、智能体实验追踪,以及 AutoML 模型自动优化功能。完整官方文档可查阅:<a href="https://docs.databricks.com/aws/en/mlflow/" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">https://docs.databricks.com/aws/en/mlflow/</a></p>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">总结</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">模型治理是企业机器学习项目落地的必备能力,是保障模型质量、满足业务合规要求的核心基础。以 MLflow 为代表的实验追踪与监控工具,能够规范化模型治理流程,实现机器学习模型从开发、迭代到部署的全生命周期标准化管理。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">推荐学习书籍 《<strong style="font-weight: border; color: rgb(248,57,41);">CDA一级教材</strong>》适合CDA一级考生备考,也适合业务及数据分析岗位的从业者提升自我。完整电子版已上线CDA网校,累计已有10万+在读~ !</span><span class="suffix" style="display: none;"></span></h3>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/4502d41c-fb3a-43a8-bf50-9e14aedde094.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">免费加入阅读:<a href="https://edu.cda.cn/goods/show/3151?targetId=5147&amp;preview=0" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">https://edu.cda.cn/goods/show/3151?targetId=5147&amp;preview=0</a></span><span class="suffix" style="display: none;"></span></h3>
</section>
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:flow LOW regression experiment registered

您需要登录后才可以回帖 登录 | 我要注册

客服
GMT+8, 2026-11-27 11:09