blob: b7a7f3879b8ff6d27d7bf34357e9daf19c32f6d0 [file] [log] [blame]
{
"cells": [
{
"cell_type": "markdown",
"id": "c163999e",
"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": "afe042d2",
"metadata": {},
"outputs": [],
"source": [
"from mxnet import np, npx\n",
"npx.set_np() # Change MXNet to the numpy-like mode."
]
},
{
"cell_type": "markdown",
"id": "db123c3f",
"metadata": {},
"source": [
"NDArray figure (TODO)\n",
"\n",
"## Creating arrays"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9c9bb64b",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[04:51:27] /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": "456d8c4d",
"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": "a71b64ab",
"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": "cb761220",
"metadata": {},
"source": [
"### Initial placeholders"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7577cf88",
"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": "11301bd9",
"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": "7cbb9ddb",
"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": "48e68cfa",
"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": "582c3223",
"metadata": {},
"outputs": [],
"source": [
"# np.full((2, 2), 7) # Create a constant array"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b55aec3f",
"metadata": {},
"outputs": [],
"source": [
"# np.eye(2) # Create a 2X2 identity matrix"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a2974a26",
"metadata": {},
"outputs": [],
"source": [
"# np.random.random((2, 2)) # Create an array with random values"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4bdcffbc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3.3631163e-44, nan],\n",
" [1.3269378e+28, 4.5856091e-41],\n",
" [1.4012985e-45, 0.0000000e+00]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.empty((3,2)) # Create an empty array"
]
},
{
"cell_type": "markdown",
"id": "f9952a9c",
"metadata": {},
"source": [
"## I/O\n",
"\n",
"### Saving and loading on disk"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "af51f376",
"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": "dea80e83",
"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": "b20099c9",
"metadata": {},
"source": [
"### Saving and loading text files"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "66fdd83a",
"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": "838a84ac",
"metadata": {},
"source": [
"## Data types"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "099e72b9",
"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": "dfadd5be",
"metadata": {},
"source": [
"## Inspecting your array"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "1fea1274",
"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": "51ff190d",
"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": "faa1bb08",
"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": "1624b339",
"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": "78e9fcb8",
"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": "3c19db0d",
"metadata": {},
"outputs": [],
"source": [
"# b.dtype.name # Name of data type"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "2f7c7cec",
"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": "e96d6e57",
"metadata": {},
"source": [
"## Asking For Help"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "059d1ee0",
"metadata": {},
"outputs": [],
"source": [
"# np.info(np.ndarray.dtype)"
]
},
{
"cell_type": "markdown",
"id": "f87f33bc",
"metadata": {},
"source": [
"## Array mathematics\n",
"\n",
"### Arithmetic operations"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "1c6bcc46",
"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": "9d20036e",
"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": "4318b75c",
"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": "3374bd17",
"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": "40557ea0",
"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": "6af62297",
"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": "973f1262",
"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": "4e70880f",
"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": "3e476953",
"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": "2f066040",
"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": "c104c447",
"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": "9c09a3cb",
"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": "6de20ea7",
"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": "2fe3df9a",
"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": "86608e9a",
"metadata": {},
"source": [
"### Comparison\n",
"\n",
"### Aggregate functions"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "e326c92a",
"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": "a39dcc94",
"metadata": {},
"outputs": [],
"source": [
"# a.min() # Array-wise minimum value"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "2cf0cc8e",
"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": "5c52b3c2",
"metadata": {},
"outputs": [],
"source": [
"# c.cumsum(axis=1) # Cumulative sum of the elements"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "e11b4039",
"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": "311b57e4",
"metadata": {},
"outputs": [],
"source": [
"# b.median() # Median"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "9a02aefd",
"metadata": {},
"outputs": [],
"source": [
"# a.corrcoef() # Correlation coefficient"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "104a47b4",
"metadata": {},
"outputs": [],
"source": [
"# np.std(b) # Standard deviation"
]
},
{
"cell_type": "markdown",
"id": "e7ead4bf",
"metadata": {},
"source": [
"## Copying arrays"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "4ddc69ce",
"metadata": {},
"outputs": [],
"source": [
"# a.view() # Create a view of the array with the same data"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "65c2765c",
"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": "c3a01108",
"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": "8b3d3a05",
"metadata": {},
"source": [
"## Sorting Arrays"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "5a08beea",
"metadata": {},
"outputs": [],
"source": [
"# a.sort() # Sort an array"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "08a63cee",
"metadata": {},
"outputs": [],
"source": [
"# c.sort(axis=0) # Sort the elements of an array's axis"
]
},
{
"cell_type": "markdown",
"id": "7336ea2b",
"metadata": {},
"source": [
"## Subsetting, slicing, indexing\n",
"\n",
"### Subsetting"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "1771f63d",
"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": "11e503ab",
"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": "9f614e6e",
"metadata": {},
"source": [
"### Slicing"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "fb2ebd1a",
"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": "ea2218b7",
"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": "1f1c3f28",
"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": "0e62f93f",
"metadata": {},
"outputs": [],
"source": [
"# c[1,...] # Same as [1,:,:]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "da8ca73f",
"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": "8c1984ec",
"metadata": {},
"source": [
"### Boolean Indexing"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "8397c93f",
"metadata": {},
"outputs": [],
"source": [
"# a[a<2] # Select elements from a less than 2"
]
},
{
"cell_type": "markdown",
"id": "3d8dc401",
"metadata": {},
"source": [
"### Fancy indexing"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "c584c259",
"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": "31e90fe0",
"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": "d8511240",
"metadata": {},
"source": [
"## Array manipulation\n",
"\n",
"### Transposing array"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "dd868def",
"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": "e3db003d",
"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": "8febe160",
"metadata": {},
"source": [
"### Changing array shape"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "3dfefd8a",
"metadata": {},
"outputs": [],
"source": [
"# b.ravel() # Flatten the array"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "a9218aef",
"metadata": {},
"outputs": [],
"source": [
"# c.reshape(3,-2) # Reshape, but don’t change data"
]
},
{
"cell_type": "markdown",
"id": "0b8d8d31",
"metadata": {},
"source": [
"### Adding and removing elements"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "06fe5608",
"metadata": {},
"outputs": [],
"source": [
"# c.resize((6,2)) # Return a new array with shape (6, 2)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "7b0d1d69",
"metadata": {},
"outputs": [],
"source": [
"# np.append(h,g) # Append items to an array"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "92803c13",
"metadata": {},
"outputs": [],
"source": [
"# np.insert(a, 1, 5) # Insert items in an array"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "f9ed6582",
"metadata": {},
"outputs": [],
"source": [
"# np.delete(a, [1]) # Delete items from an array"
]
},
{
"cell_type": "markdown",
"id": "cf101bad",
"metadata": {},
"source": [
"### Combining arrays"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "71ddba6d",
"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": "6a53d566",
"metadata": {},
"outputs": [],
"source": [
"# np.vstack((a,b)) # Stack arrays vertically (row-wise)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "8b813818",
"metadata": {},
"outputs": [],
"source": [
"# np.r_[e,f] # Stack arrays vertically (row-wise)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "d0af9887",
"metadata": {},
"outputs": [],
"source": [
"# np.hstack((e,f)) # Stack arrays horizontally (column-wise)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "1d9b69b8",
"metadata": {},
"outputs": [],
"source": [
"# np.column_stack((a,d)) # Create stacked column-wise arrays"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "604c188a",
"metadata": {},
"outputs": [],
"source": [
"# np.c_[a,d] # Create stacked column-wise arrays"
]
},
{
"cell_type": "markdown",
"id": "f2b6126f",
"metadata": {},
"source": [
"### Splitting arrays"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "364e77f1",
"metadata": {},
"outputs": [],
"source": [
"# np.hsplit(a,3) # Split the array horizontally at the 3rd index"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "ff785479",
"metadata": {},
"outputs": [],
"source": [
"# np.vsplit(c,2) # Split the array vertically at the 2nd index"
]
},
{
"cell_type": "markdown",
"id": "6ea31dd0",
"metadata": {},
"source": [
"## Use GPUs\n",
"\n",
"Prerequisites: A GPU exists and GPU-enabled MXNet is installed."
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "25ff53c6",
"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": "347b2960",
"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": "d53c15f3",
"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": "1aa8056a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.6382473 , 0.795612 , 0.61245596],\n",
" [0.07955429, 0.20390165, 0.67279226]])"
]
},
"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": "d6bf6f13",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.6382473 , 0.795612 , 0.61245596],\n",
" [0.07955429, 0.20390165, 0.67279226]])"
]
},
"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": "c5d59993",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([[0.6382473 , 0.795612 , 0.61245596],\n",
" [0.07955429, 0.20390165, 0.67279226]]),\n",
" array([[0.6382473 , 0.795612 , 0.61245596],\n",
" [0.07955429, 0.20390165, 0.67279226]]))"
]
},
"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": "27316b98",
"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": "e12f0474",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.6382473 , 0.795612 , 0.61245596],\n",
" [0.07955429, 0.20390165, 0.67279226]])"
]
},
"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": "bff5d74c",
"metadata": {},
"source": [
"## Auto differentiation"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "7c82f4d5",
"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": "7219bd47",
"metadata": {},
"source": [
"Compute the $\\nabla_a b=\\exp(2a)^T a$"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "d42830c5",
"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": "a47eba21",
"metadata": {},
"source": [
"**Acknowledgement**\n",
"\n",
"Adapted from www.datacamp.com."
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}