{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Retrieving top-$k$ indices with argsort and using argpartition" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3.86 s, sys: 72 ms, total: 3.94 s\n", "Wall time: 3.9 s\n" ] } ], "source": [ "%time x = np.random.randn(2 ** 26)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "i = 1000" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 20 s, sys: 72 ms, total: 20.1 s\n", "Wall time: 20.1 s\n" ] } ], "source": [ "%%time\n", "\n", "result = np.argsort(x)[:i]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 916 ms, sys: 60 ms, total: 976 ms\n", "Wall time: 976 ms\n" ] } ], "source": [ "%%time \n", "\n", "result = np.argsort(\n", " np.partition(x, kth=i)[:i])" ] } ], "metadata": { "hide_input": false, "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.5.1" } }, "nbformat": 4, "nbformat_minor": 2 }