Skip to content

Instantly share code, notes, and snippets.

@yingminc
Created October 23, 2017 06:04
Show Gist options
  • Save yingminc/41239e1c0fcebd2ed4d6ad52f835365c to your computer and use it in GitHub Desktop.
Save yingminc/41239e1c0fcebd2ed4d6ad52f835365c to your computer and use it in GitHub Desktop.
visualization for ensemble weights, threshold and f1_score of binary classification
import pandas as pd
import numpy as np
from sklearn.metrics import confusion_matrix,f1_score
from bokeh.io import show, output_file
from bokeh.models import (ColumnDataSource,HoverTool,FixedTicker,PrintfTickFormatter)
from bokeh.plotting import figure
def prabtoclass(series,hold):
#turn probability to binary label by target threshold
return [1 if i>=hold else 0 for i in series]
def ensemble_weight(data, step, label, input1, input2):
#data: pandasDataframe; step: number of step to test between 0~1
#label: the corresct binary label #input1, input2: the two results to ensmble
weights = np.linspace(0,1,step)
thresholds = np.linspace(0,1,step)
x=[]
y=[]
z =[]
for i in weights:
for j in thresholds:
x.append(i)
y.append(j)
z.append(f1_score(d[label],prabtoclass(d[input1]*i+d[input2]*(1-i),j)))
return x, y, z
def visual_en(figure_title, x, y, z):
cource=ColumnDataSource({'x': x, 'y': y, 'z': z})
toolbar = 'hover,save,pan,box_zoom,reset,wheel_zoom'
alpha = [r*(1/max(z)) for r in z]
p = figure(title=figure_title, tools=toolbar, width=800, height=800)
p.rect('x','y', width=0.02, height=0.02, source=cource,fill_alpha = alpha, line_alpha = alpha)
hover = p.select_one(HoverTool)
hover.tooltips = [('weight','@x'),("threshold","@y"),('f1','@f1')]
p.xaxis.axis_label = 'weight'
p.yaxis.axis_label = "boolean treshold"
output_file(figure_title)
show(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment