【问题标题】:How to install/import nb21 for plotting cumulative gain curve?如何安装/导入 nb21 以绘制累积增益曲线?
【发布时间】:2023-02-04 11:38:34
【问题描述】:

我正在尝试使用库 nb21 绘制累积增益曲线。但是,在尝试安装它时,我找不到该库的任何 pip 安装源。这个图书馆死了吗?是否有用于此目的的替代库?

from nb21 import cumulative_gain, elast

gain_curve_test_non_param = cumulative_gain(cate_test_non_param, "cate", y=y, t=T)
plt.plot(gain_curve_test_non_param, color="C0", label="Non-Parametric")
plt.plot(gain_curve_test, color="C1", label="Parametric")
plt.plot([0, 100], [0, elast(test, y, T)], linestyle="--", color="black", label="Baseline")
plt.legend();
plt.title("R-Learner");

【问题讨论】:

    标签: python


    【解决方案1】:

    假设您所指的代码来自this section 一书,“勇敢与真实的因果推理”,“nb21”模块只是其 GitHub 存储库中 Python 脚本的名称https://github.com/matheusfacure/python-causality-handbook

    从他们的代码中,cumulative_gain() 函数被最低限度地定义为:

    import pandas as pd
    import numpy as np
    from toolz import curry
    
    @curry
    def elast(data, y, t):
        return (np.sum((data[t] - data[t].mean())*(data[y] - data[y].mean())) /
                np.sum((data[t] - data[t].mean())**2))
    
    def cumulative_gain(dataset, prediction, y, t, min_periods=30, steps=100):
        size = dataset.shape[0]
        ordered_df = dataset.sort_values(prediction, ascending=False).reset_index(drop=True)
        n_rows = list(range(min_periods, size, size // steps)) + [size]
        return np.array([elast(ordered_df.head(rows), y, t) * (rows/size) for rows in n_rows])
    

    来源:GitHub Permalink

    因此,为了让您的代码正常工作,您可以将这些函数定义复制到您的文件中,代码应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      相关资源
      最近更新 更多