{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "s1", "content", "l1" ] }, "source": [ "# N-dimensional array in Python\n", "\n", "An array is a list or collection of homogenous elements, i.e., same type of items. An N-dimensional array is a collection of such arrays, and in simplest terms can be described as an array of arrays.\n", "\n", "A two dimensional array, also called a matrix (plural: matrices), is very common and most of us would be familiar with it. An array of matrices can be visualized as a 3 dimensional array. An array can be defined using the '.array' method of the numpy module. A range of functions such as dtype, shape, size, etc., are available to find out about various attributes of the array.\n", "\n", "For more details, please refer to the documentation https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html of the ndarray method.\n", "\n", "Looking at an example would help us understand in better detail.\n", "\n", "### Exercise 1\n", "\n", "* Create a three dimensional array, named tdarray consisting of 3 matrices = ${[[1,2,3],[a,b,c]],[[4,5,6],[d,e,f]],[[7,8,9],[g,h,i]]}$\n", "* Access the element 'h' using indices and store it into a variable called 'target'. Print target out.\n", "* Print the data type of the array using the '.dtype' method, and the shape of the array using the '.shape' method." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "s1", "ce", "l1" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h \n", "Datatype of the array is: 3). However, it is simple to code them. Let us try.\n", "\n", "### Exercise 2\n", "\n", "* Create a 4-dimensional array, named fdarray, with elements 1,2,3,4...so on and with a shape 4,3,2,2.\n", "* Print out the shape of the array to verify your answer. Also access the element '10' using appropriate indices, assign it to a variable target2 and print out target2." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [ "l2", "ce", "s2" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shape of the array is: (4, 3, 2, 2) \n", " 10\n" ] } ], "source": [ "# Manual creation - Bad example\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "l2", "s2", "hint" ] }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "l2", "s2", "ans" ] }, "outputs": [], "source": [ "fdarray = np.array([[[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]],[[[13,14],[15,16]],[[17,18],[19,20]],[[21,22],[23,24]]],[[[25,26],[27,28]],[[29,30],[31,32]],[[33,34],[35,36]]],[[[37,38],[39,40]],[[41,42],[43,44]],[[45,46],[47,48]]]])\n", "target2 = fdarray[0][2][0][1]\n", "print(\"Shape of the array is: \",fdarray.shape,\"\\n\",target2)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "tags": [ "l2", "hid", "s2" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "ref_tmp_var = False\n", "\n", "try:\n", " fdarray_ = np.array([[[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]],[[[13,14],[15,16]],[[17,18],[19,20]],[[21,22],[23,24]]],[[[25,26],[27,28]],[[29,30],[31,32]],[[33,34],[35,36]]],[[[37,38],[39,40]],[[41,42],[43,44]],[[45,46],[47,48]]]])\n", "\n", " if fdarray.shape == fdarray_.shape and target2 == int('10'):\n", " ref_assert_var = True\n", " ref_tmp_var = True\n", " else:\n", " ref_assert_var = False\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "assert ref_tmp_var" ] } ], "metadata": { "executed_sections": [], "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.5.2" }, "rf_version": 1 }, "nbformat": 4, "nbformat_minor": 2 }