{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Demostrate basic creation of Vector and DataFrame" ] }, { "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/html": [ "
Daru::DataFrame:19365600 rows: 1000 cols: 2
ba
02
100
22
323
432
52
630
721
83
921
1033
1123
1213
132
1420
150
1613
1712
180
1932
2021
2100
223
2301
2401
253
26
270
2801
2931
3023
3121
.........
99931
" ], "text/plain": [ "\n", "#\n", " b a \n", " 0 2 nil \n", " 1 0 0 \n", " 2 2 nil \n", " 3 2 3 \n", " 4 3 2 \n", " 5 nil 2 \n", " 6 3 0 \n", " 7 2 1 \n", " 8 3 nil \n", " 9 2 1 \n", " 10 3 3 \n", " 11 2 3 \n", " 12 1 3 \n", " 13 nil 2 \n", " 14 2 0 \n", " ... ... ... \n" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'daru'\n", "samples = 1000\n", "\n", "# We set lazy_update to *true* so that time is not wasted in updating\n", "# metdata every time an assignment happens.\n", "Daru.lazy_update = true\n", "# The 'new_with_size' function lets you specify the size of the \n", "# vector as the argument and the block specifies how each element\n", "# of the vector will be created.\n", "a = Daru::Vector.new_with_size(samples) {r=rand(5); r==4 ? nil: r}\n", "b = Daru::Vector.new_with_size(samples) {r=rand(5); r==4 ? nil: r}\n", "\n", "# Call `#update` for updating the metadata of each vector, once \n", "# creation from the block is complete.\n", "a.update\n", "b.update\n", "\n", "# Pass the Daru::Vector objects in a Hash to the DataFrame constructor\n", "# to make a DataFrame.\n", "# \n", "# The *order* option lets you specify the way the vectors in the Hash \n", "# will be ordered. Not specifyin this will order vectors in alphabetical\n", "# order by default.\n", "ds = Daru::DataFrame.new({:a=>a,:b=>b}, order: [:b, :a])\n", "\n", "# Reset lazy_update to *false* to prevent other code from breaking.\n", "Daru.lazy_update = false\n", "\n", "ds" ] } ], "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 }