In [2]:
# import
import graphlab as gl

In [3]:
# reading the data
data = gl.SFrame('data/people_wiki.gl/')
data[:3]


This non-commercial license of GraphLab Create for academic use is assigned to atul9806@yahoo.in and will expire on February 02, 2018.
[INFO] graphlab.cython.cy_server: GraphLab Create v2.1 started. Logging: C:\Users\Atul\AppData\Local\Temp\graphlab_server_1500915526.log.0
Out[3]:
URI name text
<http://dbpedia.org/resou
rce/Digby_Morrell> ...
Digby Morrell digby morrell born 10
october 1979 is a former ...
<http://dbpedia.org/resou
rce/Alfred_J._Lewy> ...
Alfred J. Lewy alfred j lewy aka sandy
lewy graduated from ...
<http://dbpedia.org/resou
rce/Harpdog_Brown> ...
Harpdog Brown harpdog brown is a singer
and harmonica player who ...
[3 rows x 3 columns]


In [4]:
# total size of dataset
len(data)


Out[4]:
59071

Explore the dataset


In [5]:
obama = data[data['name']=='Barack Obama']
obama


Out[5]:
URI name text
<http://dbpedia.org/resou
rce/Barack_Obama> ...
Barack Obama barack hussein obama ii
brk husen bm born august ...
[? rows x 3 columns]
Note: Only the head of the SFrame is printed. This SFrame is lazily evaluated.
You can use sf.materialize() to force materialization.


In [6]:
obama['text']


Out[6]:
dtype: str
Rows: ?
['barack hussein obama ii brk husen bm born august 4 1961 is the 44th and current president of the united states and the first african american to hold the office born in honolulu hawaii obama is a graduate of columbia university and harvard law school where he served as president of the harvard law review he was a community organizer in chicago before earning his law degree he worked as a civil rights attorney and taught constitutional law at the university of chicago law school from 1992 to 2004 he served three terms representing the 13th district in the illinois senate from 1997 to 2004 running unsuccessfully for the united states house of representatives in 2000in 2004 obama received national attention during his campaign to represent illinois in the united states senate with his victory in the march democratic party primary his keynote address at the democratic national convention in july and his election to the senate in november he began his presidential campaign in 2007 and after a close primary campaign against hillary rodham clinton in 2008 he won sufficient delegates in the democratic party primaries to receive the presidential nomination he then defeated republican nominee john mccain in the general election and was inaugurated as president on january 20 2009 nine months after his election obama was named the 2009 nobel peace prize laureateduring his first two years in office obama signed into law economic stimulus legislation in response to the great recession in the form of the american recovery and reinvestment act of 2009 and the tax relief unemployment insurance reauthorization and job creation act of 2010 other major domestic initiatives in his first term included the patient protection and affordable care act often referred to as obamacare the doddfrank wall street reform and consumer protection act and the dont ask dont tell repeal act of 2010 in foreign policy obama ended us military involvement in the iraq war increased us troop levels in afghanistan signed the new start arms control treaty with russia ordered us military involvement in libya and ordered the military operation that resulted in the death of osama bin laden in january 2011 the republicans regained control of the house of representatives as the democratic party lost a total of 63 seats and after a lengthy debate over federal spending and whether or not to raise the nations debt limit obama signed the budget control act of 2011 and the american taxpayer relief act of 2012obama was reelected president in november 2012 defeating republican nominee mitt romney and was sworn in for a second term on january 20 2013 during his second term obama has promoted domestic policies related to gun control in response to the sandy hook elementary school shooting and has called for full equality for lgbt americans while his administration has filed briefs which urged the supreme court to strike down the defense of marriage act of 1996 and californias proposition 8 as unconstitutional in foreign policy obama ordered us military involvement in iraq in response to gains made by the islamic state in iraq after the 2011 withdrawal from iraq continued the process of ending us combat operations in afghanistan and has sought to normalize us relations with cuba', ... ]

In [8]:
clooney = data[data['name']=='George Clooney']
clooney['text']


Out[8]:
dtype: str
Rows: ?
['george timothy clooney born may 6 1961 is an american actor writer producer director and activist he has received three golden globe awards for his work as an actor and two academy awards one for acting and the other for producingclooney made his acting debut on television in 1978 and later gained wide recognition in his role as dr doug ross on the longrunning medical drama er from 1994 to 1999 for which he received two emmy award nominations while working on er he began attracting a variety of leading roles in films including the superhero film batman robin 1997 and the crime comedy out of sight 1998 in which he first worked with a director who would become a longtime collaborator steven soderbergh in 1999 clooney took the lead role in three kings a wellreceived war satire set during the gulf warin 2001 clooneys fame widened with the release of his biggest commercial success the heist comedy oceans eleven the first of the film trilogy a remake of the 1960 film with frank sinatra as danny ocean he made his directorial debut a year later with the biographical thriller confessions of a dangerous mind and has since directed the drama good night and good luck 2005 the sports comedy leatherheads 2008 the political drama the ides of march 2011 and the comedydrama war film the monuments men 2014he won an academy award for best supporting actor for the middle east thriller syriana 2005 and subsequently earned best actor nominations for the legal thriller michael clayton 2007 the comedydrama up in the air 2009 and the drama the descendants 2011 in 2013 he received the academy award for best picture for producing the political thriller argo alongside ben affleck and grant heslov he is the only person ever to be nominated for academy awards in six categoriesclooney is sometimes described as one of the most handsome men in the world in 2005 tv guide ranked clooney no 1 on its 50 sexiest stars of all time list in 2009 he was included in times annual time 100 as one of the most influential people in the world clooney is also noted for his political activism and has served as one of the united nations messengers of peace since january 31 2008 his humanitarian work includes his advocacy of finding a resolution for the darfur conflict raising funds for the 2010 haiti earthquake 2004 tsunami and 911 victims and creating documentaries such as sand and sorrow to raise awareness about international crises he is also a member of the council on foreign relations', ... ]

Word count


In [9]:
obama['word_count'] = gl.text_analytics.count_words(obama['text'])

In [10]:
obama['word_count']


Out[10]:
dtype: dict
Rows: 1
[{'operations': 1L, 'represent': 1L, 'office': 2L, 'unemployment': 1L, 'doddfrank': 1L, 'over': 1L, 'unconstitutional': 1L, 'domestic': 2L, 'major': 1L, 'years': 1L, 'against': 1L, 'proposition': 1L, 'seats': 1L, 'graduate': 1L, 'debate': 1L, 'before': 1L, 'death': 1L, '20': 2L, 'taxpayer': 1L, 'representing': 1L, 'obamacare': 1L, 'barack': 1L, 'to': 14L, '4': 1L, 'policy': 2L, '8': 1L, 'he': 7L, '2011': 3L, '2010': 2L, '2013': 1L, '2012': 1L, 'bin': 1L, 'then': 1L, 'his': 11L, 'march': 1L, 'gains': 1L, 'cuba': 1L, 'school': 3L, '1992': 1L, 'new': 1L, 'not': 1L, 'during': 2L, 'ending': 1L, 'continued': 1L, 'presidential': 2L, 'states': 3L, 'husen': 1L, 'osama': 1L, 'californias': 1L, 'equality': 1L, 'prize': 1L, 'lost': 1L, 'made': 1L, 'inaugurated': 1L, 'january': 3L, 'university': 2L, 'rights': 1L, 'july': 1L, 'gun': 1L, 'stimulus': 1L, 'rodham': 1L, 'troop': 1L, 'withdrawal': 1L, 'brk': 1L, 'nine': 1L, 'where': 1L, 'referred': 1L, 'affordable': 1L, 'attorney': 1L, 'on': 2L, 'often': 1L, 'senate': 3L, 'regained': 1L, 'national': 2L, 'creation': 1L, 'related': 1L, 'hawaii': 1L, 'born': 2L, 'second': 2L, 'defense': 1L, 'election': 3L, 'close': 1L, 'operation': 1L, 'insurance': 1L, 'sandy': 1L, 'afghanistan': 2L, 'initiatives': 1L, 'for': 4L, 'reform': 1L, 'house': 2L, 'review': 1L, 'representatives': 2L, 'current': 1L, 'state': 1L, 'won': 1L, 'limit': 1L, 'victory': 1L, 'unsuccessfully': 1L, 'reauthorization': 1L, 'keynote': 1L, 'full': 1L, 'patient': 1L, 'august': 1L, 'degree': 1L, '44th': 1L, 'bm': 1L, 'mitt': 1L, 'attention': 1L, 'delegates': 1L, 'lgbt': 1L, 'job': 1L, 'harvard': 2L, 'term': 3L, 'served': 2L, 'ask': 1L, 'november': 2L, 'debt': 1L, 'by': 1L, 'wall': 1L, 'care': 1L, 'received': 1L, 'great': 1L, 'signed': 3L, 'libya': 1L, 'receive': 1L, 'of': 18L, 'months': 1L, 'urged': 1L, 'foreign': 2L, 'american': 3L, 'protection': 2L, 'economic': 1L, 'act': 8L, 'military': 4L, 'hussein': 1L, 'or': 1L, 'first': 3L, 'control': 4L, 'named': 1L, 'clinton': 1L, 'dont': 2L, 'campaign': 3L, 'russia': 1L, 'civil': 1L, 'reinvestment': 1L, 'into': 1L, 'address': 1L, 'primary': 2L, 'community': 1L, 'mccain': 1L, 'down': 1L, 'hook': 1L, '63': 1L, 'americans': 1L, 'elementary': 1L, 'total': 1L, 'earning': 1L, 'repeal': 1L, 'from': 3L, 'raise': 1L, 'district': 1L, 'spending': 1L, 'republican': 2L, 'legislation': 1L, 'three': 1L, 'relations': 1L, 'nobel': 1L, 'start': 1L, 'tell': 1L, 'iraq': 4L, 'convention': 1L, 'resulted': 1L, 'john': 1L, 'was': 5L, '2012obama': 1L, 'form': 1L, 'that': 1L, 'tax': 1L, 'sufficient': 1L, 'republicans': 1L, 'strike': 1L, 'hillary': 1L, 'ended': 1L, 'arms': 1L, 'honolulu': 1L, 'filed': 1L, 'worked': 1L, 'hold': 1L, 'with': 3L, 'obama': 9L, 'street': 1L, 'ii': 1L, 'has': 4L, '1997': 1L, '1996': 1L, 'whether': 1L, 'reelected': 1L, 'budget': 1L, 'us': 6L, 'nations': 1L, 'recession': 1L, 'while': 1L, 'taught': 1L, 'marriage': 1L, 'policies': 1L, 'promoted': 1L, 'called': 1L, 'and': 21L, 'supreme': 1L, 'ordered': 3L, 'nominee': 2L, 'process': 1L, '2000in': 1L, 'is': 2L, 'romney': 1L, 'briefs': 1L, 'defeated': 1L, 'general': 1L, '13th': 1L, 'as': 6L, 'at': 2L, 'in': 30L, 'sought': 1L, 'organizer': 1L, 'shooting': 1L, 'increased': 1L, 'normalize': 1L, 'lengthy': 1L, 'united': 3L, 'court': 1L, 'recovery': 1L, 'laden': 1L, 'laureateduring': 1L, 'peace': 1L, 'administration': 1L, '1961': 1L, 'illinois': 2L, 'other': 1L, 'which': 1L, 'party': 3L, 'primaries': 1L, 'sworn': 1L, 'relief': 2L, 'war': 1L, 'columbia': 1L, 'combat': 1L, 'after': 4L, 'islamic': 1L, 'running': 1L, 'levels': 1L, 'two': 1L, 'involvement': 3L, 'response': 3L, 'included': 1L, 'president': 4L, 'law': 6L, 'nomination': 1L, '2008': 1L, 'a': 7L, '2009': 3L, 'chicago': 2L, 'constitutional': 1L, 'defeating': 1L, 'treaty': 1L, 'federal': 1L, '2007': 1L, '2004': 3L, 'african': 1L, 'the': 40L, 'democratic': 4L, 'consumer': 1L, 'began': 1L, 'terms': 1L}]

Sorting the word count


In [11]:
obama_word_count_table = obama[['word_count']].stack('word_count', new_column_name = ['word', 'count'])

In [12]:
obama_word_count_table.head()


Out[12]:
word count
normalize 1
sought 1
combat 1
continued 1
unconstitutional 1
8 1
californias 1
1996 1
marriage 1
defense 1
[10 rows x 2 columns]


In [13]:
obama_word_count_table.sort('count', ascending=False)


Out[13]:
word count
the 40
in 30
and 21
of 18
to 14
his 11
obama 9
act 8
a 7
he 7
[273 rows x 2 columns]
Note: Only the head of the SFrame is printed.
You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.

Compute if-idf for the corpus


In [14]:
data['word_count'] = gl.text_analytics.count_words(data['text'])
data[:4]


Out[14]:
URI name text word_count
<http://dbpedia.org/resou
rce/Digby_Morrell> ...
Digby Morrell digby morrell born 10
october 1979 is a former ...
{'since': 1L, 'carltons':
1L, 'being': 1L, '2005': ...
<http://dbpedia.org/resou
rce/Alfred_J._Lewy> ...
Alfred J. Lewy alfred j lewy aka sandy
lewy graduated from ...
{'precise': 1L, 'thomas':
1L, 'closely': 1L, ...
<http://dbpedia.org/resou
rce/Harpdog_Brown> ...
Harpdog Brown harpdog brown is a singer
and harmonica player who ...
{'just': 1L, 'issued':
1L, 'mainly': 1L, ...
<http://dbpedia.org/resou
rce/Franz_Rottensteiner> ...
Franz Rottensteiner franz rottensteiner born
in waidmannsfeld lower ...
{'all': 1L,
'bauforschung': 1L, ...
[4 rows x 4 columns]


In [15]:
tfidf = gl.text_analytics.tf_idf(data['word_count'])

In [20]:
tfidf[:4]


Out[20]:
dtype: dict
Rows: 4
[{'since': 1.455376717308041, 'carltons': 7.0744723837970485, 'being': 1.7938099524877322, '2005': 1.6425861253275964, '2008': 1.5093391374786154, 'coach': 5.444264118987054, 'its': 1.6875948402695313, 'before': 2.9935647453367427, 'australia': 2.86858644684204, '21': 2.797250863489293, 'northern': 3.310021742836038, 'bullants': 7.489987827758714, 'to': 0.23472468840899613, 'perth': 5.051601193605607, 'sydney': 3.5981675296480873, 'selection': 3.836578553093086, '2014': 2.2073995783446634, 'has': 0.428497539744039, '2011': 1.7023470901042919, '2013': 1.9545642372230505, 'division': 2.7906099979103978, 'his': 0.7878343656409719, 'was': 0.3968289280609173, 'rules': 3.8272034844276295, 'assistant': 2.5220702633476124, 'spanned': 5.531174273867493, 'early': 1.929422753652229, 'game': 2.4168995190159084, 'five': 2.2137301792754096, 'during': 1.3174651479035495, 'continued': 2.720588055069447, '44game': 9.887883100557085, 'cause': 4.8023464982877115, 'twice': 3.3301582227950113, 'round': 2.897933583948961, 'parade': 5.510031837293684, 'born': 0.268196273764765, 'clubs': 3.4464050690798693, 'college': 1.5613662703175555, 'blues': 4.064837205074066, 'for': 0.29145011737314763, 'falcons': 5.868501576808439, 'currently': 1.637088969126014, 'hill': 3.794313330511949, 'drawn': 4.96062941539988, 'kangaroos': 20.726873835958425, 'kicked': 5.142950972193835, 'exchange': 4.113331555012676, 'mckernan': 9.600201028105303, '19982000': 6.509158574746988, 'losing': 3.773463729390325, 'essendon': 6.016682089649193, 'along': 2.5088749729287803, 'teaches': 3.7712554104950966, 'by': 0.37455341206197373, 'box': 4.576320507259028, 'league': 8.342695137239867, 'career': 1.3050270203415668, 'of': 0.016624704796446097, 'against': 4.015921958283749, 'david': 2.4512658353228582, 'melbourne': 3.8914310119380633, 'digby': 8.347438059609935, 'games': 2.2331239682242914, 'leading': 5.061504320250359, 'traded': 3.952107459309691, 'first': 0.6956048713993103, 'goalkicker': 6.859361004180102, 'morrell': 46.88528738395547, 'corey': 6.486685718894929, 'acted': 4.137429106591736, 'football': 25.58570665567139, 'carlton': 10.682096983163913, 'from': 0.5875106759712689, 'district': 2.774469584601757, 'west': 5.205210090246754, 'hawks': 5.531174273867493, 'draft': 3.240194726993755, 'coburg': 7.851001173296044, 'forward': 3.8848194176057507, 'australian': 8.630007339620153, 'recruited': 4.4897203990393315, 'until': 1.7591012626831841, 'shifted': 5.634637255749128, 'club': 11.043158373020127, 'season': 5.6626008156972025, 'vflaffiliate': 7.431147327735781, 'western': 3.0880842964135953, 'with': 1.0123432126103034, 'former': 1.3573131120992086, 'he': 1.280211345952344, '10': 2.3157231098806563, 'october': 1.9182947844101343, 'third': 2.3506306680914584, 'footballer': 3.2388985505323085, 'victorian': 4.564873121418676, 'played': 4.636320601315451, '32': 4.3717697890214335, 'following': 1.9609195556941061, 'teague': 9.04058524016988, 'and': 0.002980575592194913, 'strathmore': 8.588600116426823, 'is': 0.02761625047551999, 'premier': 3.6766139034004075, 'delisted': 5.816011394187043, 'as': 0.2543390440248236, 'brisbaneafter': 10.986495389225194, 'at': 0.8612771466165147, 'in': 0.01255028255157884, 'education': 2.4487155642005685, 'physical': 4.001779069106929, 'afl': 4.70049729471633, 'end': 4.839120211828286, 'premiership': 4.5439552227569955, 'retiring': 3.8140708121003493, 'edflhe': 10.986495389225194, 'also': 0.4627270916162349, 'other': 1.4424007566948476, 'rookie': 4.104057918227347, 'play': 2.270287418073342, 'coached': 3.9925624140020046, 'who': 0.9098952189804214, 'senior': 6.897159666747814, 'a': 0.022476737890332586, 'vfl': 5.253154112327449, '1979': 2.6032908378122737, 'age': 2.138848033513307, '2002': 1.8753125887822302, '2003': 1.8013702663900752, '2000': 1.8763068991994527, '2001': 1.9280249665871378, '2006': 1.520737905384506, '2007': 1.4879730697555795, 'time': 1.3253342074200498, 'the': 0.0027426017494956603, '2009': 1.5644364836042695, 'aflfrom': 10.986495389225194, 'playing': 4.182005515547123, 'goals': 3.4581636225179473}, {'precise': 6.44320060695519, 'thomas': 3.3202734635624696, 'closely': 4.591233791109745, 'disturbances': 7.808441558877249, 'wehr': 10.293348208665249, 'bright': 5.602000326436105, 'sleep': 11.773257922801992, 'nimh': 8.588600116426823, 'monitored': 7.460134864609033, 'disorders': 23.746557527902628, 'had': 1.177428412308558, 'to': 0.1408348130453977, 'chronobiologic': 10.986495389225194, 'treatment': 13.071396407363236, 'rhythm': 4.985080511264044, 'maryland': 4.251903729252247, 'include': 2.174545211685214, 'plasma': 7.0744723837970485, 'scheduled': 4.870603263742161, 'hormone': 7.489987827758714, 'assay': 8.683910296231149, 'pharmacology': 6.908957945319475, 'very': 2.811792506755733, 'ophthalmology': 8.153282045168979, 'every': 3.306319748788604, 'they': 1.8993401178193898, 'day': 5.322863391187994, 'follows': 5.421974981902501, 'university': 1.6946860096423695, 'l': 3.969885705330975, 'morning': 7.646645996757104, 'winter': 4.011081461769242, 'individuals': 4.577966598165696, 'each': 3.168869946171825, 'biological': 5.041074780618619, 'depression': 5.862531409821935, 'work': 1.3993637697254548, 'mental': 4.694926249666874, 'national': 1.1860931647723914, 'circadian': 25.765800349280468, 'certain': 9.82690171024958, 'laboratory': 4.290696472166703, '24hour': 6.669007275688884, 'sandy': 5.493433945884646, 'mood': 6.592046234552756, 'for': 0.14572505868657382, 'chicago': 2.9291179006672023, 'pattern': 6.158181651922893, 'robert': 2.802818806604537, 'research': 4.395194159760633, 'current': 2.8308461188591933, 'health': 5.989129738745434, 'cause': 9.604692996575423, 'publications': 3.253249742695399, 'available': 3.9468350393631186, 'be': 4.218744013624684, 'full': 3.003055326218652, 'drifting': 7.489987827758714, 'focused': 3.684673047087262, 'graduated': 2.2579073935292953, 'relying': 7.179832899454874, 'lag': 8.588600116426823, 'on': 0.5532382600518011, 'rhythms': 6.166213823620158, 'jet': 5.868501576808439, 'institute': 2.1793227663084926, 'of': 0.02909323339378067, 'prior': 2.956736868784372, 'aka': 4.654993539331503, 'studies': 2.3430220318986272, 'blind': 5.522663584199584, 'lewy': 28.131172430373283, 'modalities': 8.588600116426823, 'phasedelay': 10.986495389225194, 'sack': 6.9975113426609195, 'precisely': 7.0162034756730725, 'he': 0.853474230634896, 'from': 0.29375533798563447, 'working': 2.074561053063768, '1981': 2.5978176200443843, 'their': 1.5201958369931787, 'describes': 4.564873121418676, 'alfred': 4.8596262051110095, 'secretion': 9.19473591999714, 'was': 0.07936578561218346, 'phaseadvance': 10.986495389225194, 'that': 0.6614069466714981, 'oregon': 14.163582529462454, 'worked': 1.553891853362109, 'with': 0.40493728504412135, '94': 5.89889905399281, 'must': 4.665727094974612, 'md': 4.75012579902149, 'has': 0.856995079488078, 'hour': 4.467348101284799, 'developing': 3.7973276508048714, 'these': 5.1951804362209755, 'science': 2.3447863231113892, 'j': 3.2558813231614545, 'evening': 8.825630444529097, 'can': 5.600061919606208, 'shift': 5.437419304329975, 'my': 2.903166780438819, '25hour': 10.986495389225194, 'and': 0.007451438980487283, 'colleague': 5.103173000736915, 'his': 0.2626114552136573, 'defined': 5.629909114553182, 'december': 2.001425829579395, 'is': 0.05523250095103998, 'an': 0.5964781781637942, 'as': 0.3815085660372354, 'pubmed': 9.600201028105303, 'at': 0.645957859962386, 'have': 1.4416847832984716, 'in': 0.003861625400485797, 'melatonin': 49.43941550278542, 'vicechair': 5.8565966743021205, 'clearly': 5.82170941530168, 'ohsu': 9.887883100557085, 'studying': 3.612493529875034, 'holds': 2.8415259721373194, 'administration': 9.885885875106995, 'shifts': 13.784301654006187, 'take': 3.112517009620693, 'bethesda': 6.7380001471758355, '2005': 1.6425861253275964, 'department': 2.3398541306220704, 'unless': 6.411784410721811, 'maladaptation': 10.986495389225194, 'after': 0.9443334420013064, 'phd': 2.5603215961961254, 'moving': 3.1908488528906003, 'assessed': 6.9975113426609195, 'totally': 5.810345656651365, 'senior': 2.2990532222492712, 'types': 5.620519374203343, 'exposure': 16.443491559878495, 'a': 0.03371510683549888, 'light': 11.222520964891977, 'professor': 2.010865204934687, 'later': 1.4294496043477696, '1973': 2.808137223619358, 'capsule': 7.895452935866879, 'time': 1.3253342074200498, 'psychiatry': 11.737003153616879, 'the': 0.00020315568514782669}, {'just': 2.7007299687108643, 'issued': 4.429717033067152, 'mainly': 3.841299254228023, 'nominated': 2.8896779186528754, 'years': 1.0752380994247055, 'leads': 4.9486244693030566, 'tours': 4.057957571060529, 'teamed': 4.91345085512479, 'cds': 4.676577110998678, 'broadcaster': 4.608069205573607, 'harmonica': 13.04117454114122, 'to': 0.04694493768179923, 'voted': 3.922591427753126, '2014': 4.414799156689327, 'piano': 3.6534723748387132, 'has': 0.856995079488078, 'thousands': 4.676577110998678, '2010': 1.5928339601219734, 'brown': 7.309560838997457, 'his': 0.7878343656409719, 'big': 2.940266288149817, 'band': 7.816740648304755, 'hamilton': 4.586237943916373, 'they': 1.8993401178193898, 'association': 2.1546374540272892, 'northwestern': 4.9042764788487485, 'performing': 3.1247683114012137, 'comprising': 6.2679965179301, 'arthur': 4.274754994169014, 'miles': 4.658558605495999, 'either': 4.495771854722687, 'release': 2.9982917922026187, 'where': 1.089076212090673, 'j': 3.2558813231614545, 'honored': 4.140615513961145, 'their': 1.5201958369931787, 'society': 2.4448047262085693, 'year': 1.3423616371539895, 'home': 4.842167251076965, 'portland': 4.788016672732886, 'best': 6.134404289659914, 'harp': 6.361522575940923, 'canada': 5.37143685674748, 'what': 5.638286804338248, 'for': 0.29145011737314763, 'chicago': 5.858235801334405, 'since': 1.455376717308041, 'extensively': 3.7786355177927193, 'won': 1.3836400683164753, 'tens': 6.411784410721811, 'hailing': 7.157853992736099, 'full': 3.003055326218652, 'small': 3.140296573727769, 'active': 2.7479584590534256, 'from': 0.5875106759712689, 'by': 0.37455341206197373, 'on': 0.18441275335060037, 'dates': 5.597423659408693, 'holger': 8.153282045168979, 'influential': 3.738702807457348, 'of': 0.012468528597334574, 'duo': 4.3375108392004185, 'or': 1.9128915408224825, 'blind': 5.522663584199584, 'canadas': 4.660345916070095, 'scene': 3.553420040326614, 'venues': 4.3362163406377725, 'cd': 3.769051957528662, 'germanyover': 10.986495389225194, 'festivals': 3.7508762481584443, 'maple': 5.868501576808439, '1982': 2.559664637889348, 'guest': 3.134445181959305, 'working': 2.074561053063768, 'gutsy': 8.789270811888976, 'muddy': 7.728398851203712, 'been': 0.9774773354796025, 'awarded': 2.354189390708452, 'harpdog': 21.972990778450388, 'few': 6.002022064982743, 'time': 1.3253342074200498, 'was': 0.31746314244873386, 'naturally': 6.52058727057061, 'juggles': 9.887883100557085, 'life': 2.1907617832744593, 'that': 0.6614069466714981, 'club': 2.2086316746040255, 'award': 3.2644556968847374, 'released': 2.0078348995855078, 'with': 0.8098745700882427, 'he': 1.06684278829362, 'toronto': 3.3555488083347353, 'combos': 8.278445188122983, '1995': 2.222129668695386, 'canadian': 5.533799870096228, 'up': 1.5635467678501844, 'promoter': 5.6393878585077255, 'edmonds': 7.61919955923872, 'recording': 2.9764678607434605, 'while': 1.8364359481339414, 'crossed': 5.760748715511993, 'many': 1.639964662798746, 'petersen': 7.297615935111258, 'called': 2.0784770664403074, 'and': 0.0052160072863410975, 'seven': 2.7917137507818355, 'classic': 3.635337162794501, 'is': 0.19331375332863993, 'it': 3.9497417504814463, 'covers': 4.5407755698396155, 'states': 1.824400329877006, 'player': 4.26880525149613, 'as': 0.2543390440248236, 'in': 0.006757844450850144, 'graham': 4.536024967081018, 'blues': 36.58353484566659, 'mid1950s': 7.225295273531632, 'united': 1.5679220786705559, 'lemon': 7.035251670643767, 'guitarist': 3.658714850803563, '1': 2.0978765819243166, 'also': 0.4627270916162349, 'vancouver': 4.219152263959802, 'which': 0.7674309670437692, 'browns': 5.4811638532928315, 'nw': 8.683910296231149, 'album': 2.4512658353228582, 'electric': 4.464402591055042, 'juno': 6.04485296661589, 'who': 0.9098952189804214, 'membership': 4.6783969477156635, 'oregon': 4.721194176487485, 'a': 0.039334291308082026, 'singer': 2.7818235602743835, 'bluesgospel': 10.986495389225194, 'well': 1.5295293417875981, 'surveybrown': 10.986495389225194, 'original': 2.9143400810369444, 'traditional': 3.6283016364921616, 'cascade': 7.808441558877249, 'albumhe': 8.042056410058754, 'the': 0.00121893411088696, 'playing': 2.0910027577735617, 'songs': 2.9508926963066124}, {'all': 1.6431112434912472, 'bauforschung': 10.986495389225194, 'just': 2.7007299687108643, 'kurdlawitzpreis': 10.986495389225194, 'german': 6.505623636898587, 'ending': 4.547145018125096, 'produced': 5.023167180131126, 'including': 1.2272824458461182, 'austrian': 5.114377599749778, 'yet': 4.092839034622559, 'hundredth': 8.278445188122983, 'merkur': 10.293348208665249, '1942': 4.106111307039189, 'producing': 3.830318751744579, 'dick': 9.155933196331391, '28': 3.0552106276993034, 'start': 3.281232914358869, 'lower': 4.515695885442592, 'praised': 4.66933070247791, 'listing': 6.332535039067671, 'stanisaw': 7.851001173296044, 'has': 1.285492619232117, 'silverberg': 9.887883100557085, 'trnaslations': 10.986495389225194, 'jeanpierre': 6.532148092971687, 'klein': 5.930249583876886, 'was': 0.2380973568365504, 'translations': 5.570394987020774, 'matters': 4.606372852325429, 'ones': 4.746219544054425, 'wellss': 9.600201028105303, 'negative': 5.348140719891449, 'heinlein': 9.04058524016988, 'years': 3.2257142982741165, 'him': 1.5755843267871936, 'association': 2.1546374540272892, 'brought': 6.700451571691642, 'olaf': 7.115294378317303, 'january': 1.885412003185961, 'university': 0.8473430048211847, 'journalism': 3.7712554104950966, 'herbert': 5.227593615347914, 'book': 4.005609415785018, '1998': 2.0687826320938068, 'served': 1.5362723499305253, 'european': 2.590340526186013, 'zelazny': 10.293348208665249, 'view': 4.2214564124446525, 'reference': 4.67113738770286, 'describedroger': 10.986495389225194, 'series': 1.877080935838972, 'some': 6.592334027112441, 'rottensteiner': 32.95948616767558, 'born': 0.268196273764765, 'books': 4.497764704832687, 'fifty': 5.11719847609142, 'are': 1.7719638126305435, 'die': 4.504918259948763, 'phantastische': 10.986495389225194, 'close': 3.5416621153330006, 'special': 2.7022435916032777, 'phantastischen': 10.986495389225194, 'what': 2.819143402169124, 'total': 3.2767385247710297, 'abe': 6.943444121390644, 'cordwainer': 9.887883100557085, 'strugatski': 10.986495389225194, 'robert': 2.802818806604537, 'since': 1.455376717308041, 'nonwriter': 10.986495389225194, '18': 2.726778428203672, 'critical': 3.480453210707072, 'stapledon': 10.986495389225194, 'new': 1.7743065312250548, 'receiving': 3.73515040585298, 'numerous': 2.4220370053418425, 'public': 2.029113653642407, 'achievements': 4.48670834856934, 'edited': 3.7890600351286032, 'comparable': 7.1363477875151355, 'romane': 9.600201028105303, 'banal': 8.213906666985412, 'journal': 6.050947846683648, 'republished': 6.323056295113127, 'however': 2.41879921563585, 'lem': 35.62821539018143, 'betterknown': 7.941972951501771, 'york': 1.701047544762206, 'advisor': 3.6213152632041816, 'franz': 5.561545371743792, 'from': 0.5875106759712689, 'philip': 8.743539578042867, 'fifteen': 4.660345916070095, 'publisher': 4.286995049063516, 'about': 3.8530257976751474, 'works': 6.643049849940285, 'working': 2.074561053063768, 'language': 3.4581636225179473, 'asimov': 8.347438059609935, 'of': 0.08312352398223048, 'k': 9.00677607553599, 'sterreichisches': 10.986495389225194, 'through': 2.028984878933582, 'controversy': 4.290696472166703, 'his': 1.3130572760682866, 'und': 5.3270131734655735, 'w': 7.305637987135021, 'barry': 4.5126046928729195, 'promotion': 3.8914310119380633, 'introduced': 3.8326615876463515, 'or': 1.9128915408224825, 'fields': 4.109199317727766, 'franke': 8.907053847545358, 'smith': 3.6437161998933485, 'into': 1.6050629424066056, 'number': 6.3690632491627195, 'one': 0.9309307338087167, 'karel': 7.202305755306933, 'fantasy': 4.67113738770286, 'austria': 8.964414431377097, 'kobo': 9.19473591999714, 'another': 2.603062187988481, 'bibliothek': 10.293348208665249, 'aldiss': 9.600201028105303, 'awarded': 2.354189390708452, 'institut': 5.713495830661447, 'illustrated': 4.57961540315588, '1980': 2.5468314003181947, 'to': 0.1408348130453977, 'addition': 2.6415150204546234, 'leading': 5.061504320250359, 'there': 2.3005725945344695, 'three': 1.4915025293575952, 'been': 0.9774773354796025, 'fiction': 25.487913841658276, 'continental': 5.1965352183279405, 'quarber': 10.986495389225194, 'andersonare': 10.986495389225194, 'editor': 5.484848237858817, 'h': 3.9468350393631186, 'hundred': 4.639106179569184, 'assessment': 5.380693322929197, 'until': 1.7591012626831841, 'vienna': 8.899607583267779, 'seriesrottensteiner': 10.986495389225194, 'both': 1.6730570592454443, 'factor': 5.059569363254783, 'andrevonthe': 10.986495389225194, 'authors': 12.962435014328356, 'fantasticrottensteiner': 10.986495389225194, 'highest': 3.43846641929018, 'with': 0.6074059275661821, 'hg': 8.042056410058754, 'he': 1.280211345952344, 'apek': 9.887883100557085, '1995': 2.222129668695386, 'g': 4.262662948403985, 'this': 1.2818856957987381, 'science': 14.068717938668335, 'work': 1.3993637697254548, 'as': 0.8901866540868826, 'us': 1.9319904488071395, 'n': 4.282081034261087, 'lesser': 6.453895896071939, 'suhrkamp': 9.377057476791094, 'travesties': 9.600201028105303, 'doctorate': 3.6866980224670334, 'history': 2.1447579606246094, 'and': 0.009686870674633467, 'nesvadba': 10.986495389225194, 'seven': 2.7917137507818355, 'studied': 2.2933341149871773, 'figure': 3.950346895474658, 'stated': 3.7501560464708503, '1973': 2.808137223619358, 'is': 0.08284875142655997, 'year': 1.3423616371539895, 'it': 1.3165805834938153, 'brothersin': 8.907053847545358, 'an': 0.5964781781637942, 'critic': 3.842877786521073, 'eighteen': 5.377023594040234, 'at': 0.43063857330825733, 'in': 0.01158487620145739, '19791985': 8.347438059609935, 'saw': 3.4321605654994465, 'united': 1.5679220786705559, '1969': 2.8490995591685433, 'anthology': 4.558390116540598, 'latter': 3.9477118478366533, 'american': 1.1273777844250068, 'waidmannsfeld': 10.986495389225194, '1963': 3.2103799121264522, 'anthologies': 5.030658019760364, 'brian': 3.746562797904725, 'writers': 7.088005332861507, 'recognition': 3.6213152632041816, 'which': 0.7674309670437692, 'provoked': 6.696035948076803, 'out': 3.696806362913271, 'stanislaw': 17.003177478874388, 'malzberg': 9.887883100557085, 'statesrottensteiner': 10.986495389225194, 'fr': 5.154612911941677, '2004': 1.6903443608359008, 'verlags': 10.293348208665249, 'gerard': 5.7770092363837735, 'josef': 6.2329051981188295, 'wells': 5.410546286078878, 'produce': 4.200907744217265, 'two': 1.0988831858473562, 'librarian': 6.323056295113127, 'on': 0.7376510134024015, 'a': 0.039334291308082026, 'englishreading': 10.293348208665249, 'for': 0.14572505868657382, '1978': 2.6743602815767815, '1975': 5.524663753174666, 'well': 3.0590586835751963, 'shore': 5.644161137260383, 'greatest': 3.7530399706037554, 'volumes': 9.813124388259208, 'english': 2.239461125047026, 'occasion': 5.020348650101503, 'the': 0.0022347125366260936, 'sf': 12.782751078181208, 'typical': 5.839000912411741}]

In [21]:
data['tfidf'] = tfidf

Exploring the tf-idf


In [24]:
obama = data[data['name']=='Barack Obama']
obama[:3]


Out[24]:
URI name text word_count
<http://dbpedia.org/resou
rce/Barack_Obama> ...
Barack Obama barack hussein obama ii
brk husen bm born august ...
{'operations': 1L,
'represent': 1L, ...
tfidf
{'operations':
3.811771079388818, ...
[1 rows x 5 columns]


In [28]:
obama[['tfidf']].stack('tfidf', new_column_name=['word', 'tf-idf']).sort('tf-idf', ascending=False)


Out[28]:
word tf-idf
obama 43.2956530721
act 27.678222623
iraq 17.747378588
control 14.8870608452
law 14.7229357618
ordered 14.5333739509
military 13.1159327785
involvement 12.7843852412
response 12.7843852412
democratic 12.4106886973
[273 rows x 2 columns]
Note: Only the head of the SFrame is printed.
You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.

Manually compute the distances between a few people


In [29]:
clinton = data[data['name']=='Bill Clinton']
beckham = data[data['name']=='David Beckham']

Compute the similarity


In [31]:
# cosine similarity - higher the better
# cosine distance - lower the better as implemented in graphlab

gl.distances.cosine(obama['tfidf'][0], clinton['tfidf'][0])


Out[31]:
0.8339854936884276

In [32]:
gl.distances.cosine(obama['tfidf'][0], beckham['tfidf'][0])


Out[32]:
0.9791305844747478

Building a nearest neighbor model


In [34]:
knn_model = gl.nearest_neighbors.create(data, features=['tfidf'], label='name')


Starting brute force nearest neighbors model training.

Applying the KNN model


In [35]:
knn_model.query(obama)


Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00169288  | 52.034ms     |
| Done         |         | 100         | 399.265ms    |
+--------------+---------+-------------+--------------+
Out[35]:
query_label reference_label distance rank
0 Barack Obama 0.0 1
0 Joe Biden 0.794117647059 2
0 Joe Lieberman 0.794685990338 3
0 Kelly Ayotte 0.811989100817 4
0 Bill Clinton 0.813852813853 5
[5 rows x 4 columns]

Other examples


In [36]:
swift = data[data['name']=='Taylor Swift']

In [37]:
knn_model.query(swift)


Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00169288  | 12.508ms     |
| Done         |         | 100         | 329.218ms    |
+--------------+---------+-------------+--------------+
Out[37]:
query_label reference_label distance rank
0 Taylor Swift 0.0 1
0 Carrie Underwood 0.76231884058 2
0 Alicia Keys 0.764705882353 3
0 Jordin Sparks 0.769633507853 4
0 Leona Lewis 0.776119402985 5
[5 rows x 4 columns]


In [38]:
jolie = data[data['name']=='Angelina Jolie']

In [39]:
knn_model.query(jolie)


Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00169288  | 13.509ms     |
| Done         |         | 100         | 331.22ms     |
+--------------+---------+-------------+--------------+
Out[39]:
query_label reference_label distance rank
0 Angelina Jolie 0.0 1
0 Brad Pitt 0.784023668639 2
0 Julianne Moore 0.795857988166 3
0 Billy Bob Thornton 0.803069053708 4
0 George Clooney 0.8046875 5
[5 rows x 4 columns]

Quiz


In [40]:
john = data[data['name']=='Elton John']

In [42]:
john


Out[42]:
URI name text word_count
<http://dbpedia.org/resou
rce/Elton_John> ...
Elton John sir elton hercules john
cbe born reginald ken ...
{'all': 1L, 'six': 1L,
'producer': 1L, ...
tfidf
{'all':
1.6431112434912472, ...
[? rows x 5 columns]
Note: Only the head of the SFrame is printed. This SFrame is lazily evaluated.
You can use sf.materialize() to force materialization.


In [44]:
john[['word_count']].stack('word_count', new_column_name=['word', 'count']).sort('count', ascending=False).head(5)


Out[44]:
word count
the 27
in 18
and 15
of 13
a 10
[5 rows x 2 columns]


In [45]:
john[['tfidf']].stack('tfidf', new_column_name=['word', 'tfidf']).sort('tfidf', ascending=False).head(5)


Out[45]:
word tfidf
furnish 18.38947184
elton 17.48232027
billboard 17.3036809575
john 13.9393127924
songwriters 11.250406447
[5 rows x 2 columns]


In [46]:
vic = data[data['name']=='Victoria Beckham']
paul = data[data['name']=='Paul McCartney']

In [52]:
gl.distances.cosine(john['tfidf'][0], vic['tfidf'][0])


Out[52]:
0.9567006376655429

In [53]:
gl.distances.cosine(john['tfidf'][0], paul['tfidf'][0])


Out[53]:
0.8250310029221779

In [64]:
knn_model_word_count = gl.nearest_neighbors.create(data, features=['word_count'], label='name', distance='cosine')


Starting brute force nearest neighbors model training.

In [65]:
knn_model_word_count.query(john)


Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00169288  | 13.009ms     |
| Done         |         | 100         | 388.284ms    |
+--------------+---------+-------------+--------------+
Out[65]:
query_label reference_label distance rank
0 Elton John 2.22044604925e-16 1
0 Cliff Richard 0.16142415259 2
0 Sandro Petrone 0.16822542751 3
0 Rod Stewart 0.168327165587 4
0 Malachi O'Doherty 0.177315545979 5
[5 rows x 4 columns]


In [66]:
knn_model_tfidf = gl.nearest_neighbors.create(data, features=['tfidf'], label='name', distance='cosine')


Starting brute force nearest neighbors model training.

In [67]:
knn_model_tfidf.query(john)


Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00169288  | 11.008ms     |
| Done         |         | 100         | 398.281ms    |
+--------------+---------+-------------+--------------+
Out[67]:
query_label reference_label distance rank
0 Elton John -2.22044604925e-16 1
0 Rod Stewart 0.717219667893 2
0 George Michael 0.747600998969 3
0 Sting (musician) 0.747671954431 4
0 Phil Collins 0.75119324879 5
[5 rows x 4 columns]


In [68]:
knn_model_tfidf.query(vic)


Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00169288  | 11.007ms     |
| Done         |         | 100         | 386.276ms    |
+--------------+---------+-------------+--------------+
Out[68]:
query_label reference_label distance rank
0 Victoria Beckham 1.11022302463e-16 1
0 David Beckham 0.548169610263 2
0 Stephen Dow Beckham 0.784986706828 3
0 Mel B 0.809585523409 4
0 Caroline Rush 0.819826422919 5
[5 rows x 4 columns]


In [69]:
knn_model_word_count.query(vic)


Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00169288  | 9.004ms      |
| Done         |         | 100         | 366.256ms    |
+--------------+---------+-------------+--------------+
Out[69]:
query_label reference_label distance rank
0 Victoria Beckham -2.22044604925e-16 1
0 Mary Fitzgerald (artist) 0.207307036115 2
0 Adrienne Corri 0.214509782788 3
0 Beverly Jane Fry 0.217466468741 4
0 Raman Mundair 0.217695474992 5
[5 rows x 4 columns]


In [ ]: