From 0bec1c864b60f9fc5c836797c889c51ceb1dd225 Mon Sep 17 00:00:00 2001 From: Soonmok Date: Mon, 24 Dec 2018 15:42:44 +0900 Subject: [PATCH] changed step-2 question-8 solution (added np.squeeze) --- 2_Array_manipulation_routines_Solutions.ipynb | 1675 ++++++++--------- 1 file changed, 815 insertions(+), 860 deletions(-) diff --git a/2_Array_manipulation_routines_Solutions.ipynb b/2_Array_manipulation_routines_Solutions.ipynb index 49584ee..aa56247 100644 --- a/2_Array_manipulation_routines_Solutions.ipynb +++ b/2_Array_manipulation_routines_Solutions.ipynb @@ -1,860 +1,815 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Array manipulation routines" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import numpy as np" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'1.11.2'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.__version__" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "Q1. Let x be a ndarray [10, 10, 3] with all elements set to one. Reshape x so that the size of the second dimension equals 150." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1.]\n", - " [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1.]]\n" - ] - } - ], - "source": [ - "x = np.ones([10, 10, 3])\n", - "out = np.reshape(x, [-1, 150])\n", - "print out\n", - "assert np.allclose(out, np.ones([10, 10, 3]).reshape([-1, 150]))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q2. Let x be array [[1, 2, 3], [4, 5, 6]]. Convert it to [1 4 2 5 3 6]." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1 4 2 5 3 6]\n" - ] - } - ], - "source": [ - "x = np.array([[1, 2, 3], [4, 5, 6]])\n", - "out1 = np.ravel(x, order='F')\n", - "out2 = x.flatten(order=\"F\")\n", - "assert np.allclose(out1, out2)\n", - "print out1\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q3. Let x be array [[1, 2, 3], [4, 5, 6]]. Get the 5th element." - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5\n" - ] - } - ], - "source": [ - "x = np.array([[1, 2, 3], [4, 5, 6]])\n", - "out1 = x.flat[4]\n", - "out2 = np.ravel(x)[4]\n", - "assert np.allclose(out1, out2)\n", - "print out1\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q4. Let x be an arbitrary 3-D array of shape (3, 4, 5). Permute the dimensions of x such that the new shape will be (4,3,5).\n" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(4L, 3L, 5L)\n" - ] - } - ], - "source": [ - "x = np.zeros((3, 4, 5))\n", - "out1 = np.swapaxes(x, 1, 0)\n", - "out2 = x.transpose([1, 0, 2])\n", - "assert out1.shape == out2.shape\n", - "print out1.shape" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q5. Let x be an arbitrary 2-D array of shape (3, 4). Permute the dimensions of x such that the new shape will be (4,3)." - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(4L, 3L)\n" - ] - } - ], - "source": [ - "x = np.zeros((3, 4))\n", - "out1 = np.swapaxes(x, 1, 0)\n", - "out2 = x.transpose()\n", - "out3 = x.T\n", - "assert out1.shape == out2.shape == out3.shape\n", - "print out1.shape" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q5. Let x be an arbitrary 2-D array of shape (3, 4). Insert a nex axis such that the new shape will be (3, 1, 4)." - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3L, 1L, 4L)\n" - ] - } - ], - "source": [ - "x = np.zeros((3, 4))\n", - "print np.expand_dims(x, axis=1).shape" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q6. Let x be an arbitrary 3-D array of shape (3, 4, 1). Remove a single-dimensional entries such that the new shape will be (3, 4)." - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3L, 4L)\n" - ] - } - ], - "source": [ - "x = np.zeros((3, 4, 1))\n", - "print np.squeeze(x).shape" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q7. Lex x be an array
\n", - "[[ 1 2 3]
\n", - "[ 4 5 6].

\n", - "and y be an array
\n", - "[[ 7 8 9]
\n", - "[10 11 12]].
\n", - "Concatenate x and y so that a new array looks like
[[1, 2, 3, 7, 8, 9],
[4, 5, 6, 10, 11, 12]].\n" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 1 2 3 7 8 9]\n", - " [ 4 5 6 10 11 12]]\n" - ] - } - ], - "source": [ - "x = np.array([[1, 2, 3], [4, 5, 6]])\n", - "y = np.array([[7, 8, 9], [10, 11, 12]])\n", - "out1 = np.concatenate((x, y), 1)\n", - "out2 = np.hstack((x, y))\n", - "assert np.allclose(out1, out2)\n", - "print out2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q8. Lex x be an array
\n", - "[[ 1 2 3]
\n", - "[ 4 5 6].

\n", - "and y be an array
\n", - "[[ 7 8 9]
\n", - "[10 11 12]].
\n", - "Concatenate x and y so that a new array looks like
[[ 1 2 3]
\n", - " [ 4 5 6]
\n", - " [ 7 8 9]
\n", - " [10 11 12]]\n" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 1 2 3]\n", - " [ 4 5 6]\n", - " [ 7 8 9]\n", - " [10 11 12]]\n" - ] - } - ], - "source": [ - "x = np.array([[1, 2, 3], [4, 5, 6]])\n", - "y = np.array([[7, 8, 9], [10, 11, 12]])\n", - "out1 = np.concatenate((x, y), 0)\n", - "out2 = np.vstack((x, y))\n", - "assert np.allclose(out1, out2)\n", - "print out2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q8. Let x be an array [1 2 3] and y be [4 5 6]. Convert it to [[1, 4], [2, 5], [3, 6]]." - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[1 4]\n", - " [2 5]\n", - " [3 6]]\n" - ] - } - ], - "source": [ - "x = np.array((1,2,3))\n", - "y = np.array((4,5,6))\n", - "out1 = np.column_stack((x, y))\n", - "out2 = np.dstack((x, y))\n", - "out3 = np.vstack((x, y)).T\n", - "assert np.allclose(out1, out2)\n", - "assert np.allclose(out2, out3)\n", - "print out1\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q9. Let x be an array [[1],[2],[3]] and y be [[4], [5], [6]]. Convert x to [[[1, 4]], [[2, 5]], [[3, 6]]]." - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[[1 4]]\n", - "\n", - " [[2 5]]\n", - "\n", - " [[3 6]]]\n" - ] - } - ], - "source": [ - "x = np.array([[1],[2],[3]])\n", - "y = np.array([[4],[5],[6]])\n", - "out = np.dstack((x, y))\n", - "print out\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q10. Let x be an array [1, 2, 3, ..., 9]. Split x into 3 arrays, each of which has 4, 2, and 3 elements in the original order." - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[array([1, 2, 3, 4]), array([5, 6]), array([7, 8, 9])]\n" - ] - } - ], - "source": [ - "x = np.arange(1, 10)\n", - "print np.split(x, [4, 6])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q11. Let x be an array
\n", - "[[[ 0., 1., 2., 3.],
\n", - " [ 4., 5., 6., 7.]],
\n", - " \n", - " [[ 8., 9., 10., 11.],
\n", - " [ 12., 13., 14., 15.]]].
\n", - "Split it into two such that the first array looks like
\n", - "[[[ 0., 1., 2.],
\n", - " [ 4., 5., 6.]],
\n", - " \n", - " [[ 8., 9., 10.],
\n", - " [ 12., 13., 14.]]].
\n", - " \n", - "and the second one look like:
\n", - " \n", - "[[[ 3.],
\n", - " [ 7.]],
\n", - " \n", - " [[ 11.],
\n", - " [ 15.]]].
" - ] - }, - { - "cell_type": "code", - "execution_count": 72, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[array([[[ 0, 1, 2],\n", - " [ 4, 5, 6]],\n", - "\n", - " [[ 8, 9, 10],\n", - " [12, 13, 14]]]), array([[[ 3],\n", - " [ 7]],\n", - "\n", - " [[11],\n", - " [15]]])]\n" - ] - } - ], - "source": [ - "x = np.arange(16).reshape(2, 2, 4)\n", - "out1 = np.split(x, [3],axis=2)\n", - "out2 = np.dsplit(x, [3])\n", - "assert np.allclose(out1[0], out2[0])\n", - "assert np.allclose(out1[1], out2[1])\n", - "print out1\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q12. Let x be an array
\n", - "[[ 0., 1., 2., 3.],
\n", - " [ 4., 5., 6., 7.],
\n", - " [ 8., 9., 10., 11.],
\n", - " [ 12., 13., 14., 15.]].
\n", - "Split it into two arrays along the second axis." - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[array([[ 0, 1],\n", - " [ 4, 5],\n", - " [ 8, 9],\n", - " [12, 13]]), array([[ 2, 3],\n", - " [ 6, 7],\n", - " [10, 11],\n", - " [14, 15]])]\n" - ] - } - ], - "source": [ - "x = np.arange(16).reshape((4, 4))\n", - "out1 = np.hsplit(x, 2)\n", - "out2 = np.split(x, 2, 1)\n", - "assert np.allclose(out1[0], out2[0])\n", - "assert np.allclose(out1[1], out2[1])\n", - "print out1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q13. Let x be an array
\n", - "[[ 0., 1., 2., 3.],
\n", - " [ 4., 5., 6., 7.],
\n", - " [ 8., 9., 10., 11.],
\n", - " [ 12., 13., 14., 15.]].
\n", - "Split it into two arrays along the first axis." - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[array([[0, 1, 2, 3],\n", - " [4, 5, 6, 7]]), array([[ 8, 9, 10, 11],\n", - " [12, 13, 14, 15]])]\n" - ] - } - ], - "source": [ - "x = np.arange(16).reshape((4, 4))\n", - "out1 = np.vsplit(x, 2)\n", - "out2 = np.split(x, 2, 0)\n", - "assert np.allclose(out1[0], out2[0])\n", - "assert np.allclose(out1[1], out2[1])\n", - "print out1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q14. Let x be an array [0, 1, 2]. Convert it to
\n", - "[[0, 1, 2, 0, 1, 2],
\n", - " [0, 1, 2, 0, 1, 2]]." - ] - }, - { - "cell_type": "code", - "execution_count": 93, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[0 1 2 0 1 2]\n", - " [0 1 2 0 1 2]]\n" - ] - } - ], - "source": [ - "x = np.array([0, 1, 2])\n", - "out1 = np.tile(x, [2, 2])\n", - "out2 = np.resize(x, [2, 6])\n", - "assert np.allclose(out1, out2)\n", - "print out1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q15. Let x be an array [0, 1, 2]. Convert it to
\n", - "[0, 0, 1, 1, 2, 2]." - ] - }, - { - "cell_type": "code", - "execution_count": 83, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0 0 1 1 2 2]\n" - ] - } - ], - "source": [ - "x = np.array([0, 1, 2])\n", - "print np.repeat(x, 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q16. Let x be an array [0, 0, 0, 1, 2, 3, 0, 2, 1, 0].
\n", - "remove the leading the trailing zeros." - ] - }, - { - "cell_type": "code", - "execution_count": 105, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1 2 3 0 2 1]\n" - ] - } - ], - "source": [ - "x = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))\n", - "out = np.trim_zeros(x)\n", - "print out" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q17. Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 107, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1 2 3 4 5] [2 3 1 1 2]\n" - ] - } - ], - "source": [ - "x = np.array([2, 2, 1, 5, 4, 5, 1, 2, 3])\n", - "u, indices = np.unique(x, return_counts=True)\n", - "print u, indices" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q18. Lex x be an array
\n", - "[[ 1 2]
\n", - " [ 3 4].
\n", - "Flip x along the second axis." - ] - }, - { - "cell_type": "code", - "execution_count": 120, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[2 1]\n", - " [4 3]]\n" - ] - } - ], - "source": [ - "x = np.array([[1,2], [3,4]])\n", - "out1 = np.fliplr(x)\n", - "out2 = x[:, ::-1]\n", - "assert np.allclose(out1, out2)\n", - "print out1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q19. Lex x be an array
\n", - "[[ 1 2]
\n", - " [ 3 4].
\n", - "Flip x along the first axis." - ] - }, - { - "cell_type": "code", - "execution_count": 121, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[3 4]\n", - " [1 2]]\n" - ] - } - ], - "source": [ - "x = np.array([[1,2], [3,4]])\n", - "out1 = np.flipud(x)\n", - "out2 = x[::-1, :]\n", - "assert np.allclose(out1, out2)\n", - "print out1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q20. Lex x be an array
\n", - "[[ 1 2]
\n", - " [ 3 4].
\n", - "Rotate x 90 degrees counter-clockwise." - ] - }, - { - "cell_type": "code", - "execution_count": 122, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[2 4]\n", - " [1 3]]\n" - ] - } - ], - "source": [ - "x = np.array([[1,2], [3,4]])\n", - "out = np.rot90(x)\n", - "print out" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q21 Lex x be an array
\n", - "[[ 1 2 3 4]
\n", - " [ 5 6 7 8].
\n", - "Shift elements one step to right along the second axis." - ] - }, - { - "cell_type": "code", - "execution_count": 126, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[4 1 2 3]\n", - " [8 5 6 7]]\n" - ] - } - ], - "source": [ - "x = np.arange(1, 9).reshape([2, 4])\n", - "print np.roll(x, 1, axis=1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Array manipulation routines" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1.11.2'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.__version__" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "Q1. Let x be a ndarray [10, 10, 3] with all elements set to one. Reshape x so that the size of the second dimension equals 150." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1.]\n", + " [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1.]]\n" + ] + } + ], + "source": [ + "x = np.ones([10, 10, 3])\n", + "out = np.reshape(x, [-1, 150])\n", + "print out\n", + "assert np.allclose(out, np.ones([10, 10, 3]).reshape([-1, 150]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q2. Let x be array [[1, 2, 3], [4, 5, 6]]. Convert it to [1 4 2 5 3 6]." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 4 2 5 3 6]\n" + ] + } + ], + "source": [ + "x = np.array([[1, 2, 3], [4, 5, 6]])\n", + "out1 = np.ravel(x, order='F')\n", + "out2 = x.flatten(order=\"F\")\n", + "assert np.allclose(out1, out2)\n", + "print out1\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q3. Let x be array [[1, 2, 3], [4, 5, 6]]. Get the 5th element." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "x = np.array([[1, 2, 3], [4, 5, 6]])\n", + "out1 = x.flat[4]\n", + "out2 = np.ravel(x)[4]\n", + "assert np.allclose(out1, out2)\n", + "print out1\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q4. Let x be an arbitrary 3-D array of shape (3, 4, 5). Permute the dimensions of x such that the new shape will be (4,3,5).\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(4L, 3L, 5L)\n" + ] + } + ], + "source": [ + "x = np.zeros((3, 4, 5))\n", + "out1 = np.swapaxes(x, 1, 0)\n", + "out2 = x.transpose([1, 0, 2])\n", + "assert out1.shape == out2.shape\n", + "print out1.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q5. Let x be an arbitrary 2-D array of shape (3, 4). Permute the dimensions of x such that the new shape will be (4,3)." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(4L, 3L)\n" + ] + } + ], + "source": [ + "x = np.zeros((3, 4))\n", + "out1 = np.swapaxes(x, 1, 0)\n", + "out2 = x.transpose()\n", + "out3 = x.T\n", + "assert out1.shape == out2.shape == out3.shape\n", + "print out1.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q5. Let x be an arbitrary 2-D array of shape (3, 4). Insert a nex axis such that the new shape will be (3, 1, 4)." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3L, 1L, 4L)\n" + ] + } + ], + "source": [ + "x = np.zeros((3, 4))\n", + "print np.expand_dims(x, axis=1).shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q6. Let x be an arbitrary 3-D array of shape (3, 4, 1). Remove a single-dimensional entries such that the new shape will be (3, 4)." + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3L, 4L)\n" + ] + } + ], + "source": [ + "x = np.zeros((3, 4, 1))\n", + "print np.squeeze(x).shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q7. Lex x be an array
\n", + "[[ 1 2 3]
\n", + "[ 4 5 6].

\n", + "and y be an array
\n", + "[[ 7 8 9]
\n", + "[10 11 12]].
\n", + "Concatenate x and y so that a new array looks like
[[1, 2, 3, 7, 8, 9],
[4, 5, 6, 10, 11, 12]].\n" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3 7 8 9]\n", + " [ 4 5 6 10 11 12]]\n" + ] + } + ], + "source": [ + "x = np.array([[1, 2, 3], [4, 5, 6]])\n", + "y = np.array([[7, 8, 9], [10, 11, 12]])\n", + "out1 = np.concatenate((x, y), 1)\n", + "out2 = np.hstack((x, y))\n", + "assert np.allclose(out1, out2)\n", + "print out2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q8. Lex x be an array
\n", + "[[ 1 2 3]
\n", + "[ 4 5 6].

\n", + "and y be an array
\n", + "[[ 7 8 9]
\n", + "[10 11 12]].
\n", + "Concatenate x and y so that a new array looks like
[[ 1 2 3]
\n", + " [ 4 5 6]
\n", + " [ 7 8 9]
\n", + " [10 11 12]]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3]\n", + " [ 4 5 6]\n", + " [ 7 8 9]\n", + " [10 11 12]]\n" + ] + } + ], + "source": [ + "x = np.array([[1, 2, 3], [4, 5, 6]])\n", + "y = np.array([[7, 8, 9], [10, 11, 12]])\n", + "out1 = np.concatenate((x, y), 0)\n", + "out2 = np.vstack((x, y))\n", + "assert np.allclose(out1, out2)\n", + "print out2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q8. Let x be an array [1 2 3] and y be [4 5 6]. Convert it to [[1, 4], [2, 5], [3, 6]]." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 4],\n", + " [2, 5],\n", + " [3, 6]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = np.array((1,2,3))\n", + "y = np.array((4,5,6))\n", + "out1 = np.column_stack((x, y))\n", + "out2 = np.squeeze(np.dstack((x, y)))\n", + "out3 = np.vstack((x, y)).T\n", + "assert np.allclose(out1, out2)\n", + "assert np.allclose(out2, out3)\n", + "print out1\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q9. Let x be an array [[1],[2],[3]] and y be [[4], [5], [6]]. Convert x to [[[1, 4]], [[2, 5]], [[3, 6]]]." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1 4]]\n", + "\n", + " [[2 5]]\n", + "\n", + " [[3 6]]]\n" + ] + } + ], + "source": [ + "x = np.array([[1],[2],[3]])\n", + "y = np.array([[4],[5],[6]])\n", + "out = np.dstack((x, y))\n", + "print out\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q10. Let x be an array [1, 2, 3, ..., 9]. Split x into 3 arrays, each of which has 4, 2, and 3 elements in the original order." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[array([1, 2, 3, 4]), array([5, 6]), array([7, 8, 9])]\n" + ] + } + ], + "source": [ + "x = np.arange(1, 10)\n", + "print np.split(x, [4, 6])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q11. Let x be an array
\n", + "[[[ 0., 1., 2., 3.],
\n", + " [ 4., 5., 6., 7.]],
\n", + " \n", + " [[ 8., 9., 10., 11.],
\n", + " [ 12., 13., 14., 15.]]].
\n", + "Split it into two such that the first array looks like
\n", + "[[[ 0., 1., 2.],
\n", + " [ 4., 5., 6.]],
\n", + " \n", + " [[ 8., 9., 10.],
\n", + " [ 12., 13., 14.]]].
\n", + " \n", + "and the second one look like:
\n", + " \n", + "[[[ 3.],
\n", + " [ 7.]],
\n", + " \n", + " [[ 11.],
\n", + " [ 15.]]].
" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[array([[[ 0, 1, 2],\n", + " [ 4, 5, 6]],\n", + "\n", + " [[ 8, 9, 10],\n", + " [12, 13, 14]]]), array([[[ 3],\n", + " [ 7]],\n", + "\n", + " [[11],\n", + " [15]]])]\n" + ] + } + ], + "source": [ + "x = np.arange(16).reshape(2, 2, 4)\n", + "out1 = np.split(x, [3],axis=2)\n", + "out2 = np.dsplit(x, [3])\n", + "assert np.allclose(out1[0], out2[0])\n", + "assert np.allclose(out1[1], out2[1])\n", + "print out1\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q12. Let x be an array
\n", + "[[ 0., 1., 2., 3.],
\n", + " [ 4., 5., 6., 7.],
\n", + " [ 8., 9., 10., 11.],
\n", + " [ 12., 13., 14., 15.]].
\n", + "Split it into two arrays along the second axis." + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[array([[ 0, 1],\n", + " [ 4, 5],\n", + " [ 8, 9],\n", + " [12, 13]]), array([[ 2, 3],\n", + " [ 6, 7],\n", + " [10, 11],\n", + " [14, 15]])]\n" + ] + } + ], + "source": [ + "x = np.arange(16).reshape((4, 4))\n", + "out1 = np.hsplit(x, 2)\n", + "out2 = np.split(x, 2, 1)\n", + "assert np.allclose(out1[0], out2[0])\n", + "assert np.allclose(out1[1], out2[1])\n", + "print out1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q13. Let x be an array
\n", + "[[ 0., 1., 2., 3.],
\n", + " [ 4., 5., 6., 7.],
\n", + " [ 8., 9., 10., 11.],
\n", + " [ 12., 13., 14., 15.]].
\n", + "Split it into two arrays along the first axis." + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[array([[0, 1, 2, 3],\n", + " [4, 5, 6, 7]]), array([[ 8, 9, 10, 11],\n", + " [12, 13, 14, 15]])]\n" + ] + } + ], + "source": [ + "x = np.arange(16).reshape((4, 4))\n", + "out1 = np.vsplit(x, 2)\n", + "out2 = np.split(x, 2, 0)\n", + "assert np.allclose(out1[0], out2[0])\n", + "assert np.allclose(out1[1], out2[1])\n", + "print out1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q14. Let x be an array [0, 1, 2]. Convert it to
\n", + "[[0, 1, 2, 0, 1, 2],
\n", + " [0, 1, 2, 0, 1, 2]]." + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 1 2 0 1 2]\n", + " [0 1 2 0 1 2]]\n" + ] + } + ], + "source": [ + "x = np.array([0, 1, 2])\n", + "out1 = np.tile(x, [2, 2])\n", + "out2 = np.resize(x, [2, 6])\n", + "assert np.allclose(out1, out2)\n", + "print out1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q15. Let x be an array [0, 1, 2]. Convert it to
\n", + "[0, 0, 1, 1, 2, 2]." + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 0 1 1 2 2]\n" + ] + } + ], + "source": [ + "x = np.array([0, 1, 2])\n", + "print np.repeat(x, 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q16. Let x be an array [0, 0, 0, 1, 2, 3, 0, 2, 1, 0].
\n", + "remove the leading the trailing zeros." + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 0 2 1]\n" + ] + } + ], + "source": [ + "x = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))\n", + "out = np.trim_zeros(x)\n", + "print out" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q17. Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4 5] [2 3 1 1 2]\n" + ] + } + ], + "source": [ + "x = np.array([2, 2, 1, 5, 4, 5, 1, 2, 3])\n", + "u, indices = np.unique(x, return_counts=True)\n", + "print u, indices" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q18. Lex x be an array
\n", + "[[ 1 2]
\n", + " [ 3 4].
\n", + "Flip x along the second axis." + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2 1]\n", + " [4 3]]\n" + ] + } + ], + "source": [ + "x = np.array([[1,2], [3,4]])\n", + "out1 = np.fliplr(x)\n", + "out2 = x[:, ::-1]\n", + "assert np.allclose(out1, out2)\n", + "print out1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q19. Lex x be an array
\n", + "[[ 1 2]
\n", + " [ 3 4].
\n", + "Flip x along the first axis." + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[3 4]\n", + " [1 2]]\n" + ] + } + ], + "source": [ + "x = np.array([[1,2], [3,4]])\n", + "out1 = np.flipud(x)\n", + "out2 = x[::-1, :]\n", + "assert np.allclose(out1, out2)\n", + "print out1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q20. Lex x be an array
\n", + "[[ 1 2]
\n", + " [ 3 4].
\n", + "Rotate x 90 degrees counter-clockwise." + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2 4]\n", + " [1 3]]\n" + ] + } + ], + "source": [ + "x = np.array([[1,2], [3,4]])\n", + "out = np.rot90(x)\n", + "print out" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q21 Lex x be an array
\n", + "[[ 1 2 3 4]
\n", + " [ 5 6 7 8].
\n", + "Shift elements one step to right along the second axis." + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[4 1 2 3]\n", + " [8 5 6 7]]\n" + ] + } + ], + "source": [ + "x = np.arange(1, 9).reshape([2, 4])\n", + "print np.roll(x, 1, axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "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.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}