<jupyter_start><jupyter_text>Reddit submissions graph analysis Step 1: Data preparation  Convert dataset to the proper Graph structure [What’s in a name? Understanding the Interplay betweenTitles, Content, and Communities in Social Media](http://i.stanford.edu/~julian/pdfs/icwsm13.pdf)<jupyter_code>import pandas as pd
import numpy as np
import matplotlib
import seaborn


import warnings
warnings.filterwarnings('ignore')
import igraph
print (igraph.__version__)
# !pip install igraph<jupyter_output><empty_output><jupyter_text>Load raw data<jupyter_code>data_foled = 'data/'
data_file = 'redditSubmissions.csv'
df = pd.read_csv(data_foled+data_file, header=0, error_bad_lines=False)
df.shape
df.head(10)
graph = df[["#image_id","subreddit"]]
graph.head(10)
graph["subreddit2"] = pd.Categorical(graph["subreddit"]).codes
graph.head()
graph.shape<jupyter_output><empty_output><jupyter_text>Aggregate data<jupyter_code>subreddit_dict = graph.copy()

subreddit_dict['count'] = 1
subreddit_dict = subreddit_dict.groupby(['subreddit','subreddit2']).agg({'count':'sum'}).sort_values(by='count', ascending=False)
subreddit_dict.reset_index(inplace=True)
subreddit_dict = subreddit_dict.rename(index=str, columns={"subreddit": "name", "subreddit2": "id", "count":"count"})<jupyter_output><empty_output><jupyter_text>Save NodesSubreddit dictionary<jupyter_code>subreddit_dict.head(10)
subreddit_dict.to_csv('./data/subreddit_dict.csv', index=None)<jupyter_output><empty_output><jupyter_text>Transform Table to Graph<jupyter_code>graph = graph.rename(index=str, columns={"#image_id": "img", "subreddit": "subreddit_name", "subreddit2": "subreddit"})
graph.shape
graph.drop_duplicates(inplace=True)
graph.shape
tmp = graph.join(graph.set_index('img'),on='img',rsuffix='_neighbor')
tmp.head(10)
tmp.shape
tmp = tmp[tmp['subreddit']!=tmp['subreddit_neighbor']]
tmp.shape
graph[graph['img']==0]
tmp.sort_values(by='subreddit',inplace=True)
tmp["ukey1"] = tmp["subreddit"].astype(str)+'_'+tmp["subreddit_neighbor"].astype(str)
tmp["ukey2"] = tmp["subreddit_neighbor"].astype(str)+'_'+tmp["subreddit"].astype(str)

tmp = tmp[(tmp['subreddit']<tmp['subreddit_neighbor'])]
tmp.head(10)
tmp.shape
tmp.drop_duplicates(subset=['subreddit','subreddit_neighbor'],inplace=True)
result = tmp.drop(labels=['img','subreddit_name','subreddit_name_neighbor','ukey1','ukey2'], axis=True)
result.head(10)
result.shape
result.to_csv('./data/network.csv', index=None)<jupyter_output><empty_output><jupyter_text>Graph structure validation<jupyter_code>graph[graph["subreddit"]==718]
graph[graph["img"]==23320]
result[(result["subreddit"]==718)|(result["subreddit_neighbor"]==718)]
result[(result["subreddit"]==644)|(result["subreddit_neighbor"]==644)]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16951618770461988, 'fraction_numerical': 0.01673335758457621, 'mean_word_length': 12.157894736842104, 'pattern_counts': {'":': 6, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '649096', 'license': 'MIT', 'n_tokens_mistral': 1009, 'n_tokens_neox': 1015, 'n_words': 130, 'path': '0_data_preparation.ipynb', 'repo_name': 'SanGreel/reddit-network-exploration', 'size': '2749'}