Skip to content

Instantly share code, notes, and snippets.

Created January 8, 2018 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e7566b6d23b98c1144c221c5c69a6761 to your computer and use it in GitHub Desktop.
Save anonymous/e7566b6d23b98c1144c221c5c69a6761 to your computer and use it in GitHub Desktop.
Experimenting with index
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\nimport sys",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "\ndef original_version(cs, idx):\n c1_dat = [idx[i] for i in range(0, len(idx)-1-cs, cs)]\n c2_dat = [idx[i+1] for i in range(0, len(idx)-1-cs, cs)]\n c3_dat = [idx[i+2] for i in range(0, len(idx)-1-cs, cs)]\n c4_dat = [idx[i+3] for i in range(0, len(idx)-1-cs, cs)]\n\n x1 = np.stack(c1_dat[:-2])\n x2 = np.stack(c2_dat[:-2])\n x3 = np.stack(c3_dat[:-2])\n xs = np.stack([x1, x2, x3], axis=1)\n y = np.stack(c4_dat[:-2])\n return xs, y\n",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def original_shorter_version(cs, idx):\n c_in_dat = [[idx[i+j] for i in range(cs)] for j in range(len(idx)-cs-1)]\n c_out_dat = [idx[j+cs] for j in range(len(idx)-cs-1)]\n xs = np.stack(c_in_dat, axis=0)\n y = np.stack(c_out_dat)\n return xs, y",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def candidate_version(cs, idx):\n c_in_dat = [[idx[i+j] for i in range(cs)] for j in range(len(idx)-cs)]\n c_out_dat = [idx[j+cs] for j in range(len(idx)-cs)]\n xs = np.stack(c_in_dat, axis=0)\n y = np.stack(c_out_dat)\n return xs, y",
"execution_count": 4,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Experiment"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "cs = 3\nfor i in range(20):\n idx = [a for a in range(i)]\n try:\n xs, ys = original_version(cs, idx)\n print(f'>>>>>>>>>>>>>>>>>>>>>>>>>>> i: {i}')\n print(f'idx: {idx}')\n print(f'xs: {xs}')\n print(f'ys: {ys}')\n except:\n print(f'error for i = {i}: {sys.exc_info()[0]}')",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "error for i = 0: <class 'ValueError'>\nerror for i = 1: <class 'ValueError'>\nerror for i = 2: <class 'ValueError'>\nerror for i = 3: <class 'ValueError'>\nerror for i = 4: <class 'ValueError'>\nerror for i = 5: <class 'ValueError'>\nerror for i = 6: <class 'ValueError'>\nerror for i = 7: <class 'ValueError'>\nerror for i = 8: <class 'ValueError'>\nerror for i = 9: <class 'ValueError'>\nerror for i = 10: <class 'ValueError'>\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 11\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nxs: [[0 1 2]]\nys: [3]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 12\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\nxs: [[0 1 2]]\nys: [3]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 13\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\nxs: [[0 1 2]]\nys: [3]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 14\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\nxs: [[0 1 2]\n [3 4 5]]\nys: [3 6]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 15\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\nxs: [[0 1 2]\n [3 4 5]]\nys: [3 6]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 16\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\nxs: [[0 1 2]\n [3 4 5]]\nys: [3 6]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 17\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]\nxs: [[0 1 2]\n [3 4 5]\n [6 7 8]]\nys: [3 6 9]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 18\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]\nxs: [[0 1 2]\n [3 4 5]\n [6 7 8]]\nys: [3 6 9]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 19\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]\nxs: [[0 1 2]\n [3 4 5]\n [6 7 8]]\nys: [3 6 9]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "cs = 3\nfor i in range(20):\n idx = [a for a in range(i)]\n try:\n xs, ys = original_shorter_version(cs, idx)\n print(f'>>>>>>>>>>>>>>>>>>>>>>>>>>> i: {i}')\n print(f'idx: {idx}')\n print(f'xs: {xs}')\n print(f'ys: {ys}')\n except:\n print(f'error for i = {i}: {sys.exc_info()[0]}')",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "error for i = 0: <class 'ValueError'>\nerror for i = 1: <class 'ValueError'>\nerror for i = 2: <class 'ValueError'>\nerror for i = 3: <class 'ValueError'>\nerror for i = 4: <class 'ValueError'>\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 5\nidx: [0, 1, 2, 3, 4]\nxs: [[0 1 2]]\nys: [3]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 6\nidx: [0, 1, 2, 3, 4, 5]\nxs: [[0 1 2]\n [1 2 3]]\nys: [3 4]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 7\nidx: [0, 1, 2,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 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\nxs: [[ 0 1 2]\n [ 1 2 3]\n [ 2 3 4]\n [ 3 4 5]\n [ 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]]\nys: [ 3 4 5 6 7 8 9 10 11 12]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 15\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\nxs: [[ 0 1 2]\n [ 1 2 3]\n [ 2 3 4]\n [ 3 4 5]\n [ 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]\n [10 11 12]]\nys: [ 3 4 5 6 7 8 9 10 11 12 13]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 16\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]\nxs: [[ 0 1 2]\n [ 1 2 3]\n [ 2 3 4]\n [ 3 4 5]\n [ 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]\n [10 11 12]\n [11 12 13]]\nys: [ 3 4 5 6 7 8 9 10 11 12 13 14]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 17\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]\nxs: [[ 0 1 2]\n [ 1 2 3]\n [ 2 3 4]\n [ 3 4 5]\n [ 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]\n [10 11 12]\n [11 12 13]\n [12 13 14]]\nys: [ 3 4 5 6 7 8 9 10 11 12 13 14 15]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 18\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]\nxs: [[ 0 1 2]\n [ 1 2 3]\n [ 2 3 4]\n [ 3 4 5]\n [ 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]\n [10 11 12]\n [11 12 13]\n [12 13 14]\n [13 14 15]]\nys: [ 3 4 5 6 7 8 9 10 11 12 13 14 15 16]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 19\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]\nxs: [[ 0 1 2]\n [ 1 2 3]\n [ 2 3 4]\n [ 3 4 5]\n [ 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]\n [10 11 12]\n [11 12 13]\n [12 13 14]\n [13 14 15]\n [14 15 16]]\nys: [ 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "cs = 3\nfor i in range(20):\n idx = [a for a in range(i)]\n try:\n xs, ys = candidate_version(cs, idx)\n print(f'>>>>>>>>>>>>>>>>>>>>>>>>>>> i: {i}')\n print(f'idx: {idx}')\n print(f'xs: {xs}')\n print(f'ys: {ys}')\n except:\n print(f'error for i = {i}: {sys.exc_info()[0]}')",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": "error for i = 0: <class 'ValueError'>\nerror for i = 1: <class 'ValueError'>\nerror for i = 2: <class 'ValueError'>\nerror for i = 3: <class 'ValueError'>\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 4\nidx: [0, 1, 2, 3]\nxs: [[0 1 2]]\nys: [3]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 5\nidx: [0, 1, 2, 3, 4]\nxs: [[0 1 2]\n [1 2 3]]\nys: [3 4]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 6\nidx: [0, 1, 2, 3, 4, 5]\nxs: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 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]\n [10 11 12]\n [11 12 13]\n [12 13 14]\n [13 14 15]]\nys: [ 3 4 5 6 7 8 9 10 11 12 13 14 15 16]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 18\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]\nxs: [[ 0 1 2]\n [ 1 2 3]\n [ 2 3 4]\n [ 3 4 5]\n [ 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]\n [10 11 12]\n [11 12 13]\n [12 13 14]\n [13 14 15]\n [14 15 16]]\nys: [ 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]\n>>>>>>>>>>>>>>>>>>>>>>>>>>> i: 19\nidx: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]\nxs: [[ 0 1 2]\n [ 1 2 3]\n [ 2 3 4]\n [ 3 4 5]\n [ 4 5 6]\n [ 5 6 7]\n [ 6 7 8]\n [ 7 8 9]\n [ 8 9 10]\n [ 9 10 11]\n [10 11 12]\n [11 12 13]\n [12 13 14]\n [13 14 15]\n [14 15 16]\n [15 16 17]]\nys: [ 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "conda-env-fastai-py",
"display_name": "Python [conda env:fastai]",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.4",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Experimenting with index",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment