Skip to content

Instantly share code, notes, and snippets.

@jeremy-rutman
Last active February 14, 2021 10:34
Show Gist options
  • Select an option

  • Save jeremy-rutman/487d771ddfeede07c59bcc0bf213acc5 to your computer and use it in GitHub Desktop.

Select an option

Save jeremy-rutman/487d771ddfeede07c59bcc0bf213acc5 to your computer and use it in GitHub Desktop.
venn diagram with three circles showing overlap order
from matplotlib_venn import venn2, venn3
import matplotlib.pyplot as plt
# venn2 subsets are a b ab (where a is n_unique_to_a, ab is intersection of a,b)
# called by venn2(subsets = (a_name,b_name,intersection_name), set_labels = (pair[0], pair[1],'both'), alpha = 0.5)
# venn3 subsets are a b ab c ca cb abc
# called by venn3(subsets = (a,b,ab,c,ca,cb,n),
# set_labels = (a_name,b_name,c_name), alpha = 0.5)
df = pd.read_excel('Analsys with Super Codes.xlsx', engine='openpyxl')
d1 = defaultdict(int)
d2 = defaultdict(int)
d3 = defaultdict(int)
d4 = defaultdict(int)
d5 = defaultdict(int)
for i,row in df.iterrows():
row
print(i,row)
allitems = []
for code,val in row.iteritems():
if val:
d1[code] += 1
allitems.append(code)
if len(allitems)>1:
c2 = combinations(allitems,2)
for comb in c2:
d2[comb] += 1
if len(allitems)>2:
c3 = combinations(allitems,3)
for comb in c3:
d3[comb] += 1
if len(allitems)>3:
c4 = combinations(allitems,4)
for comb in c4:
d4[comb] += 1
if len(allitems)>4:
c5 = combinations(allitems,5)
for comb in c5:
d5[comb] += 1
for pair,n in d2.items():
n_single1 = d1[pair[0]]-n
n_single2 = d1[pair[1]]-n
# fig=plt.figure()
plt.clf()
venn2(subsets = (n_single1,n_single2,n), set_labels = (pair[0], pair[1],'both'), alpha = 0.5)
plt.savefig(f'{pair[0]}_{pair[1]}.png')
for triplet,n in d3.items():
tot_single1 = d1[triplet[0]]
tot_single2 = d1[triplet[1]]
tot_single3 = d1[triplet[2]]
double12 = d2_all[(triplet[0],triplet[1])]
double23 = d2_all[(triplet[1], triplet[2])]
double31 = d2_all[(triplet[0], triplet[2])]
plt.clf()
a= tot_single1-double12-double31+n
b=tot_single2-double12-double23+n
ab=double12-n
c=tot_single3 - double31-double23+n
ca = double31-n
cb = double23-n
venn3(subsets = (a,b,ab,c,ca,cb,n),
set_labels = (triplet[0],triplet[1],triplet[2]), alpha = 0.5)
plt.savefig(f'{triplet[0]}_{triplet[1]}_{triplet[2]}.png')
@jeremy-rutman
Copy link
Copy Markdown
Author

Directly speaking to encourage settlement_Litigotiation_Prediction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment