Created
April 24, 2022 09:43
-
-
Save ehzawad/31cd1096b9d182d6a5f436e2452c6cf0 to your computer and use it in GitHub Desktop.
Vectorization is simpler than you think!
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "817d8805-5e05-40af-8280-0ba5b8c0fce2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"sequences = [\n", | |
" [3, 5, 7], [2, 5], [4, 8], [6, 4, 2, 3] ]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "66a36a63-4b61-4b8c-8c93-268e6a248fe1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[3, 5, 7], [2, 5], [4, 8], [6, 4, 2, 3]]" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sequences" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "f4f0af99-4a1a-4c52-9357-8d7035105bd5", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "ee1a51fd-e777-4ffc-b9bf-4ed0120eb4f3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<module 'numpy' from '/home/ehz/ehz_codespace/lib/python3.10/site-packages/numpy/__init__.py'>" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "4088e0ee-d25c-4a26-b005-125cf429f3aa", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def vectorize_sequences(sequences, dimension=10):\n", | |
" results = np.zeros((len(sequences), dimension))\n", | |
" print(results)\n", | |
" for i, sequence in enumerate(sequences):\n", | |
" for j in sequence:\n", | |
" results[i, j] = 1\n", | |
" return results" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "146fdc09-33aa-4ef9-b2ce-c90907011727", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<function __main__.vectorize_sequences(sequences, dimension=10)>" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"vectorize_sequences" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "13fb1162-33f4-451e-a226-0beaae26c07a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", | |
" [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", | |
" [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", | |
" [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0., 0., 0., 1., 0., 1., 0., 1., 0., 0.],\n", | |
" [0., 0., 1., 0., 0., 1., 0., 0., 0., 0.],\n", | |
" [0., 0., 0., 0., 1., 0., 0., 0., 1., 0.],\n", | |
" [0., 0., 1., 1., 1., 0., 1., 0., 0., 0.]])" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"vectorize_sequences(sequences)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "c884c55c-455c-451d-b8f6-e822f2b60989", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment