{ "metadata": { "name": "", "signature": "sha256:75d807f509bd9f76b2e14a5a048cb44852a3318bcd0d95afc95d1c9b2904c078" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Sebastian Raschka \n", "last updated: 04/14/2014 \n", "\n", "[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/timeit_tests.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "A = np.array([ [1,2,3], [4,5,6], [7,8,9] ])\n", ">>> A\n", "array([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])\n", "\n", "I want my result to be:\n", "
\n", "array([[1],\n", " [4],\n", " [7]])\n", "\n", "with `.shape` = `(3,1)`\n", "\n", "\n", "However, the default behavior of numpy is to return the column as a row vector:\n", "\n", "
\n", ">>> A[:,0]\n", "array([1, 4, 7])\n", ">>> A[:,0].shape\n", "(3,)\n", "" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "\n", "# 1st column, e.g., A[:,0,np.newaxis]\n", "\n", "def colvec_method1(A):\n", " for col in A.T:\n", " colvec = row[:,np.newaxis]\n", " yield colvec" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "# 1st column, e.g., A[:,0:1]\n", "\n", "def colvec_method2(A):\n", " for idx in range(A.shape[1]):\n", " colvec = A[:,idx:idx+1]\n", " yield colvec" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "# 1st column, e.g., A[:,0].reshape(-1,1)\n", "\n", "def colvec_method3(A):\n", " for idx in range(A.shape[1]):\n", " colvec = A[:,idx].reshape(-1,1)\n", " yield colvec" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 81 }, { "cell_type": "code", "collapsed": false, "input": [ "# 1st column, e.g., np.vstack(A[:,0]\n", "\n", "def colvec_method4(A):\n", " for idx in range(A.shape[1]):\n", " colvec = np.vstack(A[:,idx])\n", " yield colvec" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "# 1st column, e.g., np.row_stack(A[:,0])\n", "\n", "def colvec_method5(A):\n", " for idx in range(A.shape[1]):\n", " colvec = np.row_stack(A[:,idx])\n", " yield colvec" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "# 1st column, e.g., np.column_stack((A[:,0],))\n", "\n", "def colvec_method6(A):\n", " for idx in range(A.shape[1]):\n", " colvec = np.column_stack((A[:,idx],))\n", " yield colvec" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 74 }, { "cell_type": "code", "collapsed": false, "input": [ "# 1st column, e.g., A[:,[0]]\n", "\n", "def colvec_method7(A):\n", " for idx in range(A.shape[1]):\n", " colvec = A[:,[idx]]\n", " yield colvec" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 89 }, { "cell_type": "code", "collapsed": false, "input": [ "def test_method(method, A):\n", " for i in method(A): \n", " assert i.shape == (A.shape[0],1), \"{}, {}\".format(i.shape, A.shape[0],1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "import timeit\n", "\n", "A = np.random.random((300, 3))\n", "\n", "for method in [\n", " colvec_method1, colvec_method2, \n", " colvec_method3, colvec_method4, \n", " colvec_method5, colvec_method6,\n", " colvec_method7]:\n", " print('\\nTest:', method.__name__)\n", " %timeit test_method(colvec_method2, A)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Test: colvec_method1\n", "100000 loops, best of 3: 16.6 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Test: colvec_method2\n", "10000 loops, best of 3: 16.1 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Test: colvec_method3\n", "100000 loops, best of 3: 16.2 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Test: colvec_method4\n", "100000 loops, best of 3: 16.4 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Test: colvec_method5\n", "100000 loops, best of 3: 16.2 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Test: colvec_method6\n", "100000 loops, best of 3: 16.8 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Test: colvec_method7\n", "100000 loops, best of 3: 16.3 \u00b5s per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 91 }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "