{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using `numpy`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import itertools as it\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic array properties" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.random.normal(0, 1, (3,3))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.0512308 , -0.71167095, 1.33249074],\n", " [ 1.09373726, -0.95574896, 0.74410462],\n", " [-0.15179047, 0.59365203, -0.47439611]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.shape" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creation of arrays" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,2,3])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 2., 3.],\n", " [ 4., 5., 6.]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[1,2,3],[4,5,6]], dtype='float')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((2,3))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1.],\n", " [ 1., 1., 1.]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((2,3))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1.],\n", " [ 1., 1., 1.]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.empty((2, 3))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 0.],\n", " [ 0., 1., 0.],\n", " [ 0., 0., 1.]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(3)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 1., 0.],\n", " [ 0., 0., 1.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(3, k=1)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0.],\n", " [ 1., 0., 0.],\n", " [ 0., 1., 0.]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye(3, k=-1)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 0, 0],\n", " [0, 3, 0],\n", " [0, 0, 4]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.diag([2,3,4])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "A = np.arange(10)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.25, 0.5 , 0.75, 1. ])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(0, 1, 5)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.00000000e+00, 1.00000000e+01, 1.00000000e+02,\n", " 1.00000000e+03, 1.00000000e+04])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.logspace(0, 4, 5)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros_like(A)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones_like(A)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-8070450532247928832, -8070450532247928832, 30,\n", " 0, 0, 0,\n", " 0, 0, 0,\n", " 0])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.empty_like(A)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3., 1., 2., 3., 1., 2., 3., 1.])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.fromiter(it.cycle([1,2,3]), dtype='float', count=10)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def fib():\n", " a, b = 0, 1\n", " while True:\n", " yield b\n", " a, b = b, a+b" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.fromiter(fib(), dtype='int', count=10)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-4, -3, -2, -1, 0],\n", " [-3, -2, -1, 0, 1],\n", " [-2, -1, 0, 1, 2],\n", " [-1, 0, 1, 2, 3],\n", " [ 0, 1, 2, 3, 4]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.fromfunction(lambda i, j: i-2 + j-2, shape=(5,5), dtype='int')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.fromstring('1-2-3', dtype='int', sep='-')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting christmas.txt\n" ] } ], "source": [ "%%file christmas.txt\n", "4 Calling Birds\n", "5 Gold Rings\n", "6 Geese a-Laying\n", "7 Swans a-Swimming\n", "8 Maids a-Milking\n", "9 Ladies Dancing\n", "10 Lords a-Leaping\n", "11 Pipers Piping\n", "12 Drummers Drumming" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pattern = r'(\\d+)\\s+(.*)'\n", "swag = np.fromregex('christmas.txt', pattern, [('num', np.int64), ('gift', object)])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(12, b'Drummers Drumming')" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "swag[-1]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 4, 5, 6, 7, 8, 9, 10, 11, 12])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "swag['num']" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([b'Calling Birds', b'Gold Rings', b'Geese a-Laying',\n", " b'Swans a-Swimming', b'Maids a-Milking', b'Ladies Dancing',\n", " b'Lords a-Leaping', b'Pipers Piping', b'Drummers Drumming'], dtype=object)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "swag['gift']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.0512308 , -0.71167095, 1.33249074],\n", " [ 1.09373726, -0.95574896, 0.74410462],\n", " [-0.15179047, 0.59365203, -0.47439611]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.0512308 , -0.71167095, 1.33249074])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[0]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.15179047, 0.59365203, -0.47439611])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[-1]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.0512308 , 1.09373726, -0.15179047])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[:,0]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.71167095, 1.33249074],\n", " [-0.95574896, 0.74410462]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[:2, 1:]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.0512308 , -0.71167095, 1.33249074],\n", " [-0.15179047, 0.59365203, -0.47439611]])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[::2]" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.0512308 , 1.33249074],\n", " [-0.15179047, -0.47439611]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[::2, ::2]" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.15179047, 0.59365203, -0.47439611],\n", " [ 1.09373726, -0.95574896, 0.74410462],\n", " [ 0.0512308 , -0.71167095, 1.33249074]])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[::-1]" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1.33249074, -0.71167095, 0.0512308 ],\n", " [ 0.74410462, -0.95574896, 1.09373726],\n", " [-0.47439611, 0.59365203, -0.15179047]])" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[:, ::-1]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.0512308 , -0.71167095, 1.33249074],\n", " [-0.15179047, 0.59365203, -0.47439611]])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[[0,2],:]" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.0512308 , 1.33249074],\n", " [ 1.09373726, 0.74410462],\n", " [-0.15179047, -0.47439611]])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[:, [0,2]]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.0512308 , -0.47439611])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[[0,2], [0,2]]" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.0512308 , 1.33249074],\n", " [-0.15179047, -0.47439611]])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[np.ix_([0,2], [0,2])]" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0. , 0. , 1.33249074],\n", " [ 1.09373726, 0. , 0.74410462],\n", " [ 0. , 0.59365203, 0. ]])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(x > 0.5, x, 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strides" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.arange(4, dtype=np.int8)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1,)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.strides" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.arange(4, dtype=np.int16)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2,)" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.strides" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.arange(4, dtype=int)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(8,)" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.strides" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 2])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.lib.stride_tricks.as_strided(x, shape=(2,), strides=(16,))" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3],\n", " [0, 1, 2, 3],\n", " [0, 1, 2, 3],\n", " [0, 1, 2, 3]])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.lib.stride_tricks.as_strided(x, shape=(4,4), strides=(0,8))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vectorization and ufuncs" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.arange(1,11, dtype='int')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + 1" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + x" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x**2" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.cumsum()" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.69314718, 1.09861229, 1.38629436, 1.60943791,\n", " 1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.log(x)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.30103 , 0.47712125, 0.60205999, 0.69897 ,\n", " 0.77815125, 0.84509804, 0.90308999, 0.95424251, 1. ])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.log10(x)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2., 4., 8., 16., 32., 64., 128., 256.,\n", " 512., 1024.])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.exp2(x)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.expm1(np.log1p(x))" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 3, 3, 4, 5, 6, 7, 8, 8, 8])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.clip(x, 3, 8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can pseudo-vectorize custom functions with the `vectorize` decorator.\n", "\n", "Note: This runs at the speed of a for loop. " ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": true }, "outputs": [], "source": [ "myfunc = np.vectorize(lambda x: x + 10)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfunc(x)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "33.1 µs ± 1.08 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%timeit -n 1000 myfunc(x)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.46 µs ± 482 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%timeit -n 1000 x + 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reshaping" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10]])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.reshape((2,5))" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2],\n", " [ 3, 4],\n", " [ 5, 6],\n", " [ 7, 8],\n", " [ 9, 10]])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.reshape((5,-1))" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10]])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.reshape((-1,5))" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1],\n", " [ 2],\n", " [ 3],\n", " [ 4],\n", " [ 5],\n", " [ 6],\n", " [ 7],\n", " [ 8],\n", " [ 9],\n", " [10]])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.reshape((10,-1))" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = np.arange(16).reshape((4,4))" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11],\n", " [12, 13, 14, 15]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.ravel()" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15])" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.ravel(order='F')" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": true }, "outputs": [], "source": [ "z = np.ones((1,5,1), 'int')" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1],\n", " [1],\n", " [1],\n", " [1],\n", " [1]]])" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 5, 1)" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.shape" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 1, 1, 1])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.squeeze()" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5,)" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.squeeze().shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Broadcasting" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = np.arange(12).reshape((3,4))" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]])" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4],\n", " [ 5, 6, 7, 8],\n", " [ 9, 10, 11, 12]])" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y + 1" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 4, 8, 12],\n", " [16, 20, 24, 28],\n", " [32, 36, 40, 44]])" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y + 3*y" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": true }, "outputs": [], "source": [ "c = np.arange(4)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 4)" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.shape" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4,)" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.shape" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4, 6],\n", " [ 4, 6, 8, 10],\n", " [ 8, 10, 12, 14]])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y + c" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "collapsed": true }, "outputs": [], "source": [ "r = np.arange(3)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.shape" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 1)" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r[:, None].shape" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 5, 6, 7, 8],\n", " [10, 11, 12, 13]])" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y + r[:, None]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }