{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# [NTDS'17] demo 3: Numpy\n", "[ntds'17]: https://github.com/mdeff/ntds_2017\n", "\n", "Hermina Petric Maretic, [EPFL LTS4](http://lts4.epfl.ch)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[NumPy](http://www.numpy.org) is the fundamental package for scientific computing with Python. It contains among other things:\n", "* a powerful N-dimensional array object\n", "* sophisticated (broadcasting) functions\n", "* tools for integrating C/C++ and Fortran code\n", "* useful linear algebra, Fourier transform, and random number capabilities\n", "\n", "Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#create a numpy array\n", "a = np.array([1,2,3,4])\n", "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#or a 2 dimensional array\n", "m = np.array([[1,2],[3,4]])\n", "m" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m[0,0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m[:,1]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:2]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-2] #second last element of the array" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 4])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-2:] #last two elements of the array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Careful if you're used to Python list" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "b = [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b + b\n", "b" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + a\n", "a" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 1, 2, 3, 4])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#if you want to add elements to a\n", "np.append(a,a)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 1, 2, 3])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.append(a,[1,2,3])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 5, 2, 3, 4])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.insert(a, 1, 5) #insert 5 on position number 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic arithmetics with numpy arrays" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 5, 6, 7])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + 3" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3, 6, 9, 12])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 3" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 8, 27, 64])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ** 3" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 9, 16])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * a" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sum()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 4],\n", " [ 9, 16]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m * m #still elementwise multiplication" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 7, 10],\n", " [15, 22]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(m,m) #standard matrix multiplication" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[ 7, 10],\n", " [15, 22]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = np.matrix(m) #there is a type matrix\n", "m * m #for matrices, multiplication works as we're used to" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Some functions to create arrays" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 2, 4, 6, 8])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(0,10,2) #beginning, end, step\n", "x" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 2.5, 5. , 7.5, 10. ])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(0,10,5) #beginning, end, number of variables" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.00000000e+00, 2.16011948e+00, 4.66611616e+00,\n", " 1.00793684e+01, 2.17726400e+01, 4.70315038e+01,\n", " 1.01593667e+02, 2.19454460e+02, 4.74047853e+02,\n", " 1.02400000e+03])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.logspace(0,10,10,base=2) #beginning, end, number of variables" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 0],\n", " [0, 2, 0],\n", " [0, 0, 3]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.diag([1,2,3])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0., 0., 0., 0., 0.])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros(5)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1.],\n", " [ 1., 1., 1.],\n", " [ 1., 1., 1.]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((3,3))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.95867801, 0.36665502],\n", " [ 0.12711337, 0.17199144],\n", " [ 0.06886422, 0.08173259],\n", " [ 0.07495435, 0.73382897],\n", " [ 0.49609348, 0.4109899 ]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.rand(5,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Some linear algebra functions" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 4])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.diag(m)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.trace(m)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[1, 3],\n", " [2, 4]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.T" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[-2. , 1. ],\n", " [ 1.5, -0.5]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m1 = np.linalg.inv(m)\n", "m1" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-2.0000000000000004" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.det(m)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.49999999999999967" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.det(m1)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "[eival, eivec] = np.linalg.eig(m)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.37228132, 5.37228132])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eival" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[-0.82456484, -0.41597356],\n", " [ 0.56576746, -0.90937671]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eivec" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 2 }