<jupyter_start><jupyter_text>Load the tweets into a dataframe and create a retweet network from the tweets.<jupyter_code>fname_tweets ="data/tweets_coronavirus.csv"

#df is a dataframe with all of the tweets
df = pd.read_csv(fname_tweets)

#  create a retweet network from the tweets
G = retweet_network_from_tweets(df)
nv = G.number_of_nodes()
ne = G.number_of_edges()
print(f"Network has {nv} nodes and {ne} edges")<jupyter_output>Network has 71319 nodes and 79956 edges<jupyter_text>five  nodes  with  the  highest  out-degree  and  the  value  of  their  out-degree<jupyter_code>Din = G.in_degree()
Dout = G.out_degree()

df_degree = pd.DataFrame({'screen_name':[x[0] for x in Dout],
                          'in_degree':[x[1] for x in Din],
                          'out_degree':[x[1] for x in Dout] })

df_degree[['screen_name','out_degree']].sort_values(by=['out_degree'],ascending = False).head(n=5)<jupyter_output><empty_output><jupyter_text>the five nodes with the highest in-degree and the value of their in-degree<jupyter_code>df_degree[['screen_name','in_degree']].sort_values(by=['in_degree'],ascending = False).head(n=5)<jupyter_output><empty_output><jupyter_text>Out-degree is a better measure, because it is a retweet network, out-degree measures how many times someone's posts are retweeted. The higher the out-degree is, the more influence this person has. But the in-degree means how many times this person retweets other's post, which does not show the influence, maybe shows the activity.  OH_mes2 is the most influential person in the retweet network. OH_mes2 was a translator for girl groups but did some translations for pop news. He/She only translates the headlines and news subject which leads to many misunderstandings. People will discuss his/her tweets, and that's the reason he/she is so popular.   Sample 2,000 tweets from the tweets dataframe and create a retweet network from the sample.  Draw the retweet network, but make the node size proportional to the out-degree.<jupyter_code>ntweets = 2000
df_sample = df.sample(n=ntweets)
Gsample = retweet_network_from_tweets(df_sample)

pos = nx.spring_layout(Gsample)

node_color = 'red'
edge_color = 'purple'
background_color = 'black'
edge_width = 1
size_min = 0.1
size_max = 1000

#define parameters for linear interpolation of node size from out-degree
dmax = df_degree.out_degree.max()
dmin = df_degree.out_degree.min()
slope = (size_max-size_min)/(dmax-dmin)
intercept = size_min-slope*dmin 

#Go through each node and calculate its size
node_size = [Gsample.out_degree(node)*slope+intercept  for node in Gsample.nodes()]

    
    
    
fig = plt.figure(figsize=(8,8))
nx.draw(Gsample.to_undirected(), pos, node_color = node_color, width= edge_width,edge_color=edge_color,node_size=node_size)
fig.set_facecolor(background_color)<jupyter_output>went through 2000 tweets, found 1730 retweets, 1726 retweet edges
Retweet network has 2513 nodes and 1726 edges<jupyter_text>Follower NetworkWe are going to look at the follower network of all users followed by YaleSOM.  The nodes in the network are Twitter users followed by YaleSOM and the edges represent a following relationship.  For instance, if @KimKardashian follows @kanyewest, this creates a directed edge pointing from @kanyewest to @KimKardashian   Load the follower network into a networkx object.The follower network is in the file *data/data/friends_network_YaleSOM.pickle*.<jupyter_code>fname_following = 'data/friends_network_YaleSOM.pickle'
G = nx.read_gpickle(fname_following)

nv = G.number_of_nodes()
ne = G.number_of_edges()
print(f"Network has {nv} nodes and {ne} edges")

Din = G.in_degree()
Dout = G.out_degree()
df_degree_2 = pd.DataFrame({'screen_name':[x[0] for x in Dout],
                          'in_degree':[x[1] for x in Din],
                          'out_degree':[x[1] for x in Dout] })

df_degree_2[['screen_name','out_degree']].sort_values(by=['out_degree'],ascending = False).head(n=5)

df_degree_2[['screen_name','in_degree']].sort_values(by=['in_degree'],ascending = False).head(n=5)<jupyter_output><empty_output><jupyter_text>Make a barplot of the out-degree for the nodes with out-degree greater than or equal to 50.<jupyter_code>#Your answer here

dout_min = 50  #minimum out-degree value to be included in plot
df_plot = df_degree_2[df_degree_2.out_degree>=dout_min].sort_values(by=['out_degree'],ascending=False)

fig = plt.figure(figsize = (12,6))
ax = sns.barplot(data=df_plot, x='screen_name', y="out_degree")
ax.set_xticklabels(ax.get_xticklabels(),rotation = 90)

plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.ylabel('Out-degree',fontsize = 18)
plt.xlabel('Screen name',fontsize = 18)
plt.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>Draw the following network, but make the node size proportional to the out-degree.<jupyter_code>#Your answer here
pos = nx.spring_layout(G)

node_color = 'red'
edge_color = 'purple'
background_color = 'black'
edge_width = 1
size_min = 1
size_max = 1000

#define parameters for linear interpolation of node size from out-degree
dmax = df_degree_2.out_degree.max()
dmin = df_degree_2.out_degree.min()
slope = (size_max-size_min)/(dmax-dmin)
intercept = size_min-slope*dmin

#Go through each node and calculate its size
node_size = [G.out_degree(node)*slope+intercept  for node in G.nodes()]

fig = plt.figure(figsize=(8,8))
nx.draw(G.to_undirected(), pos, node_color = node_color, width= edge_width,edge_color=edge_color,node_size=node_size)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10355458832063838, 'fraction_numerical': 0.016503445774392455, 'mean_word_length': 5.45784543325527, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '418381', 'license': 'MIT', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1867, 'n_words': 524, 'path': 'Tweets_analysis/retweets_network.ipynb', 'repo_name': 'MilesHui/NLP', 'size': '5514'}