└── plot.py /plot.py: -------------------------------------------------------------------------------- 1 | import contextlib 2 | import numpy 3 | import random 4 | from matplotlib import pyplot 5 | 6 | pyplot.style.use('seaborn-whitegrid') 7 | pyplot.rcParams.update({'font.size': 15}) 8 | 9 | 10 | @contextlib.contextmanager 11 | def gfx_setup(filename, xlabel='Ability with thing A', ylabel='Ability with thing B'): 12 | fig, axes = pyplot.subplots(2, 2, sharex='col', sharey='row', 13 | gridspec_kw={'width_ratios': [7, 1], 14 | 'height_ratios': [1, 7], 15 | 'wspace': 0, 16 | 'hspace': 0}) 17 | for i, j in [(0, 0), (0, 1), (1, 1)]: 18 | axes[i][j].axis('off') 19 | ax = axes[1][0] 20 | fig.set_size_inches(7, 7) 21 | ax.set_xlabel(xlabel) 22 | ax.set_ylabel(ylabel) 23 | ax.set_aspect('equal', 'box') 24 | ax.set_xticks(range(-3, 4)) 25 | ax.set_yticks(range(-3, 4)) 26 | ax.grid(which='major', alpha=0.2) 27 | ax.tick_params(colors=(0, 0, 0, 0.2), labelsize='small') 28 | yield fig, axes 29 | ax.set_xlim([-4, 4]) 30 | ax.set_ylim([-4, 4]) 31 | handles, labels = ax.get_legend_handles_labels() 32 | fig.legend(handles, labels, loc='upper right', frameon=True, facecolor='white', bbox_to_anchor=(1, 1)) 33 | # fig.tight_layout() 34 | fig.savefig(filename) 35 | 36 | 37 | xys = numpy.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 1]], size=5000) 38 | xs, ys = xys.T # xys[:,0], xys[:,1] 39 | 40 | def scatter(axes, fn, color, label): 41 | axes[1][0].scatter([], [], color=color, label=label) # just for label 42 | axes[1][0].scatter([x for x, y in zip(xs, ys) if fn(x, y)], 43 | [y for x, y in zip(xs, ys) if fn(x, y)], 44 | alpha=0.1, color=color, edgecolors='none') 45 | hist_bins = numpy.arange(-4, 4, 0.25) 46 | axes[0][0].hist([x for x, y in zip(xs, ys) if fn(x, y)], 47 | bins=hist_bins, alpha=0.3, color=color, density=True) 48 | axes[1][1].hist([y for x, y in zip(xs, ys) if fn(x, y)], 49 | bins=hist_bins, alpha=0.3, color=color, density=True, orientation='horizontal') 50 | 51 | with gfx_setup('plot.png') as (fig, axes): 52 | scatter(axes, lambda x, y: True, 53 | color='b', label='Candidates we consider') 54 | 55 | with gfx_setup('plot2.png') as (fig, axes): 56 | scatter(axes, lambda x, y: x+y > 0.5, 57 | color='g', label='Candidates we bring in') 58 | scatter(axes, lambda x, y: x+y <= 0.5, 59 | color='r', label='Candidates we do not bring in') 60 | 61 | with gfx_setup('plot3.png') as (fig, axes): 62 | scatter(axes, lambda x, y: x+y > 1.5, 63 | color=(1, 0.5, 0), label='Candidates that do not want to talk to us') 64 | scatter(axes, lambda x, y: 0.5 < x+y <= 1.5, 65 | color='g', label='Candidates we bring in') 66 | scatter(axes, lambda x, y: x+y <= 0.5, 67 | color='r', label='Candidates we do not bring in') 68 | 69 | with gfx_setup('plot_confidence.png', xlabel='Competence', ylabel='Confidence') as (fig, axes): 70 | scatter(axes, lambda x, y: x+y > 1.5, 71 | color=(1, 0.5, 0), label='Candidates that do not want to talk to us') 72 | scatter(axes, lambda x, y: 1 < x and x+y <= 1.5, 73 | color='g', label='Candidates we bring in') 74 | scatter(axes, lambda x, y: x <= 1 and x+y <= 1.5, 75 | color='r', label='Candidates we do not bring in') 76 | 77 | with gfx_setup('plot_fancy_school.png', xlabel='Competence', ylabel='School fanciness') as (fig, axes): 78 | scatter(axes, lambda x, y: x+y > 1.5, 79 | color=(1, 0.5, 0), label='Candidates that do not want to talk to us') 80 | scatter(axes, lambda x, y: 2 < 2*x+y and x+y <= 1.5, 81 | color='g', label='Candidates we bring in') 82 | scatter(axes, lambda x, y: 2*x+y <= 2 and x+y <= 1.5, 83 | color='r', label='Candidates we do not bring in') 84 | 85 | def split_by_exp_model(company_kx, company_ky, market_kx=1, market_ky=1): 86 | func = lambda x, y: numpy.log(numpy.exp(company_kx*x + company_ky*y) / (1 + numpy.exp(market_kx*x + market_ky*y))) 87 | vs_sorted = sorted(func(x, y) for x, y in zip(xs, ys)) 88 | thresh_lo, thresh_hi = vs_sorted[len(vs_sorted)//3], vs_sorted[len(vs_sorted)*2//3] 89 | return (lambda x, y: func(x, y) < thresh_lo, 90 | lambda x, y: thresh_lo <= func(x, y) < thresh_hi, 91 | lambda x, y: thresh_hi <= func(x, y)) 92 | 93 | for company_kx, company_ky in [(1, 0), (1, 0.5), (0.75, 0.75), (2, 2), (0, 1), (0, 0), (0, 3)]: 94 | fn = 'exp_model_%.2f_%.2f.png' % (company_kx, company_ky) 95 | with gfx_setup(fn) as (fig, axes): 96 | bucket_1_f, bucket_2_f, bucket_3_f = split_by_exp_model(company_kx, company_ky) 97 | scatter(axes, bucket_1_f, color=(1, 0, 0), label='Least matching candidates') 98 | scatter(axes, bucket_2_f, color=(0.5, 0, 0.5), label='Middle group') 99 | scatter(axes, bucket_3_f, color=(0, 0, 1), label='Best matching candidates') 100 | pad_f = 0.9 101 | for dx, dy, color, label in [(1, 1, (0.4, 0.4, 0), 'Market'), (company_kx, company_ky, (0, 0.4, 0.4), 'Company')]: 102 | axes[1][0].arrow(0, 0, dx*pad_f, dy*pad_f, head_width=0.4, head_length=0.4, width=0.1, ec='white', fc=color, alpha=0.7) 103 | axes[1][0].text(dx, dy, label, color=color, va='bottom', ha='center', bbox=dict(facecolor='white', alpha=0.3, ec='none', pad=0.1)) 104 | 105 | fig.suptitle('Market vector = (%.2f, %.2f)\nCompany vector = (%.2f, %.2f)' % (1, 1, company_kx, company_ky), x=0.02, y=0.98, ha='left') 106 | --------------------------------------------------------------------------------