Created
September 10, 2019 19:03
-
-
Save alinaselega/3bd44fbba54ca823208e4f026e5ac703 to your computer and use it in GitHub Desktop.
Notebook Gavin and I made for illustrating numpy data structure operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Numpy Rough Notes", | |
"version": "0.3.2", | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TiJ8lQQMbKwT", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def f(x,y):\n", | |
" x = x+y\n", | |
" y = x-y\n", | |
" return (x, y)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ZRS3xLgibTnR", | |
"colab_type": "code", | |
"outputId": "53984608-2d83-4692-a154-eab8f4e58da0", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 221 | |
} | |
}, | |
"source": [ | |
"import numpy as np\n", | |
"results = np.zeros((5,2))\n", | |
"print(results)\n", | |
"for i in range(5):\n", | |
" results[i,:] = f(i,i+1)\n", | |
"print(results)\n", | |
"np.random.rand(2,2)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[[0. 0.]\n", | |
" [0. 0.]\n", | |
" [0. 0.]\n", | |
" [0. 0.]\n", | |
" [0. 0.]]\n", | |
"[[1. 0.]\n", | |
" [3. 1.]\n", | |
" [5. 2.]\n", | |
" [7. 3.]\n", | |
" [9. 4.]]\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[0.8143215 , 0.71388998],\n", | |
" [0.19456093, 0.05751311]])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 13 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "c-xK2LbddFWA", | |
"colab_type": "code", | |
"outputId": "9a957f0a-ccea-4743-df39-bb9b291b5e5c", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 204 | |
} | |
}, | |
"source": [ | |
"print(results.shape)\n", | |
"print(np.concatenate([results, results], 1))\n", | |
"print(np.hstack([results, results]))" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"(5, 2)\n", | |
"[[1. 0. 1. 0.]\n", | |
" [3. 1. 3. 1.]\n", | |
" [5. 2. 5. 2.]\n", | |
" [7. 3. 7. 3.]\n", | |
" [9. 4. 9. 4.]]\n", | |
"[[1. 0. 1. 0.]\n", | |
" [3. 1. 3. 1.]\n", | |
" [5. 2. 5. 2.]\n", | |
" [7. 3. 7. 3.]\n", | |
" [9. 4. 9. 4.]]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "hjn7TZIicSg4", | |
"colab_type": "code", | |
"outputId": "fed8e2db-83bd-413f-f14b-72a9477560a9", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"def f(x,y):\n", | |
" x = x+y\n", | |
" y = x-y\n", | |
" return x, (x, y)\n", | |
"c, (a,b) = f(3,4)\n", | |
"print(a,b,c)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"7 3 7\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TozygjKJdjWb", | |
"colab_type": "code", | |
"outputId": "fda18a49-e948-48c6-8838-18106c666ee1", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 68 | |
} | |
}, | |
"source": [ | |
"print([x for x in range(10)])\n", | |
"l = [x for x in range(4)]\n", | |
"z = list(zip(l,l))\n", | |
"print(z)\n", | |
"print([x for y in z for x in y])" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | |
"[(0, 0), (1, 1), (2, 2), (3, 3)]\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[0, 0, 1, 1, 2, 2, 3, 3]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 24 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "yhaxpIhafeDc", | |
"colab_type": "code", | |
"outputId": "b570f8f2-e37e-453d-e456-75fc05574b0c", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 102 | |
} | |
}, | |
"source": [ | |
"a = np.array(z)\n", | |
"print(a.ravel())\n", | |
"print(a.flatten())\n", | |
"print(a.reshape(4*2))\n", | |
"print(a.reshape(-1))\n", | |
"a.reshape(2,2,2,-1).shape" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[0 0 1 1 2 2 3 3]\n", | |
"[0 0 1 1 2 2 3 3]\n", | |
"[0 0 1 1 2 2 3 3]\n", | |
"[0 0 1 1 2 2 3 3]\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(2, 2, 2, 1)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 41 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "EcL6ZEs6gd68", | |
"colab_type": "code", | |
"outputId": "541fe830-341f-4155-fc34-2519077f286a", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 221 | |
} | |
}, | |
"source": [ | |
"a = np.random.rand(4,4)\n", | |
"print(a.dot(a))\n", | |
"print(np.dot(a,a))\n", | |
"print(a@a)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[[0.06407937 0.46642787 0.50150555 0.41982632]\n", | |
" [0.35461308 0.80955616 0.57644224 0.5643094 ]\n", | |
" [0.34637637 1.33118504 1.21895512 1.08989549]\n", | |
" [0.60038044 1.86484301 1.59468673 1.47127433]]\n", | |
"[[0.06407937 0.46642787 0.50150555 0.41982632]\n", | |
" [0.35461308 0.80955616 0.57644224 0.5643094 ]\n", | |
" [0.34637637 1.33118504 1.21895512 1.08989549]\n", | |
" [0.60038044 1.86484301 1.59468673 1.47127433]]\n", | |
"[[0.06407937 0.46642787 0.50150555 0.41982632]\n", | |
" [0.35461308 0.80955616 0.57644224 0.5643094 ]\n", | |
" [0.34637637 1.33118504 1.21895512 1.08989549]\n", | |
" [0.60038044 1.86484301 1.59468673 1.47127433]]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "iug5R3V8g8IV", | |
"colab_type": "code", | |
"outputId": "64ff39e8-85ad-4e82-d0c8-61609577e501", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 85 | |
} | |
}, | |
"source": [ | |
"a[0,:]\n", | |
"a[:,0] = a[0,:]\n", | |
"a" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[0.13979676, 0.26408563, 0.03151214, 0.3591907 ],\n", | |
" [0.26408563, 0.43797932, 0.56103178, 0.141232 ],\n", | |
" [0.03151214, 0.86672734, 0.3486368 , 0.63103214],\n", | |
" [0.3591907 , 0.79771853, 0.94087495, 0.86981681]])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 48 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BxPgmll5hOlg", | |
"colab_type": "code", | |
"outputId": "ce618905-9b9b-4698-80b6-befe6a9d5a35", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 85 | |
} | |
}, | |
"source": [ | |
"w, h = a.shape\n", | |
"a = a.ravel()\n", | |
"for i in np.where(a > 0.1):\n", | |
" a[i] = - a[i]\n", | |
"a.reshape(w,h)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[-0.13979676, -0.26408563, 0.03151214, -0.3591907 ],\n", | |
" [-0.26408563, -0.43797932, -0.56103178, -0.141232 ],\n", | |
" [ 0.03151214, -0.86672734, -0.3486368 , -0.63103214],\n", | |
" [-0.3591907 , -0.79771853, -0.94087495, -0.86981681]])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 52 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "vyyEcIUNh_wp", | |
"colab_type": "code", | |
"outputId": "f222fad3-c7db-4b92-8a16-798744c897b6", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 181 | |
} | |
}, | |
"source": [ | |
"assert (a.ravel() - a.reshape(-1)+1).max() < 1e-3\n", | |
"np.allclose(a,a)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "AssertionError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-58-38e7d12f7eb0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m1e-3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mallclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mAssertionError\u001b[0m: " | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment