blob: c976f3660db0c2b980fceb0956375a560a356035 [file] [log] [blame]
{
"cells": [
{
"cell_type": "markdown",
"id": "c98bfb52",
"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",
"# Manipulate data with `ndarray`\n",
"\n",
"We'll start by introducing the `NDArray`, MXNet’s primary tool for storing and transforming data. If you’ve worked with `NumPy` before, you’ll notice that an NDArray is, by design, similar to NumPy’s multi-dimensional array.\n",
"\n",
"## Get started\n",
"\n",
"To get started, let's import the `ndarray` package (`nd` is a shorter alias) from MXNet."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "741750b0",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "1"
}
},
"outputs": [],
"source": [
"# If you haven't installed MXNet yet, you can uncomment the following line to\n",
"# install the latest stable release\n",
"# !pip install -U mxnet\n",
"\n",
"from mxnet import nd"
]
},
{
"cell_type": "markdown",
"id": "b84abac2",
"metadata": {},
"source": [
"Next, let's see how to create a 2D array (also called a matrix) with values from two sets of numbers: 1, 2, 3 and 4, 5, 6. This might also be referred to as a tuple of a tuple of integers."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "38df8b1c",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "2"
}
},
"outputs": [],
"source": [
"nd.array(((1,2,3),(5,6,7)))"
]
},
{
"cell_type": "markdown",
"id": "5b51f4e5",
"metadata": {},
"source": [
"We can also create a very simple matrix with the same shape (2 rows by 3 columns), but fill it with 1s."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "76bac28e",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "3"
}
},
"outputs": [],
"source": [
"x = nd.ones((2,3))\n",
"x"
]
},
{
"cell_type": "markdown",
"id": "82d4858b",
"metadata": {},
"source": [
"Often we’ll want to create arrays whose values are sampled randomly. For example, sampling values uniformly between -1 and 1. Here we create the same shape, but with random sampling."
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "d051a1e1",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "15"
}
},
"outputs": [],
"source": [
"y = nd.random.uniform(-1,1,(2,3))\n",
"y"
]
},
{
"cell_type": "markdown",
"id": "41e23d01",
"metadata": {},
"source": [
"You can also fill an array of a given shape with a given value, such as `2.0`.\n",
"<!-- added to improve multiplication example -->"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "afdda974",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "16"
}
},
"outputs": [],
"source": [
"x = nd.full((2,3), 2.0)\n",
"x"
]
},
{
"cell_type": "markdown",
"id": "3ef8ff28",
"metadata": {},
"source": [
"As with NumPy, the dimensions of each NDArray are accessible by accessing the `.shape` attribute. We can also query its `size`, which is equal to the product of the components of the shape. In addition, `.dtype` tells the data type of the stored values."
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "44c73162",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "17"
}
},
"outputs": [],
"source": [
"(x.shape, x.size, x.dtype)"
]
},
{
"cell_type": "markdown",
"id": "763b2486",
"metadata": {},
"source": [
"## Operations\n",
"\n",
"NDArray supports a large number of standard mathematical operations, such as element-wise multiplication:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "911185f7",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "18"
}
},
"outputs": [],
"source": [
"x * y"
]
},
{
"cell_type": "markdown",
"id": "d24a6ead",
"metadata": {},
"source": [
"Exponentiation:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "9e77020a",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "23"
}
},
"outputs": [],
"source": [
"y.exp()"
]
},
{
"cell_type": "markdown",
"id": "a2a49a3a",
"metadata": {},
"source": [
"And transposing a matrix to compute a proper matrix-matrix product:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "5a611c7c",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "24"
}
},
"outputs": [],
"source": [
"nd.dot(x, y.T)"
]
},
{
"cell_type": "markdown",
"id": "2623a61f",
"metadata": {},
"source": [
"## Indexing\n",
"\n",
"MXNet NDArrays support slicing in all the ridiculous ways you might imagine accessing your data. Here’s an example of reading a particular element, which returns a 1D array with shape `(1,)`."
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "bbb70a5b",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "25"
}
},
"outputs": [],
"source": [
"y[1,2]"
]
},
{
"cell_type": "markdown",
"id": "8b04a32b",
"metadata": {},
"source": [
"Read the second and third columns from `y`."
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "3d792c5a",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "26"
}
},
"outputs": [],
"source": [
"y[:,1:3]"
]
},
{
"cell_type": "markdown",
"id": "de769b77",
"metadata": {},
"source": [
"and write to a specific element."
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "a14d06c4",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "27"
}
},
"outputs": [],
"source": [
"y[:,1:3] = 2\n",
"y"
]
},
{
"cell_type": "markdown",
"id": "54a2f8c0",
"metadata": {},
"source": [
"Multi-dimensional slicing is also supported."
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "31fde607",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "28"
}
},
"outputs": [],
"source": [
"y[1:2,0:2] = 4\n",
"y"
]
},
{
"cell_type": "markdown",
"id": "d93d60c3",
"metadata": {},
"source": [
"## Converting between MXNet NDArray and NumPy\n",
"\n",
"Converting MXNet NDArrays to and from NumPy is easy. The converted arrays do not share memory."
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "35ebd511",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "29"
}
},
"outputs": [],
"source": [
"a = x.asnumpy()\n",
"(type(a), a)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "bb7cc827",
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "30"
}
},
"outputs": [],
"source": [
"nd.array(a)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}