%matplotlib inline

import pandas as pd
import networkx as nx

# Ignore matplotlib warnings
import warnings
warnings.filterwarnings("../ignore")

Using NetworkX to find centrality of points in your graph

Read in our data

df = pd.read_csv("clubs.csv")
df.set_index('name', inplace=True)
df = df.pivot(columns='club', values='club').astype(bool).astype(int)
df.head(3)
club Boston Committee London Enemies Long Room Club Loyal Nine North Caucus St Andrews Lodge Tea Party
name
Adams John 0 0 1 0 1 0 0
Adams Samuel 1 1 1 0 1 0 0
Allen Dr 0 0 0 0 1 0 0

Build the adjacency matrix

Except I’m pretty sure technically it isn’t an adjacency matrix, since an adjacency matrix is only 1 to mean “connected” and 0 to mean “not connected.”

people_adj = df.dot(df.T)
people_adj.head(3)
name Adams John Adams Samuel Allen Dr Appleton Nathaniel Ash Gilbert Austin Benjamin Austin Samuel Avery John Baldwin Cyrus Ballard John ... Whitwell William Williams Jeremiah Williams Jonathan Williams Thomas Willis Nathaniel Wingfield William Winslow John Winthrop John Wyeth Joshua Young Thomas
name
Adams John 2 2 1 1 0 0 0 0 0 1 ... 0 0 0 0 0 0 1 1 0 1
Adams Samuel 2 4 1 2 0 1 1 1 1 1 ... 1 0 1 0 0 0 1 2 0 2
Allen Dr 1 1 1 1 0 0 0 0 0 1 ... 0 0 0 0 0 0 0 1 0 1

3 rows × 254 columns

Build a graph…

We’ll draw it first, but we’re going to use it research, not for drawing!

%matplotlib inline
import networkx as nx

people_graph = nx.from_numpy_matrix(people_adj.values)

renamed = dict(zip(people_graph.nodes(), people_adj.columns))
print("Renaming nodes with", renamed)
nx.relabel_nodes(people_graph, renamed, copy=False)

