WOE 转换器

WOE 代表证据权重。它包括检查特征 X 是否属于一系列区域 - 区间 -。结果是每个包含该特征的区间的标签。

一个简单的例子

X 是由前十个整数组成的向量。类 WOETransformer 检查每个整数是否属于两个区间,]1, 3[(左右开)和 [5, 7](左右闭)。第一个区间与权重 55 相关联,第二个区间与 107 相关联。

import os
import numpy as np
import pandas as pd
from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer
from onnxruntime import InferenceSession
import matplotlib.pyplot as plt
from skl2onnx import to_onnx
from skl2onnx.sklapi import WOETransformer

# automatically registers the converter for WOETransformer
import skl2onnx.sklapi.register  # noqa

X = np.arange(10).astype(np.float32).reshape((-1, 1))

intervals = [[(1.0, 3.0, False, False), (5.0, 7.0, True, True)]]
weights = [[55, 107]]

woe1 = WOETransformer(intervals, onehot=False, weights=weights)
woe1.fit(X)
prd = woe1.transform(X)
df = pd.DataFrame({"X": X.ravel(), "woe": prd.ravel()})
df
X woe
0 0.0 0.0
1 1.0 0.0
2 2.0 55.0
3 3.0 0.0
4 4.0 0.0
5 5.0 107.0
6 6.0 107.0
7 7.0 107.0
8 8.0 0.0
9 9.0 0.0


独热编码

转换器输出一个包含权重的列。但它可以返回每个区间一个列。

woe2 = WOETransformer(intervals, onehot=True, weights=weights)
woe2.fit(X)
prd = woe2.transform(X)
df = pd.DataFrame(prd)
df.columns = ["I1", "I2"]
df["X"] = X
df
I1 I2 X
0 0.0 0.0 0.0
1 0.0 0.0 1.0
2 55.0 0.0 2.0
3 0.0 0.0 3.0
4 0.0 0.0 4.0
5 0.0 107.0 5.0
6 0.0 107.0 6.0
7 0.0 107.0 7.0
8 0.0 0.0 8.0
9 0.0 0.0 9.0


在这种情况下,可以省略权重。输出是二进制的。

woe = WOETransformer(intervals, onehot=True)
woe.fit(X)
prd = woe.transform(X)
df = pd.DataFrame(prd)
df.columns = ["I1", "I2"]
df["X"] = X
df
I1 I2 X
0 0.0 0.0 0.0
1 0.0 0.0 1.0
2 1.0 0.0 2.0
3 0.0 0.0 3.0
4 0.0 0.0 4.0
5 0.0 1.0 5.0
6 0.0 1.0 6.0
7 0.0 1.0 7.0
8 0.0 0.0 8.0
9 0.0 0.0 9.0


转换为 ONNX

skl2onnx 为所有情况实现了一个转换器。

onehot=False

onx1 = to_onnx(woe1, X)
sess = InferenceSession(onx1.SerializeToString(), providers=["CPUExecutionProvider"])
print(sess.run(None, {"X": X})[0])
[[  0.]
 [  0.]
 [ 55.]
 [  0.]
 [  0.]
 [107.]
 [107.]
 [107.]
 [  0.]
 [  0.]]

onehot=True

onx2 = to_onnx(woe2, X)
sess = InferenceSession(onx2.SerializeToString(), providers=["CPUExecutionProvider"])
print(sess.run(None, {"X": X})[0])
[[  0.   0.]
 [  0.   0.]
 [ 55.   0.]
 [  0.   0.]
 [  0.   0.]
 [  0. 107.]
 [  0. 107.]
 [  0. 107.]
 [  0.   0.]
 [  0.   0.]]

ONNX 图

onehot=False

pydot_graph = GetPydotGraph(
    onx1.graph,
    name=onx1.graph.name,
    rankdir="TB",
    node_producer=GetOpNodeProducer(
        "docstring", color="yellow", fillcolor="yellow", style="filled"
    ),
)
pydot_graph.write_dot("woe1.dot")

os.system("dot -O -Gdpi=300 -Tpng woe1.dot")

image = plt.imread("woe1.dot.png")
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(image)
ax.axis("off")
plot woe transformer
(-0.5, 2674.5, 3321.5, -0.5)

onehot=True

pydot_graph = GetPydotGraph(
    onx2.graph,
    name=onx2.graph.name,
    rankdir="TB",
    node_producer=GetOpNodeProducer(
        "docstring", color="yellow", fillcolor="yellow", style="filled"
    ),
)
pydot_graph.write_dot("woe2.dot")

os.system("dot -O -Gdpi=300 -Tpng woe2.dot")

image = plt.imread("woe2.dot.png")
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(image)
ax.axis("off")
plot woe transformer
(-0.5, 2743.5, 5696.5, -0.5)

半线

区间可能只有一个端点定义,另一个端点可以是无穷大。

intervals = [[(-np.inf, 3.0, True, True), (5.0, np.inf, True, True)]]
weights = [[55, 107]]

woe1 = WOETransformer(intervals, onehot=False, weights=weights)
woe1.fit(X)
prd = woe1.transform(X)
df = pd.DataFrame({"X": X.ravel(), "woe": prd.ravel()})
df
X woe
0 0.0 55.0
1 1.0 55.0
2 2.0 55.0
3 3.0 55.0
4 4.0 0.0
5 5.0 107.0
6 6.0 107.0
7 7.0 107.0
8 8.0 107.0
9 9.0 107.0


使用相同的指令将其转换为 ONNX。

onxinf = to_onnx(woe1, X)
sess = InferenceSession(onxinf.SerializeToString(), providers=["CPUExecutionProvider"])
print(sess.run(None, {"X": X})[0])
[[ 55.]
 [ 55.]
 [ 55.]
 [ 55.]
 [  0.]
 [107.]
 [107.]
 [107.]
 [107.]
 [107.]]

脚本的总运行时间:(0 分钟 3.955 秒)

由 Sphinx-Gallery 生成的库