Skip to content

Instantly share code, notes, and snippets.

@doraneko94
Last active December 1, 2018 20:31
Show Gist options
  • Save doraneko94/9b558569c87446b30a8c072b87781bd0 to your computer and use it in GitHub Desktop.
Save doraneko94/9b558569c87446b30a8c072b87781bd0 to your computer and use it in GitHub Desktop.
The code of "Brier Score", "ePCP", "the separation plot". Please import from this file to your project. The "actual_outcome" is a list of real labels (0 or 1), and the "fitted_value" is a list of probabilities that the data's label is 1. This code requires "numpy" and "pandas".
from matplotlib import pyplot as plt
from matplotlib import patches
import pandas as pd
import numpy as np
def BrierScores(actual_outcome, fitted_value):
X = np.array(actual_outcome)
p = np.array(fitted_value)
B = sum((p-X)**2)/len(X)
return B
def ePCP(actual_outcome, fitted_value):
ps = 0
for X,p in zip(actual_outcome,fitted_value):
if X == 0:
ps += 1 - p
else:
ps += p
ePCP = ps / len(actual_outcome)
return ePCP
def SeparationPlot(fitted_value, actual_outcome):
data = np.vstack([fitted_value, actual_outcome])
data = data[:, np.argsort(data[0])]
l = len(actual_outcome)
ENoE = np.sum(data[0])
plt.figure(figsize=(10, 2))
ax = plt.axes()
fx = [p for p in data[0]]
x = range(len(fx))
lw = 1000 / len(x)
plt.xlim([-(lw/2),l-lw/2])
plt.ylim([0,1])
for i, y in enumerate(data[1]):
if y == 0:
ax.add_patch(patches.Rectangle(xy=(float(i)-lw/2, 0), width=lw, height=1.0, fc="w", ec='w', fill=True))
else:
ax.add_patch(patches.Rectangle(xy=(float(i)-lw/2, 0), width=lw, height=1.0, fc="g", ec='g', fill=True))
plt.plot(x,fx,color="#000000")
plt.plot([float(len(fitted_value)-ENoE)-lw/2, float(len(fitted_value)-ENoE)-lw/2], [0,1], color="r")
plt.title("Expected Number of Events = {:.2f}".format(ENoE))
plt.xlabel("rank")
plt.ylabel("probability")
plt.tight_layout()
plt.show()
return data, ENoE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment