| { |
| "cells": [ |
| { |
| "cell_type": "markdown", |
| "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", |
| "# Step 1: Manipulate data with NP on MXNet\n", |
| "\n", |
| "This getting started exercise introduces the `np` package, which is similar to Numpy. For more information, please see [Differences between NP on MXNet and NumPy](/api/python/docs/tutorials/getting-started/np/np-vs-numpy.html).\n", |
| "\n", |
| "## Import packages and create an array\n", |
| "\n", |
| "\n", |
| "To get started, run the following commands to import the `np` package together with the NumPy extensions package `npx`. Together, `np` with `npx` make up the NP on MXNet front end." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 1, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "1" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "from mxnet import np, npx\n", |
| "npx.set_np() # Activate NumPy-like mode." |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "In this step, create a 2D array (also called a matrix). The following code example creates 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, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "2" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "np.array(((1,2,3),(5,6,7)))" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "You 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, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "3" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "x = np.ones((2,3))\n", |
| "x" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "You can create arrays whose values are sampled randomly. For example, sampling values uniformly between -1 and 1. The following code example creates the same shape, but with random sampling." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 15, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "15" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "y = np.random.uniform(-1,1, (2,3))\n", |
| "y" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "As with NumPy, the dimensions of each ndarray are shown by accessing the `.shape` attribute. As the following code example shows, you can also query for `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, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "17" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "(x.shape, x.size, x.dtype)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "## Performing operations on an array\n", |
| "\n", |
| "An ndarray supports a large number of standard mathematical operations. Here are three examples. You can perform element-wise multiplication by using the following code example." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 18, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "18" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "x * y" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "You can perform exponentiation by using the following code example." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 23, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "23" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "np.exp(y)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "You can also find a matrix’s transpose to compute a proper matrix-matrix product by using the following code example." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 24, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "24" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "np.dot(x, y.T)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "## Indexing an array\n", |
| "\n", |
| "The ndarrays support slicing in many ways you might want to access your data. The following code example shows how to read a particular element, which returns a 1D array with shape `(1,)`." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 25, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "25" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "y[1,2]" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "This example shows how to read the second and third columns from `y`." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 26, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "26" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "y[:,1:3]" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "This example shows how to write to a specific element." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 27, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "27" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "y[:,1:3] = 2\n", |
| "y" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "You can perform multi-dimensional slicing, which is shown in the following code example." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 28, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "28" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "y[1:2,0:2] = 4\n", |
| "y" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "## Converting between MXNet ndarrays and NumPy ndarrays\n", |
| "\n", |
| "You can convert MXNet ndarrays to and from NumPy ndarrays, as shown in the following example. The converted arrays do not share memory." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 29, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "29" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "a = x.asnumpy()\n", |
| "(type(a), a)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 30, |
| "metadata": { |
| "attributes": { |
| "classes": [], |
| "id": "", |
| "n": "30" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "np.array(a)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "metadata": {}, |
| "source": [ |
| "## Next steps\n", |
| "\n", |
| "Learn how to construct a neural network with the Gluon module: [Step 2: Create a neural network](2-nn.md)." |
| ] |
| } |
| ], |
| "metadata": { |
| "language_info": { |
| "name": "python" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 4 |
| } |