竞赛评价指标
The evaluation metric, M, for this competition is the mean of two measures of rank ordering: Normalized Gini Coefficient, G, and default rate captured at 4%, D.

本次比赛的评估指标M 是两个排名指标的平均值:归一化基尼系数G,以及4%的违约率D 。

M=0.5⋅(G+D)

4%捕获的默认率是在最高排名的4%预测中捕获的阳性标签(默认值)的百分比,表示敏感度/召回统计。对于子指标G和D,负标签的权重为20,以调整下采样。此度量的最大值为1.0。
计算此度量的Python代码可以在本笔记本中找到。链接

  1. import numpy as np
  2. import pandas as pd
  3. from pathlib import Path
  4. input_path = Path('/kaggle/input/amex-default-prediction/')
  5. def amex_metric(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:
  6. def top_four_percent_captured(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:
  7. df = (pd.concat([y_true, y_pred], axis='columns')
  8. .sort_values('prediction', ascending=False)) # 拼接预测值和实际值
  9. df['weight'] = df['target'].apply(lambda x: 20 if x==0 else 1) # 欺诈例检测*20
  10. four_pct_cutoff = int(0.04 * df['weight'].sum()) # 去整体得分的 4%
  11. df['weight_cumsum'] = df['weight'].cumsum() # cumsum() 函数,累加值
  12. df_cutoff = df.loc[df['weight_cumsum'] <= four_pct_cutoff] # 累加小于等于4%的书
  13. return (df_cutoff['target'] == 1).sum() / (df['target'] == 1).sum()
  14. def weighted_gini(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:
  15. df = (pd.concat([y_true, y_pred], axis='columns')
  16. .sort_values('prediction', ascending=False))
  17. df['weight'] = df['target'].apply(lambda x: 20 if x==0 else 1)
  18. df['random'] = (df['weight'] / df['weight'].sum()).cumsum()
  19. total_pos = (df['target'] * df['weight']).sum()
  20. df['cum_pos_found'] = (df['target'] * df['weight']).cumsum()
  21. df['lorentz'] = df['cum_pos_found'] / total_pos
  22. df['gini'] = (df['lorentz'] - df['random']) * df['weight']
  23. return df['gini'].sum()
  24. def normalized_weighted_gini(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:
  25. y_true_pred = y_true.rename(columns={'target': 'prediction'})
  26. return weighted_gini(y_true, y_pred) / weighted_gini(y_true, y_true_pred)
  27. g = normalized_weighted_gini(y_true, y_pred)
  28. d = top_four_percent_captured(y_true, y_pred)
  29. return 0.5 * (g + d)
  30. train_data = pd.read_csv(
  31. input_path / 'train_data.csv',
  32. index_col='customer_ID',
  33. usecols=['customer_ID', 'P_2'])
  34. train_labels = pd.read_csv(input_path / 'train_labels.csv', index_col='customer_ID')
  35. ave_p2 = (train_data
  36. .groupby('customer_ID')
  37. .mean()
  38. .rename(columns={'P_2': 'prediction'}))
  39. # Scale the mean P_2 by the max value and take the compliment
  40. ave_p2['prediction'] = 1.0 - (ave_p2['prediction'] / ave_p2['prediction'].max())
  41. print(amex_metric(train_labels, ave_p2)) # 0.572773

Markdown 和快捷键全覆盖

💡 Tips:语雀支持全功能 markdown 语法,可以点击文档编辑页右下角小键盘查看全部支持的语法和快捷键。

  • 支持导入导出 markdown 文件。
  • 支持自动识别粘贴的 markdown 格式内容转换为富文本。

    行内代码

    💡 Tips:可通过 markdown 语法(+ `code` + + 空格)或者快捷键 ctrl/cmd + E快速插入行内代码。

在文本中使用行内代码,可以顺畅地显示代码变量名。

代码块

💡 Tips:输入/代码块或点击上方工具栏点击上方工具栏image.png,选择「代码块」、插入代码卡片。

代码块同时支持多种颜色主题:

  1. export default class QuickSort extends Sort {
  2. sort(originalArray) {
  3. const array = [...originalArray];
  4. if (array.length <= 1) {
  5. return array;
  6. }
  7. // Init left and right arrays.
  8. const leftArray = [];
  9. const rightArray = [];
  10. // Take the first element of array as a pivot.
  11. const pivotElement = array.shift();
  12. const centerArray = [pivotElement];
  13. // Split all array elements between left, center and right arrays.
  14. while (array.length) {
  15. const currentElement = array.shift();
  16. // Call visiting callback.
  17. this.callbacks.visitingCallback(currentElement);
  18. if (this.comparator.equal(currentElement, pivotElement)) {
  19. centerArray.push(currentElement);
  20. } else if (this.comparator.lessThan(currentElement, pivotElement)) {
  21. leftArray.push(currentElement);
  22. } else {
  23. rightArray.push(currentElement);
  24. }
  25. }
  26. // Sort left and right arrays.
  27. const leftArraySorted = this.sort(leftArray);
  28. const rightArraySorted = this.sort(rightArray);
  29. return leftArraySorted.concat(centerArray, rightArraySorted);
  30. }
  31. }
export default class QuickSort extends Sort {
  sort(originalArray) {
    const array = [...originalArray];

    if (array.length <= 1) {
      return array;
    }

    // Init left and right arrays.
    const leftArray = [];
    const rightArray = [];

    // Take the first element of array as a pivot.
    const pivotElement = array.shift();
    const centerArray = [pivotElement];

    // Split all array elements between left, center and right arrays.
    while (array.length) {
      const currentElement = array.shift();

      // Call visiting callback.
      this.callbacks.visitingCallback(currentElement);

      if (this.comparator.equal(currentElement, pivotElement)) {
        centerArray.push(currentElement);
      } else if (this.comparator.lessThan(currentElement, pivotElement)) {
        leftArray.push(currentElement);
      } else {
        rightArray.push(currentElement);
      }
    }
    // Sort left and right arrays.
    const leftArraySorted = this.sort(leftArray);
    const rightArraySorted = this.sort(rightArray);

    return leftArraySorted.concat(centerArray, rightArraySorted);
  }
}
export default class QuickSort extends Sort {
  sort(originalArray) {
    const array = [...originalArray];

    if (array.length <= 1) {
      return array;
    }

    // Init left and right arrays.
    const leftArray = [];
    const rightArray = [];

    // Take the first element of array as a pivot.
    const pivotElement = array.shift();
    const centerArray = [pivotElement];

    // Split all array elements between left, center and right arrays.
    while (array.length) {
      const currentElement = array.shift();

      // Call visiting callback.
      this.callbacks.visitingCallback(currentElement);

      if (this.comparator.equal(currentElement, pivotElement)) {
        centerArray.push(currentElement);
      } else if (this.comparator.lessThan(currentElement, pivotElement)) {
        leftArray.push(currentElement);
      } else {
        rightArray.push(currentElement);
      }
    }
    // Sort left and right arrays.
    const leftArraySorted = this.sort(leftArray);
    const rightArraySorted = this.sort(rightArray);

    return leftArraySorted.concat(centerArray, rightArraySorted);
  }
}

数学公式

💡 Tips:输入 /公式或点击上方工具栏点击上方工具栏image.png,选择「公式」、插入公式卡片。

公式支持行内嵌套:评价指标代码注释 - 图3,也支持块级嵌入。
评价指标代码注释 - 图4

画板

💡 Tips:输入/画板或点击上方工具栏image.png,选择「画板」、绘制流程图、架构图等各种图形。

评价指标代码注释 - 图6
评价指标代码注释 - 图7

评价指标代码注释 - 图8

文本绘图

💡 Tips:输入/文本绘图点击上方工具栏image.png,选择「文本绘图」、插入文本绘图卡片。
支持 plantumlmermaid 等多种格式,点击预览可看到图形。具体代码样式见说明文档

评价指标代码注释 - 图10评价指标代码注释 - 图11