{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Data Manipulation with Ndarray\n", "\n", "Importing `np` (numpy-like) module and `npx` (numpy extensions) module from MXNet. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.205412Z", "start_time": "2019-07-03T21:48:36.544703Z" }, "attributes": { "classes": [], "id": "", "n": "1" } }, "outputs": [], "source": [ "from mxnet import np, npx\n", "# Invoke the experimental numpy-compatible feature in MXNet \n", "npx.set_np() " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Create a vector and query its attributes" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.232852Z", "start_time": "2019-07-03T21:48:38.208019Z" }, "attributes": { "classes": [], "id": "", "n": "2" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(12)\n", "x" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.239446Z", "start_time": "2019-07-03T21:48:38.235098Z" }, "attributes": { "classes": [], "id": "", "n": "8" } }, "outputs": [ { "data": { "text/plain": [ "(12,)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.shape" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.246552Z", "start_time": "2019-07-03T21:48:38.242380Z" }, "attributes": { "classes": [], "id": "", "n": "9" } }, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.size" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "More ways to construct arrays" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.254509Z", "start_time": "2019-07-03T21:48:38.248757Z" }, "attributes": { "classes": [], "id": "", "n": "4" } }, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((3, 4))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.263742Z", "start_time": "2019-07-03T21:48:38.256770Z" }, "attributes": { "classes": [], "id": "", "n": "6" } }, "outputs": [ { "data": { "text/plain": [ "array([[2., 1., 4., 3.],\n", " [1., 2., 3., 4.],\n", " [4., 3., 2., 1.]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.283115Z", "start_time": "2019-07-03T21:48:38.266029Z" }, "attributes": { "classes": [], "id": "", "n": "7" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 2.2122064 , 0.7740038 , 1.0434405 , 1.1839255 ],\n", " [ 1.8917114 , -1.2347414 , -1.771029 , -0.45138445],\n", " [ 0.57938355, -1.856082 , -1.9768796 , -0.20801921]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.normal(0, 1, size=(3, 4))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Elemental-wise operators" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.298150Z", "start_time": "2019-07-03T21:48:38.286183Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = [1. 2. 4. 8.]\n", "x + y [ 3. 4. 6. 10.]\n", "x - y [-1. 0. 2. 6.]\n", "x * y [ 2. 4. 8. 16.]\n", "x ** y [ 1. 4. 16. 64.]\n", "x / y [0.5 1. 2. 4. ]\n" ] } ], "source": [ "x = np.array([1, 2, 4, 8])\n", "y = np.ones_like(x) * 2\n", "print('x =', x)\n", "print('x + y', x + y)\n", "print('x - y', x - y)\n", "print('x * y', x * y)\n", "print('x ** y', x ** y)\n", "print('x / y', x / y)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Matrix multiplication." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.320311Z", "start_time": "2019-07-03T21:48:38.300609Z" }, "attributes": { "classes": [], "id": "", "n": "13" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 18., 20., 10.],\n", " [ 58., 60., 50.],\n", " [ 98., 100., 90.]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(12).reshape((3,4))\n", "y = np.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])\n", "np.dot(x, y.T)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Concatenate arrays along a particular axis. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.353948Z", "start_time": "2019-07-03T21:48:38.322469Z" } }, "outputs": [ { "data": { "text/plain": [ "(array([[ 0., 1., 2., 3.],\n", " [ 4., 5., 6., 7.],\n", " [ 8., 9., 10., 11.],\n", " [ 2., 1., 4., 3.],\n", " [ 1., 2., 3., 4.],\n", " [ 4., 3., 2., 1.]]),\n", " array([[ 0., 1., 2., 3., 2., 1., 4., 3.],\n", " [ 4., 5., 6., 7., 1., 2., 3., 4.],\n", " [ 8., 9., 10., 11., 4., 3., 2., 1.]]))" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.concatenate([x, y], axis=0), np.concatenate([x, y], axis=1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Broadcast Mechanism" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.382407Z", "start_time": "2019-07-03T21:48:38.355839Z" }, "attributes": { "classes": [], "id": "", "n": "14" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a:\n", " [[0.]\n", " [1.]\n", " [2.]]\n", "b:\n", " [[0. 1.]]\n" ] }, { "data": { "text/plain": [ "array([[0., 1.],\n", " [1., 2.],\n", " [2., 3.]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(3).reshape((3, 1))\n", "b = np.arange(2).reshape((1, 2))\n", "print('a:\\n', a)\n", "print('b:\\n', b)\n", "a + b" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Indexing and Slicing\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.400208Z", "start_time": "2019-07-03T21:48:38.384624Z" }, "attributes": { "classes": [], "id": "", "n": "19" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x[-1] =\n", " [ 8. 9. 10. 11.]\n", "x[1:3] =\n", " [[ 4. 5. 6. 7.]\n", " [ 8. 9. 10. 11.]]\n", "x[1:3, 2:4] =\n", " [[ 6. 7.]\n", " [10. 11.]]\n", "x[1,2] = 6.0\n" ] } ], "source": [ "print('x[-1] =\\n', x[-1])\n", "print('x[1:3] =\\n', x[1:3])\n", "print('x[1:3, 2:4] =\\n', x[1:3, 2:4])\n", "print('x[1,2] =', x[1,2])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "`mxnet.numpy.ndarray` and `numpy.ndarray`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T21:48:38.406593Z", "start_time": "2019-07-03T21:48:38.402022Z" }, "attributes": { "classes": [], "id": "", "n": "22" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "a = x.asnumpy()\n", "print(type(a))\n", "b = np.array(a)\n", "print(type(b))" ] } ], "metadata": { "celltoolbar": "Slideshow", "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" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }