{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Numpy\n", "=====" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "`numpy` je paket (modul) za (efikasno) numeričko računanje u Pythonu. Naglasak je na efikasnom računanju s nizovima, vektorima i matricama, uključivo višedimenzionalne stukture. Napisan je u C-u i Fortanu te koristi BLAS biblioteku." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "from numpy import *" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Kreiranje nizova pomoću `numpy` modula" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = array([1,2,3,4])\n", "v" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M = array([[1, 2], [3, 4]])\n", "M" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "(numpy.ndarray, numpy.ndarray)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(v), type(M)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(4,)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.shape" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.shape" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(4, 4)" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.size, M.size" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Možemo koristiti i funkcije `numpy.shape`, `numpy.size`" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shape(M)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "size(M)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Koja je razlika između `numpy.ndarray` tipa i standardnih lista u Pythonu?\n", "\n", "- liste u Pythonu mogu sadržavati bilo kakve vrste objekata, to nije slučaj s `numpy.ndarray`\n", "- `numpy.ndarray` nisu dinamički objekti: pri kreiranju im je određen tip\n", "- za `numpy.ndarray` implementirane su razne efikasne metode važne u numerici\n", "- de facto sva računanja se odvijaju u C-u i Fortranu pomoću BLAS rutina\n", "\n", "`dtype` (data type) nam daje informaciju o tipu podataka u nizu:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.dtype" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Kako je *M* statički objekt, ne možemo napraviti ovo:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'hello'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mM\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"hello\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'hello'" ] } ], "source": [ "M[0,0] = \"hello\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Naravno, ovo je ok:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "M[0,0]=5" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "`dtype` se može eksplicitno zadati:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1.+0.j, 2.+0.j],\n", " [ 3.+0.j, 4.+0.j]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M = array([[1, 2], [3, 4]], dtype=complex)\n", "M" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Tipično `dtype` su: `int`, `float`, `complex`, `bool`, `object`, itd.\n", "\n", "Ali možemo biti i eksplicitni vezano za veličinu registra: `int64`, `int16`, `float128`, `complex128`." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Funkcije koje generiraju nizove" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = arange(0, 10, 1) # argumenti: početak, kraj, korak\n", "\n", "x # 10 nije u nizu!" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([ -1.00000000e+00, -9.00000000e-01, -8.00000000e-01,\n", " -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,\n", " -4.00000000e-01, -3.00000000e-01, -2.00000000e-01,\n", " -1.00000000e-01, -2.22044605e-16, 1.00000000e-01,\n", " 2.00000000e-01, 3.00000000e-01, 4.00000000e-01,\n", " 5.00000000e-01, 6.00000000e-01, 7.00000000e-01,\n", " 8.00000000e-01, 9.00000000e-01])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = arange(-1, 1, 0.1)\n", "x" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.41666667, 0.83333333, 1.25 ,\n", " 1.66666667, 2.08333333, 2.5 , 2.91666667,\n", " 3.33333333, 3.75 , 4.16666667, 4.58333333,\n", " 5. , 5.41666667, 5.83333333, 6.25 ,\n", " 6.66666667, 7.08333333, 7.5 , 7.91666667,\n", " 8.33333333, 8.75 , 9.16666667, 9.58333333, 10. ])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ovdje su i početak i kraj uključeni!\n", "linspace(0, 10, 25)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1.00000000e+00, 3.03773178e+00, 9.22781435e+00,\n", " 2.80316249e+01, 8.51525577e+01, 2.58670631e+02,\n", " 7.85771994e+02, 2.38696456e+03, 7.25095809e+03,\n", " 2.20264658e+04])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "logspace(0, 10, 10, base=e)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "x, y = mgrid[0:5, 0:5] # slično kao meshgrid u MATLAB-u" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0, 0, 0],\n", " [1, 1, 1, 1, 1],\n", " [2, 2, 2, 2, 2],\n", " [3, 3, 3, 3, 3],\n", " [4, 4, 4, 4, 4]])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4],\n", " [0, 1, 2, 3, 4]])" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "from numpy import random" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.46634159, 0.18383259, 0.99043082, 0.95553566, 0.86117026],\n", " [ 0.18758889, 0.66275122, 0.88660117, 0.48603761, 0.58020297],\n", " [ 0.76289553, 0.66796829, 0.70291958, 0.84876206, 0.90293859],\n", " [ 0.62222178, 0.46259364, 0.86471336, 0.73420665, 0.21731253],\n", " [ 0.63499971, 0.49470533, 0.06818067, 0.85432396, 0.87013258]])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# uniformna distribucija na [0,1]\n", "random.rand(5,5)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1.8841264 , 1.79222299, -0.15896515, -0.95471483, 0.604597 ],\n", " [ 0.01879898, -0.21818353, -0.02871515, -0.17077362, 2.32474892],\n", " [ 0.47474772, 0.30405552, -0.37323118, -0.60411745, 1.06795274],\n", " [-0.2546505 , -0.10511935, 0.73920025, 2.50885605, -1.04196785],\n", " [ 0.2522967 , -0.87931277, -0.93793984, -0.71194124, 0.67464873]])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# standardna normalna distribucija\n", "random.randn(5,5)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 0],\n", " [0, 2, 0],\n", " [0, 0, 3]])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# dijagonalna matrica\n", "diag([1,2,3])" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 0, 0],\n", " [0, 0, 2, 0],\n", " [0, 0, 0, 3],\n", " [0, 0, 0, 0]])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# matrica sa sporednom dijagonalom\n", "diag([1,2,3], k=1)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0.],\n", " [ 0., 0., 0.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zeros((3,3))" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1.],\n", " [ 1., 1., 1.],\n", " [ 1., 1., 1.]])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones((3,3))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Učitavanje podataka" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Često učitavamo podatke iz datoteka (lokalno ili s weba). Važni formati su cvs (comma-separated values) i tsv (tab-separated values)." ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1850,-0.211\r\n", "1851,-0.403\r\n", "1852,0.334\r\n", "1853,-0.762\r\n", "1854,-0.019\r\n", "1855,-0.961\r\n", "1856,-0.243\r\n", "1857,0.420\r\n", "1858,-0.059\r\n", "1859,0.018\r\n" ] } ], "source": [ "!head tpt-europe.csv" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "data = genfromtxt('tpt-europe.csv')" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "((158,), dtype('float64'))" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape, data.dtype" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Uz `numpy.savetxt` možemo napraviti i obrnuto." ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.93875912, 0.43215004, 0.23129988],\n", " [ 0.13593153, 0.0413665 , 0.21458492],\n", " [ 0.96261572, 0.22372779, 0.44478241]])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M = random.rand(3,3)\n", "M" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "savetxt(\"random-matrix.csv\", M)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9.387591161588458855e-01 4.321500444939530006e-01 2.312998801967627305e-01\r\n", "1.359315283144698627e-01 4.136650288835774791e-02 2.145849165333063580e-01\r\n", "9.626157155774026641e-01 2.237277861211310892e-01 4.447824126746069417e-01\r\n" ] } ], "source": [ "!cat random-matrix.csv" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.93876 0.43215 0.23130\r\n", "0.13593 0.04137 0.21458\r\n", "0.96262 0.22373 0.44478\r\n" ] } ], "source": [ "savetxt(\"random-matrix.csv\", M, fmt='%.5f') # s fmt specificiramo format\n", "\n", "!cat random-matrix.csv" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Postoji i interni format za `numpy` nizove:" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "random-matrix.npy: data\r\n" ] } ], "source": [ "save(\"random-matrix.npy\", M)\n", "\n", "!file random-matrix.npy" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.93875912, 0.43215004, 0.23129988],\n", " [ 0.13593153, 0.0413665 , 0.21458492],\n", " [ 0.96261572, 0.22372779, 0.44478241]])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "load(\"random-matrix.npy\")" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.itemsize # byte-ovi po elementu" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "72" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.nbytes" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.ndim" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Rad s nizovima" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Indeksiranje funkcionira standardno." ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[0]" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.041366502888357748" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ " M[1,1]" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.93875912, 0.43215004, 0.23129988],\n", " [ 0.13593153, 0.0413665 , 0.21458492],\n", " [ 0.96261572, 0.22372779, 0.44478241]])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.13593153, 0.0413665 , 0.21458492])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Naravno, možemo koristiti i `:` operator: " ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.13593153, 0.0413665 , 0.21458492])" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[1,:] # redak 1" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.43215004, 0.0413665 , 0.22372779])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[:,1] # stupac 1" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "M[1,:] = 0\n", "M[:,2] = -1" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.93875912, 0.43215004, -1. ],\n", " [ 0. , 0. , -1. ],\n", " [ 0.96261572, 0.22372779, -1. ]])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = array([1,2,3,4,5])\n", "A" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([2, 3])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3]" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -2, -3, 4, 5])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3] = [-2,-3]\n", "A" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -2, -3, 4, 5])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[::]" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -3, 5])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[::2]" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -2, -3])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:3]" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([4, 5])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[3:]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "S negativnim indeksima računamo od kraja niza:" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "A = array([1,2,3,4,5])" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[-1] # zadnji element niza" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 5])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[-3:] # zadnja tri elementa" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Naravno, iste operacije imamo i za višedimenzionalne nizove." ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4],\n", " [10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34],\n", " [40, 41, 42, 43, 44]])" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = array([[n+m*10 for n in range(5)] for m in range(5)])\n", "A" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[11, 12, 13],\n", " [21, 22, 23],\n", " [31, 32, 33]])" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:4, 1:4]" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4],\n", " [20, 22, 24],\n", " [40, 42, 44]])" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[::2, ::2]" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34]])" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indeksi_redaka = [1, 2, 3]\n", "A[indeksi_redaka]" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([11, 22, 34])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indeksi_stupaca = [1, 2, -1]\n", "A[indeksi_redaka, indeksi_stupaca]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Možemo koristiti i tzv. maske: ako je maska `numpy` niz tipa `bool`, tada se izabiru oni elementi koji u maski odgovaraju vrijednosti `True`." ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4])" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = array([n for n in range(5)])\n", "B" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 2])" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maska = array([True, False, True, False, False])\n", "B[maska]" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 2])" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maska = array([1,0,1,0,0], dtype=bool)\n", "B[maska]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Zanimljiviji primjer:" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ,\n", " 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = arange(0, 10, 0.5)\n", "x" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, False, False, False, False, False,\n", " False, False, True, True, True, True, False, False, False,\n", " False, False], dtype=bool)" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maska = (5 < x) * (x < 7.5)\n", "maska" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 5.5, 6. , 6.5, 7. ])" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[maska]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Funkcije na nizovima" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "(array([11, 12, 13, 14]),)" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indeksi = where(maska)\n", "indeksi" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 5.5, 6. , 6.5, 7. ])" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[indeksi]" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3 4]\n", " [10 11 12 13 14]\n", " [20 21 22 23 24]\n", " [30 31 32 33 34]\n", " [40 41 42 43 44]]\n" ] }, { "data": { "text/plain": [ "array([ 0, 11, 22, 33, 44])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(A)\n", "diag(A)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([10, 21, 32, 43])" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diag(A, -1)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([-3, -2, -1, 0, 1, 2])" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v2 = arange(-3,3)\n", "v2" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([-2, 0, 2])" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indeksi_redaka = [1, 3, 5]\n", "v2[indeksi_redaka]" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([-2, 0, 2])" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v2.take(indeksi_redaka)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "U sljedećem primjeru `take` djeluje na listu, a izlaz je `array`: " ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([-2, 0, 2])" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "take([-3, -2, -1, 0, 1, 2], indeksi_redaka)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Funkcija `choose`:" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 5, -2, 3, -4])" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "koji = [1, 0, 1, 0]\n", "izbori = [[-1,-2,-3,-4], [5,4,3,2]]\n", "\n", "choose(koji, izbori)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Što radi ova funkcija?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Vektorizacija koda\n", "\n", "Što je više operacija s nizovima, to će kod generalno biti brži." ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v1 = arange(0, 5)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 2, 4, 6, 8])" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1 * 2" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 4, 5, 6])" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1 + 2" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3 4]\n", " [10 11 12 13 14]\n", " [20 21 22 23 24]\n", " [30 31 32 33 34]\n", " [40 41 42 43 44]]\n" ] }, { "data": { "text/plain": [ "(array([[ 0, 2, 4, 6, 8],\n", " [20, 22, 24, 26, 28],\n", " [40, 42, 44, 46, 48],\n", " [60, 62, 64, 66, 68],\n", " [80, 82, 84, 86, 88]]), array([[ 2, 3, 4, 5, 6],\n", " [12, 13, 14, 15, 16],\n", " [22, 23, 24, 25, 26],\n", " [32, 33, 34, 35, 36],\n", " [42, 43, 44, 45, 46]]))" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(A)\n", "A * 2, A + 2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Defaultne operacije na nizovima su **uvijek** definirane po elementima." ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 4, 9, 16],\n", " [ 100, 121, 144, 169, 196],\n", " [ 400, 441, 484, 529, 576],\n", " [ 900, 961, 1024, 1089, 1156],\n", " [1600, 1681, 1764, 1849, 1936]])" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A * A" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 4, 9, 16])" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1 * v1" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "((5, 5), (5,))" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.shape, v1.shape" ] }, { "cell_type": "code", "execution_count": 122, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3 4]\n", " [10 11 12 13 14]\n", " [20 21 22 23 24]\n", " [30 31 32 33 34]\n", " [40 41 42 43 44]] [0 1 2 3 4]\n" ] }, { "data": { "text/plain": [ "array([[ 0, 1, 4, 9, 16],\n", " [ 0, 11, 24, 39, 56],\n", " [ 0, 21, 44, 69, 96],\n", " [ 0, 31, 64, 99, 136],\n", " [ 0, 41, 84, 129, 176]])" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(A,v1)\n", "A * v1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Kako doći do standardnog umnoška?" ] }, { "cell_type": "code", "execution_count": 123, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 300, 310, 320, 330, 340],\n", " [1300, 1360, 1420, 1480, 1540],\n", " [2300, 2410, 2520, 2630, 2740],\n", " [3300, 3460, 3620, 3780, 3940],\n", " [4300, 4510, 4720, 4930, 5140]])" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(A, A)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 300, 310, 320, 330, 340],\n", " [1300, 1360, 1420, 1480, 1540],\n", " [2300, 2410, 2520, 2630, 2740],\n", " [3300, 3460, 3620, 3780, 3940],\n", " [4300, 4510, 4720, 4930, 5140]])" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A @ A # nova operacija definirana u Python-u 3.5+" ] }, { "cell_type": "code", "execution_count": 125, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 300, 310, 320, 330, 340],\n", " [1300, 1360, 1420, 1480, 1540],\n", " [2300, 2410, 2520, 2630, 2740],\n", " [3300, 3460, 3620, 3780, 3940],\n", " [4300, 4510, 4720, 4930, 5140]])" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matmul(A,A) # @ je zapravo pokrata za matmul, dot i matmul nisu iste operacije (poklapaju se na 1D i 2D nizovima)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 30, 130, 230, 330, 430])" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(A, v1)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 30, 130, 230, 330, 430])" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A @ v1" ] }, { "cell_type": "code", "execution_count": 128, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1 @ v1 # analogno dot(v1, v1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Matrice mogu biti i višedimenzionalne" ] }, { "cell_type": "code", "execution_count": 129, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(8, 13, 13)" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = random.rand(8,13,13)\n", "b = random.rand(8,13,13)\n", "matmul(a, b).shape" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Postoji i tip `matrix`. Kod njega operacije `+, -, *` se ponašaju onako kako smo navikli." ] }, { "cell_type": "code", "execution_count": 130, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "M = matrix(A)\n", "v = matrix(v1).T # da bi dobili stupčasti vektor" ] }, { "cell_type": "code", "execution_count": 131, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[0],\n", " [1],\n", " [2],\n", " [3],\n", " [4]])" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "code", "execution_count": 132, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 300, 310, 320, 330, 340],\n", " [1300, 1360, 1420, 1480, 1540],\n", " [2300, 2410, 2520, 2630, 2740],\n", " [3300, 3460, 3620, 3780, 3940],\n", " [4300, 4510, 4720, 4930, 5140]])" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M*M" ] }, { "cell_type": "code", "execution_count": 133, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 30],\n", " [130],\n", " [230],\n", " [330],\n", " [430]])" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M*v" ] }, { "cell_type": "code", "execution_count": 134, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[30]])" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# skalarni produkt\n", "v.T * v" ] }, { "cell_type": "code", "execution_count": 135, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 30],\n", " [131],\n", " [232],\n", " [333],\n", " [434]])" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v + M*v" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Naravno, dimenzije trebaju biti kompatibilne." ] }, { "cell_type": "code", "execution_count": 136, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = matrix([1,2,3,4,5,6]).T" ] }, { "cell_type": "code", "execution_count": 137, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "((5, 5), (6, 1))" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shape(M), shape(v)" ] }, { "cell_type": "code", "execution_count": 138, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "ValueError", "evalue": "shapes (5,5) and (6,1) not aligned: 5 (dim 1) != 6 (dim 0)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mM\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/projects/anaconda3/lib/python3.5/site-packages/numpy/matrixlib/defmatrix.py\u001b[0m in \u001b[0;36m__mul__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 341\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 342\u001b[0m \u001b[0;31m# This promotes 1-D vectors to row vectors\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 343\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0masmatrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 344\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misscalar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'__rmul__'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 345\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: shapes (5,5) and (6,1) not aligned: 5 (dim 1) != 6 (dim 0)" ] } ], "source": [ "M * v" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Još neke funkcije: `inner`, `outer`, `cross`, `kron`, `tensordot`." ] }, { "cell_type": "code", "execution_count": 139, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0.+1.j, 0.+2.j],\n", " [ 0.+3.j, 0.+4.j]])" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C = matrix([[1j, 2j], [3j, 4j]])\n", "C" ] }, { "cell_type": "code", "execution_count": 140, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0.-1.j, 0.-2.j],\n", " [ 0.-3.j, 0.-4.j]])" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conjugate(C)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Adjungiranje:" ] }, { "cell_type": "code", "execution_count": 141, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0.-1.j, 0.-3.j],\n", " [ 0.-2.j, 0.-4.j]])" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C.H" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Za izvlačenje realnog, odnosno imaginarnog dijela: `real` i `imag`:" ] }, { "cell_type": "code", "execution_count": 142, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0., 0.],\n", " [ 0., 0.]])" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "real(C) # isto što i C.real" ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 1., 2.],\n", " [ 3., 4.]])" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imag(C) # isto što i C.imag" ] }, { "cell_type": "code", "execution_count": 144, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.78539816, 1.10714872],\n", " [ 1.24904577, 1.32581766]])" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "angle(C+1) # u MATLAB-u je to funkcija arg, dakle argument (faza) kompleksnog broja" ] }, { "cell_type": "code", "execution_count": 145, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 1., 2.],\n", " [ 3., 4.]])" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(C)" ] }, { "cell_type": "code", "execution_count": 146, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0.+2.j , 0.-1.j ],\n", " [ 0.-1.5j, 0.+0.5j]])" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from numpy.linalg import inv, det\n", "inv(C) # isto što i C.I " ] }, { "cell_type": "code", "execution_count": 147, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "matrix([[ 1.00000000e+00+0.j, 0.00000000e+00+0.j],\n", " [ 1.11022302e-16+0.j, 1.00000000e+00+0.j]])" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C.I * C" ] }, { "cell_type": "code", "execution_count": 148, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(2.0000000000000004+0j)" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "det(C)" ] }, { "cell_type": "code", "execution_count": 149, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(0.49999999999999967+0j)" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "det(C.I)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Izvlačenje osnovih informacija iz nizova" ] }, { "cell_type": "code", "execution_count": 150, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(77431, 7)" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# u stockholm_td_adj.dat su podaci o vremenu za Stockholm\n", "dataStockholm = genfromtxt('stockholm_td_adj.dat')\n", "dataStockholm.shape" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "6.1971096847515854" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# temperatura se nalazi u 4. stupcu (znači stupcu broj 3)\n", "mean(dataStockholm[:,3])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Prosječna dnevna temperatura u Stockholmu u zadnjiih 200 godina je bila 6.2 C." ] }, { "cell_type": "code", "execution_count": 152, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(8.2822716213405734, 68.596023209663414)" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std(dataStockholm[:,3]), var(dataStockholm[:,3])" ] }, { "cell_type": "code", "execution_count": 153, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "-25.800000000000001" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataStockholm[:,3].min()" ] }, { "cell_type": "code", "execution_count": 154, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "28.300000000000001" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataStockholm[:,3].max()" ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = arange(0, 10)\n", "d" ] }, { "cell_type": "code", "execution_count": 156, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "45" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(d)" ] }, { "cell_type": "code", "execution_count": 157, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3628800" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prod(d+1)" ] }, { "cell_type": "code", "execution_count": 158, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45])" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# kumulativa suma\n", "cumsum(d)" ] }, { "cell_type": "code", "execution_count": 159, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 6, 24, 120, 720, 5040,\n", " 40320, 362880, 3628800])" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# kumulativan produkt\n", "cumprod(d+1)" ] }, { "cell_type": "code", "execution_count": 160, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "110" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# isto što i: diag(A).sum()\n", "trace(A)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Naravno, sve ove operacije možemo raditi na dijelovima nizova." ] }, { "cell_type": "code", "execution_count": 161, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1800 1 1 -6.1 -6.1 -6.1 1\r\n", "1800 1 2 -15.4 -15.4 -15.4 1\r\n", "1800 1 3 -15.0 -15.0 -15.0 1\r\n" ] } ], "source": [ "!head -n 3 stockholm_td_adj.dat" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Format je: godina, mjesec, dan, prosječna dnevna temperatura, najniža, najviša, lokacija.\n", "\n", "Recimo da nas zanimaju samo temperature u veljači." ] }, { "cell_type": "code", "execution_count": 162, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.,\n", " 12.])" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# mjeseci su 1.,..., 12.\n", "unique(dataStockholm[:,1])" ] }, { "cell_type": "code", "execution_count": 163, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "maska_velj = dataStockholm[:,1] == 2" ] }, { "cell_type": "code", "execution_count": 164, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "-3.2121095707365961" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean(dataStockholm[maska_velj,3])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Sada nije problem doći do histograma za prosječne mjesečne temperature u par redaka." ] }, { "cell_type": "code", "execution_count": 165, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEKCAYAAAAFJbKyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAF6lJREFUeJzt3X20ZXV93/H3B0arIojCgAiMQ5WixALaW5SwYkAUeRJa\nSwxUCSp2QpZWbU0r1CxpTNvgsmJNcIkTmUJSBLMQ4lgGhaoJwYUPA/IwBIyIg46DzCBVUBNx8Ns/\nzh5zeznnztn3nnP2vZf3a62zzn74nb2/e82s+cxvP/1SVUiSNKydui5AkrS4GBySpFYMDklSKwaH\nJKkVg0OS1IrBIUlqxeCQJLVicEiSWjE4JEmtLOu6gHHYc889a+XKlV2XIUmLxs033/xgVS0fpu2S\nDI6VK1eyfv36rsuQpEUjyX3DtvVUlSSpFYNDktSKwSFJamXs1ziSrAFOArZU1YuaZZ8EDmqa7A78\nsKoO6/PbjcAjwGPAtqqaGne9kqTZTeLi+CXAhcCfbl9QVb+5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W3N6+7Px1YN0vTVwIH914BBcBuTe/iS8AFSS4DrqqqTU1wHAt8vWn7dHpBcihwZVU9\n2Ox75rgT0sQZHNLcrKU3jsVRwB4D2uwEHNFcC5nu/CTXACcAX0ryanq9jD+sqo9Nb5jk7Szy4QG0\n9HiNQ5qbNcD7quqOWdpcR+/FmwAkOaz5fl5V3VFV76f3VucX0DuN9ebm1BVJ9k2yF71hT1+XZI9m\nuaeq1Dl7HNIcVNUmetcw+q5uvt8OfKR5g+4y4AbgbOCdSY4GHqP3OvZrq+pnSV4I3NSc2vox8Iaq\nujPJfwX+Kslj9E5lvXFMhyUNxbfjSiOU5F3AblV1Xte1SONij0MakSRn0+sNvLbjUqSxsschSWrF\ni+OSpFYMDklSKwaHJKkVg0OS1IrBIUlqxeCQJLXy/wB8XtlDH2NmowAAAABJRU5ErkJggg==\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mjeseci = arange(1,13)\n", "mjeseci_prosjek = [mean(dataStockholm[dataStockholm[:,1] == mjesec, 3]) for mjesec in mjeseci]\n", "\n", "from pylab import *\n", "%matplotlib inline\n", "fig, ax = subplots()\n", "ax.bar(mjeseci, mjeseci_prosjek)\n", "ax.set_xlabel(\"Mjesec\")\n", "ax.set_ylabel(\"Prosj. mj. temp.\");" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Rad s višedimenzionalnim podacima" ] }, { "cell_type": "code", "execution_count": 166, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.66942337, 0.98488678, 0.06962048],\n", " [ 0.93170674, 0.70455373, 0.10112863],\n", " [ 0.4101218 , 0.20957115, 0.9954116 ]])" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = rand(3,3)\n", "m" ] }, { "cell_type": "code", "execution_count": 167, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.99541159521774658" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.max()" ] }, { "cell_type": "code", "execution_count": 168, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.93170674, 0.98488678, 0.9954116 ])" ] }, "execution_count": 168, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# max u svakom stupcu\n", "m.max(axis=0)" ] }, { "cell_type": "code", "execution_count": 169, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.98488678, 0.93170674, 0.9954116 ])" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# max u svakom retku\n", "m.max(axis=1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Oblik niza se može promijeniti bez da se dira memorija, dakle mogu se primijenjivati i na veliku količinu podataka." ] }, { "cell_type": "code", "execution_count": 170, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4],\n", " [10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34],\n", " [40, 41, 42, 43, 44]])" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 171, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "n, m = A.shape" ] }, { "cell_type": "code", "execution_count": 172, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n", " 32, 33, 34, 40, 41, 42, 43, 44]])" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = A.reshape((1,n*m))\n", "B" ] }, { "cell_type": "code", "execution_count": 173, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 5, 5, 5, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n", " 32, 33, 34, 40, 41, 42, 43, 44]])" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B[0,0:5] = 5 # promijenili smo B\n", "B" ] }, { "cell_type": "code", "execution_count": 174, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 5, 5, 5, 5],\n", " [10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34],\n", " [40, 41, 42, 43, 44]])" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A # a time smo promijenili i A" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Funkcija `flatten` radi kopiju." ] }, { "cell_type": "code", "execution_count": 175, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 5, 5, 5, 5, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n", " 32, 33, 34, 40, 41, 42, 43, 44])" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = A.flatten()\n", "B" ] }, { "cell_type": "code", "execution_count": 176, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([10, 10, 10, 10, 10, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n", " 32, 33, 34, 40, 41, 42, 43, 44])" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B[0:5] = 10\n", "B" ] }, { "cell_type": "code", "execution_count": 177, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 5, 5, 5, 5],\n", " [10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34],\n", " [40, 41, 42, 43, 44]])" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A # A je sad ostao isti" ] }, { "cell_type": "code", "execution_count": 178, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "v = array([1,2,3])" ] }, { "cell_type": "code", "execution_count": 179, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shape(v)" ] }, { "cell_type": "code", "execution_count": 180, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2],\n", " [3]])" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pretvorimo v u matricu\n", "v[:, newaxis]" ] }, { "cell_type": "code", "execution_count": 181, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "(3, 1)" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[:,newaxis].shape" ] }, { "cell_type": "code", "execution_count": 182, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(1, 3)" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[newaxis,:].shape" ] }, { "cell_type": "code", "execution_count": 183, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "a = array([[1, 2], [3, 4]])" ] }, { "cell_type": "code", "execution_count": 184, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ponovimo svaki element tri puta\n", "repeat(a, 3)" ] }, { "cell_type": "code", "execution_count": 185, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 1, 2, 1, 2],\n", " [3, 4, 3, 4, 3, 4]])" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tile(a, 3)" ] }, { "cell_type": "code", "execution_count": 186, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "b = array([[5, 6]])" ] }, { "cell_type": "code", "execution_count": 187, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4],\n", " [5, 6]])" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "concatenate((a, b), axis=0)" ] }, { "cell_type": "code", "execution_count": 188, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 5],\n", " [3, 4, 6]])" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "concatenate((a, b.T), axis=1)" ] }, { "cell_type": "code", "execution_count": 189, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4],\n", " [5, 6]])" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vstack((a,b))" ] }, { "cell_type": "code", "execution_count": 190, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 5],\n", " [3, 4, 6]])" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hstack((a,b.T))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Kopiranje nizova" ] }, { "cell_type": "code", "execution_count": 191, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = array([[1, 2], [3, 4]])\n", "A" ] }, { "cell_type": "code", "execution_count": 192, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# B je isto što i A (bez kopiranja podataka)\n", "B = A" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Ako želimo napraviti novu kopiju, koristimo funkciju `copy`:" ] }, { "cell_type": "code", "execution_count": 193, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "B = copy(A)" ] }, { "cell_type": "code", "execution_count": 194, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "v = array([1,2,3,4])\n", "\n", "for element in v:\n", " print (element)" ] }, { "cell_type": "code", "execution_count": 195, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "redak [1 2]\n", "1\n", "2\n", "redak [3 4]\n", "3\n", "4\n" ] } ], "source": [ "M = array([[1,2], [3,4]])\n", "\n", "for row in M:\n", " print (\"redak {}\".format(row))\n", " \n", " for element in row:\n", " print (element)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Funkcija `enumerate` nam daje i element i njegov indeks:" ] }, { "cell_type": "code", "execution_count": 196, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "indeks retka 0 redak [1 2]\n", "col_idx 0 element 1\n", "col_idx 1 element 2\n", "indeks retka 1 redak [3 4]\n", "col_idx 0 element 3\n", "col_idx 1 element 4\n" ] } ], "source": [ "for row_idx, row in enumerate(M):\n", " print (\"indeks retka {} redak {}\".format(row_idx, row))\n", " \n", " for col_idx, element in enumerate(row):\n", " print (\"col_idx {} element {}\".format(col_idx, element))\n", " M[row_idx, col_idx] = element ** 2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Vektorizacija funkcija" ] }, { "cell_type": "code", "execution_count": 197, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "def Theta(x):\n", " \"\"\"\n", " Sklarna verzija step funkcije.\n", " \"\"\"\n", " if x >= 0:\n", " return 1\n", " else:\n", " return 0" ] }, { "cell_type": "code", "execution_count": 198, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "ename": "ValueError", "evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mTheta\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mTheta\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mSklarna\u001b[0m \u001b[0mverzija\u001b[0m \u001b[0mstep\u001b[0m \u001b[0mfunkcije\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \"\"\"\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" ] } ], "source": [ "Theta(array([-3,-2,-1,0,1,2,3]))" ] }, { "cell_type": "code", "execution_count": 199, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "Theta_vec = vectorize(Theta)" ] }, { "cell_type": "code", "execution_count": 200, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 1, 1, 1, 1])" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Theta_vec(array([-3,-2,-1,0,1,2,3]))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "To smo mogli napraviti i ručno." ] }, { "cell_type": "code", "execution_count": 201, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def Theta(x):\n", " \"\"\"\n", " Vektorska verzija step funkcije.\n", " \"\"\"\n", " return 1 * (x >= 0)" ] }, { "cell_type": "code", "execution_count": 202, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 1, 1, 1, 1])" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Theta(array([-3,-2,-1,0,1,2,3]))" ] }, { "cell_type": "code", "execution_count": 203, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(0, 1)" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# radi naravno i za skalare\n", "Theta(-1.2), Theta(2.6)" ] }, { "cell_type": "code", "execution_count": 204, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 4],\n", " [ 9, 16]])" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M" ] }, { "cell_type": "code", "execution_count": 205, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "barem jedan element iz M je veći od 5\n" ] } ], "source": [ "if (M > 5).any():\n", " print (\"barem jedan element iz M je veći od 5\")\n", "else:\n", " print (\"svi elementi iz M su manji ili jednaki od 5\")" ] }, { "cell_type": "code", "execution_count": 206, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "barem jedan element je manji ili jednak od 5\n" ] } ], "source": [ "if (M > 5).all():\n", " print (\"svi elementi iz M su veći od 5\")\n", "else:\n", " print (\"barem jedan element je manji ili jednak od 5\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Eksplicitno pretvaranje podataka. Uvijek stvara novi niz." ] }, { "cell_type": "code", "execution_count": 207, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.dtype" ] }, { "cell_type": "code", "execution_count": 208, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 4.],\n", " [ 9., 16.]])" ] }, "execution_count": 208, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M2 = M.astype(float)\n", "M2" ] }, { "cell_type": "code", "execution_count": 209, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M2.dtype" ] }, { "cell_type": "code", "execution_count": 210, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ True, True],\n", " [ True, True]], dtype=bool)" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M3 = M.astype(bool)\n", "M3" ] }, { "cell_type": "code", "execution_count": 211, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
Python verzija3.5.3
kompajlerGCC 4.8.2 20140120 (Red Hat 4.8.2-15)
sustavLinux
broj CPU-a8
interpreter64bit
numpy verzija1.11.3
matplotlib verzija2.0.0
" ], "text/plain": [ "" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from verzije import *\n", "from IPython.display import HTML\n", "HTML(print_sysinfo()+info_packages('numpy,matplotlib'))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Zadaci za vježbu\n", "\n", "- Matrica $A$ reda 5 je dobijena s `A=random.randn(5,5)`. Izvucite $2\\times 2$ matricu $B$ iz gornjeg desnog kuta matrice $A$. Pomnožite matricu $B$ s nekom drugom $2\\times 2$ matricom te ubacite tu matricu u gornji lijevi kut matrice $A$.\n", "- Dan je kod\n", "```\n", "x = linspace(0, 1, 3)\n", "# y = 2*x + 1:\n", "y=x; y*=2; y+=1\n", "# z = 4*x - 4:\n", "z=x; z*=4; z-=4 \n", "print (x, y, z)\n", "```\n", "Izvršite ovaj kod. Objasnite zašto `x`, `y` i `z` imaju iste vrijednosti." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "- Vektorizirajte kod iz 3. zadatka za vježbu iz prvog predavanja. Vektorizirana funkcija treba za argumente primati funkciju `f` i broj `n`\n", " te dva niza `a,b` a vraćati niz brojeva koji odgovaraju aproksimaciji integrala $\\int_{a_i}^{b_i} f(x) dx$ trapeznom formulom." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "- Dana je funkcija\n", "$$\n", "f(x)=\\frac{n}{n+1}\\begin{cases}\n", "(1/2)^{1+1/n}-(1/2-x)^{1+1/n}, & 0\\le x\\le 1/2,\\\\\n", "(1/2)^{1+1/n}-(x-1/2)^{1+1/n}, & 1/2 < x\\le 1.\n", "\\end{cases}\n", "$$\n", "Ovdje je $n$ realan broj, $0 < n \\le 1$. \n", "Napišite vektoriziranu funkciju za računanje $f(x)$ na skupu $m$ ekvidistribuiranih brojeva između 0 i 1 (dakle, bez korištenja petlji).\n", "- Neka su $x_1,\\ldots,x_n$ uniformno distribuirani slučajni brojevi između $a$ i $b$. Tada se $\\int_a^b f(x)\\mathrm{d}\\,x$ može aproksimirati s \n", "$\\frac{b-a}{n} \\sum_{i=1}^n f(x_i)$, postupak koji se obično zove _Monte Carlo integracija_.\n", "Napišite funkciju koja kao ulazne varijable prima $a$, $b$, $f$ te $n$ s predefiniranom vrijednošću 1000, a vraća rezultat Monte Carlo integracije.\n", "I u ovom zadatku se ne smiju koristiti petlje." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Anaconda (Python 3)", "language": "python", "name": "anaconda3" }, "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.3" } }, "nbformat": 4, "nbformat_minor": 0 }