In [1]:
import pandas as pd
import networkx as nx
dfattributes=pd.read_csv('politician-data.csv', sep='\t')
In [2]:
def makenetwork (revision):
with open(revision , 'rb') as inf:
next(inf, '')
G= nx.read_edgelist(inf, delimiter=',', create_using=nx.DiGraph(), nodetype=int, encoding="utf-8")
return G
In [ ]:
networklist=[]
for j in range (2001,2017):
for i in range (1,13):
if i>9:
networklist.append(makenetwork(str(j) + '_' + str(i) + '.csv'))
else :
networklist.append(makenetwork(str(j) + '_0' + str(i) + '.csv'))
In [10]:
def Homophilie (G):
Dict = dict(zip(dfattributes['ID'], dfattributes['gender']))
idlist=G.nodes()
GenderDict=dict ([(k, Dict[k]) for k in idlist])
nx.set_node_attributes(G,'gender',GenderDict)
ff=0
mm=0
fm=0
mf=0
for n1, n2 in G.edges_iter():
if G.node[n1]['gender']=='female':
if G.node[n2]['gender']=='female':
ff=ff+1
if G.node[n2]['gender']=='male':
fm=fm+1
if G.node[n1]['gender']=='male':
if G.node[n2]['gender']=='female':
mf=mf+1
if G.node[n2]['gender']=='male':
mm=mm+1
ffproportion=0
fmproportion=0
mmproportion=0
mfproportion=0
if (ff+fm)!= 0 :
ffproportion=(ff/(ff+fm))*100
fmproportion=(fm/(ff+fm))*100
if (mm+mf)!= 0 :
mmproportion=(mm/(mm+mf))*100
mfproportion=(mf/(mm+mf))*100
FemaleNumber=0
MaleNumber=0
for n in G.nodes_iter():
if G.node[n]['gender']=='female':
FemaleNumber=FemaleNumber+1
if G.node[n]['gender']=='male':
MaleNumber=MaleNumber+1
Total=FemaleNumber+MaleNumber
FemaleProportion=0
MaleProportion=0
if Total!=0 :
FemaleProportion=(FemaleNumber/Total)*100
MaleProportion=(MaleNumber/Total)*100
result=[round(ffproportion,3),round(FemaleProportion,3),round(mfproportion,3),round(mmproportion,3),round(MaleProportion,3),round(fmproportion,3)]
return result
In [44]:
Homo=[]
for i in range(0,192):
a=networklist[i]
Homo.append(Homophilie (a))
In [51]:
FFLinks=[]
for i in range(0,192):
FFLinks.append(Homo [i][0])
In [52]:
FP=[]
for i in range(0,192):
FP.append(Homo [i][1])
In [53]:
MFLinks=[]
for i in range(0,192):
MFLinks.append(Homo [i][2])
In [182]:
from datetime import date
import plotly.plotly as py
import plotly.graph_objs as go
RevisionDate=[day.isoformat() for day in rrule(MONTHLY, dtstart=date(2001,1,1), until=date.today())]
py.sign_in('lily1363', 'bCswA9XuSCwedV9u5fJc')
trace0 = go.Scatter(
x = RevisionDate,
y = FFLinks,
mode = 'lines',
name = 'Female to Female Links'
)
trace1 = go.Scatter(
x = RevisionDate,
y = FP,
mode = 'lines',
name = 'Female Pages'
)
trace2 = go.Scatter(
x = RevisionDate,
y = MFLinks,
mode = 'lines',
name = 'Male to Female Links'
)
data = [trace0,trace1,trace2]
layout = dict(title = 'Gender Homophily',
xaxis = dict(title = 'Revision'),
yaxis = dict(title = 'Percentage'),
)
fig = dict(data=data, layout=layout)
# Plot and embed in ipython notebook!
py.iplot(fig, filename='line-mode')
Out[182]:
In [55]:
MMLinks=[]
for i in range(0,192):
MMLinks.append(Homo [i][3])
In [56]:
MP=[]
for i in range(0,192):
MP.append(Homo [i][4])
In [57]:
FMLinks=[]
for i in range(0,192):
FMLinks.append(Homo [i][5])
In [184]:
trace0 = go.Scatter(
x = RevisionDate,
y = MMLinks,
mode = 'lines',
name = 'Male to Male Links'
)
trace1 = go.Scatter(
x = RevisionDate,
y = MP,
mode = 'lines',
name = 'Male Pages'
)
trace2 = go.Scatter(
x = RevisionDate,
y = FMLinks,
mode = 'lines',
name = 'Female to Male Links'
)
data = [trace0,trace1,trace2]
layout = dict(title = 'Gender Homophily',
xaxis = dict(title = 'Revision'),
yaxis = dict(title = 'Percentage'),
)
fig = dict(data=data, layout=layout)
# Plot and embed in ipython notebook!
py.iplot(fig, filename='line-mode')
Out[184]:
In [59]:
def GenderNumber(G):
F=0
M=0
for n in G.nodes_iter():
if G.node[n]['gender']=='female':
F=F+1
if G.node[n]['gender']=='male':
M=M+1
return [F,M]
In [61]:
GenNum=[]
for i in range(0,192):
a=networklist[i]
GenNum.append(GenderNumber (a))
In [62]:
FNumber=[]
for i in range(0,192):
FNumber.append(GenNum [i][0])
In [63]:
MNumber=[]
for i in range(0,192):
MNumber.append(GenNum [i][1])
In [65]:
trace0 = go.Scatter(
x = RevisionDate,
y = FNumber,
mode = 'lines',
name = 'Number of Females'
)
trace1 = go.Scatter(
x = RevisionDate,
y = MNumber,
mode = 'lines',
name ='Number of Males'
)
data = [trace0, trace1]
layout = dict(title = 'Number of Females and Males',
xaxis = dict(title = 'Revision'),)
fig = dict(data=data, layout=layout)
# Plot and embed in ipython notebook!
py.iplot(fig, filename='line-mode')
Out[65]:
In [168]:
import numpy as np
def indegree(G):
F=[]
M=[]
a=G.in_degree()
for n in G.nodes_iter():
if G.node[n]['gender']=='female':
F.append(a[n])
if G.node[n]['gender']=='male':
M.append(a[n])
meanf=0
meanm=0
stdf=0
stdm=0
if F:
meanf=np.mean(F)
stdf=np.std(F)/13.85
if M:
meanm=np.mean(M)
stdm=np.std(M)/13.85
return [meanf,meanm,stdf,stdm]
In [169]:
InDeg=[]
for i in range(0,192):
a=networklist[i]
InDeg.append(indegree (a))
In [170]:
AFIndegree=[]
VFIndegree=[]
for i in range(0,192):
AFIndegree.append(InDeg [i][0])
VFIndegree.append(InDeg [i][2])
In [171]:
AMIndegree=[]
VMIndegree=[]
for i in range(0,192):
AMIndegree.append(InDeg [i][1])
VMIndegree.append(InDeg [i][3])
In [173]:
trace0 = go.Scatter(
x = RevisionDate,
y = AFIndegree,
mode = 'lines',
name = 'Average In-degree of Females'
)
trace01 = go.Scatter(
x = RevisionDate,
y = [x + y for x, y in zip(VFIndegree, AFIndegree)],
mode = 'lines',
marker=dict(color="444"),
line=dict(width=0),
fillcolor='rgba(68, 68, 68, 0.3)',
fill='tonexty'
)
trace02 = go.Scatter(
x = RevisionDate,
y = [x - y for x, y in zip(AFIndegree,VFIndegree)],
mode = 'lines',
marker=dict(color="444"),
line=dict(width=0),
fillcolor='rgba(68, 68, 68, 0.3)',
fill='tonexty'
)
trace1 = go.Scatter(
x = RevisionDate,
y = AMIndegree,
mode = 'lines',
name ='Average In-degree of Males'
)
trace11 = go.Scatter(
x = RevisionDate,
y = [x + y for x, y in zip(VMIndegree, AMIndegree)],
mode = 'lines',
marker=dict(color="444"),
line=dict(width=0),
fillcolor='rgba(68, 68, 68, 0.3)',
fill='tonexty'
)
trace12 = go.Scatter(
x = RevisionDate,
y = [x - y for x, y in zip(AMIndegree,VMIndegree)],
mode = 'lines',
marker=dict(color="444"),
line=dict(width=0),
fillcolor='rgba(68, 68, 68, 0.3)',
fill='tonexty'
)
data = [trace0,trace01,trace02,trace1,trace11,trace12]
layout = dict(title = 'Average In-degree',
xaxis = dict(title = 'Revision'),)
fig = dict(data=data, layout=layout)
# Plot and embed in ipython notebook!
py.iplot(fig, filename='line-mode')
Out[173]:
In [177]:
AFIndegreeY=[]
AMIndegreeY=[]
VFIndegreeY=[]
VMIndegreeY=[]
for i in range(0,200,12):
if (i-1)>0:
AFIndegreeY.append(AFIndegree[i-1])
AMIndegreeY.append(AMIndegree[i-1])
VFIndegreeY.append(VFIndegree[i-1])
VMIndegreeY.append(VMIndegree[i-1])
In [186]:
RevisionDateYear=['2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016']
trace1 = go.Bar(
x=RevisionDateYear,
y=AFIndegreeY,
error_y=dict(
type='data',
array=VFIndegreeY
),
name='Average In-degree of Females'
)
trace2 = go.Bar(
x=RevisionDateYear,
y=AMIndegreeY,
error_y=dict(
type='data',
array=VMIndegreeY
),
name='Average In-degree of Males'
)
data = [trace1, trace2]
layout = go.Layout(
barmode='group',
title='Average In-degree',
xaxis = dict(title = 'Revision'),
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='grouped-bar')
Out[186]:
In [102]:
trace0 = go.Scatter(
x = RevisionDate,
y = AFIndegree,
mode = 'lines',
name = 'Average In-degree of Females'
)
trace1 = go.Scatter(
x = RevisionDate,
y = AMIndegree,
mode = 'lines',
name ='Average In-degree of Males'
)
data = [trace0, trace1]
layout = dict(title = 'Average In-degree',
xaxis = dict(title = 'Revision'),)
fig = dict(data=data, layout=layout)
# Plot and embed in ipython notebook!
py.iplot(fig, filename='line-mode')
Out[102]:
In [215]:
def KCore(G):
F=[]
M=[]
a=nx.algorithms.core_number(G)
for n in G.nodes_iter():
if G.node[n]['gender']=='female':
F.append(a[n])
if G.node[n]['gender']=='male':
M.append(a[n])
FemaleKCore=0
MaleKCore=0
stdf=0
stdm=0
if F:
FemaleKCore=np.mean(F)
stdf=np.std(F)/13.85
if M:
MaleKCore=np.mean(M)
stdm=np.std(M)/13.85
return [FemaleKCore,MaleKCore,stdf,stdm]
In [194]:
for i in range(0,192):
a=networklist[i]
a.remove_edges_from(a.selfloop_edges())
In [216]:
kcore=[]
for i in range(0,192):
a=networklist[i]
kcore.append(KCore (a))
In [221]:
FKCore=[]
VFKCore=[]
for i in range(0,192):
FKCore.append(kcore [i][0])
VFKCore.append(kcore [i][2])
In [222]:
MKCore=[]
VMKCore=[]
for i in range(0,192):
MKCore.append(kcore [i][1])
VMKCore.append(kcore [i][3])
In [223]:
trace0 = go.Scatter(
x = RevisionDate,
y = FKCore,
mode = 'lines',
name = 'Average K-core of Females'
)
trace1 = go.Scatter(
x = RevisionDate,
y = MKCore,
mode = 'lines',
name ='Average K-core of Males'
)
data = [trace0, trace1]
layout = dict(title = 'Average K-Core',
xaxis = dict(title = 'Revision'),)
fig = dict(data=data, layout=layout)
# Plot and embed in ipython notebook!
py.iplot(fig, filename='line-mode')
Out[223]:
In [225]:
trace0 = go.Scatter(
x = RevisionDate,
y = FKCore,
mode = 'lines',
name = 'Average K-core of Females'
)
trace1 = go.Scatter(
x = RevisionDate,
y = MKCore,
mode = 'lines',
name ='Average K-core of Males'
)
trace01 = go.Scatter(
x = RevisionDate,
y = [x + y for x, y in zip(FKCore, VFKCore)],
mode = 'lines',
marker=dict(color="444"),
line=dict(width=0),
fillcolor='rgba(68, 68, 68, 0.3)',
fill='tonexty'
)
trace02 = go.Scatter(
x = RevisionDate,
y = [x - y for x, y in zip(FKCore,VFKCore)],
mode = 'lines',
marker=dict(color="444"),
line=dict(width=0),
fillcolor='rgba(68, 68, 68, 0.3)',
fill='tonexty'
)
trace11 = go.Scatter(
x = RevisionDate,
y = [x + y for x, y in zip(MKCore, VMKCore)],
mode = 'lines',
marker=dict(color="444"),
line=dict(width=0),
fillcolor='rgba(68, 68, 68, 0.3)',
fill='tonexty'
)
trace12 = go.Scatter(
x = RevisionDate,
y = [x - y for x, y in zip(MKCore, VMKCore)],
mode = 'lines',
marker=dict(color="444"),
line=dict(width=0),
fillcolor='rgba(68, 68, 68, 0.3)',
fill='tonexty'
)
data = [trace0,trace01,trace02,trace1,trace11,trace12]
layout = dict(title = 'Average K-Core',
xaxis = dict(title = 'Revision'),)
fig = dict(data=data, layout=layout)
# Plot and embed in ipython notebook!
py.iplot(fig, filename='line-mode')
Out[225]:
In [229]:
FKCoreY=[]
MKCoreY=[]
VFKCoreY=[]
VMKCoreY=[]
for i in range(0,200,12):
if (i-1)>0:
FKCoreY.append(FKCore[i-1])
MKCoreY.append(MKCore[i-1])
VFKCoreY.append(VFKCore[i-1])
VMKCoreY.append(VMKCore[i-1])
In [231]:
RevisionDateYear=['2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016']
trace1 = go.Bar(
x=RevisionDateYear,
y=FKCoreY,
error_y=dict(
type='data',
array=VFKCoreY
),
name='Average K-core of Females'
)
trace2 = go.Bar(
x=RevisionDateYear,
y=MKCoreY,
error_y=dict(
type='data',
array=VMKCoreY
),
name='Average K-core of Males'
)
data = [trace1, trace2]
layout = go.Layout(
barmode='group',
title='Average K-Core',
xaxis = dict(title = 'Revision'),
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='grouped-bar')
Out[231]:
In [ ]: