{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numpy: An efficient N-dimensional array package\n", "Gonzalo Rios - grios@dim.uchile.cl\n", "## A fast introduction" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:16:21.619821Z", "start_time": "2018-10-11T19:16:20.482692Z" } }, "outputs": [], "source": [ "import numpy as np\n", "help(np)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:16:44.745932Z", "start_time": "2018-10-11T19:16:44.724178Z" }, "scrolled": true }, "outputs": [], "source": [ "a = [i for i in range(100)]\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:16:53.692378Z", "start_time": "2018-10-11T19:16:53.681880Z" } }, "outputs": [], "source": [ "b = np.array(a)\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:17:00.356362Z", "start_time": "2018-10-11T19:17:00.346332Z" } }, "outputs": [], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:17:01.287630Z", "start_time": "2018-10-11T19:17:01.280102Z" } }, "outputs": [], "source": [ "type(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:17:10.938597Z", "start_time": "2018-10-11T19:17:10.931906Z" }, "scrolled": true }, "outputs": [], "source": [ "c = [i**2 for i in b]\n", "c" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:17:16.149779Z", "start_time": "2018-10-11T19:17:16.143996Z" } }, "outputs": [], "source": [ "c = b**2\n", "c" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:17:27.197989Z", "start_time": "2018-10-11T19:17:27.168659Z" } }, "outputs": [], "source": [ "np.empty(1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:17:38.512287Z", "start_time": "2018-10-11T19:17:38.501151Z" }, "scrolled": true }, "outputs": [], "source": [ "np.ones(1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:18:07.098398Z", "start_time": "2018-10-11T19:18:07.092165Z" } }, "outputs": [], "source": [ "a = np.ones(100)*10\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:18:43.962934Z", "start_time": "2018-10-11T19:18:43.956190Z" } }, "outputs": [], "source": [ "e = np.eye(1000)\n", "e" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:18:48.771862Z", "start_time": "2018-10-11T19:18:48.768790Z" } }, "outputs": [], "source": [ "len(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:18:49.851860Z", "start_time": "2018-10-11T19:18:49.849186Z" } }, "outputs": [], "source": [ "len(e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:19:05.922217Z", "start_time": "2018-10-11T19:19:05.912751Z" } }, "outputs": [], "source": [ "a.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:19:06.464927Z", "start_time": "2018-10-11T19:19:06.455087Z" } }, "outputs": [], "source": [ "e.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:20:05.740289Z", "start_time": "2018-10-11T19:20:05.736508Z" } }, "outputs": [], "source": [ "e.reshape(1000000,1).shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:20:14.458242Z", "start_time": "2018-10-11T19:20:14.426332Z" } }, "outputs": [], "source": [ "e.flatten()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:20:22.210532Z", "start_time": "2018-10-11T19:20:22.196260Z" } }, "outputs": [], "source": [ "g = np.eye(int(1e4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:21:43.519724Z", "start_time": "2018-10-11T19:21:43.407353Z" } }, "outputs": [], "source": [ "g.flatten()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:21:50.998848Z", "start_time": "2018-10-11T19:21:50.994417Z" } }, "outputs": [], "source": [ "e.ravel()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:21:56.035336Z", "start_time": "2018-10-11T19:21:56.031152Z" } }, "outputs": [], "source": [ "ef = e.reshape(10,10,10000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:22:09.314369Z", "start_time": "2018-10-11T19:22:09.286827Z" } }, "outputs": [], "source": [ "all(e.flatten() == ef.flatten())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:22:10.971469Z", "start_time": "2018-10-11T19:22:10.968243Z" } }, "outputs": [], "source": [ "ef.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:23:00.074655Z", "start_time": "2018-10-11T19:23:00.066473Z" }, "scrolled": true }, "outputs": [], "source": [ "ef[0].shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:23:01.975545Z", "start_time": "2018-10-11T19:23:01.967327Z" } }, "outputs": [], "source": [ "ef[0][0].shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:23:13.572062Z", "start_time": "2018-10-11T19:23:13.561502Z" } }, "outputs": [], "source": [ "ef[0][0][0:10:2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:23:28.460815Z", "start_time": "2018-10-11T19:23:28.451499Z" } }, "outputs": [], "source": [ "ef[0:5].shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:23:46.473568Z", "start_time": "2018-10-11T19:23:46.455663Z" } }, "outputs": [], "source": [ "g = ef[0:5][2:6]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:23:46.960451Z", "start_time": "2018-10-11T19:23:46.956267Z" } }, "outputs": [], "source": [ "g.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:24:25.791294Z", "start_time": "2018-10-11T19:24:25.782061Z" } }, "outputs": [], "source": [ "g = ef[0:5,2:6]\n", "g.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:25:08.841424Z", "start_time": "2018-10-11T19:25:08.831967Z" } }, "outputs": [], "source": [ "g[10:1000:2].shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:25:13.819582Z", "start_time": "2018-10-11T19:25:13.817010Z" } }, "outputs": [], "source": [ "gg = ef[0:5, 2:6, 10:1000:2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:25:16.452254Z", "start_time": "2018-10-11T19:25:16.444900Z" } }, "outputs": [], "source": [ "gg.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:25:23.206738Z", "start_time": "2018-10-11T19:25:23.201309Z" } }, "outputs": [], "source": [ "gg.size" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:25:38.742568Z", "start_time": "2018-10-11T19:25:38.740237Z" } }, "outputs": [], "source": [ "gh = gg.reshape(99,100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:25:39.187070Z", "start_time": "2018-10-11T19:25:39.182094Z" } }, "outputs": [], "source": [ "gh.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:25:41.921290Z", "start_time": "2018-10-11T19:25:41.918129Z" } }, "outputs": [], "source": [ "gh.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:26:03.079444Z", "start_time": "2018-10-11T19:26:03.073489Z" } }, "outputs": [], "source": [ "gh32 = np.float32(gh)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:26:03.391080Z", "start_time": "2018-10-11T19:26:03.385792Z" } }, "outputs": [], "source": [ "gh32.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:26:40.705096Z", "start_time": "2018-10-11T19:26:40.694397Z" } }, "outputs": [], "source": [ "np.float128(gh)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:03.970864Z", "start_time": "2018-10-11T19:28:03.937029Z" } }, "outputs": [], "source": [ "r = np.random.rand(1000000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:04.135197Z", "start_time": "2018-10-11T19:28:04.132552Z" } }, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:04.485887Z", "start_time": "2018-10-11T19:28:04.286175Z" } }, "outputs": [], "source": [ "rr = [random.random() for i in range(1000000)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:04.490504Z", "start_time": "2018-10-11T19:28:04.487387Z" } }, "outputs": [], "source": [ "len(r) == len(rr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:04.760828Z", "start_time": "2018-10-11T19:28:04.746505Z" } }, "outputs": [], "source": [ "sum(rr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:05.392966Z", "start_time": "2018-10-11T19:28:05.382160Z" } }, "outputs": [], "source": [ "np.sum(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:10.011989Z", "start_time": "2018-10-11T19:28:10.006492Z" } }, "outputs": [], "source": [ "R = r.reshape(1000,1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:19.814227Z", "start_time": "2018-10-11T19:28:19.811986Z" } }, "outputs": [], "source": [ "o = np.ones((1000,1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:26.083872Z", "start_time": "2018-10-11T19:28:26.078432Z" } }, "outputs": [], "source": [ "R.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:21.476599Z", "start_time": "2018-10-11T19:28:21.471538Z" } }, "outputs": [], "source": [ "o.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:45.553066Z", "start_time": "2018-10-11T19:28:45.536222Z" } }, "outputs": [], "source": [ "ro = R * o" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:28:53.433380Z", "start_time": "2018-10-11T19:28:53.429268Z" } }, "outputs": [], "source": [ "d = R.dot(o)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:29:03.016409Z", "start_time": "2018-10-11T19:29:03.010868Z" } }, "outputs": [], "source": [ "d = R @ o" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:29:06.224158Z", "start_time": "2018-10-11T19:29:06.219120Z" } }, "outputs": [], "source": [ "d.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:29:08.988512Z", "start_time": "2018-10-11T19:29:08.980262Z" } }, "outputs": [], "source": [ "ro.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:29:42.249288Z", "start_time": "2018-10-11T19:29:42.246013Z" } }, "outputs": [], "source": [ "np.arange(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:29:51.728013Z", "start_time": "2018-10-11T19:29:51.717961Z" } }, "outputs": [], "source": [ "np.ones((3,4))*np.arange(4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:30:02.851014Z", "start_time": "2018-10-11T19:30:02.846646Z" } }, "outputs": [], "source": [ "np.arange(4)*np.ones((3,4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:30:24.539874Z", "start_time": "2018-10-11T19:30:24.534165Z" } }, "outputs": [], "source": [ "np.arange(4)*np.ones((4,3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:31:11.896949Z", "start_time": "2018-10-11T19:31:11.892819Z" } }, "outputs": [], "source": [ "np.ones((3,4)) @ np.arange(4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:31:13.528508Z", "start_time": "2018-10-11T19:31:13.524884Z" } }, "outputs": [], "source": [ "np.ones((4,3)) @ np.arange(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:31:28.787580Z", "start_time": "2018-10-11T19:31:28.776404Z" } }, "outputs": [], "source": [ "np.ones((4,3)) @ np.eye(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:31:30.448211Z", "start_time": "2018-10-11T19:31:30.442473Z" } }, "outputs": [], "source": [ "np.ones((4,3)) * np.arange(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:31:40.935427Z", "start_time": "2018-10-11T19:31:40.929793Z" } }, "outputs": [], "source": [ "from numpy import linalg as nl" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:31:53.019853Z", "start_time": "2018-10-11T19:31:52.978370Z" } }, "outputs": [], "source": [ "(R**(-1)) @ R" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:32:05.831594Z", "start_time": "2018-10-11T19:32:05.694067Z" } }, "outputs": [], "source": [ "rr = nl.inv(R)\n", "rr @ R" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:32:18.956707Z", "start_time": "2018-10-11T19:32:18.897167Z" } }, "outputs": [], "source": [ "np.mean(np.abs(rr @ R - np.eye(len(R)))**2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:32:36.081895Z", "start_time": "2018-10-11T19:32:36.077269Z" } }, "outputs": [], "source": [ "np.sum(rr.dot(d))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:32:53.931705Z", "start_time": "2018-10-11T19:32:53.912075Z" } }, "outputs": [], "source": [ "rs = nl.solve(R, d)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:32:54.256436Z", "start_time": "2018-10-11T19:32:54.247510Z" } }, "outputs": [], "source": [ "np.sum(rs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:33:23.176078Z", "start_time": "2018-10-11T19:33:23.171418Z" } }, "outputs": [], "source": [ "R" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:33:26.095953Z", "start_time": "2018-10-11T19:33:26.086200Z" } }, "outputs": [], "source": [ "R.T" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:33:45.667354Z", "start_time": "2018-10-11T19:33:45.633126Z" } }, "outputs": [], "source": [ "nl.cholesky(R+R.T+100*np.eye(len(R)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:33:52.442303Z", "start_time": "2018-10-11T19:33:51.534967Z" } }, "outputs": [], "source": [ "eig, veig = nl.eig(R)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:33:53.848830Z", "start_time": "2018-10-11T19:33:53.841630Z" } }, "outputs": [], "source": [ "eig.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:33:54.464285Z", "start_time": "2018-10-11T19:33:54.456568Z" } }, "outputs": [], "source": [ "veig.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:33:55.234168Z", "start_time": "2018-10-11T19:33:55.184229Z" } }, "outputs": [], "source": [ "nl.eigh?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:34:14.495212Z", "start_time": "2018-10-11T19:34:14.358469Z" } }, "outputs": [], "source": [ "tt = nl.eigh(R)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:34:15.664464Z", "start_time": "2018-10-11T19:34:14.757102Z" } }, "outputs": [], "source": [ "ttt = nl.eig(R)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:34:29.585655Z", "start_time": "2018-10-11T19:34:29.580304Z" } }, "outputs": [], "source": [ "nl.norm(tt[0] - ttt[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:35:14.397659Z", "start_time": "2018-10-11T19:35:14.307378Z" } }, "outputs": [], "source": [ "nl.inv(rr) @ rr" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:35:22.604791Z", "start_time": "2018-10-11T19:35:22.586679Z" } }, "outputs": [], "source": [ "nl.det(R)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:35:34.455156Z", "start_time": "2018-10-11T19:35:34.426642Z" } }, "outputs": [], "source": [ "nl.det(rr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type and shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:37:19.549735Z", "start_time": "2018-10-11T19:37:19.544993Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "p = print\n", "\n", "\n", "def debug(a, s=None):\n", " if s is None:\n", " try:\n", " r = str(a) + \" : \" + str(type(a)) + \" \" + str(a.shape) + \"\\n\"\n", " except:\n", " try:\n", " r = str(a) + \" : \" + str(type(a)) + \" (\" + str(len(a)) + \")\\n\"\n", " except:\n", " r = str(a) + \" : \" + str(type(a)) + \"\\n\"\n", " else:\n", " try:\n", " r = str(a) + \": \" + str(type(s)) + \\\n", " str(s.shape) + \"\\n\" + str(s) + \"\\n\"\n", " except:\n", " try:\n", " r = str(a) + \": \" + str(type(s)) + \\\n", " \"(\" + str(len(s)) + \")\\n\" + str(s) + \"\\n\"\n", " except:\n", " r = str(a) + \": \" + str(type(s)) + \")\\n\" + str(s) + \"\\n\"\n", " print(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:37:25.900957Z", "start_time": "2018-10-11T19:37:25.896128Z" } }, "outputs": [], "source": [ "nums = [1, 2, 3, 4, 5, 6]\n", "debug(nums)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:37:42.943552Z", "start_time": "2018-10-11T19:37:42.937684Z" } }, "outputs": [], "source": [ "# ndarray\n", "np_nums = np.array(nums)\n", "debug(np_nums)\n", "debug(np_nums.ndim)\n", "debug(np_nums.shape)\n", "debug(np_nums.size)\n", "debug(np_nums.dtype)\n", "debug(np_nums.itemsize)\n", "debug(np_nums.data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:40:32.004906Z", "start_time": "2018-10-11T19:40:31.986664Z" } }, "outputs": [], "source": [ "# dtype float32\n", "np_nums = np.array(nums, dtype=np.float32)\n", "debug(np_nums)\n", "debug(np_nums.ndim)\n", "debug(np_nums.shape)\n", "debug(np_nums.size)\n", "debug(np_nums.dtype)\n", "debug(np_nums.itemsize)\n", "debug(np_nums.data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:41:30.649346Z", "start_time": "2018-10-11T19:41:30.635400Z" } }, "outputs": [], "source": [ "# shape 2-ndarray\n", "nums2 = np.array([[1., 2, 3], [4, 5, 6]])\n", "debug(nums2)\n", "debug(nums2.ndim)\n", "debug(nums2.shape)\n", "debug(nums2.size)\n", "debug(nums2.dtype)\n", "debug(nums2.itemsize)\n", "debug(nums2.data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creation of ndarray" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:44:52.281976Z", "start_time": "2018-10-11T19:44:52.266074Z" } }, "outputs": [], "source": [ "# Static Methods ndarray\n", "debug(np.zeros((3, 2)))\n", "debug(np.ones((1, 2)))\n", "debug(np.full((2, 2), 1.4))\n", "debug(np.eye(2))\n", "debug(np.empty((5, 5)))\n", "debug(np.random.random((2, 3, 4, 5)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slice and copy" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:46:12.485359Z", "start_time": "2018-10-11T19:46:12.479624Z" } }, "outputs": [], "source": [ "# slice of an array\n", "a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\n", "p(a)\n", "p(a[0, 1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:46:54.598623Z", "start_time": "2018-10-11T19:46:54.595581Z" } }, "outputs": [], "source": [ "b = a[:2, 1:3]\n", "p(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:47:20.333358Z", "start_time": "2018-10-11T19:47:20.326174Z" } }, "outputs": [], "source": [ "b[0, 0] = 77\n", "p(b)\n", "p(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:47:56.787864Z", "start_time": "2018-10-11T19:47:56.779858Z" } }, "outputs": [], "source": [ "b = a[:2, 1:3].copy()\n", "b[0, 0] = 88\n", "\n", "p(b)\n", "p(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:52:00.505438Z", "start_time": "2018-10-11T19:52:00.502521Z" } }, "outputs": [], "source": [ "a[[0, 2]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:52:01.658695Z", "start_time": "2018-10-11T19:52:01.647488Z" } }, "outputs": [], "source": [ "a[[0], [2]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:52:18.896104Z", "start_time": "2018-10-11T19:52:18.892022Z" } }, "outputs": [], "source": [ "a > 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:53:28.295517Z", "start_time": "2018-10-11T19:53:28.287517Z" } }, "outputs": [], "source": [ "a[a > 3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:53:35.544970Z", "start_time": "2018-10-11T19:53:35.541399Z" } }, "outputs": [], "source": [ "np.nonzero(a > 3) #np.where" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:54:25.214779Z", "start_time": "2018-10-11T19:54:25.211459Z" } }, "outputs": [], "source": [ "i1, i2 = np.nonzero(a > 3)\n", "a[i1, i2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:55:03.969947Z", "start_time": "2018-10-11T19:55:03.959201Z" } }, "outputs": [], "source": [ "#Memory, iterator\n", "debug(a)\n", "debug(a.data)\n", "debug(a.flat)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:56:13.101651Z", "start_time": "2018-10-11T19:56:13.093008Z" } }, "outputs": [], "source": [ "debug(b)\n", "debug(b.data)\n", "debug(b.flat)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:58:01.912108Z", "start_time": "2018-10-11T19:58:01.903039Z" } }, "outputs": [], "source": [ "for row in a:\n", " p(\"row\", row)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:59:30.615865Z", "start_time": "2018-10-11T19:59:30.610345Z" } }, "outputs": [], "source": [ "for row in a:\n", " for c in row:\n", " p(\"v\", c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T19:59:31.015653Z", "start_time": "2018-10-11T19:59:31.007486Z" } }, "outputs": [], "source": [ "for v in a.flat:\n", " p(\"flat\", v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arange and Reshape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:00:24.793068Z", "start_time": "2018-10-11T20:00:24.784562Z" } }, "outputs": [], "source": [ "list(range(0,12,1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:00:32.172332Z", "start_time": "2018-10-11T20:00:32.169555Z" } }, "outputs": [], "source": [ "#arange , reshape\n", "nums = np.arange(0, 12, 1)\n", "debug(nums)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:00:43.090078Z", "start_time": "2018-10-11T20:00:43.080313Z" } }, "outputs": [], "source": [ "np.arange(0, 3, 1) == np.array([0, 1, 2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:01:01.678680Z", "start_time": "2018-10-11T20:01:01.674847Z" } }, "outputs": [], "source": [ "nums = nums.reshape(6, 2)\n", "p(nums)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:01:02.838446Z", "start_time": "2018-10-11T20:01:02.835258Z" } }, "outputs": [], "source": [ "nums = nums.reshape(3, 2, 2)\n", "p(nums)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:01:14.515207Z", "start_time": "2018-10-11T20:01:14.510682Z" } }, "outputs": [], "source": [ "np.arange(10000).reshape(100, 100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions and Operations" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:01:59.372763Z", "start_time": "2018-10-11T20:01:59.365304Z" } }, "outputs": [], "source": [ "# Basic Operations\n", "A = np.array([[0, 1], [0, 2]])\n", "B = np.array([[2, 0], [3, 4]])\n", "\n", "p(A, \"\\n\")\n", "p(B, \"\\n\")\n", "p(B.T, \"\\n\")\n", "p(A + B, \"\\n\")\n", "p(A * B, \"\\n\")\n", "p(A**B, \"\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:02:55.707388Z", "start_time": "2018-10-11T20:02:55.693331Z" } }, "outputs": [], "source": [ "# Universal Functions\n", "debug(\"transpose\", np.transpose(B))\n", "debug(\"add\", np.add(A, B))\n", "debug(\"multiply\", np.multiply(A, B))\n", "debug(\"power\", np.power(A, B))\n", "debug(\"exp\", np.exp(B))\n", "debug(\"sqrt\", np.sqrt(B))\n", "debug(\"sin\", np.sin(B))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:04:24.371579Z", "start_time": "2018-10-11T20:04:24.368332Z" } }, "outputs": [], "source": [ "A" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:04:14.504691Z", "start_time": "2018-10-11T20:04:14.495922Z" } }, "outputs": [], "source": [ "debug(\"A.sum()\", A.sum())\n", "debug(\"A.max()\", A.max())\n", "debug(\"A.argmax()\", A.argmax())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:05:43.928690Z", "start_time": "2018-10-11T20:05:43.921186Z" } }, "outputs": [], "source": [ "A" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:05:03.395739Z", "start_time": "2018-10-11T20:05:03.388852Z" } }, "outputs": [], "source": [ "debug(\"A.min(axis=0)\", A.min(axis=0))\n", "debug(\"A.min(axis=1)\", A.min(axis=1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:07:13.523547Z", "start_time": "2018-10-11T20:07:13.513309Z" } }, "outputs": [], "source": [ "B" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:07:41.901925Z", "start_time": "2018-10-11T20:07:41.898569Z" } }, "outputs": [], "source": [ "debug(\"B.cumsum(axis=0)\", B.cumsum(axis=0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:08:00.854892Z", "start_time": "2018-10-11T20:08:00.847309Z" } }, "outputs": [], "source": [ "debug(\"B.cumsum(axis=1)\", B.cumsum(axis=1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:10:41.696573Z", "start_time": "2018-10-11T20:10:41.686353Z" } }, "outputs": [], "source": [ "At = np.arange(24).reshape(2,3,4)\n", "At" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:10:44.632617Z", "start_time": "2018-10-11T20:10:44.623923Z" } }, "outputs": [], "source": [ "At.max(axis=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:10:47.054851Z", "start_time": "2018-10-11T20:10:47.045917Z" } }, "outputs": [], "source": [ "At.max(axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:14:32.043547Z", "start_time": "2018-10-11T20:14:32.040150Z" } }, "outputs": [], "source": [ "At.max(axis=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:12:56.122641Z", "start_time": "2018-10-11T20:12:56.118518Z" } }, "outputs": [], "source": [ "debug(\"A.dot(B)\", A.dot(B))\n", "debug(\"np.dot(A,B)\", np.dot(A, B))\n", "\n", "debug(\"B.dot(A)\", B.dot(A))\n", "debug(\"np.dot(B,A)\", np.dot(B, A))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stacking" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:17:25.230908Z", "start_time": "2018-10-11T20:17:25.220619Z" } }, "outputs": [], "source": [ "def see(s):\n", " exec(\"debug(\\\"\" + s + \"\\\",\" + s + \")\")\n", "\n", "\n", "a = np.floor(10 * np.random.random((3, 2)))\n", "b = np.floor(10 * np.random.random((3, 2)))\n", "\n", "see(\"a\")\n", "see(\"b\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:17:48.206117Z", "start_time": "2018-10-11T20:17:48.198842Z" } }, "outputs": [], "source": [ "see(\"np.array([a,b])\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:19:15.561664Z", "start_time": "2018-10-11T20:19:15.557126Z" } }, "outputs": [], "source": [ "see(\"np.r_[a,b]\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:19:42.325460Z", "start_time": "2018-10-11T20:19:42.317636Z" } }, "outputs": [], "source": [ "see(\"np.vstack((a,b))\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:20:09.617593Z", "start_time": "2018-10-11T20:20:09.609734Z" } }, "outputs": [], "source": [ "see(\"np.c_[a,b]\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:20:22.096854Z", "start_time": "2018-10-11T20:20:22.087536Z" } }, "outputs": [], "source": [ "see(\"np.hstack((a,b))\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:22:46.471107Z", "start_time": "2018-10-11T20:22:46.467645Z" } }, "outputs": [], "source": [ "see(\"np.hsplit(a,1)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:22:51.261951Z", "start_time": "2018-10-11T20:22:51.259012Z" } }, "outputs": [], "source": [ "see(\"np.hsplit(a,1)[0]\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:22:59.728058Z", "start_time": "2018-10-11T20:22:59.720724Z" } }, "outputs": [], "source": [ "see(\"np.hsplit(a,2)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:23:23.798225Z", "start_time": "2018-10-11T20:23:23.793754Z" } }, "outputs": [], "source": [ "see(\"np.hsplit(a,2)[0]\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:23:32.034111Z", "start_time": "2018-10-11T20:23:32.025410Z" } }, "outputs": [], "source": [ "see(\"np.hsplit(a.T,1)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:23:34.737860Z", "start_time": "2018-10-11T20:23:34.734666Z" } }, "outputs": [], "source": [ "see(\"np.hsplit(a.T,3)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:23:59.316843Z", "start_time": "2018-10-11T20:23:59.311683Z" } }, "outputs": [], "source": [ "np.hsplit(a.T,3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:23:56.079399Z", "start_time": "2018-10-11T20:23:56.073451Z" } }, "outputs": [], "source": [ "np.hsplit(a.T,3)[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:24:46.944106Z", "start_time": "2018-10-11T20:24:46.938574Z" } }, "outputs": [], "source": [ "see(\"np.hsplit(a.T,3)[0]\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:24:48.790259Z", "start_time": "2018-10-11T20:24:48.786474Z" } }, "outputs": [], "source": [ "see(\"np.vsplit(a,3)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:24:49.876853Z", "start_time": "2018-10-11T20:24:49.873787Z" } }, "outputs": [], "source": [ "see(\"np.vsplit(a.T,2)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linear Algebra" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:25:46.595956Z", "start_time": "2018-10-11T20:25:46.585681Z" } }, "outputs": [], "source": [ "np.linalg.norm?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:27:29.157348Z", "start_time": "2018-10-11T20:27:29.150128Z" } }, "outputs": [], "source": [ "a = np.array([[1.0, 2.0], [3.0, 4.0]])\n", "see(\"a\")\n", "see(\"a.transpose()\")\n", "see(\"np.trace(a)\")\n", "see(\"np.linalg.norm(a)\")\n", "see(\"np.linalg.det(a)\")\n", "see(\"np.linalg.inv(a)\")\n", "see(\"np.linalg.eig(a)\")\n", "see(\"np.linalg.eigvals(a)\")\n", "see(\"np.linalg.cholesky(a.dot(a.T))\")\n", "see(\"np.linalg.matrix_power(a, 2)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:27:40.612540Z", "start_time": "2018-10-11T20:27:40.607128Z" } }, "outputs": [], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:27:43.972686Z", "start_time": "2018-10-11T20:27:43.969292Z" } }, "outputs": [], "source": [ "a[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:28:03.208151Z", "start_time": "2018-10-11T20:28:03.203160Z" } }, "outputs": [], "source": [ "see(\"a[0]\")\n", "see(\"a[1]\")\n", "see(\"np.inner(a[0],a[1])\")\n", "see(\"np.outer(a[0],a[1])\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:28:41.628352Z", "start_time": "2018-10-11T20:28:41.623514Z" } }, "outputs": [], "source": [ "b = np.array([[5.], [7.]])\n", "see(\"b\")\n", "see(\"np.linalg.inv(a).dot(b)\")\n", "see(\"np.linalg.solve(a, b)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other Submodules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:28:49.955233Z", "start_time": "2018-10-11T20:28:49.941655Z" } }, "outputs": [], "source": [ "np.fft.fftn(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:28:54.105814Z", "start_time": "2018-10-11T20:28:54.102289Z" } }, "outputs": [], "source": [ "np.polynomial.Polynomial([1, 2, 3, 4])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:29:06.275719Z", "start_time": "2018-10-11T20:29:06.271142Z" } }, "outputs": [], "source": [ "np.polynomial.Polynomial([1, 2, 3, 4]).roots()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:29:14.396051Z", "start_time": "2018-10-11T20:29:14.392403Z" } }, "outputs": [], "source": [ "np.union1d([1, 2, 3, 4], [2, 3, 5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:29:32.047840Z", "start_time": "2018-10-11T20:29:32.042855Z" } }, "outputs": [], "source": [ "np.random.seed(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:29:41.118141Z", "start_time": "2018-10-11T20:29:41.109082Z" } }, "outputs": [], "source": [ "np.random.permutation([1, 2, 3, 4])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:30:20.939942Z", "start_time": "2018-10-11T20:30:20.935982Z" } }, "outputs": [], "source": [ "np.random.normal(1, 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:30:10.870124Z", "start_time": "2018-10-11T20:30:10.864583Z" } }, "outputs": [], "source": [ "np.random.randn(1, 20, 3).shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:30:51.546079Z", "start_time": "2018-10-11T20:30:51.535105Z" } }, "outputs": [], "source": [ "np.sort(np.random.randn(1, 20, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:31:07.145692Z", "start_time": "2018-10-11T20:31:07.136176Z" } }, "outputs": [], "source": [ "np.argmax([1, 5, 3, 4])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:31:07.872951Z", "start_time": "2018-10-11T20:31:07.863610Z" } }, "outputs": [], "source": [ "np.max([1, 5, 3, 4])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:31:12.599913Z", "start_time": "2018-10-11T20:31:12.591472Z" } }, "outputs": [], "source": [ "np.mean(np.random.randn(1, 20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:31:13.176918Z", "start_time": "2018-10-11T20:31:13.168001Z" } }, "outputs": [], "source": [ "np.maximum(a , b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Save and Load" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:33:06.912988Z", "start_time": "2018-10-11T20:33:06.905243Z" } }, "outputs": [], "source": [ "np.save('numpy_array.npy', (np.random.rand(1000),np.random.rand(1000)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:33:13.057597Z", "start_time": "2018-10-11T20:33:13.048007Z" } }, "outputs": [], "source": [ "np.load('numpy_array.npy').shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T20:32:12.223714Z", "start_time": "2018-10-11T20:32:12.202097Z" } }, "outputs": [], "source": [ "np.loadtxt('data.txt')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }