campus-fryslan-introduction.../Week-2/02. Vector operations.ipynb
2018-09-14 17:33:07 +02:00

137 lines
2.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this exercise, we'll do calculations on many numbers at once. Python provides the numpy library for that."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install numpy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"%matplotlib notebook\n",
"from matplotlib import pyplot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Use `np.arange(..., ...)` to create a vector from `0` up to and including `5`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = None\n",
"\n",
"print('x: ', x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Extract `1` from all the numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = None\n",
"print('y: ', y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Take the square root of all these numbers, either using `** 0.5` or `np.sqrt( ... )`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = None\n",
"print('y: ', y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that the square root of `-1` is not defined, and hence the result is *not a number* (nan).\n",
"\n",
"The cell below will make a plot of `x` and `y`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pyplot.plot(x, y)\n",
"pyplot.xlabel('x')\n",
"pyplot.ylabel('y')\n",
"None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You have learned to use `numpy` for so called vector operations, these are mathematical operations on many numbers as well. This field is called *linear algebra*, and is a usefull field on its own, in particular often used in artificial intelligence."
]
}
],
"metadata": {
"hide_input": false,
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}