| { |
| "cells": [ |
| { |
| "cell_type": "markdown", |
| "id": "74c76ed7", |
| "metadata": {}, |
| "source": [ |
| "<!--- Licensed to the Apache Software Foundation (ASF) under one -->\n", |
| "<!--- or more contributor license agreements. See the NOTICE file -->\n", |
| "<!--- distributed with this work for additional information -->\n", |
| "<!--- regarding copyright ownership. The ASF licenses this file -->\n", |
| "<!--- to you under the Apache License, Version 2.0 (the -->\n", |
| "<!--- \"License\"); you may not use this file except in compliance -->\n", |
| "<!--- with the License. You may obtain a copy of the License at -->\n", |
| "\n", |
| "<!--- http://www.apache.org/licenses/LICENSE-2.0 -->\n", |
| "\n", |
| "<!--- Unless required by applicable law or agreed to in writing, -->\n", |
| "<!--- software distributed under the License is distributed on an -->\n", |
| "<!--- \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->\n", |
| "<!--- KIND, either express or implied. See the License for the -->\n", |
| "<!--- specific language governing permissions and limitations -->\n", |
| "<!--- under the License. -->\n", |
| "\n", |
| "# The NP on MXNet cheat sheet\n", |
| "\n", |
| "To begin, import the `np` and `npx` module and update MXNet to run in\n", |
| "NumPy-like mode." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 1, |
| "id": "4aa73ba5", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "from mxnet import np, npx\n", |
| "npx.set_np() # Change MXNet to the numpy-like mode." |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "b3985790", |
| "metadata": {}, |
| "source": [ |
| "NDArray figure (TODO)\n", |
| "\n", |
| "## Creating arrays" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 2, |
| "id": "e786bb28", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "name": "stderr", |
| "output_type": "stream", |
| "text": [ |
| "[02:49:37] /work/mxnet/src/storage/storage.cc:202: Using Pooled (Naive) StorageManager for CPU\n" |
| ] |
| }, |
| { |
| "data": { |
| "text/plain": [ |
| "array([1., 2., 3.])" |
| ] |
| }, |
| "execution_count": 2, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.array([1, 2, 3]) # default datatype is float32" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 3, |
| "id": "6a8c4d66", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[1.5, 2. , 3. ],\n", |
| " [4. , 5. , 6. ]], dtype=float16)" |
| ] |
| }, |
| "execution_count": 3, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.array([(1.5, 2, 3), (4, 5, 6)], dtype='float16')" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 4, |
| "id": "94bf0ba5", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[[15, 2, 3],\n", |
| " [ 4, 5, 6]],\n", |
| "\n", |
| " [[ 3, 2, 1],\n", |
| " [ 4, 5, 6]]], dtype=int32)" |
| ] |
| }, |
| "execution_count": 4, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.array([[(15,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype='int32')" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "d05db65e", |
| "metadata": {}, |
| "source": [ |
| "### Initial placeholders" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 5, |
| "id": "f3c13929", |
| "metadata": {}, |
| "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)) # Create an array of zeros" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 6, |
| "id": "5933841d", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[[1, 1, 1, 1],\n", |
| " [1, 1, 1, 1],\n", |
| " [1, 1, 1, 1]],\n", |
| "\n", |
| " [[1, 1, 1, 1],\n", |
| " [1, 1, 1, 1],\n", |
| " [1, 1, 1, 1]]], dtype=int8)" |
| ] |
| }, |
| "execution_count": 6, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.ones((2, 3, 4), dtype='int8') # Create an array of ones" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 7, |
| "id": "87b41664", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([10., 15., 20.])" |
| ] |
| }, |
| "execution_count": 7, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.arange(10, 25, 5) # Create an array of evenly spaced values (step value)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 8, |
| "id": "42433ffe", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# Create an array of evenly spaced values (number of samples)\n", |
| "# np.linspace(0, 2, 9)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 9, |
| "id": "b76352ee", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.full((2, 2), 7) # Create a constant array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 10, |
| "id": "31c63cbe", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.eye(2) # Create a 2X2 identity matrix" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 11, |
| "id": "1f0fe2eb", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.random.random((2, 2)) # Create an array with random values" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 12, |
| "id": "9e927588", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[1.148e-41, 0.000e+00],\n", |
| " [6.390e-43, 0.000e+00],\n", |
| " [0.000e+00, 0.000e+00]])" |
| ] |
| }, |
| "execution_count": 12, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.empty((3,2)) # Create an empty array" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "d8f0beb6", |
| "metadata": {}, |
| "source": [ |
| "## I/O\n", |
| "\n", |
| "### Saving and loading on disk" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 13, |
| "id": "fa08d851", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([1., 2., 3.])" |
| ] |
| }, |
| "execution_count": 13, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "# Save one array\n", |
| "a = np.array([1, 2, 3])\n", |
| "npx.save('my_array', a)\n", |
| "npx.load('my_array')" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 14, |
| "id": "38f0277e", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "{'arr_0': array([1., 2., 3.]), 'arr_1': array([4., 6., 8.])}" |
| ] |
| }, |
| "execution_count": 14, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "# Save a list of arrays\n", |
| "b = np.array([4, 6, 8])\n", |
| "npx.savez('my_arrays', *[a, b])\n", |
| "npx.load('my_arrays')" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "13b06676", |
| "metadata": {}, |
| "source": [ |
| "### Saving and loading text files" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 15, |
| "id": "eb97c685", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.loadtxt(\"myfile.txt\")\n", |
| "# np.genfromtxt(\"my_file.csv\", delimiter=',')\n", |
| "# np.savetxt(\"myarray.txt\", a, delimiter=\" \")" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "8ed70d74", |
| "metadata": {}, |
| "source": [ |
| "## Data types" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 16, |
| "id": "2675e925", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.int64 # Signed 64-bit integer types\n", |
| "# np.float32 # Standard double-precision floating point\n", |
| "# np.complex # Complex numbers represented by 128 floats\n", |
| "# np.bool # Boolean type storing TRUE and FALSE values\n", |
| "# np.object # Python object type\n", |
| "# np.string_ # Fixed-length string type\n", |
| "# np.unicode_ # Fixed-length unicode type" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "7322fafd", |
| "metadata": {}, |
| "source": [ |
| "## Inspecting your array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 17, |
| "id": "691ea6b8", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "(3,)" |
| ] |
| }, |
| "execution_count": 17, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a.shape # Array dimensions" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 18, |
| "id": "b8a9364d", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "3" |
| ] |
| }, |
| "execution_count": 18, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "len(a) # Length of array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 19, |
| "id": "f56f1e63", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "1" |
| ] |
| }, |
| "execution_count": 19, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "b.ndim # Number of array dimensions" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 20, |
| "id": "f62c1639", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "3" |
| ] |
| }, |
| "execution_count": 20, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "b.size # Number of array elements" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 21, |
| "id": "457a5af2", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "dtype('float32')" |
| ] |
| }, |
| "execution_count": 21, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "b.dtype # Data type of array elements" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 22, |
| "id": "e305ec7a", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# b.dtype.name # Name of data type" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 23, |
| "id": "3b14e4c3", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([4, 6, 8], dtype=int64)" |
| ] |
| }, |
| "execution_count": 23, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "b.astype('int') # Convert an array to a different type" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "30d7807d", |
| "metadata": {}, |
| "source": [ |
| "## Asking For Help" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 24, |
| "id": "26b13a0c", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.info(np.ndarray.dtype)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "65eaede0", |
| "metadata": {}, |
| "source": [ |
| "## Array mathematics\n", |
| "\n", |
| "### Arithmetic operations" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 25, |
| "id": "0d98f2fc", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([-3., -4., -5.])" |
| ] |
| }, |
| "execution_count": 25, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a - b # Subtraction" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 26, |
| "id": "4d887ea3", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([-3., -4., -5.])" |
| ] |
| }, |
| "execution_count": 26, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.subtract(a, b) # Subtraction" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 27, |
| "id": "67fc2a3d", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([ 5., 8., 11.])" |
| ] |
| }, |
| "execution_count": 27, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "b + a # Addition" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 28, |
| "id": "9423c6bb", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([ 5., 8., 11.])" |
| ] |
| }, |
| "execution_count": 28, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.add(b, a) # Addition" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 29, |
| "id": "5bd0e2cc", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([0.25 , 0.33333334, 0.375 ])" |
| ] |
| }, |
| "execution_count": 29, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a / b # Division" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 30, |
| "id": "ef7881eb", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([0.25 , 0.33333334, 0.375 ])" |
| ] |
| }, |
| "execution_count": 30, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.divide(a,b) # Division" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 31, |
| "id": "82c36965", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([ 4., 12., 24.])" |
| ] |
| }, |
| "execution_count": 31, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a * b # Multiplication" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 32, |
| "id": "2fb7b17e", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([ 4., 12., 24.])" |
| ] |
| }, |
| "execution_count": 32, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.multiply(a, b) # Multiplication" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 33, |
| "id": "c9eab91e", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([ 54.59815, 403.4288 , 2980.958 ])" |
| ] |
| }, |
| "execution_count": 33, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.exp(b) # Exponentiation" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 34, |
| "id": "5486bae4", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([2. , 2.4494898, 2.828427 ])" |
| ] |
| }, |
| "execution_count": 34, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.sqrt(b) # Square root" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 35, |
| "id": "944faeeb", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([0.84147096, 0.9092974 , 0.14112 ])" |
| ] |
| }, |
| "execution_count": 35, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.sin(a) # Sines of an array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 36, |
| "id": "7b12f1a9", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([-0.6536436 , 0.96017027, -0.14550003])" |
| ] |
| }, |
| "execution_count": 36, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.cos(b) # Element-wise cosine" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 37, |
| "id": "ab28a545", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([0. , 0.6931472, 1.0986123])" |
| ] |
| }, |
| "execution_count": 37, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.log(a) # Element-wise natural logarithm" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 38, |
| "id": "4c6da86c", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array(40.)" |
| ] |
| }, |
| "execution_count": 38, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a.dot(b) # Dot product" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "de565fe1", |
| "metadata": {}, |
| "source": [ |
| "### Comparison\n", |
| "\n", |
| "### Aggregate functions" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 39, |
| "id": "2cbea2d7", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array(6.)" |
| ] |
| }, |
| "execution_count": 39, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a.sum() # Array-wise sum" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 40, |
| "id": "972e558b", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# a.min() # Array-wise minimum value" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 41, |
| "id": "b37361fe", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "c = np.array(([[1,2,3], [2,3,4]]))\n", |
| "# c.max(axis=0) # Maximum value of an array row" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 42, |
| "id": "37727eeb", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# c.cumsum(axis=1) # Cumulative sum of the elements" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 43, |
| "id": "27e1a58c", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array(2.)" |
| ] |
| }, |
| "execution_count": 43, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a.mean() # Mean" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 44, |
| "id": "b706289f", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# b.median() # Median" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 45, |
| "id": "41027670", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# a.corrcoef() # Correlation coefficient" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 46, |
| "id": "1bf04497", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.std(b) # Standard deviation" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "8d827488", |
| "metadata": {}, |
| "source": [ |
| "## Copying arrays" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 47, |
| "id": "00ed02ba", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# a.view() # Create a view of the array with the same data" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 48, |
| "id": "0de4f605", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([1., 2., 3.])" |
| ] |
| }, |
| "execution_count": 48, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.copy(a) # Create a copy of the array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 49, |
| "id": "a2ff6b31", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([1., 2., 3.])" |
| ] |
| }, |
| "execution_count": 49, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a.copy() # Create a deep copy of the array" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "a5a135e8", |
| "metadata": {}, |
| "source": [ |
| "## Sorting Arrays" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 50, |
| "id": "acca83c3", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# a.sort() # Sort an array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 51, |
| "id": "53203ccd", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# c.sort(axis=0) # Sort the elements of an array's axis" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "cda1253e", |
| "metadata": {}, |
| "source": [ |
| "## Subsetting, slicing, indexing\n", |
| "\n", |
| "### Subsetting" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 52, |
| "id": "ab70d0ab", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array(3.)" |
| ] |
| }, |
| "execution_count": 52, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a[2] # Select the element at the 2nd index 3" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 53, |
| "id": "9aa00bf4", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array(2.)" |
| ] |
| }, |
| "execution_count": 53, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "c[0,1] # Select the element at row 1 column 2" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "1a3478de", |
| "metadata": {}, |
| "source": [ |
| "### Slicing" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 54, |
| "id": "c178e98e", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([1., 2.])" |
| ] |
| }, |
| "execution_count": 54, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a[0:2] # Select items at index 0 and 1" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 55, |
| "id": "3d49c3dc", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([2., 3.])" |
| ] |
| }, |
| "execution_count": 55, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "c[0:2,1] # Select items at rows 0 and 1 in column 1" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 56, |
| "id": "96551c20", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[1., 2., 3.]])" |
| ] |
| }, |
| "execution_count": 56, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "c[:1] # Select all items at row 0" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 57, |
| "id": "f7da1482", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# c[1,...] # Same as [1,:,:]" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 58, |
| "id": "3c832778", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([3., 2., 1.])" |
| ] |
| }, |
| "execution_count": 58, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a[ : :-1] #Reversed array a array([3, 2, 1])" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "33736d07", |
| "metadata": {}, |
| "source": [ |
| "### Boolean Indexing" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 59, |
| "id": "6da75ec1", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# a[a<2] # Select elements from a less than 2" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "81177b7e", |
| "metadata": {}, |
| "source": [ |
| "### Fancy indexing" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 60, |
| "id": "b7b3ea0f", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([2., 2., 4., 1.])" |
| ] |
| }, |
| "execution_count": 60, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "c[[1,0,1,0], [0,1,2,0]] # Select elements (1,0),(0,1),(1,2) and (0,0)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 61, |
| "id": "5e07feee", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[2., 3., 4., 2.],\n", |
| " [1., 2., 3., 1.],\n", |
| " [2., 3., 4., 2.],\n", |
| " [1., 2., 3., 1.]])" |
| ] |
| }, |
| "execution_count": 61, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "c[[1,0,1,0]][:,[0,1,2,0]] # Select a subset of the matrix’s rows" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "b2da114e", |
| "metadata": {}, |
| "source": [ |
| "## Array manipulation\n", |
| "\n", |
| "### Transposing array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 62, |
| "id": "61c8dc43", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[1., 2.],\n", |
| " [2., 3.],\n", |
| " [3., 4.]])" |
| ] |
| }, |
| "execution_count": 62, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.transpose(c) # Permute array dimensions" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 63, |
| "id": "d2e9b362", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[1., 2.],\n", |
| " [2., 3.],\n", |
| " [3., 4.]])" |
| ] |
| }, |
| "execution_count": 63, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "c.T # Permute array dimensions" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "db751c75", |
| "metadata": {}, |
| "source": [ |
| "### Changing array shape" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 64, |
| "id": "f6aeabfc", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# b.ravel() # Flatten the array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 65, |
| "id": "004043a0", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# c.reshape(3,-2) # Reshape, but don’t change data" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "17db08e4", |
| "metadata": {}, |
| "source": [ |
| "### Adding and removing elements" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 66, |
| "id": "7ebcb40a", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# c.resize((6,2)) # Return a new array with shape (6, 2)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 67, |
| "id": "9ace947b", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.append(h,g) # Append items to an array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 68, |
| "id": "a6e0b040", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.insert(a, 1, 5) # Insert items in an array" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 69, |
| "id": "9e1edc9c", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.delete(a, [1]) # Delete items from an array" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "52654ed7", |
| "metadata": {}, |
| "source": [ |
| "### Combining arrays" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 70, |
| "id": "829ae492", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([1., 2., 3., 4., 6., 8.])" |
| ] |
| }, |
| "execution_count": 70, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "np.concatenate((a,b),axis=0) # Concatenate arrays" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 71, |
| "id": "a060165b", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.vstack((a,b)) # Stack arrays vertically (row-wise)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 72, |
| "id": "e10e36be", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.r_[e,f] # Stack arrays vertically (row-wise)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 73, |
| "id": "874c2fa4", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.hstack((e,f)) # Stack arrays horizontally (column-wise)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 74, |
| "id": "1b6890c1", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.column_stack((a,d)) # Create stacked column-wise arrays" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 75, |
| "id": "a2771006", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.c_[a,d] # Create stacked column-wise arrays" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "339540b4", |
| "metadata": {}, |
| "source": [ |
| "### Splitting arrays" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 76, |
| "id": "de7b0f55", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.hsplit(a,3) # Split the array horizontally at the 3rd index" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 77, |
| "id": "b6972c4d", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "# np.vsplit(c,2) # Split the array vertically at the 2nd index" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "8d55f602", |
| "metadata": {}, |
| "source": [ |
| "## Use GPUs\n", |
| "\n", |
| "Prerequisites: A GPU exists and GPU-enabled MXNet is installed." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 78, |
| "id": "d6509a49", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "1" |
| ] |
| }, |
| "execution_count": 78, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "npx.num_gpus() # Query number of GPUs" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 79, |
| "id": "d656141e", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "(gpu(0), gpu(1))" |
| ] |
| }, |
| "execution_count": 79, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "npx.gpu(0), npx.gpu(1) # Context for the first and second GPUs" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 80, |
| "id": "9a47de13", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[0., 0., 0.],\n", |
| " [0., 0., 0.]])" |
| ] |
| }, |
| "execution_count": 80, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "gpu_0 = npx.gpu(0) if npx.num_gpus() > 1 else npx.cpu()\n", |
| "g0 = np.zeros((2,3), device=gpu_0) # Create array on GPU 0\n", |
| "g0" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 81, |
| "id": "da6de091", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[0.05798891, 0.89138544, 0.37245068],\n", |
| " [0.9712065 , 0.6616475 , 0.11258256]])" |
| ] |
| }, |
| "execution_count": 81, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "gpu_1 = npx.gpu(1) if npx.num_gpus() > 2 else npx.cpu()\n", |
| "g1 = np.random.uniform(size=(2,3), device=gpu_1) # Create array on GPU 1\n", |
| "g1" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 82, |
| "id": "192cc5e5", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[0.05798891, 0.89138544, 0.37245068],\n", |
| " [0.9712065 , 0.6616475 , 0.11258256]])" |
| ] |
| }, |
| "execution_count": 82, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "# Copy to another GPU\n", |
| "g1.copyto(gpu_0)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 83, |
| "id": "66b1d9f2", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "(array([[0.05798891, 0.89138544, 0.37245068],\n", |
| " [0.9712065 , 0.6616475 , 0.11258256]]),\n", |
| " array([[0.05798891, 0.89138544, 0.37245068],\n", |
| " [0.9712065 , 0.6616475 , 0.11258256]]))" |
| ] |
| }, |
| "execution_count": 83, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "# Return itself if matching the device, otherwise copy\n", |
| "g1.copyto(gpu_0), g1.copyto(gpu_0)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 84, |
| "id": "5485fa3a", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "cpu(0)" |
| ] |
| }, |
| "execution_count": 84, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "g1.device # Query the device an array is on" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 85, |
| "id": "05064f6b", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([[0.05798891, 0.89138544, 0.37245068],\n", |
| " [0.9712065 , 0.6616475 , 0.11258256]])" |
| ] |
| }, |
| "execution_count": 85, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "## The computation is performed by the devices on which the input arrays are\n", |
| "g0 + g1.copyto(gpu_0)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "5a1c0191", |
| "metadata": {}, |
| "source": [ |
| "## Auto differentiation" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 86, |
| "id": "29bf8cbb", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([0., 0., 0.])" |
| ] |
| }, |
| "execution_count": 86, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "a.attach_grad() # Allocate gradient for a variable\n", |
| "a.grad # access the gradient" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "a78b9b3b", |
| "metadata": {}, |
| "source": [ |
| "Compute the $\\nabla_a b=\\exp(2a)^T a$" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 87, |
| "id": "5ab28f1e", |
| "metadata": {}, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "array([ 22.167168, 272.99075 , 2824.0015 ])" |
| ] |
| }, |
| "execution_count": 87, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "from mxnet import autograd\n", |
| "\n", |
| "with autograd.record():\n", |
| " b = np.exp(2*a).dot(a)\n", |
| "b.backward()\n", |
| "a.grad" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "2a929a92", |
| "metadata": {}, |
| "source": [ |
| "**Acknowledgement**\n", |
| "\n", |
| "Adapted from www.datacamp.com." |
| ] |
| } |
| ], |
| "metadata": { |
| "language_info": { |
| "name": "python" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 5 |
| } |