{"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"," | hero1 | \n","hero2 | \n","comic | \n","
---|---|---|---|
0 | \n","ZAKKA | \n","BROTHER TODE | \n","E 1 | \n","
1 | \n","ZAKKA | \n","DAMIAN, DR. DANIEL | \n","E 1 | \n","
2 | \n","ZAKKA | \n","DAMIAN, MARGO | \n","E 1 | \n","
3 | \n","ZAKKA | \n","IKARIS/IKE HARRIS [E | \n","E 1 | \n","
4 | \n","ZAKKA | \n","KARKAS [DEVIANT] | \n","E 1 | \n","
... | \n","... | \n","... | \n","... | \n","
426553 | \n","BLAQUESMITH | \n","PSYLOCKE/ELISABETH B | \n","C2 31 | \n","
426554 | \n","BLAQUESMITH | \n","PSYLOCKE/ELISABETH B | \n","X:PRIME | \n","
426555 | \n","PALADIN/PAUL DENNIS | \n","FROST, ADRIENNE | \n","GENX 52 | \n","
426556 | \n","PALADIN/PAUL DENNIS | \n","FROST, ADRIENNE | \n","GENX 53 | \n","
426557 | \n","PALADIN/PAUL DENNIS | \n","FROST, ADRIENNE | \n","GENX 54 | \n","
426558 rows × 3 columns
\n","\n"," | hero1 | \n","hero2 | \n","counts | \n","
---|---|---|---|
0 | \n","3-D MAN/CHARLES CHAN | \n","AJAK/TECUMOTZIN [ETE | \n","1 | \n","
1 | \n","3-D MAN/CHARLES CHAN | \n","ANGEL/WARREN KENNETH | \n","1 | \n","
2 | \n","3-D MAN/CHARLES CHAN | \n","ANT-MAN II/SCOTT HAR | \n","1 | \n","
3 | \n","3-D MAN/CHARLES CHAN | \n","ANT-MAN/DR. HENRY J. | \n","2 | \n","
4 | \n","3-D MAN/CHARLES CHAN | \n","ARABIAN KNIGHT/ABDUL | \n","1 | \n","
... | \n","... | \n","... | \n","... | \n","
142284 | \n","ZZZAX | \n","RODRIGUEZ, DEBRA | \n","1 | \n","
142285 | \n","ZZZAX | \n","SUMMERS, NATHAN CHRI | \n","1 | \n","
142286 | \n","ZZZAX | \n","TALBOT, GLENN | \n","1 | \n","
142287 | \n","ZZZAX | \n","TIGRA/GREER NELSON | \n","1 | \n","
142288 | \n","ZZZAX | \n","WONDER MAN/SIMON WIL | \n","1 | \n","
142289 rows × 3 columns
\n","\n"," | 3-D MAN/CHARLES CHAN | \n","AJAK/TECUMOTZIN [ETE | \n","ANGEL/WARREN KENNETH | \n","ANT-MAN II/SCOTT HAR | \n","ANT-MAN/DR. HENRY J. | \n","ARABIAN KNIGHT/ABDUL | \n","BANNER, BETTY ROSS T | \n","BEAST/HENRY &HANK& P | \n","BLACK BOLT/BLACKANTO | \n","BLACK PANTHER/T'CHAL | \n","... | \n","WOLFSBANE 2013 | \n","WOO, SONIA | \n","XEMU | \n","YELLOWJACKET II/RITA | \n","YOUNG, KIM SUNG | \n","YSSA | \n","ZAKKA | \n","ZCANN | \n","ZEAKLAR | \n","ZZZAX | \n","
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3-D MAN/CHARLES CHAN | \n","102 | \n","1 | \n","1 | \n","1 | \n","2 | \n","1 | \n","2 | \n","1 | \n","1 | \n","1 | \n","... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","
AJAK/TECUMOTZIN [ETE | \n","1 | \n","175 | \n","1 | \n","1 | \n","0 | \n","1 | \n","0 | \n","1 | \n","1 | \n","1 | \n","... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","
ANGEL/WARREN KENNETH | \n","1 | \n","1 | \n","1083 | \n","3 | \n","25 | \n","4 | \n","2 | \n","305 | \n","8 | \n","11 | \n","... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","
ANT-MAN II/SCOTT HAR | \n","1 | \n","1 | \n","3 | \n","383 | \n","14 | \n","1 | \n","1 | \n","7 | \n","9 | \n","8 | \n","... | \n","0 | \n","0 | \n","0 | \n","3 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","1 | \n","
ANT-MAN/DR. HENRY J. | \n","2 | \n","0 | \n","25 | \n","14 | \n","1063 | \n","0 | \n","8 | \n","98 | \n","7 | \n","86 | \n","... | \n","0 | \n","0 | \n","0 | \n","2 | \n","0 | \n","0 | \n","0 | \n","2 | \n","0 | \n","1 | \n","
... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","
YSSA | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","12 | \n","0 | \n","0 | \n","0 | \n","0 | \n","
ZAKKA | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","11 | \n","0 | \n","0 | \n","0 | \n","
ZCANN | \n","0 | \n","0 | \n","0 | \n","0 | \n","2 | \n","0 | \n","0 | \n","0 | \n","0 | \n","1 | \n","... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","72 | \n","0 | \n","0 | \n","
ZEAKLAR | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","2 | \n","0 | \n","
ZZZAX | \n","0 | \n","0 | \n","0 | \n","1 | \n","1 | \n","0 | \n","3 | \n","0 | \n","0 | \n","0 | \n","... | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","0 | \n","26 | \n","
6267 rows × 6267 columns
\n","