In [1]:
import networkx as nx
import json as js
In [2]:
a = open("data.txt")
In [3]:
line1 = a.readline()
In [4]:
print(line1)
{"contributors": null, "truncated": false, "text": "Pertemuan terakhir Spanyol vs Chile terjadi di laga persahabatan di Jenewa, Swiss, September 2013 bermain imbang 2-2 #ESPvsCHI", "in_reply_to_status_id": null, "id": 479157727373955076, "favorite_count": 0, "source": "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", "retweeted": false, "coordinates": null, "entities": {"symbols": [], "user_mentions": [], "hashtags": [{"indices": [117, 126], "text": "ESPvsCHI"}], "urls": []}, "in_reply_to_screen_name": null, "id_str": "479157727373955076", "retweet_count": 0, "in_reply_to_user_id": null, "favorited": false, "user": {"follow_request_sent": null, "profile_use_background_image": true, "default_profile_image": false, "id": 756534156, "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/378800000167870886/VBEgB3MS.jpeg", "verified": false, "profile_image_url_https": "https://pbs.twimg.com/profile_images/378800000481414819/045e70b3fa085ea5d00e429eecd4e141_normal.jpeg", "profile_sidebar_fill_color": "DDEEF6", "profile_text_color": "333333", "followers_count": 79576, "profile_sidebar_border_color": "000000", "id_str": "756534156", "profile_background_color": "F09409", "listed_count": 201, "is_translation_enabled": false, "utc_offset": null, "statuses_count": 67913, "description": "Reminder | ALARM | Jadwal Siaran Bola Live di TV |@SiaranBolaLive 99% Akurat! #Recommended | Sudah Tau Jadwal Bola Hari Ini? Besok? | \u2709 Siaranbolalive@yahoo.com", "friends_count": 636, "location": "Satelit ", "profile_link_color": "0084B4", "profile_image_url": "http://pbs.twimg.com/profile_images/378800000481414819/045e70b3fa085ea5d00e429eecd4e141_normal.jpeg", "following": null, "geo_enabled": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/756534156/1398417322", "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/378800000167870886/VBEgB3MS.jpeg", "name": "Siaran Bola Live ", "lang": "en", "profile_background_tile": true, "favourites_count": 158, "screen_name": "SiaranBolaLive", "notifications": null, "url": null, "created_at": "Tue Aug 14 05:31:35 +0000 2012", "contributors_enabled": false, "time_zone": null, "protected": false, "default_profile": false, "is_translator": false}, "geo": null, "in_reply_to_user_id_str": null, "lang": "in", "created_at": "Wed Jun 18 07:04:39 +0000 2014", "filter_level": "medium", "in_reply_to_status_id_str": null, "place": null}

In [5]:
jfile = js.loads(line1)
In [6]:
jfile
Out[6]:
{'contributors': None,
 'coordinates': None,
 'created_at': 'Wed Jun 18 07:04:39 +0000 2014',
 'entities': {'hashtags': [{'indices': [117, 126], 'text': 'ESPvsCHI'}],
  'symbols': [],
  'urls': [],
  'user_mentions': []},
 'favorite_count': 0,
 'favorited': False,
 'filter_level': 'medium',
 'geo': None,
 'id': 479157727373955076,
 'id_str': '479157727373955076',
 'in_reply_to_screen_name': None,
 'in_reply_to_status_id': None,
 'in_reply_to_status_id_str': None,
 'in_reply_to_user_id': None,
 'in_reply_to_user_id_str': None,
 'lang': 'in',
 'place': None,
 'retweet_count': 0,
 'retweeted': False,
 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>',
 'text': 'Pertemuan terakhir Spanyol vs Chile terjadi di laga persahabatan di Jenewa, Swiss, September 2013 bermain imbang 2-2 #ESPvsCHI',
 'truncated': False,
 'user': {'contributors_enabled': False,
  'created_at': 'Tue Aug 14 05:31:35 +0000 2012',
  'default_profile': False,
  'default_profile_image': False,
  'description': 'Reminder | ALARM | Jadwal Siaran Bola Live di TV |@SiaranBolaLive 99% Akurat! #Recommended | Sudah Tau Jadwal Bola Hari Ini? Besok? | ✉ Siaranbolalive@yahoo.com',
  'favourites_count': 158,
  'follow_request_sent': None,
  'followers_count': 79576,
  'following': None,
  'friends_count': 636,
  'geo_enabled': False,
  'id': 756534156,
  'id_str': '756534156',
  'is_translation_enabled': False,
  'is_translator': False,
  'lang': 'en',
  'listed_count': 201,
  'location': 'Satelit ',
  'name': 'Siaran Bola Live ',
  'notifications': None,
  'profile_background_color': 'F09409',
  'profile_background_image_url': 'http://pbs.twimg.com/profile_background_images/378800000167870886/VBEgB3MS.jpeg',
  'profile_background_image_url_https': 'https://pbs.twimg.com/profile_background_images/378800000167870886/VBEgB3MS.jpeg',
  'profile_background_tile': True,
  'profile_banner_url': 'https://pbs.twimg.com/profile_banners/756534156/1398417322',
  'profile_image_url': 'http://pbs.twimg.com/profile_images/378800000481414819/045e70b3fa085ea5d00e429eecd4e141_normal.jpeg',
  'profile_image_url_https': 'https://pbs.twimg.com/profile_images/378800000481414819/045e70b3fa085ea5d00e429eecd4e141_normal.jpeg',
  'profile_link_color': '0084B4',
  'profile_sidebar_border_color': '000000',
  'profile_sidebar_fill_color': 'DDEEF6',
  'profile_text_color': '333333',
  'profile_use_background_image': True,
  'protected': False,
  'screen_name': 'SiaranBolaLive',
  'statuses_count': 67913,
  'time_zone': None,
  'url': None,
  'utc_offset': None,
  'verified': False}}
In [7]:
jfile['user']['name']
Out[7]:
'Siaran Bola Live '
In [8]:
jfile['user']
Out[8]:
{'contributors_enabled': False,
 'created_at': 'Tue Aug 14 05:31:35 +0000 2012',
 'default_profile': False,
 'default_profile_image': False,
 'description': 'Reminder | ALARM | Jadwal Siaran Bola Live di TV |@SiaranBolaLive 99% Akurat! #Recommended | Sudah Tau Jadwal Bola Hari Ini? Besok? | ✉ Siaranbolalive@yahoo.com',
 'favourites_count': 158,
 'follow_request_sent': None,
 'followers_count': 79576,
 'following': None,
 'friends_count': 636,
 'geo_enabled': False,
 'id': 756534156,
 'id_str': '756534156',
 'is_translation_enabled': False,
 'is_translator': False,
 'lang': 'en',
 'listed_count': 201,
 'location': 'Satelit ',
 'name': 'Siaran Bola Live ',
 'notifications': None,
 'profile_background_color': 'F09409',
 'profile_background_image_url': 'http://pbs.twimg.com/profile_background_images/378800000167870886/VBEgB3MS.jpeg',
 'profile_background_image_url_https': 'https://pbs.twimg.com/profile_background_images/378800000167870886/VBEgB3MS.jpeg',
 'profile_background_tile': True,
 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/756534156/1398417322',
 'profile_image_url': 'http://pbs.twimg.com/profile_images/378800000481414819/045e70b3fa085ea5d00e429eecd4e141_normal.jpeg',
 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/378800000481414819/045e70b3fa085ea5d00e429eecd4e141_normal.jpeg',
 'profile_link_color': '0084B4',
 'profile_sidebar_border_color': '000000',
 'profile_sidebar_fill_color': 'DDEEF6',
 'profile_text_color': '333333',
 'profile_use_background_image': True,
 'protected': False,
 'screen_name': 'SiaranBolaLive',
 'statuses_count': 67913,
 'time_zone': None,
 'url': None,
 'utc_offset': None,
 'verified': False}
In [9]:
G=nx.Graph()
In [10]:
G.add_node(1)
G.add_nodes_from([2,3])
In [11]:
G.nodes(),G.edges()
Out[11]:
([1, 2, 3], [])
In [12]:
G.add_edge(1,2)
G.add_edge(1,4)
print(G.edges())
[(1, 2), (1, 4)]
In [13]:
e = (2,3)
G.add_edge(*e)
print(G.edges())
[(1, 2), (1, 4), (2, 3)]
In [14]:
nx.adjacency_matrix(G).toarray()
Out[14]:
array([[0, 1, 0, 1],
       [1, 0, 1, 0],
       [0, 1, 0, 0],
       [1, 0, 0, 0]], dtype=int32)
In [15]:
nx.google_matrix(G,0.98)
Out[15]:
matrix([[ 0.005,  0.495,  0.005,  0.495],
        [ 0.495,  0.005,  0.495,  0.005],
        [ 0.005,  0.985,  0.005,  0.005],
        [ 0.985,  0.005,  0.005,  0.005]])
In [16]:
nx.pagerank(G,0.98)
Out[16]:
{1: 0.3322145470892424,
 2: 0.3322145470892424,
 3: 0.1677854529107574,
 4: 0.1677854529107574}
In [17]:
G=nx.DiGraph()
G.add_edges_from(((1,2),(2,3),(1,4),(4,1),(2,1)))
print("Matriz de adyacencia",nx.adjacency_matrix(G).toarray(),
      "\nMatriz de Google",nx.google_matrix(G,0.98),
      "\nPagerank",nx.pagerank(G,0.98),sep='\n')
Matriz de adyacencia
[[0 1 0 1]
 [1 0 1 0]
 [0 0 0 0]
 [1 0 0 0]]

Matriz de Google
[[ 0.005  0.495  0.005  0.495]
 [ 0.495  0.005  0.495  0.005]
 [ 0.25   0.25   0.25   0.25 ]
 [ 0.985  0.005  0.005  0.005]]

Pagerank
{1: 0.38236541980178873, 2: 0.2306568855906546, 3: 0.15632080901690223, 4: 0.2306568855906546}
In [18]:
nx.adjacency_matrix(G).toarray()
Out[18]:
array([[0, 1, 0, 1],
       [1, 0, 1, 0],
       [0, 0, 0, 0],
       [1, 0, 0, 0]], dtype=int32)

Importante

No pasa nada al agregar dos veces el mismo arco. Se pueden usar strings para nombrar los nodos (otros objetos también pero no es tan importante para la tarea).

In [19]:
G.add_edge(1,'hola')
G.add_edge(1,'hola')
In [20]:
G.nodes(),G.edges()
Out[20]:
(['hola', 1, 2, 3, 4], [(1, 'hola'), (1, 2), (1, 4), (2, 1), (2, 3), (4, 1)])
In [21]:
nx.adjacency_matrix(G).toarray()
Out[21]:
array([[0, 0, 0, 0, 0],
       [1, 0, 1, 0, 1],
       [0, 1, 0, 1, 0],
       [0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0]], dtype=int32)

De aquí en adelante no es necesario pero es interesante

In [22]:
import numpy as np
M = np.array(nx.google_matrix(G,0.98))[:]
v = []
for key, value in nx.pagerank(G,0.98).items():
    temp = [key,value]
    v.append(temp)
v = np.array(v)[:,1].astype(np.float64)
In [23]:
v.astype(np.float64) , M
Out[23]:
(array([ 0.17410602,  0.32415323,  0.17410602,  0.1535287 ,  0.17410602]),
 array([[ 0.2       ,  0.2       ,  0.2       ,  0.2       ,  0.2       ],
        [ 0.33066667,  0.004     ,  0.33066667,  0.004     ,  0.33066667],
        [ 0.004     ,  0.494     ,  0.004     ,  0.494     ,  0.004     ],
        [ 0.2       ,  0.2       ,  0.2       ,  0.2       ,  0.2       ],
        [ 0.004     ,  0.984     ,  0.004     ,  0.004     ,  0.004     ]]))
In [24]:
np.dot(v,M)
Out[24]:
array([ 0.17410646,  0.32415226,  0.17410646,  0.15352836,  0.17410646])
In [25]:
v2 = np.ones_like(v)
v2 = v2/np.sum(v2)
for i in range(1000):
    v2 = np.dot(v2,M)
In [26]:
sum((np.dot(v2,M)-v2)**2), sum((np.dot(v,M)-v)**2)
Out[26]:
(0.0, 1.6315575542466437e-12)
In [27]:
M
Out[27]:
array([[ 0.2       ,  0.2       ,  0.2       ,  0.2       ,  0.2       ],
       [ 0.33066667,  0.004     ,  0.33066667,  0.004     ,  0.33066667],
       [ 0.004     ,  0.494     ,  0.004     ,  0.494     ,  0.004     ],
       [ 0.2       ,  0.2       ,  0.2       ,  0.2       ,  0.2       ],
       [ 0.004     ,  0.984     ,  0.004     ,  0.004     ,  0.004     ]])
In [ ]: