Skip to content

Instantly share code, notes, and snippets.

@specbug
Last active December 1, 2022 14:04
Show Gist options
  • Save specbug/33c00182aa49855c06c5f1686eecf24a to your computer and use it in GitHub Desktop.
Save specbug/33c00182aa49855c06c5f1686eecf24a to your computer and use it in GitHub Desktop.
A sample representation of Twitter information bubble. Generated by ChatGPT.
# Analyze the clusters and connections in the graph
for node, cluster in partition.items():
if cluster == 1:
# Calculate the percentage of nodes that represent Tweets and the percentage of nodes that represent people you follow
num_tweets = len([n for n in cluster1 if n['type'] == 'tweet'])
num_following = len([n for n in cluster1 if n['type'] == 'person'])
tweet_percentage = num_tweets / len(cluster1)
following_percentage = num_following / len(cluster1)
print(f'Cluster 1: {tweet_percentage}% Tweets, {following_percentage}% people you follow')
# Calculate the percentage of connections that exist within the cluster and the percentage of connections that exist between the cluster and other clusters
num_intra_connections = len([e for e in G.edges if e[0] in cluster1 and e[1] in cluster1])
num_inter_connections = len([e for e in G.edges if e[0] in cluster1 and e[1] not in cluster1])
intra_percentage = num_intra_connections / len(cluster1)
inter_percentage = num_inter_connections / len(cluster1)
print(f'Cluster 1: {intra_percentage}% intra-cluster connections, {inter_percentage}% inter-cluster connections')
elif cluster == 2:
...
import networkx as nx
from sklearn.preprocessing import StandardScaler
from sklearn_extra.cluster import KMedoids
# Collect data about the Tweets you've liked and the people you follow
tweets = ['tweet1', 'tweet2', 'tweet3']
following = ['person1', 'person2', 'person3']
# Preprocess the data
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # example data
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
# Create a graph object
G = nx.Graph()
# Add nodes to the graph
for tweet in tweets:
G.add_node(tweet, type='tweet')
for person in following:
G.add_node(person, type='person')
# Add edges to the graph
for tweet in tweets:
for person in following:
G.add_edge(tweet, person)
# Use modularity maximization to group the nodes into clusters
partition = community.best_partition(G)
# Visualize the graph using D3.js
d3.render(G, 'graph.html')
# Analyze the clusters and connections in the graph
for node, cluster in partition.items():
if cluster == 1:
print(f'{node} belongs to cluster 1')
elif cluster == 2:
print(f'{node} belongs to cluster 2')
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment