{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "\n", "\n", "| - | - | - |\n", "|-----------------------------------------------------------------------------------|-----------------------------------------------------------------------------------|-----------------------------------------------------------------------------------|\n", "| [Exercise 1 (column comparison)](<#Exercise-1-(column-comparison)>) | [Exercise 2 (first half second half)](<#Exercise-2-(first-half-second-half)>) | [Exercise 3 (most frequent first)](<#Exercise-3-(most-frequent-first)>) |\n", "| [Exercise 4 (matrix power)](<#Exercise-4-(matrix-power)>) | [Exercise 5 (correlation)](<#Exercise-5-(correlation)>) | [Exercise 6 (meeting lines)](<#Exercise-6-(meeting-lines)>) |\n", "| [Exercise 7 (meeting planes)](<#Exercise-7-(meeting-planes)>) | [Exercise 8 (almost meeting lines)](<#Exercise-8-(almost-meeting-lines)>) | |\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy (continues)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comparisons and masking\n", "\n", "Just like NumPy allows element-wise arithmetic operations between arrays, for example addition of two arrays, it is also possible to compare two arrays element-wise. For example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a=np.array([1,3,4])\n", "b=np.array([2,2,7])\n", "c = a < b\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can query whether all comparisons resulted `True`, or whether some comparison resulted `True`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(c.all()) # were all True\n", "print(c.any()) # was some comparison True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also count the number of comparisons that were `True`. This solution relies on the interpretation that `True` corresponds to 1 and `False` corresponds to 0:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(np.sum(c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because the broadcasting rules apply also to comparison, we can write" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(a > 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To try these operations on real data, we download some weather statistics from Helsinki (Kumpula) weather station. More precisely, we will get the daily average temperatures from year 2017. We use the Pandas library, which we will cover later during this course, to load the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "a=pd.read_csv(\"https://www.cs.helsinki.fi/u/jttoivon/dap/data/fmi/kumpula-weather-2017.csv\")['Air temperature (degC)'].values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Number of days with the temperature below zero\", np.sum(a < 0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In core Python we can combine truth values using the `and`, `or`, and `not` keywords. For boolean array however we have to use the elementwise operators `&`, `|`, and `~`, respectively. An example of these:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.sum((0 < a) & (a < 10)) # Temperature is greater than 0 and less that 10 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we had to use parentheses around the comparisons, because the precedence of the `&` is higher than that of `<`, and so it would have been performed before the comparisons." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another use of boolean arrays is that they can be used to select a subset of elements. For example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = a > 0\n", "print(c[:10]) # print only the first ten elements\n", "print(a[c]) # Select only the positive temperatures " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This operation is called *masking*. It can also be used to assign a new value. For example the following zeroes out the negative temperatures:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[~c] = 0\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare this result with the original array `a` to make sure you understand what's going on!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####