{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 3\n", "\n", "## Video 12: Slicing Arrays\n", "**Python for the Energy Industry**\n", "\n", "## 1D Arrays\n", "\n", "Arrays such as those seen in the previous video can be sliced in a similar way to lists:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_array - [0 1 2 3 4 5 6 7 8 9]\n", "my_array[2:6] - [2 3 4 5]\n", "my_array[4:] - [4 5 6 7 8 9]\n", "my_array[:7] - [0 1 2 3 4 5 6]\n" ] } ], "source": [ "import numpy as np\n", "\n", "my_array = np.arange(10)\n", "\n", "print('my_array -',my_array)\n", "\n", "# get values between index 2 (included) and 6 (excluded)\n", "print('my_array[2:6] -',my_array[2:6])\n", "\n", "# get values between index 4 (included) the end of the array\n", "print('my_array[4:] -',my_array[4:])\n", "\n", "# get values between the start of the array and index 7 (excluded)\n", "print('my_array[:7] -',my_array[:7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slicing with the form a:b:c, accesses indices between a and b, and indices that are multiples of c. For example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_array[2:9:2] - [2 4 6 8]\n" ] } ], "source": [ "print('my_array[2:9:2] -',my_array[2:9:2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2D Arrays\n", "\n", "### Generating Arrays\n", "\n", "Numpy arrays can have any number of dimensions. Working with 2D arrays is common, and most functions for generating 1D arrays also work for multiple dimensions:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0. 0.]\n", " [0. 0. 0. 0.]\n", " [0. 0. 0. 0.]\n", " [0. 0. 0. 0.]]\n" ] } ], "source": [ "# 4 by 4 array of zeros\n", "print(np.zeros((4,4)))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1.]\n", " [1. 1. 1.]\n", " [1. 1. 1.]\n", " [1. 1. 1.]\n", " [1. 1. 1.]\n", " [1. 1. 1.]]\n" ] } ], "source": [ "# 6 by 3 array of ones\n", "print(np.ones((6,3)))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.61405163 0.52663249 0.59655916 0.17697362 0.00868107 0.65319468\n", " 0.43577846 0.70738606 0.52549845 0.83168464]\n", " [0.61957144 0.20098962 0.20853004 0.75346162 0.22582097 0.64537208\n", " 0.51449318 0.7450675 0.31139278 0.38297449]]\n" ] } ], "source": [ "# 2 by 10 array of random numbers\n", "print(np.random.rand(2,10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the when specifying the shape of the array, the first argument is the number of rows and the second argument is columns.\n", "\n", "You can get the shape of an array:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(6, 3)\n" ] } ], "source": [ "x = np.ones((6,3))\n", "\n", "print(x.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slicing Arrays\n", "\n", "Accessing elements in 2D arrays is done by separating the indices with a comma. The element in the second row, third column of an array would be accessed with the slice [1,2]." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.02001174 0.03948266 0.74952907]\n", " [0.73733734 0.28709885 0.98744824]\n", " [0.33292761 0.6943921 0.33486185]]\n", "second row, third column: 0.9874482372627739\n" ] } ], "source": [ "a = np.random.rand(3,3)\n", "print(a)\n", "\n", "print('second row, third column:',a[1,2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This applies when slicing as well, so getting all columns of the second row would be: [1,:]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.17902056 0.31581658 0.29322188]\n", " [0.92680429 0.67945011 0.48351428]\n", " [0.54745637 0.11393077 0.54413224]]\n", "second row, all columns: [0.92680429 0.67945011 0.48351428]\n" ] } ], "source": [ "a = np.random.rand(3,3)\n", "print(a)\n", "\n", "print('second row, all columns:',a[1,:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filtering Arrays\n", "\n", "You can also access the values in an array that meet certain conditions:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.92680429 0.67945011 0.54745637 0.54413224]\n" ] } ], "source": [ "print(a[a > 0.5])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.67945011 0.54745637 0.54413224]\n" ] } ], "source": [ "print(a[np.logical_and(a > 0.5, a < 0.8)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise \n", "\n", "Create a 5 by 5 array of random numbers between 0 and 10. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }