In [1]:
import matplotlib
%matplotlib inline

In [2]:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

Data Input

We are adding the adjacency matrices for each of the confidence levels so we can get a weight for the connections bewtween the petitions. For example, a value of 4 in the adjaceny matrix means that it is at the 40% confidence level.


In [3]:
petition50 = pd.read_csv('../data/adjacency_matrix_50_percent_confidence.csv')
petition40 = pd.read_csv('../data/adjacency_matrix_40_percent_confidence.csv')
petition30 = pd.read_csv('../data/adjacency_matrix_30_percent_confidence.csv')
petition20 = pd.read_csv('../data/adjacency_matrix_20_percent_confidence.csv')
petition10 = pd.read_csv('../data/adjacency_matrix_10_percent_confidence.csv')
petitions = petition50 + petition40 + petition30 + petition20 + petition10 
adjacency_matrix = petitions.T.values[1:]
petitions['Unnamed: 0'] = petition10['Unnamed: 0']
petition_ids = petitions['Unnamed: 0']
petition_labels = {i:x for i,x in petition_ids.iteritems()}

In [4]:
print petitions


    Unnamed: 0  975  980  981  982  983  984  985  987  990  996  1003  1006  \
0          975    0    0    1    0    0    0    0    0    0    0     2     0   
1          980    0    0    0    0    0    0    1    0    2    3     0     4   
2          981    1    0    0    0    2    0    0    0    1    0     2     2   
3          982    0    0    0    0    0    0    0    0    0    0     0     0   
4          983    0    0    4    0    0    0    0    0    2    0     3     2   
5          984    0    0    0    0    0    0    0    0    0    0     0     0   
6          985    0    3    0    0    0    0    0    0    0    3     0     4   
7          987    0    0    0    0    0    0    0    0    5    0     0     5   
8          990    0    2    1    0    1    0    0    2    0    3     0     5   
9          996    0    3    0    0    0    0    1    0    3    0     0     5   
10        1003    2    0    2    0    1    0    0    0    0    0     0     1   
11        1006    0    0    0    0    0    0    0    0    1    1     0     0   
12        1008    0    0    0    0    0    0    0    0    0    0     0     0   
13        1009    0    2    1    0    0    0    0    1    4    4     0     5   
14        1010    0    2    0    0    0    0    0    2    5    3     0     5   
15        1013    0    0    0    0    0    0    0    0    0    0     0     0   
16        1016    0    0    0    0    0    0    0    0    0    0     0     0   
17        1025    0    0    0    0    0    0    0    0    0    0     0     0   
18        1029    0    0    0    0    0    0    0    5    5    3     0     5   
19        1043    0    0    0    0    0    0    0    0    0    0     0     0   
20        1052    0    0    0    0    0    0    0    0    0    0     0     1   

    1008  1009  1010  1013  1016  1025  1029      
0      0     0     0     0     0     0     0 ...  
1      0     2     1     0     0     0     0 ...  
2      0     1     0     0     0     0     0 ...  
3      0     0     0     0     0     0     0 ...  
4      0     0     0     0     0     0     0 ...  
5      0     0     0     0     0     0     0 ...  
6      0     0     0     0     0     0     0 ...  
7      0     3     4     0     0     0     5 ...  
8      0     4     3     0     0     0     2 ...  
9      0     4     2     0     0     0     1 ...  
10     0     0     0     0     0     0     0 ...  
11     0     1     0     0     0     0     0 ...  
12     0     0     0     0     0     0     0 ...  
13     0     0     2     0     0     0     1 ...  
14     0     4     0     0     0     0     3 ...  
15     0     0     0     0     0     0     0 ...  
16     0     0     0     0     0     0     0 ...  
17     0     0     0     0     0     0     0 ...  
18     0     3     4     0     0     0     0 ...  
19     0     0     0     0     0     0     0 ...  
20     0     0     0     0     0     0     0 ...  

[21 rows x 22 columns]

In [5]:
print petition10


    Unnamed: 0  975  980  981  982  983  984  985  987  990  996  1003  1006  \
0          975    0    0    1    0    0    0    0    0    0    0     1     0   
1          980    0    0    0    0    0    0    1    0    1    1     0     1   
2          981    1    0    0    0    1    0    0    0    1    0     1     1   
3          982    0    0    0    0    0    0    0    0    0    0     0     0   
4          983    0    0    1    0    0    0    0    0    1    0     1     1   
5          984    0    0    0    0    0    0    0    0    0    0     0     0   
6          985    0    1    0    0    0    0    0    0    0    1     0     1   
7          987    0    0    0    0    0    0    0    0    1    0     0     1   
8          990    0    1    1    0    1    0    0    1    0    1     0     1   
9          996    0    1    0    0    0    0    1    0    1    0     0     1   
10        1003    1    0    1    0    1    0    0    0    0    0     0     1   
11        1006    0    0    0    0    0    0    0    0    1    1     0     0   
12        1008    0    0    0    0    0    0    0    0    0    0     0     0   
13        1009    0    1    1    0    0    0    0    1    1    1     0     1   
14        1010    0    1    0    0    0    0    0    1    1    1     0     1   
15        1013    0    0    0    0    0    0    0    0    0    0     0     0   
16        1016    0    0    0    0    0    0    0    0    0    0     0     0   
17        1025    0    0    0    0    0    0    0    0    0    0     0     0   
18        1029    0    0    0    0    0    0    0    1    1    1     0     1   
19        1043    0    0    0    0    0    0    0    0    0    0     0     0   
20        1052    0    0    0    0    0    0    0    0    0    0     0     1   

    1008  1009  1010  1013  1016  1025  1029      
0      0     0     0     0     0     0     0 ...  
1      0     1     1     0     0     0     0 ...  
2      0     1     0     0     0     0     0 ...  
3      0     0     0     0     0     0     0 ...  
4      0     0     0     0     0     0     0 ...  
5      0     0     0     0     0     0     0 ...  
6      0     0     0     0     0     0     0 ...  
7      0     1     1     0     0     0     1 ...  
8      0     1     1     0     0     0     1 ...  
9      0     1     1     0     0     0     1 ...  
10     0     0     0     0     0     0     0 ...  
11     0     1     0     0     0     0     0 ...  
12     0     0     0     0     0     0     0 ...  
13     0     0     1     0     0     0     1 ...  
14     0     1     0     0     0     0     1 ...  
15     0     0     0     0     0     0     0 ...  
16     0     0     0     0     0     0     0 ...  
17     0     0     0     0     0     0     0 ...  
18     0     1     1     0     0     0     0 ...  
19     0     0     0     0     0     0     0 ...  
20     0     0     0     0     0     0     0 ...  

[21 rows x 22 columns]

In [6]:
graph = nx.from_numpy_matrix(adjacency_matrix)

In [7]:
categories_colors = pd.read_csv('../data/petitions_groups_colors.csv')

In [8]:
color_table =  [ [row[0],row[1],row[2]] for i,row in categories_colors.iterrows()]

In [9]:
petitions_categories_table = pd.read_csv('../data/petitions_grouping.csv')
petitions_categories =  { row[0]:row[1] for i,row in petitions_categories_table.iterrows() }

In [10]:
node_colors_array = [ color_table[petitions_categories[pid]] for pid in petition_ids ]

In [11]:
edge_weight = [adjacency_matrix[int(line.split()[0])][int(line.split()[1])] for line in nx.generate_edgelist(graph, data = False)]

Nodes in this graph represent petitions. The edges represent the co-signing relationship. The color of the edges represents the strength of the relationship. The darker the edge, the more co-signatures there are:

  • Blue = Mental Health
  • Red = Give us our Guns
  • Green = Guard Our Schools

In [12]:
plt.figure(num=2, figsize=(25,25))
positions = nx.spring_layout(graph,k=0.7,scale=10)
nodes = nx.draw_networkx_nodes(graph,positions,node_color = node_colors_array, node_size=1000)
labels = nx.draw_networkx_labels(graph,positions,labels = petition_labels)
edges = nx.draw_networkx_edges(graph,positions,edge_cmap = plt.get_cmap('binary'),edge_vmin = 0,edge_vmax = 5,edge_color = edge_weight, width = 3)
plt.axis('off')
plt.sci(nodes)
plt.sci(edges)
plt.show()



In [13]:
posframe = pd.DataFrame(positions)
posframe.T.to_csv('../data/graph_positions.csv')

In [14]:
posframe2 = pd.read_csv('../data/graph_positions.csv')
positions2 = {i:array([row[0],row[1]]) for i,row in posframe2.iterrows()}
print positions2
print positions
print posframe2


{0: array([ 0.,  0.]), 1: array([ 1.        ,  8.22136178]), 2: array([ 2.        ,  7.49124376]), 3: array([ 3.        ,  9.54862436]), 4: array([ 4.        ,  9.96152637]), 5: array([ 5.        ,  2.67037679]), 6: array([ 6.        ,  0.28916382]), 7: array([ 7.        ,  2.33452769]), 8: array([ 8.        ,  5.70042937]), 9: array([ 9.        ,  5.26250993]), 10: array([ 10.        ,   5.94896025]), 11: array([ 11.        ,   4.10526629]), 12: array([ 12.        ,   0.12786108]), 13: array([ 13.        ,   6.84080684]), 14: array([ 14.        ,   3.74797725]), 15: array([ 15.        ,   9.38352283]), 16: array([ 16.        ,   8.66963041]), 17: array([ 17.        ,   1.35458762]), 18: array([ 18.        ,   1.11602897]), 19: array([ 19.        ,   4.25852749]), 20: array([ 20.,  10.])}
{0: array([ 0.        ,  4.29463475]), 1: array([ 8.22136178,  8.76700439]), 2: array([ 7.49124376,  0.57819657]), 3: array([ 9.54862436,  2.83666004]), 4: array([ 9.96152637,  5.71317108]), 5: array([ 2.67037679,  0.47007217]), 6: array([ 0.28916382,  3.12037768]), 7: array([ 2.33452769,  9.21268515]), 8: array([ 5.70042937,  8.11648011]), 9: array([ 5.26250993,  9.98776472]), 10: array([ 5.94896025,  0.02237502]), 11: array([ 4.10526629,  6.21199483]), 12: array([ 0.12786108,  6.1790548 ]), 13: array([ 6.84080684,  9.30993051]), 14: array([ 3.74797725,  9.80562644]), 15: array([ 9.38352283,  7.42593997]), 16: array([ 8.66963041,  1.51797476]), 17: array([ 1.35458762,  1.51697813]), 18: array([ 1.11602897,  8.08819254]), 19: array([ 4.25852749,  0.        ]), 20: array([ 10.        ,   4.47540971])}
    Unnamed: 0          0         1
0            0   0.000000  4.294635
1            1   8.221362  8.767004
2            2   7.491244  0.578197
3            3   9.548624  2.836660
4            4   9.961526  5.713171
5            5   2.670377  0.470072
6            6   0.289164  3.120378
7            7   2.334528  9.212685
8            8   5.700429  8.116480
9            9   5.262510  9.987765
10          10   5.948960  0.022375
11          11   4.105266  6.211995
12          12   0.127861  6.179055
13          13   6.840807  9.309931
14          14   3.747977  9.805626
15          15   9.383523  7.425940
16          16   8.669630  1.517975
17          17   1.354588  1.516978
18          18   1.116029  8.088193
19          19   4.258527  0.000000
20          20  10.000000  4.475410

[21 rows x 3 columns]

In [15]:
plt.figure(num=3, figsize=(25,25))
#positions = nx.spring_layout(graph,k=0.7,scale=10)
nodes = nx.draw_networkx_nodes(graph,positions2,node_color = node_colors_array, node_size=1000)
labels = nx.draw_networkx_labels(graph,positions2,labels = petition_labels)
edges = nx.draw_networkx_edges(graph,positions2,edge_cmap = plt.get_cmap('binary'),edge_vmin = 0,edge_vmax = 5,edge_color = edge_weight, width = 3)
plt.axis('off')
plt.sci(nodes)
plt.sci(edges)
plt.show()



In [16]:
for line in nx.generate_edgelist(graph, data = False): 
    print(line)


0 2
0 10
1 6
1 8
1 9
1 11
1 13
1 14
1 20
2 4
2 8
2 10
2 11
2 13
4 8
4 11
4 10
6 11
6 9
7 8
7 18
7 11
7 13
7 14
8 9
8 11
8 13
8 14
8 18
8 20
9 11
9 13
9 14
9 18
9 20
10 11
11 13
11 14
11 18
11 20
13 14
13 18
13 20
14 18

In [17]:
graph.number_of_edges()


Out[17]:
44

In [18]:
edge_weight = [adjacency_matrix[int(line.split()[0])][int(line.split()[1])] for line in nx.generate_edgelist(graph, data = False)]

In [19]:
print adjacency_matrix[0][2]


1

In [20]:
print edge_weight


[1, 2, 3, 2, 3, 0, 2, 2, 0, 4, 1, 2, 0, 1, 1, 0, 1, 0, 1, 2, 5, 0, 1, 2, 3, 1, 4, 5, 5, 0, 1, 4, 3, 3, 0, 0, 5, 5, 5, 1, 4, 3, 0, 4]

In [20]:


In [20]: