{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### import numpy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### check python and numpy version" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python version: 3.6.6\n", "Numpy version: 1.15.0\n" ] } ], "source": [ "import platform\n", "print('Python version: ' + platform.python_version())\n", "print('Numpy version: ' + np.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 0. Numpy Data Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A list of Numpy Data Types" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TypeType Code
0int8i1
1uint8u1
2int16i2
3uint16u2
4int32i4
5uint32u4
6int64i8
7uint64u8
8float16f2
9float32f4 or f
10float64f8 or d
11float128f16 or g
12complex64c8
13complex128c16
14bool
15objectO
16string_S
17unicode_U
\n", "
" ], "text/plain": [ " Type Type Code\n", "0 int8 i1\n", "1 uint8 u1\n", "2 int16 i2\n", "3 uint16 u2\n", "4 int32 i4\n", "5 uint32 u4\n", "6 int64 i8\n", "7 uint64 u8\n", "8 float16 f2\n", "9 float32 f4 or f\n", "10 float64 f8 or d\n", "11 float128 f16 or g\n", "12 complex64 c8\n", "13 complex128 c16\n", "14 bool \n", "15 object O\n", "16 string_ S\n", "17 unicode_ U" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "dtypes = pd.DataFrame(\n", " {\n", " 'Type': ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'float16', 'float32', 'float64', 'float128', 'complex64', 'complex128', 'bool', 'object', 'string_', 'unicode_'],\n", " 'Type Code': ['i1', 'u1', 'i2', 'u2', 'i4', 'u4', 'i8', 'u8', 'f2', 'f4 or f', 'f8 or d', 'f16 or g', 'c8', 'c16', '', 'O', 'S', 'U']\n", " }\n", ")\n", "\n", "dtypes" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 2. 3.]\n", "float32\n", "[1.+2.j 3.-4.j]\n", "complex64\n", "[False True True]\n", "bool\n" ] } ], "source": [ "# create an array with a specified data type\n", "arr = np.array([1,2,3], dtype='f4')\n", "print(arr)\n", "print(arr.dtype)\n", "\n", "arr = np.array([1+2j, 3-4j], dtype=np.complex64)\n", "print(arr)\n", "print(arr.dtype)\n", "\n", "arr = np.array([0, 1, 1], dtype=np.bool)\n", "print(arr)\n", "print(arr.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### string data type" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[b'abc' b'def']\n", "|S3\n" ] } ], "source": [ "# set the max length of the string using S + some number, such as 'S3'\n", "# any string longer than the max length will be truncated\n", "s = np.array(['abc', 'defg'], dtype='S3')\n", "print(s)\n", "print(s.dtype)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|S3\n", "4)]) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### fancy indexing" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(10,10)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.71525681 0.76243827 0.63775096]\n" ] } ], "source": [ "# select arr[3,3], arr[1,2], arr[2,1]\n", "print(arr[[3,1,2], [3,2,1]]) " ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.28141752 0.62828149 0.07751835]\n", " [0.67523336 0.35299237 0.45959692]\n", " [0.53945097 0.83891327 0.56634674]]\n" ] } ], "source": [ "# select rows 3,1,2 and columns 6,4,8 \n", "print(arr[[3,1,2]][:, [6,4,8]]) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### dimension inference" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4, 4)\n" ] } ], "source": [ "# dimension inference using any negative number (usually -1)\n", "arr = np.array(range(16)).reshape((4,-1))\n", "print(arr.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### find elements/indices by conditions" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [], "source": [ "arr = np.arange(16).reshape(4,4)" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 6 7 8 9 10 11 12 13 14 15]\n" ] } ], "source": [ "# find the elements greater than 5 and return a flattened array\n", "print(arr[arr>5]) # or arr[np.where(arr>5)]" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[10 10 10 10]\n", " [10 10 -1 -1]\n", " [-1 -1 -1 -1]\n", " [-1 -1 -1 -1]]\n" ] } ], "source": [ "# return values based on conditions \n", "# np.where(condition, true_return, false_return)\n", "print(np.where(arr>5, -1, 10))" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2]\n", " [1 3]\n", " [2 0]\n", " [2 1]\n", " [2 2]\n", " [2 3]\n", " [3 0]\n", " [3 1]\n", " [3 2]\n", " [3 3]]\n" ] } ], "source": [ "# find the indices of the elements on conditions\n", "print(np.argwhere(arr>5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 6. Sort an Array" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(5,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sort an array along a specified axis" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.11702945 0.17913374 0.06753657 0.02945699 0.05396756]\n", " [0.13630925 0.18249983 0.41165849 0.12910116 0.31981183]\n", " [0.36056504 0.51866089 0.42053876 0.56232065 0.37163583]\n", " [0.44498496 0.62847058 0.42945962 0.70682921 0.43148605]\n", " [0.7740198 0.74238635 0.55147061 0.86890785 0.75572196]]\n" ] } ], "source": [ "# sort along the row and return a copy\n", "print(np.sort(arr, axis=0)) " ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.11702945 0.17913374 0.06753657 0.02945699 0.05396756]\n", " [0.13630925 0.18249983 0.41165849 0.12910116 0.31981183]\n", " [0.36056504 0.51866089 0.42053876 0.56232065 0.37163583]\n", " [0.44498496 0.62847058 0.42945962 0.70682921 0.43148605]\n", " [0.7740198 0.74238635 0.55147061 0.86890785 0.75572196]]\n" ] } ], "source": [ "# sort along the row in place\n", "arr.sort(axis=0)\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.02945699 0.05396756 0.06753657 0.11702945 0.17913374]\n", " [0.12910116 0.13630925 0.18249983 0.31981183 0.41165849]\n", " [0.36056504 0.37163583 0.42053876 0.51866089 0.56232065]\n", " [0.42945962 0.43148605 0.44498496 0.62847058 0.70682921]\n", " [0.55147061 0.74238635 0.75572196 0.7740198 0.86890785]]\n" ] } ], "source": [ "# sort along the column and return a copy\n", "print(np.sort(arr, axis=1)) " ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.02945699 0.05396756 0.06753657 0.11702945 0.17913374]\n", " [0.12910116 0.13630925 0.18249983 0.31981183 0.41165849]\n", " [0.36056504 0.37163583 0.42053876 0.51866089 0.56232065]\n", " [0.42945962 0.43148605 0.44498496 0.62847058 0.70682921]\n", " [0.55147061 0.74238635 0.75572196 0.7740198 0.86890785]]\n" ] } ], "source": [ "# sort along the column in place\n", "arr.sort(axis=1) \n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the indices that would sort an array along a specified axis" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(5,5)" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 0 0 4 3]\n", " [3 2 2 1 0]\n", " [4 4 3 0 1]\n", " [2 1 1 2 4]\n", " [0 3 4 3 2]]\n" ] } ], "source": [ "# along the row\n", "print(np.argsort(arr, axis=0))" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 4 3 0]\n", " [0 3 1 4 2]\n", " [1 2 0 3 4]\n", " [0 4 2 1 3]\n", " [3 0 1 4 2]]\n" ] } ], "source": [ "# along the column\n", "print(np.argsort(arr, axis=1))" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 5 23 1 15 2 20 19 11 8 12 17 4 3 21 6 9 24 10 16 13 14 7 22 18\n", " 0]\n" ] } ], "source": [ "# if axis=None, return the indices of a flattened array\n", "print(np.argsort(arr, axis=None))" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(3,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 7. Manipulate an Array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### transpose an array" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.80614361 0.61240133 0.73322903]\n", " [0.86728799 0.65205696 0.13585084]\n", " [0.08910079 0.05760322 0.56708195]\n", " [0.67553447 0.38582245 0.02315444]]\n", "[[0.80614361 0.61240133 0.73322903]\n", " [0.86728799 0.65205696 0.13585084]\n", " [0.08910079 0.05760322 0.56708195]\n", " [0.67553447 0.38582245 0.02315444]]\n", "[[0.80614361 0.61240133 0.73322903]\n", " [0.86728799 0.65205696 0.13585084]\n", " [0.08910079 0.05760322 0.56708195]\n", " [0.67553447 0.38582245 0.02315444]]\n" ] } ], "source": [ "# the following methods return a copy\n", "print(arr.T)\n", "# or \n", "print(np.transpose(arr))\n", "# or\n", "print(arr.transpose())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### transpose of a high dimensional array with specified order of axes" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[ 0 1 2 3]\n", " [ 4 5 6 7]]\n", "\n", " [[ 8 9 10 11]\n", " [12 13 14 15]]]\n", "[[[ 0 1 2 3]\n", " [ 4 5 6 7]]\n", "\n", " [[ 8 9 10 11]\n", " [12 13 14 15]]]\n" ] } ], "source": [ "arr1 = np.arange(16).reshape((2,2,4))\n", "print(arr1)\n", "\n", "arr1.transpose((1,0,2))\n", "print(arr1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### swap axes" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[ 0 4]\n", " [ 1 5]\n", " [ 2 6]\n", " [ 3 7]]\n", "\n", " [[ 8 12]\n", " [ 9 13]\n", " [10 14]\n", " [11 15]]]\n" ] } ], "source": [ "arr1 = np.arange(16).reshape((2,2,4))\n", "print(arr1.swapaxes(1,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### change the shape of an array" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.80614361, 0.86728799, 0.08910079, 0.67553447, 0.61240133,\n", " 0.65205696],\n", " [0.05760322, 0.38582245, 0.73322903, 0.13585084, 0.56708195,\n", " 0.02315444]])" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# change the shape of an array and return a copy\n", "arr.reshape((2,6))" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [], "source": [ "# change the shape of an array in place\n", "arr.resize((2,6))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### flatten an array" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.80614361, 0.86728799, 0.08910079, 0.67553447, 0.61240133,\n", " 0.65205696, 0.05760322, 0.38582245, 0.73322903, 0.13585084,\n", " 0.56708195, 0.02315444])" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# return a copy\n", "arr.flatten() " ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.80614361, 0.86728799, 0.08910079, 0.67553447, 0.61240133,\n", " 0.65205696, 0.05760322, 0.38582245, 0.73322903, 0.13585084,\n", " 0.56708195, 0.02315444])" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# return a view\n", "# change any element in the view will change the initial array\n", "arr.ravel() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### append elements to an array" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [], "source": [ "arr = np.array([1,2,3])" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4]\n" ] } ], "source": [ "# append a scalar and return a copy\n", "arr1 = np.append(arr, 4) \n", "print(arr1)" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "# append an array and return a copy\n", "arr2 = np.append(arr, [4,5,6]) \n", "print(arr2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### insert elements into an array" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100 1 2 3]\n" ] } ], "source": [ "# np.insert(array, position, element)\n", "\n", "# insert a scalar at a certain position\n", "arr3 = np.insert(arr, 0, 100) \n", "print(arr3)" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 1 2 3]\n" ] } ], "source": [ "# insert multiple values at a certain position\n", "arr3 = np.insert(arr, 0, [1,2,3]) \n", "print(arr3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### delete elements from an array" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 3]\n" ] } ], "source": [ "# remove the element at position 0\n", "arr4 = np.delete(arr, 0) \n", "print(arr4)" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2]\n" ] } ], "source": [ "# remove the element at multiple positions\n", "arr4 = np.delete(arr, [0,2]) \n", "print(arr4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### copy an array" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [], "source": [ "arr = np.array([1,2,3])" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [], "source": [ "# the following methods are all deep copy\n", "arr1 = np.copy(arr)\n", "# or \n", "arr1 = arr.copy()\n", "# or \n", "arr1 = np.array(arr, copy=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 8. Combine & Split an Array" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([[1,2,3,4], [1,2,3,4]])\n", "arr2 = np.array([[5,6,7,8], [5,6,7,8]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```np.concatenate((a, b), axis=0)```" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4]\n", " [1 2 3 4]\n", " [5 6 7 8]\n", " [5 6 7 8]]\n" ] } ], "source": [ "# concat along the row\n", "cat = np.concatenate((arr1, arr2), axis=0) \n", "print(cat)" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4 5 6 7 8]\n", " [1 2 3 4 5 6 7 8]]\n" ] } ], "source": [ "# concat along the column\n", "cat = np.concatenate((arr1, arr2), axis=1) \n", "print(cat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```np.vstack((a, b))``` \n", "### ```np.r_[a, b]```" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4]\n", " [1 2 3 4]\n", " [5 6 7 8]\n", " [5 6 7 8]]\n" ] } ], "source": [ "# stack arrays vertically\n", "cat = np.vstack((arr1, arr2))\n", "print(cat)" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4]\n", " [1 2 3 4]\n", " [5 6 7 8]\n", " [5 6 7 8]]\n" ] } ], "source": [ "# stack arrays vertically\n", "cat = np.r_[arr1, arr2]\n", "print(cat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```np.hstack((a, b))```\n", "### ```np.c_[a, b]```" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4 5 6 7 8]\n", " [1 2 3 4 5 6 7 8]]\n" ] } ], "source": [ "# stack arrays horizontally\n", "cat = np.hstack((arr1, arr2))\n", "print(cat)" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4 5 6 7 8]\n", " [1 2 3 4 5 6 7 8]]\n" ] } ], "source": [ "# stack arrays horizontally\n", "cat = np.c_[arr1, arr2]\n", "print(cat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### split an array " ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(6,6)" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[array([[0.0174567 , 0.47275939, 0.84410265, 0.88709809, 0.30063103,\n", " 0.938766 ],\n", " [0.58832418, 0.09871477, 0.09634253, 0.56561105, 0.75673868,\n", " 0.13910753],\n", " [0.37247418, 0.98843246, 0.90281137, 0.35097407, 0.48843214,\n", " 0.60597433]]), array([[0.00713459, 0.06267544, 0.65672272, 0.15912001, 0.91006063,\n", " 0.3950984 ],\n", " [0.46940726, 0.60966449, 0.82131635, 0.48112601, 0.24688737,\n", " 0.40234706],\n", " [0.20452672, 0.77063506, 0.11895435, 0.98447605, 0.25656521,\n", " 0.39604327]])]\n" ] } ], "source": [ "# split the array vertically into n evenly spaced chunks\n", "arr1 = np.vsplit(arr, 2)\n", "print(arr1)" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[array([[0.0174567 , 0.47275939, 0.84410265],\n", " [0.58832418, 0.09871477, 0.09634253],\n", " [0.37247418, 0.98843246, 0.90281137],\n", " [0.00713459, 0.06267544, 0.65672272],\n", " [0.46940726, 0.60966449, 0.82131635],\n", " [0.20452672, 0.77063506, 0.11895435]]), array([[0.88709809, 0.30063103, 0.938766 ],\n", " [0.56561105, 0.75673868, 0.13910753],\n", " [0.35097407, 0.48843214, 0.60597433],\n", " [0.15912001, 0.91006063, 0.3950984 ],\n", " [0.48112601, 0.24688737, 0.40234706],\n", " [0.98447605, 0.25656521, 0.39604327]])]\n" ] } ], "source": [ "# split the array horizontally into n evenly spaced chunks\n", "arr2 = np.hsplit(arr, 2)\n", "print(arr2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 9. Set Operations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### select the unique elements from an array" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "arr = np.array([1,1,2,2,3,3,4,5,6])\n", "print(np.unique(arr))" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n", "[2 2 2 1 1 1]\n" ] } ], "source": [ "# return the number of times each unique item appears\n", "arr = np.array([1,1,2,2,3,3,4,5,6])\n", "uniques, counts = np.unique(arr, return_counts=True)\n", "print(uniques)\n", "print(counts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the intersection & union of two arrays" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([1,2,3,4,5])\n", "arr2 = np.array([3,4,5,6,7])" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 4 5]\n" ] } ], "source": [ "# intersection\n", "print(np.intersect1d(arr1, arr2))" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6 7]\n" ] } ], "source": [ "# union\n", "print(np.union1d(arr1, arr2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute whether each element of an array is contained in another" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False False True True True]\n" ] } ], "source": [ "print(np.in1d(arr1, arr2))" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False False True True True]\n" ] } ], "source": [ "# preserve the shape of the array in the output, if the array is of higher dimensions\n", "print(np.isin(arr1, arr2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the elements in an array that are not in another" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2]\n" ] } ], "source": [ "print(np.setdiff1d(arr1, arr2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the elements in either of two arrays, but not both" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 6 7]\n" ] } ], "source": [ "print(np.setxor1d(arr1, arr2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 10. Linear Algebra" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.rand(5,5)\n", "arr2 = np.random.rand(5,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### matrix multiplication" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2.51623511 2.86218675 2.46149074 2.04223873 1.68511463]\n", " [0.97887139 1.12545821 1.08799709 0.91490076 0.71161775]\n", " [1.76553731 2.31931894 1.70683443 1.69936475 1.24178866]\n", " [1.16873119 1.51462574 1.20311322 1.18600669 0.82424827]\n", " [1.15498347 1.31753805 1.11707168 0.8826558 0.70418125]]\n", "[[2.51623511 2.86218675 2.46149074 2.04223873 1.68511463]\n", " [0.97887139 1.12545821 1.08799709 0.91490076 0.71161775]\n", " [1.76553731 2.31931894 1.70683443 1.69936475 1.24178866]\n", " [1.16873119 1.51462574 1.20311322 1.18600669 0.82424827]\n", " [1.15498347 1.31753805 1.11707168 0.8826558 0.70418125]]\n", "[[2.51623511 2.86218675 2.46149074 2.04223873 1.68511463]\n", " [0.97887139 1.12545821 1.08799709 0.91490076 0.71161775]\n", " [1.76553731 2.31931894 1.70683443 1.69936475 1.24178866]\n", " [1.16873119 1.51462574 1.20311322 1.18600669 0.82424827]\n", " [1.15498347 1.31753805 1.11707168 0.8826558 0.70418125]]\n" ] } ], "source": [ "print(arr1.dot(arr2))\n", "# or\n", "print(np.dot(arr1, arr2))\n", "# or\n", "print(arr1 @ arr2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### QR factorization " ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.4432654 0.32513554 0.61941338 -0.50166373 0.24992646]\n", " [-0.53313979 -0.84083639 0.01631061 -0.07809998 -0.04889265]\n", " [-0.497832 0.2725315 0.2186724 0.71963447 -0.33496042]\n", " [-0.00757871 0.09648464 -0.05319421 -0.440347 -0.89100964]\n", " [-0.52094492 0.32202228 -0.75194002 -0.17451256 0.17043949]]\n", "[[-1.71197975 -1.721771 -0.91763767 -1.14596917 -1.48828016]\n", " [ 0. 0.18511396 0.15670431 -0.3105988 -0.02561779]\n", " [ 0. 0. -0.56886488 0.09863229 -0.28981768]\n", " [ 0. 0. 0. -0.31671114 -0.28469988]\n", " [ 0. 0. 0. 0. -0.67088305]]\n" ] } ], "source": [ "arr = np.random.rand(5,5)\n", "\n", "q, r = np.linalg.qr(arr)\n", "print(q)\n", "print(r)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### singular value decomposition (SVD)" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.31707494 0.50745837 0.27489595 -0.52708908 -0.53717669]\n", " [-0.467027 -0.43107604 0.5391023 -0.31541122 0.4538102 ]\n", " [-0.53255432 0.56212351 -0.33998784 0.16799359 0.50654662]\n", " [-0.34108705 0.00379167 0.4323183 0.76786776 -0.32729992]\n", " [-0.53046657 -0.49057337 -0.57559542 -0.06964278 -0.37654055]]\n", "[2.83737775 0.69055824 0.47487856 0.23943839 0.10871122]\n", "[[-0.42059018 -0.38307359 -0.21951733 -0.6476369 -0.45687757]\n", " [-0.63976954 0.71850762 -0.25227551 0.00271298 0.10388191]\n", " [ 0.20378291 0.23608185 -0.1886435 0.38886392 -0.84613023]\n", " [-0.40137463 -0.52295215 -0.41054126 0.61598638 0.13204589]\n", " [ 0.45953212 0.08824872 -0.82706633 -0.22339365 0.21702294]]\n" ] } ], "source": [ "arr = np.random.rand(5,5)\n", "\n", "u, s, v = np.linalg.svd(arr)\n", "print(u)\n", "print(s)\n", "print(v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute eigen values" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 2.85678022 -0.67311818 0.37430308 0.1524331 -0.07643027]\n" ] } ], "source": [ "arr = np.random.rand(5,5)\n", "print(np.linalg.eigvals(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### eigen value decomposition" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 2.76395772+0.j 0.10174612+0.23654886j 0.10174612-0.23654886j\n", " -0.52935513+0.05320233j -0.52935513-0.05320233j]\n", "[[ 0.27758037+0.j -0.55997738+0.j -0.55997738-0.j\n", " 0.22379469-0.00692696j 0.22379469+0.00692696j]\n", " [ 0.45939229+0.j 0.17076292+0.34885444j 0.17076292-0.34885444j\n", " -0.70496349+0.j -0.70496349-0.j ]\n", " [ 0.48172485+0.j 0.21554976-0.3991528j 0.21554976+0.3991528j\n", " 0.0228234 -0.0005529j 0.0228234 +0.0005529j ]\n", " [ 0.48237473+0.j 0.37310246-0.2373618j 0.37310246+0.2373618j\n", " 0.12243854-0.18721414j 0.12243854+0.18721414j]\n", " [ 0.49715556+0.j -0.28133211+0.23470715j -0.28133211-0.23470715j\n", " 0.60718075+0.18347867j 0.60718075-0.18347867j]]\n" ] } ], "source": [ "arr = np.random.rand(5,5)\n", "\n", "w, v = np.linalg.eig(arr)\n", "print(w) # eigen values\n", "print(v) # eigen vectors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the trace & determinant" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.9087397083691204\n" ] } ], "source": [ "# notice this is not a function in linalg!!!\n", "print(np.trace(arr)) " ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.05187451362412388\n" ] } ], "source": [ "print(np.linalg.det(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### calculate the inverse/psedo-inverse of a matrix" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(5,5)" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2.37704915 -2.18278595 14.14976233 -17.3879446 -4.13238747]\n", " [ -2.65572368 2.85561316 6.39432678 -11.58940717 1.15627517]\n", " [ -0.64187733 2.8106897 11.3550277 -20.17896096 -0.58449384]\n", " [ 4.52924162 -5.63978854 -0.64934041 4.47682269 -1.75308404]\n", " [ -2.16914098 1.49607223 -25.1450202 37.98516176 4.2574866 ]]\n" ] } ], "source": [ "# compute the inverse of a matrix\n", "print(np.linalg.inv(arr))" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2.37704915 -2.18278595 14.14976233 -17.3879446 -4.13238747]\n", " [ -2.65572368 2.85561316 6.39432678 -11.58940717 1.15627517]\n", " [ -0.64187733 2.8106897 11.3550277 -20.17896096 -0.58449384]\n", " [ 4.52924162 -5.63978854 -0.64934041 4.47682269 -1.75308404]\n", " [ -2.16914098 1.49607223 -25.1450202 37.98516176 4.2574866 ]]\n" ] } ], "source": [ "# compute the psudo-inverse of a matrix\n", "print(np.linalg.pinv(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### solve a linear system" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-49.75295153 -18.33776982 -44.59372785 0.44351389 98.61602289]\n" ] } ], "source": [ "# solve a linear system in closed form\n", "y = [1,2,3,4,5]\n", "print(np.linalg.solve(arr, y))" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-49.75295153 -18.33776982 -44.59372785 0.44351389 98.61602289]\n", "[]\n", "5\n", "[2.21809418 0.85765544 0.52823685 0.09333081 0.0173482 ]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\ewang\\AppData\\Local\\Continuum\\miniconda3\\lib\\site-packages\\ipykernel_launcher.py:3: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.\n", "To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.\n", " This is separate from the ipykernel package so we can avoid doing imports until\n" ] } ], "source": [ "# calculate the least-squares solution of a linear system\n", "y = [1,2,3,4,5]\n", "solution, residuals, rank, singular = np.linalg.lstsq(arr, y)\n", "print(solution)\n", "print(residuals)\n", "print(rank)\n", "print(singular)" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }