Refined Code for Bryan's blog post
Last active
March 11, 2018 09:02
-
-
Save dboyliao/3ff465b2c840136340dd2bfc794d7354 to your computer and use it in GitHub Desktop.
This file contains hidden or 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": 1, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:08.183114Z", | |
| "start_time": "2018-03-11T08:58:08.091062Z" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:08.755589Z", | |
| "start_time": "2018-03-11T08:58:08.751011Z" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "y = np.random.randint(0, 100, 10000)[None,:]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:09.404976Z", | |
| "start_time": "2018-03-11T08:58:09.394362Z" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(1, 10000)" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "y.shape" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:10.481452Z", | |
| "start_time": "2018-03-11T08:58:10.477342Z" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "10000" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "y.size" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:11.110602Z", | |
| "start_time": "2018-03-11T08:58:11.105840Z" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def one_hot(y):\n", | |
| " y_one_hot = np.zeros([y.size, np.amax(y)+1])\n", | |
| " y_one_hot[np.arange(y.size), y.ravel()] = 1\n", | |
| " return y_one_hot" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:12.151425Z", | |
| "start_time": "2018-03-11T08:58:12.141216Z" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[0., 0., 0., ..., 0., 0., 0.],\n", | |
| " [0., 0., 0., ..., 0., 0., 0.],\n", | |
| " [0., 0., 0., ..., 0., 0., 0.],\n", | |
| " ...,\n", | |
| " [0., 0., 0., ..., 0., 0., 0.],\n", | |
| " [0., 0., 0., ..., 0., 0., 0.],\n", | |
| " [0., 0., 0., ..., 0., 0., 0.]])" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "one_hot(y)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:30.348374Z", | |
| "start_time": "2018-03-11T08:58:13.989049Z" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1.64 ms ± 41.1 µs per loop (mean ± std. dev. of 10 runs, 1000 loops each)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit -n 1000 -r 10 one_hot(y)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:31.599221Z", | |
| "start_time": "2018-03-11T08:58:31.595080Z" | |
| } | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def one_hot2(y):\n", | |
| " max_v = y.max()\n", | |
| " e = np.eye(max_v+1)\n", | |
| " return e[y.ravel(), :]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:32.232390Z", | |
| "start_time": "2018-03-11T08:58:32.224439Z" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[0., 0., 0., ..., 0., 0., 0.],\n", | |
| " [0., 0., 0., ..., 0., 0., 0.],\n", | |
| " [0., 0., 0., ..., 0., 0., 0.],\n", | |
| " ...,\n", | |
| " [0., 0., 0., ..., 0., 0., 0.],\n", | |
| " [0., 0., 0., ..., 0., 0., 0.],\n", | |
| " [0., 0., 0., ..., 0., 0., 0.]])" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "one_hot2(y)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": { | |
| "ExecuteTime": { | |
| "end_time": "2018-03-11T08:58:47.933443Z", | |
| "start_time": "2018-03-11T08:58:34.568948Z" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1.34 ms ± 19.2 µs per loop (mean ± std. dev. of 10 runs, 1000 loops each)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit -n 1000 -r 10 one_hot2(y)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "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.6.4" | |
| }, | |
| "toc": { | |
| "nav_menu": {}, | |
| "number_sections": true, | |
| "sideBar": true, | |
| "skip_h1_title": false, | |
| "toc_cell": false, | |
| "toc_position": {}, | |
| "toc_section_display": "block", | |
| "toc_window_display": false | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment