Advertisement
Danila_lipatov

CAP_curve_1_variable_1_flag

Jul 5th, 2023 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. import numpy as np
  2. import sklearn.datasets
  3. import sklearn.model_selection
  4. import sklearn.linear_model
  5. import numpy
  6. #import compue_auc
  7. import scipy.stats
  8. import random
  9. import pandas as pd
  10. #import numpy as np
  11. from scipy.stats import sem
  12. from sklearn.metrics import roc_auc_score
  13. import matplotlib.pyplot as plt
  14.  
  15. #https://github.com/APavlides/cap_curve/blob/master/cap_curve.py
  16.  
  17. data = pd.read_excel(r'ur_path')   
  18. """
  19.  
  20. if necessary to read specific sheet, use sheet_name = 'ur_sheet'
  21. pd.read_excel(r'ur_path', sheet_name = '')
  22.  
  23. """
  24.  
  25.  
  26. pred = data['pred']     #change for your column name/names
  27. y_test = data['flag']   #change for your column name
  28. # length of the test data
  29. total = len(y_test)
  30.  
  31. # Counting '1' labels in test data
  32. one_count = np.sum(y_test)
  33.  
  34. # counting '0' labels in test data
  35. zero_count = total - one_count
  36.  
  37. plt.figure(figsize=(10, 6))
  38.  
  39. # x-axis ranges from 0 to total people contacted
  40. # y-axis ranges from 0 to the total positive outcomes.
  41.  
  42. plt.plot([0, total], [0, one_count], c='b',
  43.          linestyle='--', label='Random Model')
  44. plt.legend()
  45.  
  46.  
  47. lm = [y for _, y in sorted(zip(pred, y_test), reverse = True)]
  48. x = np.arange(0, total + 1)
  49. y = np.append([0], np.cumsum(lm))
  50.  
  51. plt.plot(x, y, c = 'b', label = 'Random classifier', linewidth = 2)
  52.  
  53. plt.plot([0, one_count, total], [0, one_count, one_count],
  54.          c = 'grey', linewidth = 2, label = 'Perfect Model')
  55.  
  56. plt.savefig(f'test.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement