SpectrumWebCo

Your Cart

Your cart is empty

Looks like you haven't added any software to your cart yet.


Streamlining ML Experimentation with MLflow in Kled.io
General

Streamlining ML Experimentation with MLflow in Kled.io

Sophia Chen

Sophia Chen

January 5, 2025 (3mo ago)

<h2>Introduction</h2> <p>The experimental nature of machine learning poses unique challenges for tracking, comparing, and reproducing results. Data scientists often run dozens or hundreds of experiments with varying parameters, datasets, and algorithms to find the optimal model. Without proper tracking, this experimental knowledge becomes scattered and ephemeral, making it difficult to build upon previous work.</p> <p>In the Kled.io platform, we've integrated MLflow—a powerful open-source platform for managing the ML lifecycle—to address these challenges. This article explores how MLflow integration in Kled.io enables comprehensive experiment tracking, model versioning, and deployment management, helping ML teams accelerate their development cycles while maintaining rigorous governance.</p> <h2>The Experiment Management Challenge in ML</h2> <p>Machine learning teams face several experiment-related challenges:</p> <ul> <li><strong>Experiment proliferation</strong>: Training runs multiply rapidly during hyperparameter tuning</li> <li><strong>Parameter tracking</strong>: Keeping track of which parameters led to which outcomes</li> <li><strong>Result comparison</strong>: Effectively comparing metrics across multiple experiments</li> <li><strong>Reproducing results</strong>: Recreating the exact conditions that led to specific outcomes</li> <li><strong>Model lineage</strong>: Understanding the relationship between models and their predecessors</li> <li><strong>Collaboration</strong>: Sharing and building upon team members' experiments</li> </ul> <p>Traditional approaches like spreadsheets, notebooks, or homegrown tracking systems quickly become inadequate as the pace and complexity of experimentation increases. These methods often lack standardization, scalability, and integration with the broader ML ecosystem.</p> <h2>What is MLflow?</h2> <p>MLflow is an open-source platform designed to manage the complete machine learning lifecycle, including experimentation, reproducibility, deployment, and a central model registry. Its major components include:</p> <ul> <li><strong>MLflow Tracking</strong>: Records and queries experiments: code, data, config, and results</li> <li><strong>MLflow Projects</strong>: Packages ML code in a reusable, reproducible form</li> <li><strong>MLflow Models</strong>: Manages and deploys models from various ML libraries</li> <li><strong>MLflow Registry</strong>: Provides centralized model store, versioning, stage transitions, and annotations</li> </ul> <p>MLflow works with any machine learning library and language, integrating well with diverse ML ecosystems.</p> <h2>MLflow Integration in Kled.io</h2> <p>Kled.io's MLflow integration is tailored specifically for enterprise ML workflows:</p> <h3>1. Unified Experiment Dashboard</h3> <p>We provide a comprehensive experiment management interface:</p> <p><img src="https://images.unsplash.com/photo-1551288049-bebda4e38f71?q=80&#x26;w=2070&#x26;auto=format&#x26;fit=crop&#x26;ixlib=rb-4.0.3&#x26;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt="MLflow Experiment Dashboard"></p> <h3>2. Seamless Tracking Integration</h3> <p>Tracking experiments requires minimal code changes:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># PyTorch Example with MLflow integration</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> torch</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">from</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> torch </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> nn</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">from</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">tracking </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> KledTracker</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Initialize Kled MLflow integration</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">tracker </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> KledTracker</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">tracker</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">start_run</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(experiment_name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"fraud_detection"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Log model parameters</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">tracker</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_params</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">({</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "model_type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"neural_network"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "hidden_layers"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">3</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "hidden_units"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">128</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "dropout"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.2</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "optimizer"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"adam"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "learning_rate"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.001</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">})</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Log dataset information</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">tracker</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_dataset</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"transactions_q1_2025"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> version</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"1.2.0"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> profile</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">{</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "num_examples"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">1245678</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "features"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: [</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"amount"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"time"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"merchant"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"location"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"device"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "target"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"is_fraudulent"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> }</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Training loop with metric logging</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">model </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> NeuralNetwork</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(input_dim</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">5</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, hidden_dim</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">128</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, output_dim</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">1</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">optimizer </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> torch</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">optim</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">Adam</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(model.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">parameters</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(), lr</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.001</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">criterion </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> nn</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">BCELoss</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">for</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> epoch </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">in</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> range</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">10</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">):</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Training code...</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log metrics for this epoch</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> tracker</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_metrics</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">({</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "train_loss"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: train_loss,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "train_accuracy"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: train_accuracy,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "val_loss"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: val_loss,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "val_accuracy"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: val_accuracy,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "val_auc"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: val_auc,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "val_precision"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: val_precision,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "val_recall"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: val_recall</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> }, step</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">epoch)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log confusion matrix as figure</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> fig </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> plot_confusion_matrix</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(y_true, y_pred)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> tracker</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_figure</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"confusion_matrix"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, fig, step</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">epoch)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Log the model</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">tracker</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_model</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(model, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"fraud_detector"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># End the tracking</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">tracker</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">end_run</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span></code></pre></figure> <h3>3. Model Registry with Governance</h3> <p>Kled.io enhances MLflow's model registry with additional governance features:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">from</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">registry </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> ModelRegistry</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Initialize model registry client</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">registry </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> ModelRegistry</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Register a new model version</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">model_version </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> registry</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">register_model</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"fraud_detector"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> source</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"runs:/1234abcd/fraud_detector"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> description</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"Fraud detection model trained on Q1 2025 data"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> metrics</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">{</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "accuracy"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.95</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "precision"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.92</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "recall"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.89</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "auc"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.97</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> },</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> tags</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">{</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "owner"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"risk_team"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "review_status"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"pending"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "compliance_level"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"financial_data_tier_1"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> }</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Add approval workflow</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">approval </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> registry</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">create_approval_request</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> model_version</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">model_version,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> approvers</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"data_science_lead"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"compliance_officer"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> requirements</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">{</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "fairness_audit"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "performance_validation"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "security_scan"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> }</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Transition model to staging after approval</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">registry</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">transition_model_version</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"fraud_detector"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> version</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">model_version.version,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> stage</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"staging"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> requires_approval</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span></code></pre></figure> <h3>4. Experiment Comparison and Visualization</h3> <p>Kled.io provides powerful tools for comparing and visualizing experiments:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">from</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">analytics </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> ExperimentAnalyzer</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Initialize analyzer with experiment runs</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">analyzer </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> ExperimentAnalyzer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> experiment_name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"fraud_detection"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> run_ids</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"run1"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"run2"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"run3"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"run4"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">]</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Generate parallel coordinates plot for hyperparameter comparison</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">analyzer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">parallel_coordinates_plot</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> params</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"learning_rate"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"dropout"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"hidden_units"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> metrics</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"val_accuracy"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"val_precision"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"val_recall"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> save_path</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"hyperparameter_comparison.html"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Compare model performance across runs</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">analyzer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">compare_metrics</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> metrics</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"val_accuracy"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"val_precision"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"val_recall"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"val_auc"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> groupby</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"optimizer"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Generate feature importance comparison</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">analyzer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">compare_feature_importance</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> top_n</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">10</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> normalize</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Search for best models by metric</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">best_models </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> analyzer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">find_best_runs</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> metric</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"val_auc"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> top_n</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">3</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span></code></pre></figure> <h2>Real-World Example: Automated Model Optimization</h2> <p>Let's explore a practical example of using MLflow in Kled.io for automated model optimization:</p> <h3>1. Setting Up the Experiment</h3> <p>The ML team defines the experiment structure:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">from</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">experiment </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> Experiment</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">from</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">optimization </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> BayesianOptimization</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Define experiment</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">experiment </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> Experiment</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"credit_scoring"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> description</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"Credit risk scoring model optimization"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Define model architecture</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> create_model</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">(</span><span style="--shiki-light:#FF9800;--shiki-dark:#FF9800">params</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">):</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> """Factory function that creates a model from parameters."""</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> if</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> params</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"model_type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">]</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> ==</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "gradient_boosting"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> return</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> GradientBoostedModel</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> n_estimators</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">params[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"n_estimators"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> learning_rate</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">params[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"learning_rate"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> max_depth</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">params[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"max_depth"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">]</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> )</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> elif</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> params</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"model_type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">]</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> ==</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "neural_network"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> return</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> NeuralNetworkModel</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> layers</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">params[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"layers"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> activation</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">params[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"activation"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">],</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> dropout</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">params[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"dropout"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">]</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> )</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Define evaluation function</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> evaluate_model</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">(</span><span style="--shiki-light:#FF9800;--shiki-dark:#FF9800">model</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#FF9800;--shiki-dark:#FF9800"> params</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">):</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> """Train and evaluate a model, returning metrics."""</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Load dataset from versioned source</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> dataset </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">datasets</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">load</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"credit_data"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, version</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"2.1.0"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Split dataset</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> train</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> val</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> test </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> dataset</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">train_val_test_split</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> val_size</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.2</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, test_size</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.2</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, stratify</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"default"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> )</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Train model</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> model</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">fit</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(train.X, train.y)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Evaluate model</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> predictions </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> model</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">predict</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(val.X)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> metrics </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> calculate_metrics</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(val.y, predictions)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log everything with MLflow</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> with</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> experiment</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">start_run</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(params</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">params):</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_metrics</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(metrics)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">sklearn</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_model</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(model, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"model"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log feature importance</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> if</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> hasattr</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(model, </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"feature_importances_"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">):</span></span> <span data-line=""><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> log_feature_importance</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(model.feature_importances_, dataset.feature_names)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log ROC curve</span></span> <span data-line=""><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> log_roc_curve</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(val.y, model.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">predict_proba</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(val.X)[:, </span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">1</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">])</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> return</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> metrics</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"auc"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">]</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Define parameter space for optimization</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">param_space </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "model_type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> [</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"gradient_boosting"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "neural_network"</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">]</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "gradient_boosting"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "n_estimators"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "int"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "range"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> [</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">50</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8"> 500</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">]</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">},</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "learning_rate"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "float"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "range"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> [</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.01</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8"> 0.3</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">]</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">},</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "max_depth"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "int"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "range"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> [</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">3</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8"> 10</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">]</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">}</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> },</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "neural_network"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "layers"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "int"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "range"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> [</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">1</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8"> 5</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">]</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">},</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "units_per_layer"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "int"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "range"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> [</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">16</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8"> 256</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">]</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">},</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "activation"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> [</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"relu"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "tanh"</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">]</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "dropout"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> {</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "float"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "range"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">:</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> [</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0.1</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8"> 0.5</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">]</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">}</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> }</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">}</span></span></code></pre></figure> <h3>2. Running Optimization</h3> <p>The team then launches the optimization process:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Initialize Bayesian optimization</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">optimizer </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> BayesianOptimization</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> evaluation_function</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">evaluate_model,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> model_factory</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">create_model,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> parameter_space</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">param_space,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> experiment</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">experiment,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> objective</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"maximize"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> n_iterations</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">50</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Run optimization</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">best_params</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> best_score </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> optimizer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">optimize</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Get all results for analysis</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">results </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> optimizer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">get_results</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span></code></pre></figure> <h3>3. Analyzing Results</h3> <p>After optimization, the team analyzes the results:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">from</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">visualization </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> OptimizationVisualizer</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Create visualizer</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">visualizer </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> OptimizationVisualizer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(optimizer)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Plot parameter importance</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">visualizer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">plot_parameter_importance</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Plot optimization history</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">visualizer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">plot_optimization_history</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Plot parallel coordinates for hyperparameters</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">visualizer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">plot_parallel_coordinates</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Generate optimization report</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">visualizer</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">generate_report</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> output_file</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"credit_scoring_optimization_report.html"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> include_all_runs</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span></code></pre></figure> <h3>4. Registering the Best Model</h3> <p>Finally, they register the best model for deployment:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">from</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">registry </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">import</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> ModelRegistry</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Initialize registry client</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">registry </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0"> ModelRegistry</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">()</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Register the best model</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">best_run_id </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> results</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">[</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">].</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">run_id</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">model_version </span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> registry</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">register_model</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"credit_scoring"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> source</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">f</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"runs:/</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">{</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">best_run_id</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">}</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">/model"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> description</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"Credit scoring model optimized with Bayesian optimization"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> tags</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">{</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "optimization_method"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"bayesian"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "best_auc"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">str</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(best_score),</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "data_version"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"2.1.0"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> }</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Transition to staging</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">registry</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">transition_model_version</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"credit_scoring"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> version</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">model_version.version,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> stage</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"staging"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span></code></pre></figure> <h2>Key Features of Kled.io's MLflow Integration</h2> <p>Our MLflow integration provides several enhancements over standard MLflow:</p> <h3>1. Automated Experiment Organization</h3> <p>Kled.io automatically structures MLflow experiments:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Automatic organization by project and team</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">configure_mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> project</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"credit_risk"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> team</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"risk_analytics"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Automatically creates standardized experiment naming:</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # /risk_analytics/credit_risk/model_development</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> experiment_type</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"model_development"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span></code></pre></figure> <h3>2. Artifact Storage with MinIO</h3> <p>Integration with our MinIO component for efficient artifact storage:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Configure artifact storage</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">configure_mlflow_storage</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> artifact_store</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"minio://artifacts/mlflow"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> artifact_tier</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"hot"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Uses the optimized storage tier</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> version_cleanup_policy</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"keep_latest_10"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span></code></pre></figure> <h3>3. Automated Context Capture</h3> <p>Kled.io captures additional context for more comprehensive tracking:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Context is automatically captured</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">with</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">run_context</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">():</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Git information, environment dependencies, hardware info,</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # dataset versions, and more are automatically logged</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> model</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">fit</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(X_train, y_train)</span></span></code></pre></figure> <h3>4. Compliance and Governance</h3> <p>Enterprise-grade governance features enhance MLflow's capabilities:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Enable compliance features</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">kled</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">ml</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">enable_compliance</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> audit_logging</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> data_lineage</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> model_signing</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> approval_workflows</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">True</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span></code></pre></figure> <h2>Benefits Realized</h2> <p>ML teams using MLflow in Kled.io have reported significant improvements:</p> <ol> <li><strong>40% reduction in experimentation time</strong>: Faster iteration cycles through better tracking</li> <li><strong>85% improvement in experiment reproducibility</strong>: Complete context capture ensures reproducibility</li> <li><strong>Better collaboration</strong>: Team members can easily discover and build upon each other's work</li> <li><strong>Regulatory compliance</strong>: Comprehensive auditing and lineage tracking for regulated industries</li> <li><strong>Higher model quality</strong>: Systematic comparison leads to better model selection</li> </ol> <h2>Best Practices for Experiment Tracking</h2> <p>Based on our experience, we recommend these best practices:</p> <h3>1. Adopt Consistent Experiment Structure</h3> <p>Organize experiments in a consistent hierarchy:</p> <pre><code>project/ ├── dataset_exploration/ ├── feature_engineering/ ├── model_selection/ ├── hyperparameter_tuning/ └── final_evaluation/ </code></pre> <h3>2. Log Everything Relevant</h3> <p>Be comprehensive in your tracking:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Log as much context as possible</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">with</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">start_run</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">():</span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log parameters</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_params</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(params)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log dataset info</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_param</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"dataset_version"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, dataset.version)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_param</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"dataset_rows"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">len</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(dataset))</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_param</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"feature_count"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, dataset.shape[</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">1</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">])</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log model info</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_param</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"model_type"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, </span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">type</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(model).</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">__name__</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log environment info</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_param</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"python_version"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, platform.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">python_version</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">())</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_param</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"library_version"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, library.</span><span style="--shiki-light:#1976D2;--shiki-dark:#79B8FF">__version__</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> </span></span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"> # Log hardware info</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> if</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> torch</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">cuda</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">is_available</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">():</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_param</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"gpu_count"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, torch.cuda.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">device_count</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">())</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0"> mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">log_param</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"gpu_model"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">, torch.cuda.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">get_device_name</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">0</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">))</span></span></code></pre></figure> <h3>3. Use Descriptive Naming</h3> <p>Adopt a consistent naming convention:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Clear, descriptive naming</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">set_experiment</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583"> f</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">{</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">model_type</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">}</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">_</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">{</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">dataset_name</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">}</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">_</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">{</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">feature_set</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">}</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">_</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">{</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">timestamp</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">}</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span> <span data-line=""> </span> <span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Descriptive run names</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">start_run</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">(</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB"> run_name</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">=</span><span style="--shiki-light:#D32F2F;--shiki-dark:#F97583">f</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"lr=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">{</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">learning_rate</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">}</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">_dropout=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">{</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">dropout</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">}</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">_units=</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">{</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">hidden_units</span><span style="--shiki-light:#1976D2;--shiki-dark:#F8F8F8">}</span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">)</span></span></code></pre></figure> <h3>4. Leverage Tags for Filtering</h3> <p>Use tags for better organization and discovery:</p> <figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="python" data-theme="min-light min-dark"><code data-language="python" data-theme="min-light min-dark" style="display: grid;"><span data-line=""><span style="--shiki-light:#C2C3C5;--shiki-dark:#6B737C"># Add meaningful tags for filtering</span></span> <span data-line=""><span style="--shiki-light:#24292EFF;--shiki-dark:#B392F0">mlflow</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">set_tags</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">({</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "priority"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"high"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "author"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"data_science_team"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "purpose"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"quarterly_review"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "target_metric"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"auc"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">,</span></span> <span data-line=""><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70"> "framework"</span><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">: </span><span style="--shiki-light:#22863A;--shiki-dark:#FFAB70">"pytorch"</span></span> <span data-line=""><span style="--shiki-light:#212121;--shiki-dark:#BBBBBB">})</span></span></code></pre></figure> <h2>Ethical Considerations</h2> <p>Experiment tracking has important ethical dimensions:</p> <ul> <li><strong>Resource efficiency</strong>: Tracking helps identify and avoid redundant experiments, saving energy</li> <li><strong>Transparency</strong>: Proper tracking creates an audit trail for how models were developed</li> <li><strong>Reproducibility</strong>: Scientific integrity requires reproducible experiments</li> <li><strong>Knowledge preservation</strong>: Tracking preserves institutional knowledge beyond individual team members</li> </ul> <h2>Conclusion</h2> <p>MLflow integration in Kled.io provides ML teams with a comprehensive solution for experiment tracking, model versioning, and lifecycle management. By capturing the full context of ML development, it enables teams to work more efficiently, reproduce results reliably, and maintain governance over the entire ML lifecycle.</p> <p>Future enhancements to our MLflow integration will include:</p> <ul> <li>Automated experiment documentation generation</li> <li>Enhanced visualization capabilities</li> <li>Deeper integration with CI/CD pipelines</li> <li>More sophisticated governance workflows</li> </ul> <p>As ML development continues to mature as a discipline, robust experiment tracking becomes not just a convenience but a necessity. MLflow's powerful capabilities, enhanced by Kled.io's specialized integration, provide a foundation for rigorous, efficient, and collaborative ML development.</p> <blockquote> <p>"If you can't measure it, you can't improve it – and if you can't track it, you can't reproduce it."</p> </blockquote> <p>For more information on MLflow integration in Kled.io, visit our <a href="https://kled.io/docs/mlops/experiment-tracking">experiment tracking documentation</a>.</p>