{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"EvTuwbs9r4aw","executionInfo":{"status":"ok","timestamp":1709307950772,"user_tz":300,"elapsed":18530,"user":{"displayName":"清水幸","userId":"09251711040897455590"}},"outputId":"e4c04ebf-fb5a-4990-da77-a450598b3df7"},"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/drive\n","/content/drive/MyDrive/Colab_Notebooks/Marvel_network\n"]}],"source":["import numpy as np\n","import pandas as pd\n","import networkx as nx\n","import matplotlib.pyplot as plt\n","from tqdm import trange\n","from google.colab import drive\n","drive.mount('/content/drive')\n","%cd /content/drive/MyDrive/Colab_Notebooks/Marvel_network"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"QHxuWhh8r4ay","executionInfo":{"status":"ok","timestamp":1709308038937,"user_tz":300,"elapsed":1658,"user":{"displayName":"清水幸","userId":"09251711040897455590"}},"outputId":"ff562e1f-3640-49d7-bcf1-9f69c9f127ad"},"outputs":[{"output_type":"stream","name":"stdout","text":["96104\n","574467\n"]}],"source":["# Read datasets\n","elements=pd.read_csv('source_data/nodes.csv')\n","hero=elements[elements['type']=='hero']['node']\n","comic=elements[elements['type']=='comic']['node']\n","hero_comic=pd.read_csv('source_data/edges.csv')\n","print(len(hero_comic))\n","hero_hero=pd.read_csv('source_data/hero-network.csv')\n","print(len(hero_hero))"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"KFLbMoter4ay","executionInfo":{"status":"ok","timestamp":1709308042509,"user_tz":300,"elapsed":306,"user":{"displayName":"清水幸","userId":"09251711040897455590"}},"outputId":"f7898146-fca2-4056-b846-1151f5d34583"},"outputs":[{"output_type":"stream","name":"stdout","text":["82593\n","424270\n"]}],"source":["# Create hero and comic lists\n","hero=list((set(hero_hero['hero1']).union(\n"," set(hero_hero['hero2']))).intersection(\n"," set(hero_comic['hero'])).intersection(\n"," set(hero)\n"," ))\n","\n","comic=list(set(hero_comic['comic']).intersection(set(comic)))\n","\n","# Delete edges containing unknown hero/comic\n","hero_comic=hero_comic[hero_comic['comic'].isin(comic)]\n","hero_comic=hero_comic[hero_comic['hero'].isin(hero)]\n","print(len(hero_comic))\n","\n","hero_hero=hero_hero[hero_hero['hero1'].isin(hero)]\n","hero_hero=hero_hero[hero_hero['hero2'].isin(hero)]\n","print(len(hero_hero))"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"_BrGgTMMr4az","executionInfo":{"status":"ok","timestamp":1709308185730,"user_tz":300,"elapsed":140840,"user":{"displayName":"清水幸","userId":"09251711040897455590"}},"outputId":"8b2f9eea-5144-48e7-b33c-f17e8525610d"},"outputs":[{"output_type":"stream","name":"stderr","text":["100%|██████████| 6282/6282 [02:20<00:00, 44.62it/s]\n"]}],"source":["# Determine in which comics the hero pairs meet\n","hero_hero_comic=pd.DataFrame(columns=['hero1','hero2','comic'])\n","for i in trange(len(hero)):\n"," hero1=hero[i]\n"," pair1=hero_comic[hero_comic['hero']==hero1]\n"," hero_comic=hero_comic[hero_comic['hero']!=hero1]\n"," pair2=hero_comic[hero_comic['comic'].isin(pair1['comic'])]\n"," pair2.columns=['hero2', 'comic']\n"," pair2.insert(0,'hero1',hero1,True)\n"," hero_hero_comic=pd.concat([hero_hero_comic,pair2])"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"gBvjnLn0r4az","executionInfo":{"status":"ok","timestamp":1709308287123,"user_tz":300,"elapsed":101398,"user":{"displayName":"清水幸","userId":"09251711040897455590"}},"outputId":"c6757189-dd0f-4f67-9e92-709dcd87be0b"},"outputs":[{"output_type":"stream","name":"stderr","text":["100%|██████████| 142289/142289 [00:25<00:00, 5543.05it/s]\n","100%|██████████| 183645/183645 [01:15<00:00, 2442.47it/s]\n"]}],"source":["def flatten_list(matrix):\n"," flat_list = []\n"," for row in matrix:\n"," flat_list += row\n"," return flat_list\n","\n","# List unique pairs\n","existing_pairs=hero_hero_comic[['hero1','hero2']].drop_duplicates()\n","existing_pairs=[[(existing_pairs.iloc[i]['hero1'],existing_pairs.iloc[i]['hero2']),\n"," (existing_pairs.iloc[i]['hero2'],existing_pairs.iloc[i]['hero1']) ]\n"," for i in trange(len(existing_pairs))]\n","existing_pairs=flatten_list(existing_pairs)\n","\n","hero_hero=hero_hero.groupby(['hero1','hero2']).size().reset_index(name='counts')\n","adding_pairs=hero_hero.drop_duplicates()\n","adding_pairs=[(adding_pairs.iloc[i]['hero1'],adding_pairs.iloc[i]['hero2'])\n"," for i in trange(len(adding_pairs))\n"," if adding_pairs.iloc[i]['hero1']!=adding_pairs.iloc[i]['hero2']]\n"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":424},"id":"A07g1IEzr4az","executionInfo":{"status":"ok","timestamp":1709308287288,"user_tz":300,"elapsed":311,"user":{"displayName":"清水幸","userId":"09251711040897455590"}},"outputId":"b0a8969e-0f00-4f16-dbb8-590e5b77795c"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" hero1 hero2 comic\n","0 ZAKKA BROTHER TODE E 1\n","1 ZAKKA DAMIAN, DR. DANIEL E 1\n","2 ZAKKA DAMIAN, MARGO E 1\n","3 ZAKKA IKARIS/IKE HARRIS [E E 1\n","4 ZAKKA KARKAS [DEVIANT] E 1\n","... ... ... ...\n","426553 BLAQUESMITH PSYLOCKE/ELISABETH B C2 31\n","426554 BLAQUESMITH PSYLOCKE/ELISABETH B X:PRIME\n","426555 PALADIN/PAUL DENNIS FROST, ADRIENNE GENX 52\n","426556 PALADIN/PAUL DENNIS FROST, ADRIENNE GENX 53\n","426557 PALADIN/PAUL DENNIS FROST, ADRIENNE GENX 54\n","\n","[426558 rows x 3 columns]"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
hero1hero2comic
0ZAKKABROTHER TODEE 1
1ZAKKADAMIAN, DR. DANIELE 1
2ZAKKADAMIAN, MARGOE 1
3ZAKKAIKARIS/IKE HARRIS [EE 1
4ZAKKAKARKAS [DEVIANT]E 1
............
426553BLAQUESMITHPSYLOCKE/ELISABETH BC2 31
426554BLAQUESMITHPSYLOCKE/ELISABETH BX:PRIME
426555PALADIN/PAUL DENNISFROST, ADRIENNEGENX 52
426556PALADIN/PAUL DENNISFROST, ADRIENNEGENX 53
426557PALADIN/PAUL DENNISFROST, ADRIENNEGENX 54
\n","

426558 rows × 3 columns

\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n"," \n"," \n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"hero_hero_comic"}},"metadata":{},"execution_count":8}],"source":["# Create a dataframe, containing hero pairs and the comics with them\n","unknown_pairs=list(set(adding_pairs)-set(existing_pairs))\n","for pair in unknown_pairs:\n"," hero1=pair[0]\n"," hero2=pair[1]\n"," if len(hero_hero[(hero_hero['hero1']==pair[0]) & (hero_hero['hero2']==pair[1])])==1:\n"," count=hero_hero[(hero_hero['hero1']==pair[0]) & (hero_hero['hero2']==pair[1])]['counts'].iloc[0]\n"," for i in range(int(count)):\n"," hero_hero_comic.loc[len(hero_hero_comic)]=[hero1,hero2,'UNKNOWN']\n"," else:\n"," continue\n","\n","hero_hero_comic=hero_hero_comic.reset_index(drop=True)\n","hero_hero_comic"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":424},"id":"uCmZdjgMr4az","executionInfo":{"status":"ok","timestamp":1709308287568,"user_tz":300,"elapsed":285,"user":{"displayName":"清水幸","userId":"09251711040897455590"}},"outputId":"4e747d00-5111-437c-9386-929a973402e7"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" hero1 hero2 counts\n","0 3-D MAN/CHARLES CHAN AJAK/TECUMOTZIN [ETE 1\n","1 3-D MAN/CHARLES CHAN ANGEL/WARREN KENNETH 1\n","2 3-D MAN/CHARLES CHAN ANT-MAN II/SCOTT HAR 1\n","3 3-D MAN/CHARLES CHAN ANT-MAN/DR. HENRY J. 2\n","4 3-D MAN/CHARLES CHAN ARABIAN KNIGHT/ABDUL 1\n","... ... ... ...\n","142284 ZZZAX RODRIGUEZ, DEBRA 1\n","142285 ZZZAX SUMMERS, NATHAN CHRI 1\n","142286 ZZZAX TALBOT, GLENN 1\n","142287 ZZZAX TIGRA/GREER NELSON 1\n","142288 ZZZAX WONDER MAN/SIMON WIL 1\n","\n","[142289 rows x 3 columns]"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
hero1hero2counts
03-D MAN/CHARLES CHANAJAK/TECUMOTZIN [ETE1
13-D MAN/CHARLES CHANANGEL/WARREN KENNETH1
23-D MAN/CHARLES CHANANT-MAN II/SCOTT HAR1
33-D MAN/CHARLES CHANANT-MAN/DR. HENRY J.2
43-D MAN/CHARLES CHANARABIAN KNIGHT/ABDUL1
............
142284ZZZAXRODRIGUEZ, DEBRA1
142285ZZZAXSUMMERS, NATHAN CHRI1
142286ZZZAXTALBOT, GLENN1
142287ZZZAXTIGRA/GREER NELSON1
142288ZZZAXWONDER MAN/SIMON WIL1
\n","

142289 rows × 3 columns

\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n"," \n"," \n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"hero_hero"}},"metadata":{},"execution_count":9}],"source":["# Create weighted adjacent matrix/list for this network\n","hero_hero=hero_hero_comic.groupby(['hero1','hero2']).size().reset_index(name='counts')\n","hero_hero"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":528},"id":"4NDlz9mpr4a0","executionInfo":{"status":"ok","timestamp":1709308289543,"user_tz":300,"elapsed":1977,"user":{"displayName":"清水幸","userId":"09251711040897455590"}},"outputId":"6b3f7c37-41ba-4b8c-d881-498fa7a6addf"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" 3-D MAN/CHARLES CHAN AJAK/TECUMOTZIN [ETE \\\n","3-D MAN/CHARLES CHAN 102 1 \n","AJAK/TECUMOTZIN [ETE 1 175 \n","ANGEL/WARREN KENNETH 1 1 \n","ANT-MAN II/SCOTT HAR 1 1 \n","ANT-MAN/DR. HENRY J. 2 0 \n","... ... ... \n","YSSA 0 0 \n","ZAKKA 0 0 \n","ZCANN 0 0 \n","ZEAKLAR 0 0 \n","ZZZAX 0 0 \n","\n"," ANGEL/WARREN KENNETH ANT-MAN II/SCOTT HAR \\\n","3-D MAN/CHARLES CHAN 1 1 \n","AJAK/TECUMOTZIN [ETE 1 1 \n","ANGEL/WARREN KENNETH 1083 3 \n","ANT-MAN II/SCOTT HAR 3 383 \n","ANT-MAN/DR. HENRY J. 25 14 \n","... ... ... \n","YSSA 0 0 \n","ZAKKA 0 0 \n","ZCANN 0 0 \n","ZEAKLAR 0 0 \n","ZZZAX 0 1 \n","\n"," ANT-MAN/DR. HENRY J. ARABIAN KNIGHT/ABDUL \\\n","3-D MAN/CHARLES CHAN 2 1 \n","AJAK/TECUMOTZIN [ETE 0 1 \n","ANGEL/WARREN KENNETH 25 4 \n","ANT-MAN II/SCOTT HAR 14 1 \n","ANT-MAN/DR. HENRY J. 1063 0 \n","... ... ... \n","YSSA 0 0 \n","ZAKKA 0 0 \n","ZCANN 2 0 \n","ZEAKLAR 0 0 \n","ZZZAX 1 0 \n","\n"," BANNER, BETTY ROSS T BEAST/HENRY &HANK& P \\\n","3-D MAN/CHARLES CHAN 2 1 \n","AJAK/TECUMOTZIN [ETE 0 1 \n","ANGEL/WARREN KENNETH 2 305 \n","ANT-MAN II/SCOTT HAR 1 7 \n","ANT-MAN/DR. HENRY J. 8 98 \n","... ... ... \n","YSSA 0 0 \n","ZAKKA 0 0 \n","ZCANN 0 0 \n","ZEAKLAR 0 0 \n","ZZZAX 3 0 \n","\n"," BLACK BOLT/BLACKANTO BLACK PANTHER/T'CHAL ... \\\n","3-D MAN/CHARLES CHAN 1 1 ... \n","AJAK/TECUMOTZIN [ETE 1 1 ... \n","ANGEL/WARREN KENNETH 8 11 ... \n","ANT-MAN II/SCOTT HAR 9 8 ... \n","ANT-MAN/DR. HENRY J. 7 86 ... \n","... ... ... ... \n","YSSA 0 0 ... \n","ZAKKA 0 0 ... \n","ZCANN 0 1 ... \n","ZEAKLAR 0 0 ... \n","ZZZAX 0 0 ... \n","\n"," WOLFSBANE 2013 WOO, SONIA XEMU YELLOWJACKET II/RITA \\\n","3-D MAN/CHARLES CHAN 0 0 0 0 \n","AJAK/TECUMOTZIN [ETE 0 0 0 0 \n","ANGEL/WARREN KENNETH 0 0 0 0 \n","ANT-MAN II/SCOTT HAR 0 0 0 3 \n","ANT-MAN/DR. HENRY J. 0 0 0 2 \n","... ... ... ... ... \n","YSSA 0 0 0 0 \n","ZAKKA 0 0 0 0 \n","ZCANN 0 0 0 0 \n","ZEAKLAR 0 0 0 0 \n","ZZZAX 0 0 0 0 \n","\n"," YOUNG, KIM SUNG YSSA ZAKKA ZCANN ZEAKLAR ZZZAX \n","3-D MAN/CHARLES CHAN 0 0 0 0 0 0 \n","AJAK/TECUMOTZIN [ETE 0 0 0 0 0 0 \n","ANGEL/WARREN KENNETH 0 0 0 0 0 0 \n","ANT-MAN II/SCOTT HAR 0 0 0 0 0 1 \n","ANT-MAN/DR. HENRY J. 0 0 0 2 0 1 \n","... ... ... ... ... ... ... \n","YSSA 0 12 0 0 0 0 \n","ZAKKA 0 0 11 0 0 0 \n","ZCANN 0 0 0 72 0 0 \n","ZEAKLAR 0 0 0 0 2 0 \n","ZZZAX 0 0 0 0 0 26 \n","\n","[6267 rows x 6267 columns]"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
3-D MAN/CHARLES CHANAJAK/TECUMOTZIN [ETEANGEL/WARREN KENNETHANT-MAN II/SCOTT HARANT-MAN/DR. HENRY J.ARABIAN KNIGHT/ABDULBANNER, BETTY ROSS TBEAST/HENRY &HANK& PBLACK BOLT/BLACKANTOBLACK PANTHER/T'CHAL...WOLFSBANE 2013WOO, SONIAXEMUYELLOWJACKET II/RITAYOUNG, KIM SUNGYSSAZAKKAZCANNZEAKLARZZZAX
3-D MAN/CHARLES CHAN102111212111...0000000000
AJAK/TECUMOTZIN [ETE117511010111...0000000000
ANGEL/WARREN KENNETH11108332542305811...0000000000
ANT-MAN II/SCOTT HAR1133831411798...0003000001
ANT-MAN/DR. HENRY J.20251410630898786...0002000201
..................................................................
YSSA0000000000...00000120000
ZAKKA0000000000...00000011000
ZCANN0000200001...00000007200
ZEAKLAR0000000000...0000000020
ZZZAX0001103000...00000000026
\n","

6267 rows × 6267 columns

\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n"," \n"," \n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"Adj"}},"metadata":{},"execution_count":10}],"source":["# Create Weighted Adjcent Matrix. weight to be the times the hero pair shows together; values on diagnal to be the degree of each node\n","G=nx.from_pandas_edgelist(hero_hero,'hero1','hero2',edge_attr='counts')\n","G=nx.Graph(G)\n","Adj=nx.adjacency_matrix(G,weight='counts').todense()\n","deg=[x[1] for x in list(G.degree)]\n","for i in range(len(Adj)):\n"," Adj[i,i]=deg[i]\n","hero=list(G.nodes)\n","Adj=pd.DataFrame(Adj,columns=hero,index=hero)\n","Adj"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Ww_M_4Isr4a0"},"outputs":[],"source":["# save the tables\n","hero_hero_comic.to_csv('hero_hero_comic.csv',index=False)\n","hero_hero.to_csv('adjacency_list.csv',index=False)\n","Adj.to_csv('adjacency_matrix.csv',index=False)\n","Adj.iloc[:50,:50].to_csv('adjacency_matrix_preview.csv',index=False)"]}],"metadata":{"kernelspec":{"display_name":"Python 3.11.4 64-bit","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.4"},"orig_nbformat":4,"vscode":{"interpreter":{"hash":"9c3b746aacb193ef1cc3db259ac9b06b57bc8ebd00118a6d0bcea7cc554049a7"}},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":0}