0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで月別件数と構成比を分けて検算する:分母ゼロもテストする

0
Posted at

Ofox運営側として、公開教材の月次問い合わせ比較を小さなPython例にしました。入力は架空の集計済みデータです。LLMもAPIも呼ばず、件数・構成比・ポイント差を検算します。分類や重複判定の正しさは、このコードでは検証しません。

入力と前提

元教材は22行から確認済みの重複インポート2行だけを除き、9月8件、10月12件を残します。各問い合わせには1分類を割り当て、「要確認」も分母に含めます。同一顧客の別チケットを重複扱いしないことが前提です。

以下をPython 3.9以降でcheck_shares.pyとして保存します。標準ライブラリだけを使います。

from fractions import Fraction


def share(count, total):
    if type(count) is not int or type(total) is not int:
        raise TypeError("integer counts required")
    if not 0 <= count <= total:
        raise ValueError("require 0 <= count <= total")
    return None if total == 0 else Fraction(count, total)


sep = [3, 2, 1, 1, 1]
oct_ = [3, 3, 2, 3, 1]
assert sum(sep) == 8 and sum(oct_) == 12
assert share(0, 0) is None
assert share(3, 8) == Fraction(3, 8)
assert share(3, 12) == Fraction(1, 4)
assert share(2, 8) == share(3, 12)
for invalid in [(True, 8), (1, 0), (-1, 8), (9, 8)]:
    try:
        share(*invalid)
    except (TypeError, ValueError):
        pass
    else:
        raise AssertionError(invalid)

for theme, a, b in zip(
    ["export", "billing", "onboarding", "performance", "needs_review"],
    sep, oct_
):
    before, after = share(a, sum(sep)), share(b, sum(oct_))
    delta = (after - before) * 100
    print(f"{theme}: {a}/8 -> {b}/12, {float(delta):+.2f} pp")

実行と期待結果

python3 check_shares.pyで実行します。exportは3/8 -> 3/12, -12.50 pp、billingは2/8 -> 3/12, +0.00 ppです。件数が同じでも割合は変わり、件数が増えても割合は同じ場合があります。計算はFractionで行い、表示時だけ丸めます。

分母ゼロでは割合をゼロにせずNoneを返します。この出力ループは両月とも正の分母がある固定例用です。実データでNoneを受け取った場合は差を計算せず「算出不可」と表示してください。boolを整数として受け付けない点もテストしています。

この検算で分からないこと

分類定義、収集チャネル、期間が違えば、計算が正しくても比較の意味は変わります。9月と10月の日数差もあるため、構成比は一日あたりの問い合わせ量ではありません。顧客参照IDのユニーク数は別に集計し、問い合わせの割合を顧客全体の割合へ読み替えないでください。

元教材の出力ブックは静的な計算値です。データを更新したら再計算が必要です。実務では各値から元の行IDへ戻れる照合表を保存し、未知分類や除外行の欠落も確認します。この例は分類精度や業務上の改善を実証するものではありません。

入力CSV、分類定義、元の出力例(英語):https://ofox.ai/blog/ai-customer-feedback-monthly-comparison/?utm_source=qiita&utm_medium=organic_social&utm_campaign=echo_gpt6_202611&utm_content=20261122_ai-customer-feedback-monthly-comparison_technical_tutorial

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?