{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Finding and plotting the most heard artists on last.fm\n", "\n", "This notebook is a super basic use case of daru for finding a plotting the most heard artists from a dataset from last.fm.\n", "\n", "For detailed and in-depth examples see the other notebooks." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "if(window['d3'] === undefined ||\n", " window['Nyaplot'] === undefined){\n", " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", "\n", "\n", "\n", " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", "\n", " require.config({paths: path, shim:shim});\n", "\n", "\n", "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", "\n", "\tvar script = d3.select(\"head\")\n", "\t .append(\"script\")\n", "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", "\t .attr(\"async\", true);\n", "\n", "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", "\n", "\n", "\t var event = document.createEvent(\"HTMLEvents\");\n", "\t event.initEvent(\"load_nyaplot\",false,false);\n", "\t window.dispatchEvent(event);\n", "\t console.log('Finished loading Nyaplotjs');\n", "\n", "\t};\n", "\n", "\n", "});});\n", "}\n" ], "text/plain": [ "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "true" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'daru'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
Daru::DataFrame:24929480 rows: 2500 cols: 6
artidartnametimestamptraidtranameuserid
0e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:55:00Z0138547f-374d-49d4-8f1c-9b2c8b5c65c0X-Ray Eyesuser_000705
1e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:51:28Z22ce9a32-3510-4c8e-8151-c044a62eb4fdHard Timesuser_000705
2e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:46:55Z22ce9a32-3510-4c8e-8151-c044a62eb4fdHard Timesuser_000705
3e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:42:17Zc386b85f-1bfe-4038-b323-cb8229ab2f8bMagic Touchuser_000705
4e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:37:53Z8ec9b4f8-2d1b-4616-9ae4-4711ca328fc3Charismauser_000705
5e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:33:37Zff47d82e-313d-4bd0-8e61-fe61124a361bDirty Livin'user_000705
6e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:29:39Z3621800b-87b7-43c6-b6a3-47ea4b658407Sure Know Somethinguser_000705
7e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:24:46Z91cc5010-e65b-4f49-94b5-1deed61cfcc72,000 Manuser_000705
8e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:20:17Zdaa13e72-9056-476f-b431-335b2c26842dI Was Made For Lovin' Youuser_000705
933b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:16:20Z1895c79d-ae5f-40bf-bc18-83fdcd8d2842Burning Of The Midnight Lampuser_000705
1033b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:12:35Z1ae3c250-7b16-49d4-9c71-0928724eecceGypsy Eyesuser_000705
1133b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:08:26Zfca63fe0-c9e6-4347-92b7-21f6cf79226cCome On, Part 1user_000705
1233b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:04:59Z36beb31d-ed74-4a49-832f-a4a38dc96babLong Hot Summer Nightuser_000705
1333b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:02:08Z4ad6064e-7d54-49a2-a134-320a1612ee27Little Miss Strangeuser_000705
1433b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T11:47:07Z40ffef8e-b851-473a-a239-08685eab22b0Voodoo Chileuser_000705
1533b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T11:44:43Zfeb64d01-6e05-4974-bb44-0046deca1bdbCrosstown Trafficuser_000705
1633b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T11:42:35Z7f9b462a-8ac5-472d-a5aa-716b3b3d169cHave You Ever Been (To Electric Ladyland)user_000705
1733b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T11:41:13Z88b57468-8d58-4310-b135-c94620fcfbc1...And The Gods Made Loveuser_000705
180039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:24:28Z788878e0-8965-489e-86e3-add4c1f3d134Line Of Best Fituser_000705
190039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:19:58Z7cfc403d-18e7-4b8e-b0c4-1864ca393fbbFake Frownsuser_000705
200039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:15:03Z34a2d7ad-6b3a-4cfc-b4ca-edfc55a6685bAmputationsuser_000705
210039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:11:21Z99db2ed6-4493-481c-ac20-54ad323cb04bThe Face That Launched 1000 Shitsuser_000705
220039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:07:44Z269084ba-3065-4b43-a733-f491e618b26aSleep Spentuser_000705
230039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:03:54Z96fac269-5308-4aee-827f-d01a66674694Pictures In An Exhibitionuser_000705
240039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T08:59:34Zc29440d9-03e6-4e18-91d6-ef8bf1e1ca32Your Bruiseuser_000705
250039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T08:56:55Zed55b695-976e-4898-8fde-51a8e3b0e262Champagne From A Paper Cupuser_000705
260039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T08:49:07Zc5238d6d-9b04-496c-8674-4ea7a07aecd6President Of What?user_000705
270039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T08:44:34Zbf59dfef-03a8-4270-9456-50df79da56c2Bend To Squaresuser_000705
280039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-01T19:08:37Z788878e0-8965-489e-86e3-add4c1f3d134Line Of Best Fituser_000705
290039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-01T19:04:07Z7cfc403d-18e7-4b8e-b0c4-1864ca393fbbFake Frownsuser_000705
300039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-01T18:59:12Z34a2d7ad-6b3a-4cfc-b4ca-edfc55a6685bAmputationsuser_000705
310039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-01T18:55:30Z99db2ed6-4493-481c-ac20-54ad323cb04bThe Face That Launched 1000 Shitsuser_000705
.....................
2499e0bba708-bdd3-478d-84ea-c706413bedabA. R. Rahman2009-01-12T15:26:51ZPaper Planes (Dfa Remix)user_000705
" ], "text/plain": [ "\n", "#\n", " artid artname timestamp traid traname userid \n", " 0 e1f1e33e-2 Kiss 2009-05-03 0138547f-3 X-Ray Eyes user_00070 \n", " 1 e1f1e33e-2 Kiss 2009-05-03 22ce9a32-3 Hard Times user_00070 \n", " 2 e1f1e33e-2 Kiss 2009-05-03 22ce9a32-3 Hard Times user_00070 \n", " 3 e1f1e33e-2 Kiss 2009-05-03 c386b85f-1 Magic Touc user_00070 \n", " 4 e1f1e33e-2 Kiss 2009-05-03 8ec9b4f8-2 Charisma user_00070 \n", " 5 e1f1e33e-2 Kiss 2009-05-03 ff47d82e-3 Dirty Livi user_00070 \n", " 6 e1f1e33e-2 Kiss 2009-05-03 3621800b-8 Sure Know user_00070 \n", " 7 e1f1e33e-2 Kiss 2009-05-03 91cc5010-e 2,000 Man user_00070 \n", " 8 e1f1e33e-2 Kiss 2009-05-03 daa13e72-9 I Was Made user_00070 \n", " 9 33b3c323-7 The Jimi H 2009-05-03 1895c79d-a Burning Of user_00070 \n", " 10 33b3c323-7 The Jimi H 2009-05-03 1ae3c250-7 Gypsy Eyes user_00070 \n", " 11 33b3c323-7 The Jimi H 2009-05-03 fca63fe0-c Come On, P user_00070 \n", " 12 33b3c323-7 The Jimi H 2009-05-03 36beb31d-e Long Hot S user_00070 \n", " 13 33b3c323-7 The Jimi H 2009-05-03 4ad6064e-7 Little Mis user_00070 \n", " 14 33b3c323-7 The Jimi H 2009-05-03 40ffef8e-b Voodoo Chi user_00070 \n", " ... ... ... ... ... ... ... \n" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'open-uri'\n", "content = open('https://raw.githubusercontent.com/v0dro/daru/master/spec/fixtures/music_data.tsv')\n", "df = Daru::DataFrame.from_csv content, col_sep: \"\\t\"\n", "df.vectors = Daru::Index.new(df.vectors.to_a.map(&:to_sym))\n", "df" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
Daru::Vector:35332940 size: 376
size
3 Doors Down3
A Perfect Circle1
A. R. Rahman231
Abba1
Aditi Singh Sharma3
Aerosmith1
Air1
Akurat1
Alanis Morissette5
Alice Cooper2
Alice In Chains1
Alison Krauss1
Altamiro Carrilho1
Alvin Youngblood Hart3
America1
Amit Trivedi20
Amon Amarth1
Anathema4
Andrea Bocelli16
Andrea Bocelli;Christina Aquilera2
Andrea Bocelli;Kenny G2
Andrea Bocelli;Mario Reyes1
Andrew Lloyd Webber1
Anton Maiden2
Antsy Pants2
Apocalyptica9
Archive1
Arctic Monkeys1
Aretha Franklin4
Asia1
Audioslave1
Aurelio Fierro1
......
シートベルツ32
" ], "text/plain": [ "\n", "#\n", " size\n", " 3 Doors Down 3\n", " A Perfect Circle 1\n", " A. R. Rahman 231\n", " Abba 1\n", " Aditi Singh Sharma 3\n", " Aerosmith 1\n", " Air 1\n", " Akurat 1\n", " Alanis Morissette 5\n", " Alice Cooper 2\n", " Alice In Chains 1\n", " Alison Krauss 1\n", " Altamiro Carrilho 1\n", "Alvin Youngblood Har 3\n", " America 1\n", " Amit Trivedi 20\n", " Amon Amarth 1\n", " ... ...\n" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Group by artist name and call 'size' to see the number of rows each artist populates.\n", "artists = df.group_by(:artname).size" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
Daru::Vector:35427380 size: 1
size
A. R. Rahman231
" ], "text/plain": [ "\n", "#\n", " size\n", "A. R. Rahman 231\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "artists.max_index" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n" ], "text/plain": [ "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"f0aed652-9d85-4036-b707-e7033477bfd7\"}, @xrange=[\"A. R. Rahman\", \"Death Cab For Cutie\", \"Pink Floyd\", \"The Rolling Stones\", \"Opeth\", \"The Beatles\", \"The Cranberries\", \"Cat Stevens\", \"Howard Shore\", \"Nick Drake\"], @yrange=[0, 231]>], :options=>{:yrange=>[0, 300], :width=>1120, :height=>800, :legend=>true, :rotate_x_label=>45, :xrange=>[\"A. R. Rahman\", \"Death Cab For Cutie\", \"Pink Floyd\", \"The Rolling Stones\", \"Opeth\", \"The Beatles\", \"The Cranberries\", \"Cat Stevens\", \"Howard Shore\", \"Nick Drake\"]}}>], :data=>{\"f0aed652-9d85-4036-b707-e7033477bfd7\"=>#\"A. R. Rahman\", :data1=>231}, {:data0=>\"Death Cab For Cutie\", :data1=>206}, {:data0=>\"Pink Floyd\", :data1=>158}, {:data0=>\"The Rolling Stones\", :data1=>91}, {:data0=>\"Opeth\", :data1=>89}, {:data0=>\"The Beatles\", :data1=>67}, {:data0=>\"The Cranberries\", :data1=>61}, {:data0=>\"Cat Stevens\", :data1=>57}, {:data0=>\"Howard Shore\", :data1=>56}, {:data0=>\"Nick Drake\", :data1=>51}]>}, :extension=>[]}>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "top_ten = artists.sort(ascending: false).head\n", "\n", "top_ten.plot type: :bar do |plt|\n", " plt.yrange [0,300]\n", " plt.width 1120 \n", " plt.height 800\n", " plt.legend true\n", " plt.rotate_x_label 45\n", "end" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.2.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.2.1" } }, "nbformat": 4, "nbformat_minor": 0 }