{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Indexing in Daru has grown more powerful. Here's a glimpse. Enjoy!" ] }, { "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\":\"http://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\", \"http://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\\\":\\\"http://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\\\", \\\"http://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": "markdown", "metadata": {}, "source": [ "# Categorical Index" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A new class of index got added to Daru to handle indexing which can be categorical. This is basically how it works." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx = Daru::CategoricalIndex.new [:a, 1, :a, 1, :c]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(5)
aa
1b
ac
1d
ce
" ], "text/plain": [ "#\n", " a a\n", " 1 b\n", " a c\n", " 1 d\n", " c e" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv = Daru::Vector.new 'a'..'e', index: idx" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get elements with single category." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(2)
aa
ac
" ], "text/plain": [ "#\n", " a a\n", " a c" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[:a]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Retrive elements by multiple categories." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(4)
aa
1b
ac
1d
" ], "text/plain": [ "#\n", " a a\n", " 1 b\n", " a c\n", " 1 d" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[:a, 1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get elements by position using `#at`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"b\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv.at 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can get multiple element as a vector if you specify more than one position." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(3)
aa
1b
ac
" ], "text/plain": [ "#\n", " a a\n", " 1 b\n", " a c" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv.at 0, 1, 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note**: You can also use `#[]` to get elements by position only if that position is not a index." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get elements by single position." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"d\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get elements by multiple positions." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(2)
aa
1d
" ], "text/plain": [ "#\n", " a a\n", " 1 d" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[0, 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Change values at certain indexes using `[]=`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(5)
a20
1b
a20
1d
ce
" ], "text/plain": [ "#\n", " a 20\n", " 1 b\n", " a 20\n", " 1 d\n", " c e" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[:a] = 20\n", "dv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Change values at certain positions using `at_set`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(5)
a100
1100
a20
1d
ce
" ], "text/plain": [ "#\n", " a 100\n", " 1 100\n", " a 20\n", " 1 d\n", " c e" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv.at_set [0, 1], 100\n", "dv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other index classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Overall all Index classes API are improved. Everyclass respond to multiple indexes, positional index, give proper error messages, etc.\n", "\n", "Here are few examples:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Index" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx = Daru::Index.new [:a, :b, :c, :d]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(4)
a1
b2
c3
d4
" ], "text/plain": [ "#\n", " a 1\n", " b 2\n", " c 3\n", " d 4" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv = Daru::Vector.new 1..4, index: idx" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Retrive values by index or positions.\n", "\n", "**Note:** When the value is both a valid index and a valid position, it will be treated as index value." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(2)
a1
b2
" ], "text/plain": [ "#\n", " a 1\n", " b 2" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[:a, :b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Retive values by positions." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(2)
a1
b2
" ], "text/plain": [ "#\n", " a 1\n", " b 2" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv.at 0, 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set values by index or positions.\n", "\n", "**Note:** When value is both a valid index and a valid position, it will be treated as index value." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(4)
ax
bx
c3
d4
" ], "text/plain": [ "#\n", " a x\n", " b x\n", " c 3\n", " d 4" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[:a, :b] = 'x'\n", "dv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set values in vector by positions." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(4)
ay
bx
cy
d4
" ], "text/plain": [ "#\n", " a y\n", " b x\n", " c y\n", " d 4" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv.at_set [0, 2], 'y'\n", "dv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# MultiIndex" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Daru::MultiIndex(5x3)
aonebar
baz
twobat
bonebar
twobaz
" ], "text/plain": [ "#\n", " a one bar\n", " baz\n", " two bat\n", " b one bar\n", " two baz" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx = Daru::MultiIndex.from_tuples [\n", " [:a,:one,:bar],\n", " [:a,:one,:baz],\n", " [:a,:two,:bat],\n", " [:b,:one,:bar],\n", " [:b,:two,:baz],\n", " ]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(5)
aonebar1
baz2
twobat3
bonebar4
twobaz5
" ], "text/plain": [ "#\n", " a one bar 1\n", " baz 2\n", " two bat 3\n", " b one bar 4\n", " two baz 5" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv = Daru::Vector.new 1..5, index: idx" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Retrive values by index or positions.\n", "\n", "**Note:** When value is both a valid index or position then value will be treated as index rather position." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Partial Index" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(3)
onebar1
baz2
twobat3
" ], "text/plain": [ "#\n", " one bar 1\n", " baz 2\n", " two bat 3" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[:a]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On mentioning complete index, one will get the value at that index" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[:a, :one, :bar]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Retrive values by positions" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(3)
aonebar1
baz2
twobat3
" ], "text/plain": [ "#\n", " a one bar 1\n", " baz 2\n", " two bat 3" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv.at 0, 1, 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assign values by index or positions" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(5)
aonebarx
bazx
twobat3
bonebar4
twobaz5
" ], "text/plain": [ "#\n", " a one bar x\n", " baz x\n", " two bat 3\n", " b one bar 4\n", " two baz 5" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv[:a, :one] = 'x'\n", "dv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assign values by positions" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "
Daru::Vector(5)
aonebarz
bazz
twobatz
bonebar4
twobaz5
" ], "text/plain": [ "#\n", " a one bar z\n", " baz z\n", " two bat z\n", " b one bar 4\n", " two baz 5" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dv.at_set [0, 1, 2], 'z'\n", "dv" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 0 }