nx.draw(people_graph, node_size=2, edge_color='lightgrey')
Renaming nodes with {0: 'Adams John', 1: 'Adams Samuel', 2: 'Allen Dr', 3: 'Appleton Nathaniel', 4: 'Ash Gilbert', 5: 'Austin Benjamin', 6: 'Austin Samuel', 7: 'Avery John', 8: 'Baldwin Cyrus', 9: 'Ballard John', 10: 'Barber Nathaniel', 11: 'Barnard Samuel', 12: 'Barrett Samuel', 13: 'Bass Henry', 14: 'Bell William', 15: 'Bewer James', 16: 'Blake Increase', 17: 'Boit John', 18: 'Bolter Thomas', 19: 'Boyer Peter', 20: 'Boynton Richard', 21: 'Brackett Jos', 22: 'Bradford John', 23: 'Bradlee David', 24: 'Bradlee Josiah', 25: 'Bradlee Nathaniel', 26: 'Bradlee Thomas', 27: 'Bray George', 28: 'Breck William', 29: 'Brimmer Herman', 30: 'Brimmer Martin', 31: 'Broomfield Henry', 32: 'Brown Enoch', 33: 'Brown Hugh', 34: 'Brown John', 35: 'Bruce Stephen', 36: 'Burbeck Edward', 37: 'Burbeck William', 38: 'Burt Benjamin', 39: 'Burton Benjamin', 40: 'Cailleteau Edward', 41: 'Callendar Elisha', 42: 'Campbell Nicholas', 43: 'Cazneau Capt', 44: 'Chadwell Mr', 45: 'Champney Caleb', 46: 'Chase Thomas', 47: 'Cheever Ezekiel', 48: 'Chipman Seth', 49: 'Chrysty Thomas', 50: 'Church Benjamin', 51: 'Clarke Benjamin', 52: 'Cleverly Stephen', 53: 'Cochran John', 54: 'Colesworthy Gilbert', 55: 'Collier Gershom', 56: 'Collins Ezra', 57: 'Collson Adam', 58: 'Condy JamesFoster', 59: 'Cooper Samuel', 60: 'Cooper William', 61: 'Crafts Thomas', 62: 'Crane John', 63: 'Davis Caleb', 64: 'Davis Edward', 65: 'Davis Robert', 66: 'Davis William', 67: 'Dawes Thomas', 68: 'Dennie William', 69: 'Deshon Moses', 70: 'Dexter Samuel', 71: 'Dolbear Edward', 72: 'Doyle Peter', 73: 'Eaton Joseph', 74: 'Eayres Joseph', 75: 'Eckley Unknown', 76: 'Edes Benjamin', 77: 'Emmes Samuel', 78: 'Etheridge William', 79: 'Fenno Samuel', 80: 'Ferrell Ambrose', 81: 'Field Joseph', 82: 'Flagg Josiah', 83: 'Fleet Thomas', 84: 'Foster Bos', 85: 'Foster Samuel', 86: 'Frothingham Nathaniel', 87: 'Gammell John', 88: 'Gill Moses', 89: 'Gore Samuel', 90: 'Gould William', 91: 'Graham James', 92: 'Grant Moses', 93: 'Gray Wait', 94: 'Greene Nathaniel', 95: 'Greenleaf Joseph', 96: 'Greenleaf William', 97: 'Greenough Newn', 98: 'Ham William', 99: 'Hammond Samuel', 100: 'Hancock Eben', 101: 'Hancock John', 102: 'Hendley William', 103: 'Hewes George', 104: 'Hickling William', 105: 'Hicks John', 106: 'Hill Alexander', 107: 'Hitchborn Nathaniel', 108: 'Hitchborn Thomas', 109: 'Hobbs Samuel', 110: 'Hoffins John', 111: 'Holmes Nathaniel', 112: 'Hooton John', 113: 'Hopkins Caleb', 114: 'Hoskins William', 115: 'Howard Samuel', 116: 'Howe Edward', 117: 'Hunnewell Jonathan', 118: 'Hunnewell Richard', 119: 'Hunstable Thomas', 120: 'Hunt Abraham', 121: 'Ingersoll Daniel', 122: 'Inglish Alexander', 123: 'Isaac Pierce', 124: 'Ivers James', 125: 'Jarvis Charles', 126: 'Jarvis Edward', 127: 'Jefferds Unknown', 128: 'Jenkins John', 129: 'Johnston Eben', 130: 'Johonnott Gabriel', 131: 'Kent Benjamin', 132: 'Kerr Walter', 133: 'Kimball Thomas', 134: 'Kinnison David', 135: 'Lambert John', 136: 'Lee Joseph', 137: 'Lewis Phillip', 138: 'Lincoln Amos', 139: 'Loring Matthew', 140: 'Lowell John', 141: 'MacKintosh Capt', 142: 'MacNeil Archibald', 143: 'Machin Thomas', 144: 'Mackay William', 145: 'Marett Phillip', 146: 'Marlton John', 147: 'Marshall Thomas', 148: 'Marson John', 149: 'Mason Jonathan', 150: 'Matchett John', 151: 'May John', 152: 'McAlpine William', 153: 'Melville Thomas', 154: 'Merrit John', 155: 'Milliken Thomas', 156: 'Molineux William', 157: 'Moody Samuel', 158: 'Moore Thomas', 159: 'Morse Anthony', 160: 'Morton Perez', 161: 'Mountford Joseph', 162: 'Newell Eliphelet', 163: 'Nicholls Unknown', 164: 'Noyces Nat', 165: 'Obear Israel', 166: 'Otis James', 167: 'Palfrey William', 168: 'Palmer Joseph', 169: 'Palms Richard', 170: 'Parker Jonathan', 171: 'Parkman Elias', 172: 'Partridge Sam', 173: 'Payson Joseph', 174: 'Pearce Isaac', 175: 'Pearce IsaacJun', 176: 'Peck Samuel', 177: 'Peck Thomas', 178: 'Peters John', 179: 'Phillips John', 180: 'Phillips Samuel', 181: 'Phillips William', 182: 'Pierce William', 183: 'Pierpont Robert', 184: 'Pitts John', 185: 'Pitts Lendall', 186: 'Pitts Samuel', 187: 'Porter Thomas', 188: 'Potter Edward', 189: 'Powell William', 190: 'Prentiss Henry', 191: 'Prince Job', 192: 'Prince John', 193: 'Proctor Edward', 194: 'Pulling John', 195: 'Pulling Richard', 196: 'Purkitt Henry', 197: 'Quincy Josiah', 198: 'Randall John', 199: 'Revere Paul', 200: 'Roby Joseph', 201: 'Roylson Thomas', 202: 'Ruddock Abiel', 203: 'Russell John', 204: 'Russell William', 205: 'Sessions Robert', 206: 'Seward James', 207: 'Sharp Gibbens', 208: 'Shed Joseph', 209: 'Sigourney John', 210: 'Simpson Benjamin', 211: 'Slater Peter', 212: 'Sloper Ambrose', 213: 'Smith John', 214: 'Spear Thomas', 215: 'Sprague Samuel', 216: 'Spurr John', 217: 'Stanbridge Henry', 218: 'Starr James', 219: 'Stearns Phineas', 220: 'Stevens Ebenezer', 221: 'Stoddard Asa', 222: 'Stoddard Jonathan', 223: 'Story Elisha', 224: 'Swan James', 225: 'Sweetser John', 226: 'Symmes Eben', 227: 'Symmes John', 228: 'Tabor Philip', 229: 'Tileston Thomas', 230: 'Trott George', 231: 'Tyler Royall', 232: 'Urann Thomas', 233: 'Vernon Fortesque', 234: 'Waldo Benjamin', 235: 'Warren Joseph', 236: 'Webb Joseph', 237: 'Webster Thomas', 238: 'Welles Henry', 239: 'Wendell Oliver', 240: 'Wheeler Josiah', 241: 'White Samuel', 242: 'Whitten John', 243: 'Whitwell Samuel', 244: 'Whitwell William', 245: 'Williams Jeremiah', 246: 'Williams Jonathan', 247: 'Williams Thomas', 248: 'Willis Nathaniel', 249: 'Wingfield William', 250: 'Winslow John', 251: 'Winthrop John', 252: 'Wyeth Joshua', 253: 'Young Thomas'}

png

Horrifying, let’s try it again.

import matplotlib.pyplot as plt

plt.figure(figsize=(30,30))

# 1. Create the graph
people_graph = nx.from_numpy_matrix(people_adj.values)

# 1.5. Rename our nodes so it isn't 0, 1, 2, 3, 4...
renamed = dict(zip(people_graph.nodes(), people_adj.columns))
nx.relabel_nodes(people_graph, renamed, copy=False)

# 2. Create a layout for our nodes 
layout = nx.spring_layout(people_graph, iterations=20, k=0.25)

# 3. Draw the parts we want
nx.draw_networkx_edges(people_graph, layout, edge_color='#e1e1e1')
nx.draw_networkx_nodes(people_graph, layout)
nx.draw_networkx_labels(people_graph, layout)

# 4. Turn off the axis because I know you don't want it
plt.axis('off')

# 5. Tell matplotlib to show it
plt.show()