メインコンテンツへ移動

Shopper Purchase Prediction with MLflow and scikit-learn

MLflow で購入予測モデルを学習、評価、登録、配信します。品質ゲート、champion エイリアス、Docker でも動く REST API までそろっています。

  • Python
  • uv
  • MLflow
  • scikit-learn
  • pandas

スクリーンショット

スクリーンショット 1 / 7:タイトル カード: Shopper Purchase Prediction、MLflow 用の Python ソース コード。技術の一覧と、バージョン 1 が品質ゲートに合格して champion になるターミナルがあります。

タイトル カード: Shopper Purchase Prediction、MLflow 用の Python ソース コード。技術の一覧と、バージョン 1 が品質ゲートに合格して champion になるターミナルがあります。

買い切り価格

  • ₹449+GST(合計 ₹529.82)
  • $9
  • お支払いが確認されると、すぐにライブラリからダウンロードできます。
  • このプロジェクトの今後のリリースもすべて、追加費用なしで含まれます。
最新リリース
1.0.0 · 2026/11/28
ライセンス
Source Code Licence

概要

MLflow 3.18 でモデルの一生を扱う、Python の完全なソース コードです。オンライン ショップのどのセッションが購入に至るかを予測する scikit-learn モデルを学習し、品質ゲートで評価し、使用中のバージョンを上回ったときだけ Model Registry で champion に昇格させ、コマンド ライン、REST API、Docker コンテナーからセッションをスコアリングします。

モデルの学習は簡単な部分です。どのバージョンが使われているのか、なぜそうなったのか、そしてそのバージョンを正確に配信すること。そのために Shopper Purchase Prediction with MLflow and scikit-learn があり、すべての手順が MLflow に記録されます。

モデルの一生

  • 学習と記録。 scikit-learn のモデルを、シグネチャ、入力例、パッケージの正確なバージョン、テスト ROC AUC とともに記録します。最初の実行では 0.9258 でした。
  • 品質ゲートでの評価。 ROC AUC 0.90 以上、balanced accuracy 0.75 以上。champion がある場合は、それより balanced accuracy が 0.01 以上高いことが条件です。各評価は MLflow の実行として残ります。
  • 昇格。 promote は各モデルを新しいバージョンとして登録し、合格したときだけ champion エイリアスを移します。不合格のバージョンには quality_gate: failed タグが残り、champion はそのままです。
  • champion でスコアリング。 predict は champion になっているバージョンを読み込み、モデルのシグネチャに合わないファイルを拒否します。
  • 配信。 mlflow models serve が champion を REST API の後ろに置き、call-api がセッションを送ります。同じモデルは MLflow が作る Docker イメージでも動きます。3 つとも同じ確率を返しました。
  • どこでも動くことの確認。 validate は、モデル自身の要件から作った新しい環境で入力例を予測します。

安全で追跡可能

  • 安全なモデル形式。 モデルは pickle ではなく skops で保存し、プロジェクトが信頼する型だけを読み込みます。
  • 各モデルの元になったコード。 すべての実行がコードの Git コミットを記録します。
  • 検証済みのダウンロード。 データは一度だけダウンロードし、読み込む前に SHA-256 フィンガープリントを確認します。

拡張しやすいコード

  • 整理された Python パッケージ。 uv で管理する src 構成で、train、evaluate、promote、predict、validate、call-api の 6 つのコマンドがあります。
  • 設定を 1 か所に。 pydantic-settings が .env を読み込み、.env.example がすべての設定を説明します。
  • 正確なバージョン。 uv.lock がすべてのパッケージを固定します: MLflow 3.18.0、scikit-learn 1.9.1、skops 0.16.0、Python 3.15。
  • アーカイブにデータは含みません。 最初の実行でプロジェクトが公開データセットをダウンロードします。

含まれないもの

  • ホスティングはありません。 ご自身のコンピューターで動きます。1 つのコマンドで起動するトラッキング サーバーと、同じコンピューター上のモデル サーバーまたはコンテナーを使います。
  • テスト プロジェクトはありません。 上記の結果は実際の実行によるもので、アーカイブ内の自動テストによるものではありません。

7 つの手順で実行

  1. アーカイブを展開します。
  2. MLflow トラッキング サーバーを起動します。
  3. .env.example を .env にコピーします。
  4. uv sync でパッケージをインストールします。
  5. プロジェクトを Git に入れます。
  6. uv run train で学習します。
  7. uv run promote でモデルを昇格させます。

README には macOS と Linux、PowerShell 用のすべてのコマンドに加え、モデル サーバーと Docker の手順もあります。How to Set Up and Run the Shopper Purchase Prediction Project (新しいタブで開きます)(英語)では、各手順と表示される内容を紹介しています。macOS 上でアーカイブのクリーンなコピーからテストしました。

作り方を学ぶ

無料のチュートリアル シリーズ MLflow Tutorial for Machine Learning in Python (新しいタブで開きます)(英語)の第 6 回から第 9 回で、このコードを使ってモデルの記録と検証、評価と品質ゲート、Model Registry、モデルの配信を解説しています。

無料チュートリアルシリーズ

MLflow Tutorial for Machine Learning in Python

作りながら学びたい方へ。ブログの無料ステップガイドで、このプロジェクトの作り方を順を追って紹介しています。

シリーズを読む (新しいタブで開きます)

手に入るもの

アーカイブの中身が一目でわかります。

  • 完全なソース コードのバージョン 1.0.0: uv で管理する Python パッケージ
  • train コマンド: シグネチャ、入力例、要件、テスト ROC AUC とともに MLflow に記録する scikit-learn モデル
  • evaluate コマンドと、ROC AUC と balanced accuracy の品質ゲート (基準モデルとの比較を含む)
  • 各モデルを登録し、新しいバージョンが合格したときだけ champion エイリアスを移す promote コマンド
  • champion でセッションの CSV をスコアリングし、モデルのシグネチャで確認する predict コマンド
  • モデルの要件から作った新しい環境で予測する validate コマンド
  • mlflow models serve や MLflow の Docker イメージで配信したモデルを呼び出す call-api コマンド
  • CSV と REST リクエストの本文としての 3 つのサンプル セッション
  • pydantic-settings による .env の設定と、説明付きの .env.example
  • macOS、Linux、PowerShell のセットアップ、モデル サーバー、Docker、すべての設定、データセットのクレジットをまとめた README

使用している技術

開発と動作確認に使った実行環境とバージョンです。

  • Python3.15.2
  • uv0.13.10
  • MLflow3.18.0
  • scikit-learn1.9.1
  • pandas3.1.1
  • NumPy2.5.5
  • skops0.16.0
  • pydantic-settings2.16.3
  • Requests2.34.2
  • Docker Engine29.9.3

アーカイブの中身

展開したあとに手元に残るファイル構成です。

shopper-purchase-prediction-mlflow-scikit-learn/

  • src/
    • shopper_purchase_prediction/
      • __init__.py
      • call_api.py
      • code_version.py
      • config.py
      • data.py
      • evaluate.py
      • model.py
      • predict.py
      • promote.py
      • registry.py
      • train.py
      • validate.py
  • examples/
    • sessions.csv
    • sessions.json
  • .env.example
  • .gitignore
  • .python-version
  • pyproject.toml
  • uv.lock
  • README.md

必要になるもの

動かすために必要なアカウントやサービスです。あとから驚くことはありません。

  • Python 3.15 とパッケージをインストールしてくれる uv
  • 各実行がコードのコミットを記録するための Git
  • MLflow トラッキング サーバー: README の手順で、1 つのコマンドでお使いのコンピューターに起動できます
  • 最初のインストール (約 580 MB のパッケージ) と、データをダウンロードする最初の実行のためのインターネット接続
  • 任意: モデルをコンテナーで動かすための Docker

リリース

これまでのすべてのバージョンを新しい順に並べています。

  1. 1.0.0最新

    完全なソース コードの最初のリリースです。

    • オンライン ショップのどのセッションが購入に至るかを予測する scikit-learn モデルを学習し、MLflow 3.18.0 に記録します。
    • モデルを品質ゲートで評価して登録し、合格したバージョンだけを champion に昇格させます。
    • champion でセッションをコマンド ライン、mlflow models serve、Docker イメージからスコアリングします。
    • アーカイブから予測を配信する champion までを案内する README を含みます。

Source Code Licence

利用できる範囲

What you may and may not do with the source code you buy.

ライセンスの全文を読む
  1. What this licence covers

This licence governs every file included in a product you buy from Dewiride Store: source code, configuration, database scripts, sample data, documentation and any other file in the download. It takes effect when your payment is confirmed and applies to every release of that product that reaches your library. Files that reach your library through an annual updates membership are governed in exactly the same way.

It forms part of the Terms of Sale and Use. Where a product page states different or additional licence terms for that product, those terms prevail for that product. The membership page may likewise state different or additional licence terms for the files a membership carries, and those terms then prevail for those files.

  1. The licence we grant you

We grant you a perpetual, worldwide, non-exclusive, non-transferable licence to use, reproduce, adapt, translate and modify the files, and to include the result in your own work, on the conditions set out below.

Perpetual means it does not expire. Worldwide means it is not limited to any country. Non-exclusive means we license the same product to other customers. Non-transferable means you cannot pass it on to someone else.

The licence is granted to you — either you personally, or the organisation you identified when buying. Everyone working within that organisation may use the files for that organisation’s projects. Nobody outside it may.

  1. What you may do
  • Use and change the files in your own projects, commercial or otherwise, with no limit on the number of projects.
  • Build a product or a service on them and charge your own customers for it.
  • Deploy the result for a client, as part of work you are doing for that client.
  • Distribute the result in compiled, deployed or otherwise non-source form.
  • Keep as many back-up copies as you reasonably need.
  • Study, observe and test how the code works, and obtain the information you need to make it work with your own software.

The last two are yours by law under sections 52(1)(aa) to (ac) of the Indian Copyright Act, 1957, and nothing in this licence is intended to take them away.

  1. What you may not do
  • Resell, sublicense, redistribute or publish the files themselves, in whole or in substantial part, in source form.
  • Include them in a template, starter kit, course, tutorial repository or component library that you sell or give away, where the code is the thing being supplied.
  • Share your account, your download links or your invoices with anyone outside your organisation.
  • Publish the files in a public repository, or in any other place where they can be obtained without buying them.
  • Remove or obscure the copyright and attribution notices in the source files.
  • Use the files to build something whose main purpose is to compete with Dewiride Store as a supplier of the same code.

The distinction that matters is simple: you may sell what you build with the code; you may not sell the code.

  1. Third-party components

Our products depend on open-source libraries, frameworks and services that we did not write. Those remain under their own licences, which we do not and cannot change. Where a product includes or requires such a component, its licence is named in the product’s documentation, and complying with it is your responsibility.

  1. Releases

Every release of a product you own is covered by this same licence and appears in your library at no extra cost. A release you have already downloaded remains licensed to you even if we later stop publishing the product. A release that reached your library through an annual updates membership is licensed to you on these same terms, and stays licensed after the period that carried it has ended.

  1. Ownership

We keep ownership of the files and of the copyright in them. You own whatever you create with them: your changes, your additions and the product you build are yours.

  1. If this licence ends

This licence ends if you materially breach it and do not put the breach right within thirty days of us asking you to in writing. It also ends if your payment is reversed or refunded, because the purchase it depends on no longer stands. Where a membership period is refunded or reversed, the licence ends for the releases that period carried and for nothing else: anything you bought separately, or that another period carried, is unaffected.

If it ends, you must stop using the files and delete the copies you hold. Work you had already delivered to a client before that point may continue in use, so that ending this licence never breaks something your own customer relies on.

  1. No warranty beyond the description

The warranties and limits in section 19 and section 20 of the Terms of Sale and Use apply to this licence as well.

  1. Governing law

This licence is governed by the law of India, and section 24 of the Terms of Sale and Use applies to any dispute about it. If a translation and the English version differ in meaning, the English version governs.