Git Product home page Git Product logo

intro_ds's Issues

逻辑回归模型的评估指标——程序清单中TP、FP、FN、TN的计算的疑惑

唐老师,
在阅读到程序清单5-7时,我有个疑惑:

def evaluation(re):
    """
    计算预测结果的查准查全率以及f1
    参数
    ----
    re :DataFrame,预测结果,里面包含两列:真实值‘lable_code’、预测值‘pred’
    """
    bins = np.array([0, 0.5, 1])
    label = re["label_code"]
    pred = re["pred"]
    tp, fp, fn, tn = np.histogram2d(label, pred, bins=bins)[0].flatten()
    precision = tp / (tp + fp)  # 0.951
    recall = tp / (tp + fn)  # 0.826
    f1 = 2 * precision * recall / (precision + recall)  # 0.884
    print("查准率: %.3f, 查全率: %.3f, f1: %.3f" % (precision, recall, f1))

tp, fp, fn, tn = np.histogram2d(label, pred, bins=bins)[0].flatten()

我查阅了np.histogram2d的帮助文档,我觉得返回的顺序应当是tn,fp,fn,tp呀?

image

这是我认为返回的H:
image
H[0][0]对应TN,H[0][1]对应FP,H[1][0]对应FN,H[1][1]对应TP。

我也用一个小例子试了一下(这个例子里的tp=3, fp=2, fn=1, tn=1)

bins=np.array([0, 0.5, 1])
x=pd.Series([0, 1, 0, 1, 1, 1, 0])
y=pd.Series([1, 1, 0, 0, 1, 1, 1])
a, b, c, d = np.histogram2d(x,y,bins=bins)[0].flatten()

运行结果如下:

In [119]: a,b,c,d
Out[119]: (1.0, 2.0, 1.0, 3.0)

我不知道是否np.histogram2d的帮助文档没有理解到位?还请唐老师指点一二。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.