From 475e930f8ee187d7dd8a8fb3d85efda6c5f39d7b Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Thu, 9 Apr 2015 13:28:09 -0400 Subject: [PATCH] Added numpy snippets for reshaping and in-place editing. --- numpy/numpy.ipynb | 120 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/numpy/numpy.ipynb b/numpy/numpy.ipynb index fa03f72..852a696 100644 --- a/numpy/numpy.ipynb +++ b/numpy/numpy.ipynb @@ -322,6 +322,126 @@ } ], "prompt_number": 14 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reshaping and In-Place Updating" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "e = np.arange(12)\n", + "print(e)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0 1 2 3 4 5 6 7 8 9 10 11]\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# f is a view of contents of e\n", + "f = e.reshape(3, 4)\n", + "print(f)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 0 1 2 3]\n", + " [ 4 5 6 7]\n", + " [ 8 9 10 11]]\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Set last five values of e to zero\n", + "e[5:] = 0\n", + "print(e)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[0 1 2 3 4 0 0 0 0 0 0 0]\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# f is also updated\n", + "f" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 18, + "text": [ + "array([[0, 1, 2, 3],\n", + " [4, 0, 0, 0],\n", + " [0, 0, 0, 0]])" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# OWNDATA shows f does not own its data\n", + "f.flags" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 19, + "text": [ + " C_CONTIGUOUS : True\n", + " F_CONTIGUOUS : False\n", + " OWNDATA : False\n", + " WRITEABLE : True\n", + " ALIGNED : True\n", + " UPDATEIFCOPY : False" + ] + } + ], + "prompt_number": 19 } ], "metadata": {}