├── LICENSE ├── README.md ├── Chapter09 ├── linalg_benchmark.py └── Chapter_09.ipynb ├── Chapter03 └── My_file.txt ├── Chapter08 └── Chapter_08.ipynb ├── Chapter01 └── Working_with_Numpy_Arrays.ipynb ├── Chapter07 └── Chapter07.ipynb └── Chapter02 └── Linear Algebra with Numpy.ipynb /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Mastering-Numerical-Computing-with-NumPy 5 | 6 | Mastering Numerical Computing with NumPy 7 | 8 | This is the code repository for [Mastering Numerical Computing with NumPy](https://www.packtpub.com/big-data-and-business-intelligence/mastering-numerical-computing-numpy?utm_source=github&utm_medium=repositary&utm_campaign=9781788993357), published by Packt. 9 | 10 | **Master scientific computing and perform complex operations with ease** 11 | 12 | ## What is this book about? 13 | NumPy is one of the most important scientific computing libraries available for Python. Mastering Numerical Computing with NumPy teaches you how to achieve expert level competency to perform complex operations, with in-depth coverage of advanced concepts. 14 | 15 | This book covers the following exciting features: 16 | * Perform vector and matrix operations using NumPy 17 | * Perform exploratory data analysis (EDA) on US housing data 18 | * Develop a predictive model using simple and multiple linear regression 19 | * Understand unsupervised learning and clustering algorithms with practical use cases 20 | * Write better NumPy code and implement the algorithms from scratch 21 | 22 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1788993357) today! 23 | 24 | https://www.packtpub.com/ 26 | 27 | 28 | ## Instructions and Navigations 29 | All of the code is organized into folders. For example, Chapter02. 30 | 31 | The code will look like the following: 32 | 33 | 'sepal width (cm)', 34 | 'petal length (cm)', 35 | 'petal width (cm)']) 36 | 37 | 38 | **Following is what you need for this book:** 39 | Mastering Numerical Computing with NumPy is for you if you are a Python programmer, data analyst, data engineer, or a data science enthusiast, who wants to master the intricacies of NumPy and build solutions for your numeric and scientific computational problems. You are expected to have familiarity with mathematics to get the most out of this book. 40 | 41 | With the following software and hardware list you can run all code files present in the book (Chapter 1-9). 42 | 43 | ### Software and Hardware List 44 | 45 | | Chapter | Software required | OS required | 46 | | -------- | ------------------------------------| -----------------------------------| 47 | | 1-9 | Anaconda distribution of Python 3 | Windows, Mac OS X, and Linux (Any) | 48 | 49 | 50 | We also provide a PDF file that has color images of the screenshots/diagrams used in this book. [Click here to download it](https://www.packtpub.com/sites/default/files/downloads/MasteringNumericalComputingwithNumPy_ColorImages.pdf). 51 | 52 | ### Related products 53 | * SciPy Recipes [[Packt]](https://www.packtpub.com/big-data-and-business-intelligence/scipy-recipes?utm_source=github&utm_medium=repositary&utm_campaign=9781788291460) [[Amazon]](https://www.amazon.com/dp/1788291468) 54 | 55 | * Python Data Analysis - Second Edition [[Packt]](https://www.packtpub.com/big-data-and-business-intelligence/python-data-analysis-second-edition?utm_source=github&utm_medium=repositary&utm_campaign=9781787127487) [[Amazon]](https://www.amazon.com/dp/1787127486) 56 | 57 | ## Get to Know the Authors 58 | 59 | **Umit Mert Cakmak** is a data scientist at IBM, where he excels at helping clients solve complex data science problems, from inception to delivery of deployable assets. His research spans multiple disciplines beyond his industry and he likes sharing his insights at conferences, universities, and meet-ups. 60 | 61 | 62 | **Mert Cuhadaroglu** is a BI Developer in EPAM, developing E2E analytics solutions for complex business problems in various industries, mostly investment banking, FMCG, media, communication, and pharma. He consistently uses advanced statistical models and ML algorithms to provide actionable insights. Throughout his career, he has worked in several other industries, such as banking and asset management. He continues his academic research in AI for trading algorithms. 63 | 64 | 65 | ## Other book by the authors 66 | * [Hands-On Automated Machine Learning](https://www.packtpub.com/big-data-and-business-intelligence/hands-automated-machine-learning?utm_source=github&utm_medium=repositary&utm_campaign=9781788629898) 67 | 68 | ### Suggestions and Feedback 69 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. 70 | ### Download a free PDF 71 | 72 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
73 |

https://packt.link/free-ebook/9781788993357

-------------------------------------------------------------------------------- /Chapter09/linalg_benchmark.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import time 4 | from datetime import datetime 5 | 6 | import numpy as np 7 | from numpy.random import rand 8 | from numpy.linalg import qr 9 | from numpy.linalg import eig 10 | from scipy.linalg import lu 11 | from scipy.linalg import cholesky 12 | 13 | 14 | def timer(*args, operation, n): 15 | """ 16 | Returns average time spent 17 | for given operation and arguments. 18 | 19 | Parameters 20 | ---------- 21 | *args: list (of numpy.ndarray, numpy.matrixlib.defmatrix.matrix or both) 22 | one or more numpy vectors or matrices 23 | operation: function 24 | numpy or scipy operation to be applied to given arguments 25 | n: int 26 | number of iterations to apply given operation 27 | 28 | Returns 29 | ------- 30 | avg_time_spent: double 31 | Average time spent to apply given operation 32 | std_time_spent: double 33 | Standard deviation of time spent to apply given operation 34 | 35 | Examples 36 | -------- 37 | >>> import numpy as np 38 | 39 | >>> vec1 = np.array(np.random.rand(1000)) 40 | >>> vec2 = np.array(np.random.rand(1000)) 41 | 42 | >>> args = (vec1, vec2) 43 | 44 | >>> timer(*args, operation=np.dot, n=1000000) 45 | 8.942582607269287e-07 46 | """ 47 | 48 | # Following list will hold the 49 | # time spent value for each iteration 50 | time_spent = [] 51 | 52 | # Configuration info 53 | print(""" 54 | ------------------------------------------- 55 | 56 | ### {} Operation ### 57 | 58 | Arguments Info 59 | -------------- 60 | args[0] Dimension: {}, 61 | args[0] Shape: {}, 62 | args[0] Length: {} 63 | """.format(operation.__name__, 64 | args[0].ndim, 65 | args[0].shape, 66 | len(args[0]))) 67 | 68 | # If *args length is greater than 1, 69 | # print out the info for second argument 70 | args_len = 0 71 | for i, arg in enumerate(args): 72 | args_len += 1 73 | 74 | if args_len > 1: 75 | print(""" 76 | args[1] Dimension: {}, 77 | args[1] Shape: {}, 78 | args[1] Length: {} 79 | """.format(args[1].ndim, 80 | args[1].shape, 81 | len(args[1]))) 82 | 83 | print(""" 84 | Operation Info 85 | -------------- 86 | Name: {}, 87 | Docstring: {} 88 | 89 | Iterations Info 90 | --------------- 91 | # of iterations: {}""".format( 92 | operation.__name__, 93 | operation.__doc__[:100] + 94 | "... For more info type 'operation?'", 95 | n)) 96 | 97 | print(""" 98 | -> Starting {} of iterations at: {}""".format(n, datetime.now())) 99 | 100 | if args_len > 1: 101 | for i in range(n): 102 | start = time.time() 103 | operation(args[0], args[1]) 104 | time_spent.append(time.time()-start) 105 | else: 106 | for i in range(n): 107 | start = time.time() 108 | operation(args[0]) 109 | time_spent.append(time.time()-start) 110 | 111 | avg_time_spent = np.sum(time_spent) / n 112 | std_time_spent = np.std(time_spent) 113 | 114 | print(""" 115 | -> Average time spent: {} seconds, 116 | -> Std. deviation time spent: {} seconds 117 | 118 | ------------------------------------------- 119 | """.format(avg_time_spent, std_time_spent)) 120 | 121 | return avg_time_spent, std_time_spent 122 | 123 | 124 | 125 | # Seed for reproducibility 126 | np.random.seed(8053) 127 | 128 | dim = 100 129 | n = 10000 130 | 131 | v1, v2 = np.array(rand(dim)), np.array(rand(dim)) 132 | m1, m2 = rand(dim, dim), rand(dim, dim) 133 | 134 | # Vector - Vector Product 135 | args = [v1, v2] 136 | vv_product = timer(*args, operation=np.dot, n=n) 137 | 138 | # Vector - Matrix Product 139 | args = [v1, m1] 140 | vm_product = timer(*args, operation=np.dot, n=n) 141 | 142 | # Matrix - Matrix Product 143 | args = [m1, m2] 144 | mm_product = timer(*args, operation=np.dot, n=n) 145 | 146 | # Singular-value Decomposition 147 | args = [m1] 148 | sv_dec = timer(*args, operation=np.linalg.svd, n=n) 149 | 150 | # LU Decomposition 151 | args = [m1] 152 | lu_dec = timer(*args, operation=lu, n=n) 153 | 154 | # QR Decomposition 155 | args = [m1] 156 | qr_dec = timer(*args, operation=qr, n=n) 157 | 158 | # Cholesky Decomposition 159 | M = np.array([[1, 3, 4], 160 | [2, 13, 15], 161 | [5, 31, 33]]) 162 | args = [M] 163 | cholesky_dec = timer(*args, operation=cholesky, n=n) 164 | 165 | # Eigenvalue Decomposition 166 | args = [m1] 167 | eig_dec = timer(*args, operation=eig, n=n) 168 | 169 | print(""" 170 | V-V Product: {}, 171 | V-M Product: {}, 172 | M-M Product: {}, 173 | SV Decomp.: {}, 174 | LU Decomp.: {}, 175 | QR Decomp.: {}, 176 | Cholesky D.: {}, 177 | Eigval Dec.: {} 178 | """.format(vv_product, 179 | vm_product, 180 | mm_product, 181 | sv_dec, 182 | lu_dec, 183 | qr_dec, 184 | cholesky_dec, 185 | eig_dec)) 186 | 187 | print(""" 188 | NumPy Configuration: 189 | -------------------- 190 | """) 191 | np.__config__.show() -------------------------------------------------------------------------------- /Chapter03/My_file.txt: -------------------------------------------------------------------------------- 1 | The following numbers are generated for the purporse of this chapter 2 | Numpy arange function used for generation 3 | In this chapter you will see several load and save methods 4 | The numbers are as below 5 | 0.000000000000000000e+00 6 | 1.000000000000000000e+00 7 | 2.000000000000000000e+00 8 | 3.000000000000000000e+00 9 | 4.000000000000000000e+00 10 | 5.000000000000000000e+00 11 | 6.000000000000000000e+00 12 | 7.000000000000000000e+00 13 | 8.000000000000000000e+00 14 | 9.000000000000000000e+00 15 | 1.000000000000000000e+01 16 | 1.100000000000000000e+01 17 | 1.200000000000000000e+01 18 | 1.300000000000000000e+01 19 | 1.400000000000000000e+01 20 | 1.500000000000000000e+01 21 | 1.600000000000000000e+01 22 | 1.700000000000000000e+01 23 | 1.800000000000000000e+01 24 | 1.900000000000000000e+01 25 | 2.000000000000000000e+01 26 | 2.100000000000000000e+01 27 | 2.200000000000000000e+01 28 | 2.300000000000000000e+01 29 | 2.400000000000000000e+01 30 | 2.500000000000000000e+01 31 | 2.600000000000000000e+01 32 | 2.700000000000000000e+01 33 | 2.800000000000000000e+01 34 | 2.900000000000000000e+01 35 | 3.000000000000000000e+01 36 | 3.100000000000000000e+01 37 | 3.200000000000000000e+01 38 | 3.300000000000000000e+01 39 | 3.400000000000000000e+01 40 | 3.500000000000000000e+01 41 | 3.600000000000000000e+01 42 | 3.700000000000000000e+01 43 | 3.800000000000000000e+01 44 | 3.900000000000000000e+01 45 | 4.000000000000000000e+01 46 | 4.100000000000000000e+01 47 | 4.200000000000000000e+01 48 | 4.300000000000000000e+01 49 | 4.400000000000000000e+01 50 | 4.500000000000000000e+01 51 | 4.600000000000000000e+01 52 | 4.700000000000000000e+01 53 | 4.800000000000000000e+01 54 | 4.900000000000000000e+01 55 | 5.000000000000000000e+01 56 | 5.100000000000000000e+01 57 | 5.200000000000000000e+01 58 | 5.300000000000000000e+01 59 | 5.400000000000000000e+01 60 | 5.500000000000000000e+01 61 | 5.600000000000000000e+01 62 | 5.700000000000000000e+01 63 | 5.800000000000000000e+01 64 | 5.900000000000000000e+01 65 | 6.000000000000000000e+01 66 | 6.100000000000000000e+01 67 | 6.200000000000000000e+01 68 | 6.300000000000000000e+01 69 | 6.400000000000000000e+01 70 | 6.500000000000000000e+01 71 | 6.600000000000000000e+01 72 | 6.700000000000000000e+01 73 | 6.800000000000000000e+01 74 | 6.900000000000000000e+01 75 | 7.000000000000000000e+01 76 | 7.100000000000000000e+01 77 | 7.200000000000000000e+01 78 | 7.300000000000000000e+01 79 | 7.400000000000000000e+01 80 | 7.500000000000000000e+01 81 | 7.600000000000000000e+01 82 | 7.700000000000000000e+01 83 | 7.800000000000000000e+01 84 | 7.900000000000000000e+01 85 | 8.000000000000000000e+01 86 | 8.100000000000000000e+01 87 | 8.200000000000000000e+01 88 | 8.300000000000000000e+01 89 | 8.400000000000000000e+01 90 | 8.500000000000000000e+01 91 | 8.600000000000000000e+01 92 | 8.700000000000000000e+01 93 | 8.800000000000000000e+01 94 | 8.900000000000000000e+01 95 | 9.000000000000000000e+01 96 | 9.100000000000000000e+01 97 | 9.200000000000000000e+01 98 | 9.300000000000000000e+01 99 | 9.400000000000000000e+01 100 | 9.500000000000000000e+01 101 | 9.600000000000000000e+01 102 | 9.700000000000000000e+01 103 | 9.800000000000000000e+01 104 | 9.900000000000000000e+01 105 | 1.000000000000000000e+02 106 | 1.010000000000000000e+02 107 | 1.020000000000000000e+02 108 | 1.030000000000000000e+02 109 | 1.040000000000000000e+02 110 | 1.050000000000000000e+02 111 | 1.060000000000000000e+02 112 | 1.070000000000000000e+02 113 | 1.080000000000000000e+02 114 | 1.090000000000000000e+02 115 | 1.100000000000000000e+02 116 | 1.110000000000000000e+02 117 | 1.120000000000000000e+02 118 | 1.130000000000000000e+02 119 | 1.140000000000000000e+02 120 | 1.150000000000000000e+02 121 | 1.160000000000000000e+02 122 | 1.170000000000000000e+02 123 | 1.180000000000000000e+02 124 | 1.190000000000000000e+02 125 | 1.200000000000000000e+02 126 | 1.210000000000000000e+02 127 | 1.220000000000000000e+02 128 | 1.230000000000000000e+02 129 | 1.240000000000000000e+02 130 | 1.250000000000000000e+02 131 | 1.260000000000000000e+02 132 | 1.270000000000000000e+02 133 | 1.280000000000000000e+02 134 | 1.290000000000000000e+02 135 | 1.300000000000000000e+02 136 | 1.310000000000000000e+02 137 | 1.320000000000000000e+02 138 | 1.330000000000000000e+02 139 | 1.340000000000000000e+02 140 | 1.350000000000000000e+02 141 | 1.360000000000000000e+02 142 | 1.370000000000000000e+02 143 | 1.380000000000000000e+02 144 | 1.390000000000000000e+02 145 | 1.400000000000000000e+02 146 | 1.410000000000000000e+02 147 | 1.420000000000000000e+02 148 | 1.430000000000000000e+02 149 | 1.440000000000000000e+02 150 | 1.450000000000000000e+02 151 | 1.460000000000000000e+02 152 | 1.470000000000000000e+02 153 | 1.480000000000000000e+02 154 | 1.490000000000000000e+02 155 | 1.500000000000000000e+02 156 | 1.510000000000000000e+02 157 | 1.520000000000000000e+02 158 | 1.530000000000000000e+02 159 | 1.540000000000000000e+02 160 | 1.550000000000000000e+02 161 | 1.560000000000000000e+02 162 | 1.570000000000000000e+02 163 | 1.580000000000000000e+02 164 | 1.590000000000000000e+02 165 | 1.600000000000000000e+02 166 | 1.610000000000000000e+02 167 | 1.620000000000000000e+02 168 | 1.630000000000000000e+02 169 | 1.640000000000000000e+02 170 | 1.650000000000000000e+02 171 | 1.660000000000000000e+02 172 | 1.670000000000000000e+02 173 | 1.680000000000000000e+02 174 | 1.690000000000000000e+02 175 | 1.700000000000000000e+02 176 | 1.710000000000000000e+02 177 | 1.720000000000000000e+02 178 | 1.730000000000000000e+02 179 | 1.740000000000000000e+02 180 | 1.750000000000000000e+02 181 | 1.760000000000000000e+02 182 | 1.770000000000000000e+02 183 | 1.780000000000000000e+02 184 | 1.790000000000000000e+02 185 | 1.800000000000000000e+02 186 | 1.810000000000000000e+02 187 | 1.820000000000000000e+02 188 | 1.830000000000000000e+02 189 | 1.840000000000000000e+02 190 | 1.850000000000000000e+02 191 | 1.860000000000000000e+02 192 | 1.870000000000000000e+02 193 | 1.880000000000000000e+02 194 | 1.890000000000000000e+02 195 | 1.900000000000000000e+02 196 | 1.910000000000000000e+02 197 | 1.920000000000000000e+02 198 | 1.930000000000000000e+02 199 | 1.940000000000000000e+02 200 | 1.950000000000000000e+02 201 | 1.960000000000000000e+02 202 | 1.970000000000000000e+02 203 | 1.980000000000000000e+02 204 | 1.990000000000000000e+02 205 | 2.000000000000000000e+02 206 | 2.010000000000000000e+02 207 | 2.020000000000000000e+02 208 | 2.030000000000000000e+02 209 | 2.040000000000000000e+02 210 | 2.050000000000000000e+02 211 | 2.060000000000000000e+02 212 | 2.070000000000000000e+02 213 | 2.080000000000000000e+02 214 | 2.090000000000000000e+02 215 | 2.100000000000000000e+02 216 | 2.110000000000000000e+02 217 | 2.120000000000000000e+02 218 | 2.130000000000000000e+02 219 | 2.140000000000000000e+02 220 | 2.150000000000000000e+02 221 | 2.160000000000000000e+02 222 | 2.170000000000000000e+02 223 | 2.180000000000000000e+02 224 | 2.190000000000000000e+02 225 | 2.200000000000000000e+02 226 | 2.210000000000000000e+02 227 | 2.220000000000000000e+02 228 | 2.230000000000000000e+02 229 | 2.240000000000000000e+02 230 | 2.250000000000000000e+02 231 | 2.260000000000000000e+02 232 | 2.270000000000000000e+02 233 | 2.280000000000000000e+02 234 | 2.290000000000000000e+02 235 | 2.300000000000000000e+02 236 | 2.310000000000000000e+02 237 | 2.320000000000000000e+02 238 | 2.330000000000000000e+02 239 | 2.340000000000000000e+02 240 | 2.350000000000000000e+02 241 | 2.360000000000000000e+02 242 | 2.370000000000000000e+02 243 | 2.380000000000000000e+02 244 | 2.390000000000000000e+02 245 | 2.400000000000000000e+02 246 | 2.410000000000000000e+02 247 | 2.420000000000000000e+02 248 | 2.430000000000000000e+02 249 | 2.440000000000000000e+02 250 | 2.450000000000000000e+02 251 | 2.460000000000000000e+02 252 | 2.470000000000000000e+02 253 | 2.480000000000000000e+02 254 | 2.490000000000000000e+02 255 | -------------------------------------------------------------------------------- /Chapter08/Chapter_08.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Singular-value decomposition" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 61, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "\n", 18 | "M = np.random.randint(low=0, high=20, size=20).reshape(4,5)" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 62, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "[[18 15 11 13 19]\n", 31 | " [ 1 6 8 13 18]\n", 32 | " [ 9 7 15 13 10]\n", 33 | " [17 15 12 14 12]]\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "print(M)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 63, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "U, d, VT = np.linalg.svd(M)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 64, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "U:\n", 60 | " [[-0.60773852 -0.22318957 0.5276743 -0.54990921]\n", 61 | " [-0.38123886 0.86738201 0.19333528 0.25480749]\n", 62 | " [-0.42657252 0.10181457 -0.82343563 -0.36003255]\n", 63 | " [-0.55076919 -0.43297652 -0.07832665 0.70925987]]\n", 64 | "\n", 65 | "d:\n", 66 | " [56.31276456 13.15721839 8.08763849 2.51997135]\n", 67 | "\n", 68 | "VT:\n", 69 | " [[-0.43547429 -0.40223663 -0.40386674 -0.46371223 -0.52002929]\n", 70 | " [-0.72920427 -0.29835313 0.06197899 0.27638212 0.54682545]\n", 71 | " [ 0.11733943 0.26412864 -0.73449806 -0.30022507 0.53557916]\n", 72 | " [-0.32795351 0.55511623 -0.3571117 0.56067806 -0.3773643 ]\n", 73 | " [-0.39661218 0.60932187 0.40747282 -0.55144258 0.03609177]]\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "print(\"U:\\n {}\\n\".format(U))\n", 79 | "print(\"d:\\n {}\\n\".format(d))\n", 80 | "print(\"VT:\\n {}\".format(VT))" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 65, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "U:\n", 93 | " [[-0.60773852 -0.22318957 0.5276743 -0.54990921]\n", 94 | " [-0.38123886 0.86738201 0.19333528 0.25480749]\n", 95 | " [-0.42657252 0.10181457 -0.82343563 -0.36003255]\n", 96 | " [-0.55076919 -0.43297652 -0.07832665 0.70925987]]\n", 97 | "\n", 98 | "d:\n", 99 | " [56.31276456 13.15721839 8.08763849 2.51997135]\n", 100 | "\n", 101 | "VT:\n", 102 | " [[-0.43547429 -0.40223663 -0.40386674 -0.46371223 -0.52002929]\n", 103 | " [-0.72920427 -0.29835313 0.06197899 0.27638212 0.54682545]\n", 104 | " [ 0.11733943 0.26412864 -0.73449806 -0.30022507 0.53557916]\n", 105 | " [-0.32795351 0.55511623 -0.3571117 0.56067806 -0.3773643 ]]\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "# Setting full_matrices to false gives you reduced form where small values close to zero are excluded\n", 111 | "U, d, VT = np.linalg.svd(M, full_matrices=False)\n", 112 | "\n", 113 | "print(\"U:\\n {}\\n\".format(U))\n", 114 | "print(\"d:\\n {}\\n\".format(d))\n", 115 | "print(\"VT:\\n {}\".format(VT))" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "## LU decomposition" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 66, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "[[18 12 14 15 2]\n", 135 | " [ 4 2 12 18 3]\n", 136 | " [ 9 19 5 16 8]\n", 137 | " [15 19 6 16 11]\n", 138 | " [ 1 19 2 18 17]]\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "from numpy import array\n", 144 | "from scipy.linalg import lu\n", 145 | "\n", 146 | "M = np.random.randint(low=0, high=20, size=25).reshape(5,5)\n", 147 | "print(M)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 67, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "P:\n", 160 | " [[1. 0. 0. 0. 0.]\n", 161 | " [0. 0. 1. 0. 0.]\n", 162 | " [0. 0. 0. 0. 1.]\n", 163 | " [0. 0. 0. 1. 0.]\n", 164 | " [0. 1. 0. 0. 0.]]\n", 165 | "\n", 166 | "L:\n", 167 | " [[ 1. 0. 0. 0. 0. ]\n", 168 | " [ 0.05555556 1. 0. 0. 0. ]\n", 169 | " [ 0.22222222 -0.03636364 1. 0. 0. ]\n", 170 | " [ 0.83333333 0.49090909 -0.70149254 1. 0. ]\n", 171 | " [ 0.5 0.70909091 -0.32089552 0.21279832 1. ]]\n", 172 | "\n", 173 | "U:\n", 174 | " [[18. 12. 14. 15. 2. ]\n", 175 | " [ 0. 18.33333333 1.22222222 17.16666667 16.88888889]\n", 176 | " [ 0. 0. 8.93333333 15.29090909 3.16969697]\n", 177 | " [ 0. 0. 0. 5.79918589 3.26594301]\n", 178 | " [ 0. 0. 0. 0. -4.65360318]]\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "P, L, U = lu(M)\n", 184 | "\n", 185 | "print(\"P:\\n {}\\n\".format(P))\n", 186 | "print(\"L:\\n {}\\n\".format(L))\n", 187 | "print(\"U:\\n {}\".format(U))" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 68, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "data": { 197 | "text/plain": [ 198 | "array([[18., 12., 14., 15., 2.],\n", 199 | " [ 4., 2., 12., 18., 3.],\n", 200 | " [ 9., 19., 5., 16., 8.],\n", 201 | " [15., 19., 6., 16., 11.],\n", 202 | " [ 1., 19., 2., 18., 17.]])" 203 | ] 204 | }, 205 | "execution_count": 68, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "P.dot(L).dot(U)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "## QR decomposition" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 69, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "[[14 6 0 19 3]\n", 231 | " [ 9 6 17 8 8]\n", 232 | " [ 4 13 17 4 4]\n", 233 | " [ 0 0 2 7 11]]\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "from numpy import array\n", 239 | "from numpy.linalg import qr\n", 240 | "\n", 241 | "M = np.random.randint(low=0, high=20, size=20).reshape(4,5)\n", 242 | "print(M)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 70, 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "Q:\n", 255 | " [[-0.81788873 0.28364908 -0.49345895 0.08425845]\n", 256 | " [-0.52578561 -0.01509441 0.83834961 -0.14314877]\n", 257 | " [-0.2336825 -0.95880935 -0.15918031 0.02718015]\n", 258 | " [-0. -0. 0.16831464 0.98573332]]\n", 259 | "\n", 260 | "R:\n", 261 | " [[-17.11724277 -11.09991852 -12.91095786 -20.68090082 -7.59468109]\n", 262 | " [ 0. -10.85319349 -16.5563638 1.43333978 -3.10504542]\n", 263 | " [ 0. 0. 11.88250752 -2.12744187 6.4411599 ]\n", 264 | " [ 0. 0. 0. 7.4645743 10.05937231]]\n" 265 | ] 266 | } 267 | ], 268 | "source": [ 269 | "Q, R = qr(M, 'complete')\n", 270 | "\n", 271 | "print(\"Q:\\n {}\\n\".format(Q))\n", 272 | "print(\"R:\\n {}\".format(R))" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 71, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "data": { 282 | "text/plain": [ 283 | "array([[1.40000000e+01, 6.00000000e+00, 1.77635684e-15, 1.90000000e+01,\n", 284 | " 3.00000000e+00],\n", 285 | " [9.00000000e+00, 6.00000000e+00, 1.70000000e+01, 8.00000000e+00,\n", 286 | " 8.00000000e+00],\n", 287 | " [4.00000000e+00, 1.30000000e+01, 1.70000000e+01, 4.00000000e+00,\n", 288 | " 4.00000000e+00],\n", 289 | " [0.00000000e+00, 0.00000000e+00, 2.00000000e+00, 7.00000000e+00,\n", 290 | " 1.10000000e+01]])" 291 | ] 292 | }, 293 | "execution_count": 71, 294 | "metadata": {}, 295 | "output_type": "execute_result" 296 | } 297 | ], 298 | "source": [ 299 | "Q.dot(R)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "## Cholesky decomposition" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 202, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "name": "stdout", 316 | "output_type": "stream", 317 | "text": [ 318 | "[[ 1 3 4]\n", 319 | " [ 2 13 15]\n", 320 | " [ 5 31 33]]\n" 321 | ] 322 | } 323 | ], 324 | "source": [ 325 | "from numpy import array\n", 326 | "from scipy.linalg import cholesky\n", 327 | "\n", 328 | "M = np.array([[1, 3, 4], \n", 329 | " [2, 13, 15], \n", 330 | " [5, 31, 33]])\n", 331 | "print(M)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 203, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "[[1. 3. 4. ]\n", 344 | " [0. 2. 1.5 ]\n", 345 | " [0. 0. 3.84057287]]\n" 346 | ] 347 | } 348 | ], 349 | "source": [ 350 | "L = cholesky(M)\n", 351 | "print(L)" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 204, 357 | "metadata": {}, 358 | "outputs": [ 359 | { 360 | "data": { 361 | "text/plain": [ 362 | "array([[ 1., 3., 4.],\n", 363 | " [ 3., 13., 15.],\n", 364 | " [ 4., 15., 33.]])" 365 | ] 366 | }, 367 | "execution_count": 204, 368 | "metadata": {}, 369 | "output_type": "execute_result" 370 | } 371 | ], 372 | "source": [ 373 | "L.T.dot(L)" 374 | ] 375 | }, 376 | { 377 | "cell_type": "markdown", 378 | "metadata": {}, 379 | "source": [ 380 | "## Eigenvalue decomposition" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 72, 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "name": "stdout", 390 | "output_type": "stream", 391 | "text": [ 392 | "[[13 9 5 0 12]\n", 393 | " [13 6 11 8 15]\n", 394 | " [16 17 15 12 1]\n", 395 | " [17 8 5 7 5]\n", 396 | " [10 6 18 5 19]]\n" 397 | ] 398 | } 399 | ], 400 | "source": [ 401 | "from numpy import array\n", 402 | "from numpy.linalg import eig\n", 403 | "\n", 404 | "M = np.random.randint(low=0, high=20, size=25).reshape(5,5)\n", 405 | "print(M)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 76, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "name": "stdout", 415 | "output_type": "stream", 416 | "text": [ 417 | "Eigenvalues:\n", 418 | " [50.79415691 +0.j 5.76076687+11.52079216j\n", 419 | " 5.76076687-11.52079216j -1.15784533 +3.28961651j\n", 420 | " -1.15784533 -3.28961651j]\n", 421 | "\n", 422 | "Eigenvectors:\n", 423 | " [[ 0.34875973+0.j -0.36831427+0.21725348j -0.36831427-0.21725348j\n", 424 | " -0.40737336-0.19752276j -0.40737336+0.19752276j]\n", 425 | " [ 0.46629571+0.j -0.08027011-0.03330739j -0.08027011+0.03330739j\n", 426 | " 0.58904402+0.j 0.58904402-0.j ]\n", 427 | " [ 0.50628483+0.j 0.62334823+0.j 0.62334823-0.j\n", 428 | " -0.27738359-0.22063552j -0.27738359+0.22063552j]\n", 429 | " [ 0.33975886+0.j 0.14035596+0.39427693j 0.14035596-0.39427693j\n", 430 | " 0.125282 +0.46663129j 0.125282 -0.46663129j]\n", 431 | " [ 0.53774952+0.j -0.18591079-0.45968785j -0.18591079+0.45968785j\n", 432 | " 0.20856874+0.21329768j 0.20856874-0.21329768j]]\n" 433 | ] 434 | } 435 | ], 436 | "source": [ 437 | "V, Q = eig(M)\n", 438 | "\n", 439 | "print(\"Eigenvalues:\\n {}\\n\".format(V))\n", 440 | "print(\"Eigenvectors:\\n {}\".format(Q))" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": 77, 446 | "metadata": {}, 447 | "outputs": [ 448 | { 449 | "data": { 450 | "text/plain": [ 451 | "array([[1.30000000e+01-2.88657986e-15j, 9.00000000e+00-2.33146835e-15j,\n", 452 | " 5.00000000e+00+2.38697950e-15j, 1.17683641e-14+1.77635684e-15j,\n", 453 | " 1.20000000e+01-4.99600361e-16j],\n", 454 | " [1.30000000e+01-4.32986980e-15j, 6.00000000e+00-3.99680289e-15j,\n", 455 | " 1.10000000e+01+3.38618023e-15j, 8.00000000e+00+1.72084569e-15j,\n", 456 | " 1.50000000e+01-2.77555756e-16j],\n", 457 | " [1.60000000e+01-7.21644966e-15j, 1.70000000e+01-6.66133815e-15j,\n", 458 | " 1.50000000e+01+5.71764858e-15j, 1.20000000e+01+2.99760217e-15j,\n", 459 | " 1.00000000e+00-6.66133815e-16j],\n", 460 | " [1.70000000e+01-5.27355937e-15j, 8.00000000e+00-3.10862447e-15j,\n", 461 | " 5.00000000e+00+4.27435864e-15j, 7.00000000e+00+2.22044605e-15j,\n", 462 | " 5.00000000e+00-1.22124533e-15j],\n", 463 | " [1.00000000e+01-3.60822483e-15j, 6.00000000e+00-4.21884749e-15j,\n", 464 | " 1.80000000e+01+2.27595720e-15j, 5.00000000e+00+1.55431223e-15j,\n", 465 | " 1.90000000e+01+3.88578059e-16j]])" 466 | ] 467 | }, 468 | "execution_count": 77, 469 | "metadata": {}, 470 | "output_type": "execute_result" 471 | } 472 | ], 473 | "source": [ 474 | "from numpy import diag\n", 475 | "from numpy import dot\n", 476 | "from numpy.linalg import inv\n", 477 | "\n", 478 | "Q.dot(diag(V)).dot(inv(Q))" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": null, 484 | "metadata": {}, 485 | "outputs": [], 486 | "source": [] 487 | } 488 | ], 489 | "metadata": { 490 | "kernelspec": { 491 | "display_name": "Python 3", 492 | "language": "python", 493 | "name": "python3" 494 | }, 495 | "language_info": { 496 | "codemirror_mode": { 497 | "name": "ipython", 498 | "version": 3 499 | }, 500 | "file_extension": ".py", 501 | "mimetype": "text/x-python", 502 | "name": "python", 503 | "nbconvert_exporter": "python", 504 | "pygments_lexer": "ipython3", 505 | "version": "3.6.1" 506 | } 507 | }, 508 | "nbformat": 4, 509 | "nbformat_minor": 2 510 | } 511 | -------------------------------------------------------------------------------- /Chapter01/Working_with_Numpy_Arrays.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Introduction to Vectors and Matrices" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "x = np.array([[1,0,4],[3,3,1]])\n", 18 | "y = np.array([[2,5],[1,1],[3,2]])\n", 19 | "x.dot(y)" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "# Basics of NumPy Array Object" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "import numpy as np\n", 36 | "x = np.array([[1,2,3],[4,5,6]])\n", 37 | "x" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": { 44 | "scrolled": true 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "print(\"We just create a \", type(x))" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "print(\"Our template has shape as\" ,x.shape)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "print(\"Total size is\",x.size)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "print(\"The dimension of our array is \" ,x.ndim)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "print(\"Data type of elements are\",x.dtype)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "print(\"It consumes\",x.nbytes,\"bytes\")" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "x = np.array([[1,2,3],[4,5,6]], dtype = np.float)\n", 103 | "print(x)\n", 104 | "print(x.nbytes)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "x = np.array([[1,2,3],[4,5,6]], dtype = np.complex)\n", 114 | "print(x)\n", 115 | "print(x.nbytes)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "x = np.array([[1,2,3],[4,-5,6]], dtype = np.uint32)\n", 125 | "print(x)\n", 126 | "print(x.nbytes)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": { 133 | "scrolled": true 134 | }, 135 | "outputs": [], 136 | "source": [ 137 | "x = np.array([[1,2,3],[4,5,6]], dtype = np.int64)\n", 138 | "print(\"int64 comsues\",x.nbytes, \"bytes\")\n", 139 | "x = np.array([[1,2,3],[4,5,6]], dtype = np.int32)\n", 140 | "print(\"int32 comsues\",x.nbytes, \"bytes\")" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "x_copy = np.array(x, dtype = np.float)\n", 150 | "x_copy " 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "x_copy_int = x_copy.astype(np.int)\n", 160 | "x_copy_int" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "x_copy" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "Data_Cancer = np.random.rand(100000,100)\n", 179 | "print(type(Data_Cancer))\n", 180 | "print(Data_Cancer.dtype)\n", 181 | "print(Data_Cancer.nbytes)\n", 182 | "Data_Cancer_New = np.array(Data_Cancer, dtype = np.float32)\n", 183 | "print(Data_Cancer_New.nbytes)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "# NumPy Array Operations" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "my_list = [2, 14, 6, 8]\n", 200 | "my_array = np.asarray(my_list)\n", 201 | "type(my_array)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "my_array +2" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [ 219 | "my_array -1" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "my_array *2" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "metadata": {}, 235 | "outputs": [], 236 | "source": [ 237 | "my_array /2" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [ 246 | "second_array = np.zeros(4) + 3\n", 247 | "second_array " 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [ 256 | "my_array - second_array " 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": {}, 263 | "outputs": [], 264 | "source": [ 265 | "second_array / my_array " 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": null, 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "second_array = np.ones(4)+3\n", 275 | "second_array" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "metadata": {}, 282 | "outputs": [], 283 | "source": [ 284 | "my_array - second_array " 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "metadata": {}, 291 | "outputs": [], 292 | "source": [ 293 | "second_array / my_array " 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [ 302 | "second_array = np.identity(4)\n", 303 | "second_array" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "metadata": {}, 310 | "outputs": [], 311 | "source": [ 312 | "second_array = np.identity(4)+3\n", 313 | "second_array" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "my_array - second_array " 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [ 331 | "second_array / my_array " 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [ 340 | "x = np.arange(3,7,0.5)\n", 341 | "x" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": null, 347 | "metadata": {}, 348 | "outputs": [], 349 | "source": [ 350 | "x = np.linspace(1.2, 40.5, num=20)\n", 351 | "x" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": {}, 358 | "outputs": [], 359 | "source": [ 360 | "np.geomspace(1, 625, num=5)" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": null, 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [ 369 | "np.logspace(3, 4, num=5)" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": null, 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [ 378 | "np.logspace(np.log10(3) , np.log10(4) , num=5)" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": null, 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [ 387 | "x = np.array([1,2,3,4])\n", 388 | "y = np.array([1,3,4,4])\n", 389 | "x == y " 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": null, 395 | "metadata": {}, 396 | "outputs": [], 397 | "source": [ 398 | "x = np.array([1,2,3,4])\n", 399 | "y = np.array([1,3,4,4])\n", 400 | "np.array_equal(x,y)" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": null, 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [ 409 | "x = np.array([1,2,3,4])\n", 410 | "y = np.array([1,3,4,4])\n", 411 | "x < y " 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": null, 417 | "metadata": {}, 418 | "outputs": [], 419 | "source": [ 420 | "x = np.array([0, 1, 0, 0], dtype=bool)\n", 421 | "y = np.array([1, 1, 0, 1], dtype=bool)\n", 422 | "np.logical_or(x,y)" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": null, 428 | "metadata": {}, 429 | "outputs": [], 430 | "source": [ 431 | "np.logical_and(x,y)" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": null, 437 | "metadata": {}, 438 | "outputs": [], 439 | "source": [ 440 | "x = np.array([12,16,57,11])\n", 441 | "np.logical_or(x < 13, x > 50)" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "metadata": {}, 448 | "outputs": [], 449 | "source": [ 450 | "x = np.array([1, 2, 3,4 ])\n", 451 | "np.exp(x)" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": null, 457 | "metadata": {}, 458 | "outputs": [], 459 | "source": [ 460 | "np.log(x)" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": null, 466 | "metadata": {}, 467 | "outputs": [], 468 | "source": [ 469 | "np.sin(x)" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": null, 475 | "metadata": {}, 476 | "outputs": [], 477 | "source": [ 478 | "x = np.arange(9)\n", 479 | "x" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": null, 485 | "metadata": {}, 486 | "outputs": [], 487 | "source": [ 488 | "x = np.arange(9).reshape((3, 3))\n", 489 | "x" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": null, 495 | "metadata": {}, 496 | "outputs": [], 497 | "source": [ 498 | "x.T" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": null, 504 | "metadata": {}, 505 | "outputs": [], 506 | "source": [ 507 | "x = np.arange(6).reshape(2,3)\n", 508 | "x" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": null, 514 | "metadata": {}, 515 | "outputs": [], 516 | "source": [ 517 | "x.T" 518 | ] 519 | }, 520 | { 521 | "cell_type": "code", 522 | "execution_count": null, 523 | "metadata": {}, 524 | "outputs": [], 525 | "source": [ 526 | "x = np.arange(9).reshape((3,3))\n", 527 | "x" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": null, 533 | "metadata": {}, 534 | "outputs": [], 535 | "source": [ 536 | "np.sum(x)" 537 | ] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": null, 542 | "metadata": {}, 543 | "outputs": [], 544 | "source": [ 545 | "np.amin(x)" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": null, 551 | "metadata": {}, 552 | "outputs": [], 553 | "source": [ 554 | "np.amax(x)" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "execution_count": null, 560 | "metadata": {}, 561 | "outputs": [], 562 | "source": [ 563 | "np.amin(x, axis=0)" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": null, 569 | "metadata": {}, 570 | "outputs": [], 571 | "source": [ 572 | "np.amin(x, axis=1)" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": null, 578 | "metadata": {}, 579 | "outputs": [], 580 | "source": [ 581 | "np.percentile(x, 80)" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": null, 587 | "metadata": {}, 588 | "outputs": [], 589 | "source": [ 590 | "x = np.array([1,-21,3,-3])\n", 591 | "np.argmax(x)" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": null, 597 | "metadata": {}, 598 | "outputs": [], 599 | "source": [ 600 | "np.argmin(x)" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": null, 606 | "metadata": {}, 607 | "outputs": [], 608 | "source": [ 609 | "x = np.array([[2, 3, 5], [20, 12, 4]])\n", 610 | "x" 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": null, 616 | "metadata": {}, 617 | "outputs": [], 618 | "source": [ 619 | "np.mean(x)" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": null, 625 | "metadata": {}, 626 | "outputs": [], 627 | "source": [ 628 | "np.mean(x, axis=0)" 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": null, 634 | "metadata": {}, 635 | "outputs": [], 636 | "source": [ 637 | "np.mean(x, axis=1)" 638 | ] 639 | }, 640 | { 641 | "cell_type": "code", 642 | "execution_count": null, 643 | "metadata": {}, 644 | "outputs": [], 645 | "source": [ 646 | "np.median(x)" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": null, 652 | "metadata": {}, 653 | "outputs": [], 654 | "source": [ 655 | "np.std(x)" 656 | ] 657 | }, 658 | { 659 | "cell_type": "markdown", 660 | "metadata": {}, 661 | "source": [ 662 | "# Working with Multidimensional Arrays" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": null, 668 | "metadata": { 669 | "scrolled": true 670 | }, 671 | "outputs": [], 672 | "source": [ 673 | "c = np.ones((4, 4))\n", 674 | "c*c" 675 | ] 676 | }, 677 | { 678 | "cell_type": "code", 679 | "execution_count": null, 680 | "metadata": {}, 681 | "outputs": [], 682 | "source": [ 683 | "c.dot(c)" 684 | ] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "execution_count": null, 689 | "metadata": {}, 690 | "outputs": [], 691 | "source": [ 692 | "y = np.arange(15).reshape(3,5) \n", 693 | "x = np.arange(10).reshape(2,5)\n", 694 | "new_array = np.vstack((y,x))\n", 695 | "new_array" 696 | ] 697 | }, 698 | { 699 | "cell_type": "code", 700 | "execution_count": null, 701 | "metadata": {}, 702 | "outputs": [], 703 | "source": [ 704 | "y = np.arange(15).reshape(5,3) \n", 705 | "x = np.arange(10).reshape(5,2)\n", 706 | "new_array = np.hstack((y,x))\n", 707 | "new_array" 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": null, 713 | "metadata": {}, 714 | "outputs": [], 715 | "source": [ 716 | "from scipy import stats\n", 717 | "x= np.random.rand(100,10)\n", 718 | "n, min_max, mean, var, skew, kurt = stats.describe(x)\n", 719 | "new_array = np.vstack((mean,var,skew,kurt,min_max[0],min_max[1]))\n", 720 | "new_array.T" 721 | ] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "execution_count": null, 726 | "metadata": {}, 727 | "outputs": [], 728 | "source": [ 729 | "import numpy.ma as ma\n", 730 | "x = np.arange(6)\n", 731 | "print(x.mean())\n", 732 | "masked_array = ma.masked_array(x, mask=[1,0,0,0,0,0])\n", 733 | "masked_array.mean()" 734 | ] 735 | }, 736 | { 737 | "cell_type": "code", 738 | "execution_count": null, 739 | "metadata": {}, 740 | "outputs": [], 741 | "source": [ 742 | "x = np.arange(25, dtype = float).reshape(5,5)\n", 743 | "x[x<5] = np.nan\n", 744 | "x" 745 | ] 746 | }, 747 | { 748 | "cell_type": "code", 749 | "execution_count": null, 750 | "metadata": {}, 751 | "outputs": [], 752 | "source": [ 753 | "np.where(np.isnan(x), ma.array(x, mask=np.isnan(x)).mean(axis=0), x)" 754 | ] 755 | }, 756 | { 757 | "cell_type": "markdown", 758 | "metadata": {}, 759 | "source": [ 760 | "# Indexing, Slicing, Reshaping, Resizing, Broadcasting" 761 | ] 762 | }, 763 | { 764 | "cell_type": "code", 765 | "execution_count": null, 766 | "metadata": {}, 767 | "outputs": [], 768 | "source": [ 769 | "x = [\"USA\",\"France\", \"Germany\",\"England\"]\n", 770 | "x[2]" 771 | ] 772 | }, 773 | { 774 | "cell_type": "code", 775 | "execution_count": null, 776 | "metadata": {}, 777 | "outputs": [], 778 | "source": [ 779 | "x = ('USA',3,\"France\",4)\n", 780 | "x[2]" 781 | ] 782 | }, 783 | { 784 | "cell_type": "code", 785 | "execution_count": null, 786 | "metadata": {}, 787 | "outputs": [], 788 | "source": [ 789 | "x = np.arange(10)\n", 790 | "x" 791 | ] 792 | }, 793 | { 794 | "cell_type": "code", 795 | "execution_count": null, 796 | "metadata": {}, 797 | "outputs": [], 798 | "source": [ 799 | "x[5]" 800 | ] 801 | }, 802 | { 803 | "cell_type": "code", 804 | "execution_count": null, 805 | "metadata": {}, 806 | "outputs": [], 807 | "source": [ 808 | "x[-2]" 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": null, 814 | "metadata": {}, 815 | "outputs": [], 816 | "source": [ 817 | "x[2:8]" 818 | ] 819 | }, 820 | { 821 | "cell_type": "code", 822 | "execution_count": null, 823 | "metadata": {}, 824 | "outputs": [], 825 | "source": [ 826 | "x[:]" 827 | ] 828 | }, 829 | { 830 | "cell_type": "code", 831 | "execution_count": null, 832 | "metadata": {}, 833 | "outputs": [], 834 | "source": [ 835 | "x[2:8:2]" 836 | ] 837 | }, 838 | { 839 | "cell_type": "code", 840 | "execution_count": null, 841 | "metadata": {}, 842 | "outputs": [], 843 | "source": [ 844 | "x = np.reshape(np.arange(16),(4,4))\n", 845 | "x" 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": null, 851 | "metadata": {}, 852 | "outputs": [], 853 | "source": [ 854 | "x[1:3]" 855 | ] 856 | }, 857 | { 858 | "cell_type": "code", 859 | "execution_count": null, 860 | "metadata": {}, 861 | "outputs": [], 862 | "source": [ 863 | "x[:,1:3]" 864 | ] 865 | }, 866 | { 867 | "cell_type": "code", 868 | "execution_count": null, 869 | "metadata": {}, 870 | "outputs": [], 871 | "source": [ 872 | "x[1:3,1:3]" 873 | ] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "execution_count": null, 878 | "metadata": {}, 879 | "outputs": [], 880 | "source": [ 881 | "x = np.reshape(np.arange(16),(4,4))\n", 882 | "x" 883 | ] 884 | }, 885 | { 886 | "cell_type": "code", 887 | "execution_count": null, 888 | "metadata": {}, 889 | "outputs": [], 890 | "source": [ 891 | "x[[0,1,2],[0,1,3]]" 892 | ] 893 | }, 894 | { 895 | "cell_type": "code", 896 | "execution_count": null, 897 | "metadata": {}, 898 | "outputs": [], 899 | "source": [ 900 | "x = np.arange(16).reshape(4,4)\n", 901 | "x" 902 | ] 903 | }, 904 | { 905 | "cell_type": "code", 906 | "execution_count": null, 907 | "metadata": {}, 908 | "outputs": [], 909 | "source": [ 910 | "np.resize(x,(2,2))" 911 | ] 912 | }, 913 | { 914 | "cell_type": "code", 915 | "execution_count": null, 916 | "metadata": {}, 917 | "outputs": [], 918 | "source": [ 919 | "np.resize(x,(6,6))" 920 | ] 921 | }, 922 | { 923 | "cell_type": "code", 924 | "execution_count": null, 925 | "metadata": {}, 926 | "outputs": [], 927 | "source": [ 928 | "x = np.arange(16).reshape(4,4)\n", 929 | "y = np.arange(6).reshape(2,3)\n", 930 | "x+y" 931 | ] 932 | }, 933 | { 934 | "cell_type": "code", 935 | "execution_count": null, 936 | "metadata": {}, 937 | "outputs": [], 938 | "source": [ 939 | "x = np.ones(16).reshape(4,4)\n", 940 | "y = np.arange(4)\n", 941 | "x*y" 942 | ] 943 | }, 944 | { 945 | "cell_type": "code", 946 | "execution_count": null, 947 | "metadata": {}, 948 | "outputs": [], 949 | "source": [ 950 | "x = np.arange(4).reshape(2,2)\n", 951 | "x" 952 | ] 953 | }, 954 | { 955 | "cell_type": "code", 956 | "execution_count": null, 957 | "metadata": {}, 958 | "outputs": [], 959 | "source": [ 960 | "y = np.arange(2).reshape(1,2)\n", 961 | "y" 962 | ] 963 | }, 964 | { 965 | "cell_type": "code", 966 | "execution_count": null, 967 | "metadata": {}, 968 | "outputs": [], 969 | "source": [ 970 | "x*y" 971 | ] 972 | } 973 | ], 974 | "metadata": { 975 | "kernelspec": { 976 | "display_name": "Python 3", 977 | "language": "python", 978 | "name": "python3" 979 | }, 980 | "language_info": { 981 | "codemirror_mode": { 982 | "name": "ipython", 983 | "version": 3 984 | }, 985 | "file_extension": ".py", 986 | "mimetype": "text/x-python", 987 | "name": "python", 988 | "nbconvert_exporter": "python", 989 | "pygments_lexer": "ipython3", 990 | "version": "3.6.5" 991 | } 992 | }, 993 | "nbformat": 4, 994 | "nbformat_minor": 2 995 | } 996 | -------------------------------------------------------------------------------- /Chapter07/Chapter07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "\n", 11 | "array_x = np.array([100.12, 120.23, 130.91])\n" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 3, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "float64\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "print(array_x.dtype)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 39, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "{'complex': [numpy.complex64, numpy.complex128, numpy.complex256],\n", 40 | " 'float': [numpy.float16, numpy.float32, numpy.float64, numpy.float128],\n", 41 | " 'int': [numpy.int8, numpy.int16, numpy.int32, numpy.int64],\n", 42 | " 'others': [bool, object, bytes, str, numpy.void],\n", 43 | " 'uint': [numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64]}" 44 | ] 45 | }, 46 | "execution_count": 39, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "np.sctypes" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 4, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "[numpy.float64,\n", 64 | " numpy.floating,\n", 65 | " numpy.inexact,\n", 66 | " numpy.number,\n", 67 | " numpy.generic,\n", 68 | " float,\n", 69 | " object]" 70 | ] 71 | }, 72 | "execution_count": 4, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "np.float64.mro()" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 5, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "[numpy.int64,\n", 90 | " numpy.signedinteger,\n", 91 | " numpy.integer,\n", 92 | " numpy.number,\n", 93 | " numpy.generic,\n", 94 | " object]" 95 | ] 96 | }, 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "np.int64.mro()" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 55, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "[numpy.str_, str, numpy.character, numpy.flexible, numpy.generic, object]" 115 | ] 116 | }, 117 | "execution_count": 55, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "np.unicode_.mro()" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 15, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "8" 135 | ] 136 | }, 137 | "execution_count": 15, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "np.float64(100.12).nbytes" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 35, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "4" 155 | ] 156 | }, 157 | "execution_count": 35, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "np.str_('n').nbytes" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 36, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "20" 175 | ] 176 | }, 177 | "execution_count": 36, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "np.str_('numpy').nbytes" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 42, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "data": { 193 | "text/plain": [ 194 | "20" 195 | ] 196 | }, 197 | "execution_count": 42, 198 | "metadata": {}, 199 | "output_type": "execute_result" 200 | } 201 | ], 202 | "source": [ 203 | "np.str_('').nbytes" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 16, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "24" 215 | ] 216 | }, 217 | "execution_count": 16, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "np.float64(array_x).nbytes" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 22, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "array_x2 = array_x.astype(np.float32)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 24, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "data": { 242 | "text/plain": [ 243 | "array([100.12, 120.23, 130.91], dtype=float32)" 244 | ] 245 | }, 246 | "execution_count": 24, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "array_x2" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 25, 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "data": { 262 | "text/plain": [ 263 | "12" 264 | ] 265 | }, 266 | "execution_count": 25, 267 | "metadata": {}, 268 | "output_type": "execute_result" 269 | } 270 | ], 271 | "source": [ 272 | "np.float32(array_x2).nbytes" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 2, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "data": { 282 | "text/plain": [ 283 | "{'data': (140547047960768, False),\n", 284 | " 'descr': [('', ':1()\n", 838 | " 1 0.000 0.000 0.065 0.065 {built-in method builtins.exec}\n", 839 | " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n", 840 | "\n", 841 | "\n" 842 | ] 843 | } 844 | ], 845 | "source": [ 846 | "import cProfile\n", 847 | "import re\n", 848 | "\n", 849 | "cProfile.run('array_x *= 2')" 850 | ] 851 | }, 852 | { 853 | "cell_type": "code", 854 | "execution_count": 20, 855 | "metadata": {}, 856 | "outputs": [ 857 | { 858 | "name": "stdout", 859 | "output_type": "stream", 860 | "text": [ 861 | " 3 function calls in 0.318 seconds\n", 862 | "\n", 863 | " Ordered by: standard name\n", 864 | "\n", 865 | " ncalls tottime percall cumtime percall filename:lineno(function)\n", 866 | " 1 0.318 0.318 0.318 0.318 :1()\n", 867 | " 1 0.000 0.000 0.318 0.318 {built-in method builtins.exec}\n", 868 | " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n", 869 | "\n", 870 | "\n" 871 | ] 872 | } 873 | ], 874 | "source": [ 875 | "import cProfile\n", 876 | "import re\n", 877 | "\n", 878 | "cProfile.run('array_y = array_x * 2')" 879 | ] 880 | }, 881 | { 882 | "cell_type": "code", 883 | "execution_count": 84, 884 | "metadata": {}, 885 | "outputs": [], 886 | "source": [ 887 | "shape_x = (8000,3000)\n", 888 | "array_x = np.random.random_sample(shape_x)" 889 | ] 890 | }, 891 | { 892 | "cell_type": "code", 893 | "execution_count": 85, 894 | "metadata": {}, 895 | "outputs": [ 896 | { 897 | "name": "stdout", 898 | "output_type": "stream", 899 | "text": [ 900 | "176 ms ± 2.32 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" 901 | ] 902 | } 903 | ], 904 | "source": [ 905 | "%%timeit\n", 906 | "np.cumprod(array_x)" 907 | ] 908 | }, 909 | { 910 | "cell_type": "code", 911 | "execution_count": 86, 912 | "metadata": {}, 913 | "outputs": [], 914 | "source": [ 915 | "# Output array should have same type and dimensions with expected output of the operations\n", 916 | "output_array = np.zeros(array_x.shape[0] * array_x.shape[1])" 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": 87, 922 | "metadata": {}, 923 | "outputs": [ 924 | { 925 | "name": "stdout", 926 | "output_type": "stream", 927 | "text": [ 928 | "86.4 ms ± 1.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" 929 | ] 930 | } 931 | ], 932 | "source": [ 933 | "%%timeit\n", 934 | "np.cumprod(array_x, out=output_array)" 935 | ] 936 | }, 937 | { 938 | "cell_type": "code", 939 | "execution_count": 99, 940 | "metadata": {}, 941 | "outputs": [ 942 | { 943 | "name": "stdout", 944 | "output_type": "stream", 945 | "text": [ 946 | "blas_mkl_info:\n", 947 | " NOT AVAILABLE\n", 948 | "blis_info:\n", 949 | " NOT AVAILABLE\n", 950 | "openblas_info:\n", 951 | " NOT AVAILABLE\n", 952 | "atlas_3_10_blas_threads_info:\n", 953 | " NOT AVAILABLE\n", 954 | "atlas_3_10_blas_info:\n", 955 | " NOT AVAILABLE\n", 956 | "atlas_blas_threads_info:\n", 957 | " NOT AVAILABLE\n", 958 | "atlas_blas_info:\n", 959 | " NOT AVAILABLE\n", 960 | "blas_opt_info:\n", 961 | " extra_compile_args = ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers']\n", 962 | " extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']\n", 963 | " define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]\n", 964 | "lapack_mkl_info:\n", 965 | " NOT AVAILABLE\n", 966 | "openblas_lapack_info:\n", 967 | " NOT AVAILABLE\n", 968 | "openblas_clapack_info:\n", 969 | " NOT AVAILABLE\n", 970 | "atlas_3_10_threads_info:\n", 971 | " NOT AVAILABLE\n", 972 | "atlas_3_10_info:\n", 973 | " NOT AVAILABLE\n", 974 | "atlas_threads_info:\n", 975 | " NOT AVAILABLE\n", 976 | "atlas_info:\n", 977 | " NOT AVAILABLE\n", 978 | "lapack_opt_info:\n", 979 | " extra_compile_args = ['-msse3']\n", 980 | " extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']\n", 981 | " define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]\n" 982 | ] 983 | } 984 | ], 985 | "source": [ 986 | "np.__config__.show()" 987 | ] 988 | }, 989 | { 990 | "cell_type": "code", 991 | "execution_count": 3, 992 | "metadata": {}, 993 | "outputs": [ 994 | { 995 | "name": "stdout", 996 | "output_type": "stream", 997 | "text": [ 998 | "Requirement already satisfied: vprof in /Users/umitcakmak/anaconda/lib/python3.6/site-packages\n", 999 | "Requirement already satisfied: psutil>=3 in /Users/umitcakmak/anaconda/lib/python3.6/site-packages (from vprof)\n", 1000 | "Requirement already satisfied: six>=1.10.0 in /Users/umitcakmak/anaconda/lib/python3.6/site-packages (from vprof)\n", 1001 | "\u001b[33mYou are using pip version 9.0.1, however version 10.0.1 is available.\n", 1002 | "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" 1003 | ] 1004 | } 1005 | ], 1006 | "source": [ 1007 | "!pip install vprof" 1008 | ] 1009 | }, 1010 | { 1011 | "cell_type": "code", 1012 | "execution_count": null, 1013 | "metadata": {}, 1014 | "outputs": [], 1015 | "source": [] 1016 | } 1017 | ], 1018 | "metadata": { 1019 | "kernelspec": { 1020 | "display_name": "Python 3", 1021 | "language": "python", 1022 | "name": "python3" 1023 | }, 1024 | "language_info": { 1025 | "codemirror_mode": { 1026 | "name": "ipython", 1027 | "version": 3 1028 | }, 1029 | "file_extension": ".py", 1030 | "mimetype": "text/x-python", 1031 | "name": "python", 1032 | "nbconvert_exporter": "python", 1033 | "pygments_lexer": "ipython3", 1034 | "version": "3.6.1" 1035 | } 1036 | }, 1037 | "nbformat": 4, 1038 | "nbformat_minor": 2 1039 | } 1040 | -------------------------------------------------------------------------------- /Chapter02/Linear Algebra with Numpy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Vector and matrix mathematics " 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "[[0 1]\n", 20 | " [2 3]\n", 21 | " [4 5]]\n", 22 | "[[0 1 2 3 4]\n", 23 | " [5 6 7 8 9]]\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "import numpy as np \n", 29 | "a = np.arange(6).reshape(3,2) \n", 30 | "b = np.arange(10).reshape(2,5) \n", 31 | "print(a) \n", 32 | "print(b)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "array([[ 5, 6, 7, 8, 9],\n", 44 | " [15, 20, 25, 30, 35],\n", 45 | " [25, 34, 43, 52, 61]])" 46 | ] 47 | }, 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "np.dot(a,b)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "array([[ 5, 6, 7, 8, 9],\n", 66 | " [15, 20, 25, 30, 35],\n", 67 | " [25, 34, 43, 52, 61]])" 68 | ] 69 | }, 70 | "execution_count": 3, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "np.matmul(a,b)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "array([[ 5, 6, 7, 8, 9],\n", 88 | " [15, 20, 25, 30, 35],\n", 89 | " [25, 34, 43, 52, 61]])" 90 | ] 91 | }, 92 | "execution_count": 4, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "a@b" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "array([[ 1700, 1855, 2010, 2165, 2320],\n", 110 | " [ 5300, 5770, 6240, 6710, 7180],\n", 111 | " [ 8900, 9685, 10470, 11255, 12040],\n", 112 | " [12500, 13600, 14700, 15800, 16900]])" 113 | ] 114 | }, 115 | "execution_count": 5, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "from numpy.linalg import multi_dot \n", 122 | "a = np.arange(12).reshape(4,3) \n", 123 | "b = np.arange(15).reshape(3,5) \n", 124 | "c = np.arange(25).reshape(5,5) \n", 125 | "multi_dot([a, b, c])" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 6, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "array([[ 1700, 1855, 2010, 2165, 2320],\n", 137 | " [ 5300, 5770, 6240, 6710, 7180],\n", 138 | " [ 8900, 9685, 10470, 11255, 12040],\n", 139 | " [12500, 13600, 14700, 15800, 16900]])" 140 | ] 141 | }, 142 | "execution_count": 6, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "a.dot(b).dot(c)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 7, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "Multi_dot tooks 0.12232375144958496 seconds.\n", 161 | "Chain dot tooks 0.15441036224365234 seconds.\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "import numpy as np \n", 167 | "from numpy.linalg import multi_dot \n", 168 | "import time \n", 169 | "a = np.arange(120000).reshape(400,300) \n", 170 | "b = np.arange(150000).reshape(300,500) \n", 171 | "c = np.arange(200000).reshape(500,400) \n", 172 | "start = time.time() \n", 173 | "multi_dot([a,b,c]) \n", 174 | "ft = time.time()-start \n", 175 | "print ('Multi_dot tooks', time.time()-start,'seconds.') \n", 176 | "a.dot(b).dot(c) \n", 177 | "print ('Chain dot tooks', time.time()-start-ft,'seconds.')" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 8, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "[[0 1 2]\n", 190 | " [3 4 5]\n", 191 | " [6 7 8]]\n", 192 | "[0 1 2]\n" 193 | ] 194 | } 195 | ], 196 | "source": [ 197 | "a = np.arange(9).reshape(3,3) \n", 198 | "b = np.arange(3) \n", 199 | "print(a) \n", 200 | "print(b)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 9, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "array([ 5, 14, 23])" 212 | ] 213 | }, 214 | "execution_count": 9, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "np.inner(a,b)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 10, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "array([[ 0, 0, 0],\n", 232 | " [ 0, 1, 2],\n", 233 | " [ 0, 2, 4],\n", 234 | " [ 0, 3, 6],\n", 235 | " [ 0, 4, 8],\n", 236 | " [ 0, 5, 10],\n", 237 | " [ 0, 6, 12],\n", 238 | " [ 0, 7, 14],\n", 239 | " [ 0, 8, 16]])" 240 | ] 241 | }, 242 | "execution_count": 10, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "np.outer(a,b)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 11, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "data": { 258 | "text/plain": [ 259 | "1" 260 | ] 261 | }, 262 | "execution_count": 11, 263 | "metadata": {}, 264 | "output_type": "execute_result" 265 | } 266 | ], 267 | "source": [ 268 | "a = np.arange(9) \n", 269 | "np.ndim(a)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 12, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "data": { 279 | "text/plain": [ 280 | "array([[ 0, 0, 0],\n", 281 | " [ 0, 1, 2],\n", 282 | " [ 0, 2, 4],\n", 283 | " [ 0, 3, 6],\n", 284 | " [ 0, 4, 8],\n", 285 | " [ 0, 5, 10],\n", 286 | " [ 0, 6, 12],\n", 287 | " [ 0, 7, 14],\n", 288 | " [ 0, 8, 16]])" 289 | ] 290 | }, 291 | "execution_count": 12, 292 | "metadata": {}, 293 | "output_type": "execute_result" 294 | } 295 | ], 296 | "source": [ 297 | "np.outer(a,b)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 13, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "[[[ 0 1]\n", 310 | " [ 2 3]\n", 311 | " [ 4 5]]\n", 312 | "\n", 313 | " [[ 6 7]\n", 314 | " [ 8 9]\n", 315 | " [10 11]]]\n", 316 | "[[[ 0 1 2 3 4 5 6 7]\n", 317 | " [ 8 9 10 11 12 13 14 15]]\n", 318 | "\n", 319 | " [[16 17 18 19 20 21 22 23]\n", 320 | " [24 25 26 27 28 29 30 31]]\n", 321 | "\n", 322 | " [[32 33 34 35 36 37 38 39]\n", 323 | " [40 41 42 43 44 45 46 47]]]\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "a = np.arange(12).reshape(2,3,2) \n", 329 | "b = np.arange(48).reshape(3,2,8) \n", 330 | "c = np.tensordot(a,b, axes =([1,0],[0,1])) \n", 331 | "print(a) \n", 332 | "print(b)" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 14, 338 | "metadata": {}, 339 | "outputs": [ 340 | { 341 | "data": { 342 | "text/plain": [ 343 | "array([[ 800, 830, 860, 890, 920, 950, 980, 1010],\n", 344 | " [ 920, 956, 992, 1028, 1064, 1100, 1136, 1172]])" 345 | ] 346 | }, 347 | "execution_count": 14, 348 | "metadata": {}, 349 | "output_type": "execute_result" 350 | } 351 | ], 352 | "source": [ 353 | " c" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "# What's Eigenvalue and how to compute it ? " 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 15, 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "data": { 370 | "text/plain": [ 371 | "(569, 30)" 372 | ] 373 | }, 374 | "execution_count": 15, 375 | "metadata": {}, 376 | "output_type": "execute_result" 377 | } 378 | ], 379 | "source": [ 380 | "import numpy as np \n", 381 | "from sklearn import decomposition, datasets\n", 382 | "from sklearn.preprocessing import StandardScaler \n", 383 | "data = datasets.load_breast_cancer() \n", 384 | "cancer = data.data \n", 385 | "cancer = StandardScaler().fit_transform(cancer) \n", 386 | "cancer.shape" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 16, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "array([[17.99],\n", 398 | " [20.57],\n", 399 | " [19.69],\n", 400 | " [11.42],\n", 401 | " [20.29],\n", 402 | " [12.45],\n", 403 | " [18.25],\n", 404 | " [13.71],\n", 405 | " [13. ],\n", 406 | " [12.46]])" 407 | ] 408 | }, 409 | "execution_count": 16, 410 | "metadata": {}, 411 | "output_type": "execute_result" 412 | } 413 | ], 414 | "source": [ 415 | "before_transformation = data.data \n", 416 | "before_transformation[:10,:1]" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 17, 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "data": { 426 | "text/plain": [ 427 | "array([[ 1.09706398],\n", 428 | " [ 1.82982061],\n", 429 | " [ 1.57988811],\n", 430 | " [-0.76890929],\n", 431 | " [ 1.75029663],\n", 432 | " [-0.47637467],\n", 433 | " [ 1.17090767],\n", 434 | " [-0.11851678],\n", 435 | " [-0.32016686],\n", 436 | " [-0.47353452]])" 437 | ] 438 | }, 439 | "execution_count": 17, 440 | "metadata": {}, 441 | "output_type": "execute_result" 442 | } 443 | ], 444 | "source": [ 445 | "cancer[:10,:1]" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": 18, 451 | "metadata": {}, 452 | "outputs": [ 453 | { 454 | "data": { 455 | "text/plain": [ 456 | "(30, 30)" 457 | ] 458 | }, 459 | "execution_count": 18, 460 | "metadata": {}, 461 | "output_type": "execute_result" 462 | } 463 | ], 464 | "source": [ 465 | "covariance_matrix = np.cov(cancer,rowvar=False)\n", 466 | "covariance_matrix.shape" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 19, 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "eig_val_cov, eig_vec_cov = np.linalg.eig(covariance_matrix)\n", 476 | "eig_pairs = [(np.abs(eig_val_cov[i]), eig_vec_cov[:,i]) for i in range(len(eig_val_cov))]" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 20, 482 | "metadata": {}, 483 | "outputs": [ 484 | { 485 | "name": "stdout", 486 | "output_type": "stream", 487 | "text": [ 488 | "13.304990794374557\n", 489 | "5.701374603726148\n", 490 | "2.8229101550062317\n", 491 | "1.9841275177302011\n", 492 | "1.6516332423301192\n", 493 | "1.2094822398029716\n", 494 | "0.6764088817009057\n", 495 | "0.47745625468950736\n", 496 | "0.41762878210781673\n", 497 | "0.3513108748817331\n", 498 | "0.2944331534911652\n", 499 | "0.26162116136612107\n", 500 | "0.24178242132831376\n", 501 | "0.15728614921759343\n", 502 | "0.09430069560105549\n", 503 | "0.0800034044773769\n", 504 | "0.05950361353043193\n", 505 | "0.052711422210147865\n", 506 | "0.049564700212981636\n", 507 | "0.031214260553066364\n", 508 | "0.030025663090428353\n", 509 | "0.027487711338904493\n", 510 | "0.024383691354591026\n", 511 | "0.018086793984305585\n", 512 | "0.015508527134418866\n", 513 | "0.008192037117606807\n", 514 | "0.006912612579184545\n", 515 | "0.00159213600119781\n", 516 | "0.0007501214127185791\n", 517 | "0.00013327905666374494\n" 518 | ] 519 | } 520 | ], 521 | "source": [ 522 | "sorted_pairs = eig_pairs.sort(key=lambda x: x[0], reverse=True)\n", 523 | "for i in eig_pairs:\n", 524 | " print(i[0])" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 21, 530 | "metadata": {}, 531 | "outputs": [ 532 | { 533 | "data": { 534 | "text/plain": [ 535 | "array([9.19283683, 1.94858307])" 536 | ] 537 | }, 538 | "execution_count": 21, 539 | "metadata": {}, 540 | "output_type": "execute_result" 541 | } 542 | ], 543 | "source": [ 544 | "matrix_w = np.hstack((eig_pairs[0][1].reshape(30,1), eig_pairs[1][1].reshape(30,1)))\n", 545 | "matrix_w.shape \n", 546 | "transformed = matrix_w.T.dot(cancer.T) \n", 547 | "transformed = transformed.T \n", 548 | "transformed[0]" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": 22, 554 | "metadata": {}, 555 | "outputs": [ 556 | { 557 | "data": { 558 | "text/plain": [ 559 | "(569, 2)" 560 | ] 561 | }, 562 | "execution_count": 22, 563 | "metadata": {}, 564 | "output_type": "execute_result" 565 | } 566 | ], 567 | "source": [ 568 | "transformed.shape" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 23, 574 | "metadata": {}, 575 | "outputs": [ 576 | { 577 | "data": { 578 | "text/plain": [ 579 | "array([9.19283683, 1.94858307])" 580 | ] 581 | }, 582 | "execution_count": 23, 583 | "metadata": {}, 584 | "output_type": "execute_result" 585 | } 586 | ], 587 | "source": [ 588 | "import numpy as np \n", 589 | "from sklearn import decomposition \n", 590 | "from sklearn import datasets \n", 591 | "from sklearn.preprocessing import StandardScaler \n", 592 | "pca = decomposition.PCA(n_components=2) \n", 593 | "x_std = StandardScaler().fit_transform(cancer)\n", 594 | "pca.fit_transform(x_std)[0]" 595 | ] 596 | }, 597 | { 598 | "cell_type": "markdown", 599 | "metadata": {}, 600 | "source": [ 601 | "# Computing Norm and Determinant" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 24, 607 | "metadata": {}, 608 | "outputs": [ 609 | { 610 | "data": { 611 | "text/plain": [ 612 | "3.0" 613 | ] 614 | }, 615 | "execution_count": 24, 616 | "metadata": {}, 617 | "output_type": "execute_result" 618 | } 619 | ], 620 | "source": [ 621 | "import numpy as np \n", 622 | "x = np.array([2,5,9,0]) \n", 623 | "np.linalg.norm(x,ord=0)" 624 | ] 625 | }, 626 | { 627 | "cell_type": "code", 628 | "execution_count": 25, 629 | "metadata": {}, 630 | "outputs": [ 631 | { 632 | "data": { 633 | "text/plain": [ 634 | "16.0" 635 | ] 636 | }, 637 | "execution_count": 25, 638 | "metadata": {}, 639 | "output_type": "execute_result" 640 | } 641 | ], 642 | "source": [ 643 | "np.linalg.norm(x,ord=1)" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": 26, 649 | "metadata": {}, 650 | "outputs": [ 651 | { 652 | "data": { 653 | "text/plain": [ 654 | "10.488088481701515" 655 | ] 656 | }, 657 | "execution_count": 26, 658 | "metadata": {}, 659 | "output_type": "execute_result" 660 | } 661 | ], 662 | "source": [ 663 | "np.linalg.norm(x,ord=2)" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": 27, 669 | "metadata": {}, 670 | "outputs": [ 671 | { 672 | "data": { 673 | "text/plain": [ 674 | "array([[ 3, 7, 6],\n", 675 | " [ -2, -5, 4],\n", 676 | " [ 1, 3, -14]])" 677 | ] 678 | }, 679 | "execution_count": 27, 680 | "metadata": {}, 681 | "output_type": "execute_result" 682 | } 683 | ], 684 | "source": [ 685 | "a = np.array([3,7,6,-2,-5,4,1,3,-14]).reshape(3,3)\n" 686 | ] 687 | }, 688 | { 689 | "cell_type": "code", 690 | "execution_count": 28, 691 | "metadata": {}, 692 | "outputs": [ 693 | { 694 | "data": { 695 | "text/plain": [ 696 | "array([[ 3, 7, 6],\n", 697 | " [ -2, -5, 4],\n", 698 | " [ 1, 3, -14]])" 699 | ] 700 | }, 701 | "execution_count": 28, 702 | "metadata": {}, 703 | "output_type": "execute_result" 704 | } 705 | ], 706 | "source": [ 707 | "a" 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": 29, 713 | "metadata": {}, 714 | "outputs": [ 715 | { 716 | "data": { 717 | "text/plain": [ 718 | "24.0" 719 | ] 720 | }, 721 | "execution_count": 29, 722 | "metadata": {}, 723 | "output_type": "execute_result" 724 | } 725 | ], 726 | "source": [ 727 | "np.linalg.norm(a, ord=1)" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": 30, 733 | "metadata": {}, 734 | "outputs": [ 735 | { 736 | "data": { 737 | "text/plain": [ 738 | "18.0" 739 | ] 740 | }, 741 | "execution_count": 30, 742 | "metadata": {}, 743 | "output_type": "execute_result" 744 | } 745 | ], 746 | "source": [ 747 | "np.linalg.norm(a, np.inf)" 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": 31, 753 | "metadata": {}, 754 | "outputs": [ 755 | { 756 | "data": { 757 | "text/plain": [ 758 | "15.8325010064061" 759 | ] 760 | }, 761 | "execution_count": 31, 762 | "metadata": {}, 763 | "output_type": "execute_result" 764 | } 765 | ], 766 | "source": [ 767 | "np.linalg.norm(a, ord=2)" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 32, 773 | "metadata": {}, 774 | "outputs": [], 775 | "source": [ 776 | "A= np.array([2,3,1,4]).reshape(2,2)\n" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 33, 782 | "metadata": {}, 783 | "outputs": [ 784 | { 785 | "data": { 786 | "text/plain": [ 787 | "array([[2, 3],\n", 788 | " [1, 4]])" 789 | ] 790 | }, 791 | "execution_count": 33, 792 | "metadata": {}, 793 | "output_type": "execute_result" 794 | } 795 | ], 796 | "source": [ 797 | "A" 798 | ] 799 | }, 800 | { 801 | "cell_type": "code", 802 | "execution_count": 34, 803 | "metadata": {}, 804 | "outputs": [ 805 | { 806 | "data": { 807 | "text/plain": [ 808 | "5.000000000000001" 809 | ] 810 | }, 811 | "execution_count": 34, 812 | "metadata": {}, 813 | "output_type": "execute_result" 814 | } 815 | ], 816 | "source": [ 817 | "np.linalg.det(A)" 818 | ] 819 | }, 820 | { 821 | "cell_type": "code", 822 | "execution_count": 35, 823 | "metadata": {}, 824 | "outputs": [ 825 | { 826 | "data": { 827 | "text/plain": [ 828 | "array([[2, 3, 5],\n", 829 | " [1, 4, 8],\n", 830 | " [5, 6, 2]])" 831 | ] 832 | }, 833 | "execution_count": 35, 834 | "metadata": {}, 835 | "output_type": "execute_result" 836 | } 837 | ], 838 | "source": [ 839 | "B= np.array([2,3,5,1,4,8,5,6,2]).reshape(3,3)\n" 840 | ] 841 | }, 842 | { 843 | "cell_type": "code", 844 | "execution_count": 36, 845 | "metadata": {}, 846 | "outputs": [ 847 | { 848 | "data": { 849 | "text/plain": [ 850 | "array([[2, 3, 5],\n", 851 | " [1, 4, 8],\n", 852 | " [5, 6, 2]])" 853 | ] 854 | }, 855 | "execution_count": 36, 856 | "metadata": {}, 857 | "output_type": "execute_result" 858 | } 859 | ], 860 | "source": [ 861 | "B" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": 37, 867 | "metadata": {}, 868 | "outputs": [ 869 | { 870 | "data": { 871 | "text/plain": [ 872 | "-36.0" 873 | ] 874 | }, 875 | "execution_count": 37, 876 | "metadata": {}, 877 | "output_type": "execute_result" 878 | } 879 | ], 880 | "source": [ 881 | "np.linalg.det(B)" 882 | ] 883 | }, 884 | { 885 | "cell_type": "code", 886 | "execution_count": 38, 887 | "metadata": {}, 888 | "outputs": [], 889 | "source": [ 890 | "a=np.arange(9).reshape(3,3)\n" 891 | ] 892 | }, 893 | { 894 | "cell_type": "code", 895 | "execution_count": 39, 896 | "metadata": {}, 897 | "outputs": [ 898 | { 899 | "data": { 900 | "text/plain": [ 901 | "array([[0, 1, 2],\n", 902 | " [3, 4, 5],\n", 903 | " [6, 7, 8]])" 904 | ] 905 | }, 906 | "execution_count": 39, 907 | "metadata": {}, 908 | "output_type": "execute_result" 909 | } 910 | ], 911 | "source": [ 912 | "a" 913 | ] 914 | }, 915 | { 916 | "cell_type": "code", 917 | "execution_count": 40, 918 | "metadata": {}, 919 | "outputs": [ 920 | { 921 | "data": { 922 | "text/plain": [ 923 | "12" 924 | ] 925 | }, 926 | "execution_count": 40, 927 | "metadata": {}, 928 | "output_type": "execute_result" 929 | } 930 | ], 931 | "source": [ 932 | "np.trace(a)" 933 | ] 934 | }, 935 | { 936 | "cell_type": "code", 937 | "execution_count": 41, 938 | "metadata": {}, 939 | "outputs": [], 940 | "source": [ 941 | "b = np.arange(27).reshape(3,3,3)\n" 942 | ] 943 | }, 944 | { 945 | "cell_type": "code", 946 | "execution_count": 42, 947 | "metadata": {}, 948 | "outputs": [ 949 | { 950 | "data": { 951 | "text/plain": [ 952 | "array([[[ 0, 1, 2],\n", 953 | " [ 3, 4, 5],\n", 954 | " [ 6, 7, 8]],\n", 955 | "\n", 956 | " [[ 9, 10, 11],\n", 957 | " [12, 13, 14],\n", 958 | " [15, 16, 17]],\n", 959 | "\n", 960 | " [[18, 19, 20],\n", 961 | " [21, 22, 23],\n", 962 | " [24, 25, 26]]])" 963 | ] 964 | }, 965 | "execution_count": 42, 966 | "metadata": {}, 967 | "output_type": "execute_result" 968 | } 969 | ], 970 | "source": [ 971 | "b" 972 | ] 973 | }, 974 | { 975 | "cell_type": "code", 976 | "execution_count": 43, 977 | "metadata": {}, 978 | "outputs": [ 979 | { 980 | "data": { 981 | "text/plain": [ 982 | "array([36, 39, 42])" 983 | ] 984 | }, 985 | "execution_count": 43, 986 | "metadata": {}, 987 | "output_type": "execute_result" 988 | } 989 | ], 990 | "source": [ 991 | "np.trace(b)" 992 | ] 993 | }, 994 | { 995 | "cell_type": "markdown", 996 | "metadata": {}, 997 | "source": [ 998 | "# Solving Equations" 999 | ] 1000 | }, 1001 | { 1002 | "cell_type": "code", 1003 | "execution_count": 44, 1004 | "metadata": {}, 1005 | "outputs": [ 1006 | { 1007 | "data": { 1008 | "text/plain": [ 1009 | "array([[2, 1, 2],\n", 1010 | " [3, 2, 1],\n", 1011 | " [0, 1, 1]])" 1012 | ] 1013 | }, 1014 | "execution_count": 44, 1015 | "metadata": {}, 1016 | "output_type": "execute_result" 1017 | } 1018 | ], 1019 | "source": [ 1020 | "A = np.array([[2, 1, 2], [3, 2, 1], [0, 1, 1]])\n", 1021 | "A" 1022 | ] 1023 | }, 1024 | { 1025 | "cell_type": "code", 1026 | "execution_count": 45, 1027 | "metadata": {}, 1028 | "outputs": [ 1029 | { 1030 | "data": { 1031 | "text/plain": [ 1032 | "array([8, 3, 4])" 1033 | ] 1034 | }, 1035 | "execution_count": 45, 1036 | "metadata": {}, 1037 | "output_type": "execute_result" 1038 | } 1039 | ], 1040 | "source": [ 1041 | "B = np.array([8,3,4])\n", 1042 | "B" 1043 | ] 1044 | }, 1045 | { 1046 | "cell_type": "code", 1047 | "execution_count": 46, 1048 | "metadata": {}, 1049 | "outputs": [ 1050 | { 1051 | "data": { 1052 | "text/plain": [ 1053 | "array([[ 0.2, 0.2, -0.6],\n", 1054 | " [-0.6, 0.4, 0.8],\n", 1055 | " [ 0.6, -0.4, 0.2]])" 1056 | ] 1057 | }, 1058 | "execution_count": 46, 1059 | "metadata": {}, 1060 | "output_type": "execute_result" 1061 | } 1062 | ], 1063 | "source": [ 1064 | "A_inv = np.linalg.inv(A)\n", 1065 | "A_inv" 1066 | ] 1067 | }, 1068 | { 1069 | "cell_type": "code", 1070 | "execution_count": 47, 1071 | "metadata": {}, 1072 | "outputs": [ 1073 | { 1074 | "data": { 1075 | "text/plain": [ 1076 | "array([-0.2, -0.4, 4.4])" 1077 | ] 1078 | }, 1079 | "execution_count": 47, 1080 | "metadata": {}, 1081 | "output_type": "execute_result" 1082 | } 1083 | ], 1084 | "source": [ 1085 | "np.dot(A_inv,B)" 1086 | ] 1087 | }, 1088 | { 1089 | "cell_type": "code", 1090 | "execution_count": 48, 1091 | "metadata": {}, 1092 | "outputs": [ 1093 | { 1094 | "data": { 1095 | "text/plain": [ 1096 | "array([-0.2, -0.4, 4.4])" 1097 | ] 1098 | }, 1099 | "execution_count": 48, 1100 | "metadata": {}, 1101 | "output_type": "execute_result" 1102 | } 1103 | ], 1104 | "source": [ 1105 | "A = np.array([[2, 1, 2], [3, 2, 1], [0, 1, 1]]) \n", 1106 | "B = np.array([8,3,4]) \n", 1107 | "x = np.linalg.solve(A, B) \n", 1108 | "x" 1109 | ] 1110 | }, 1111 | { 1112 | "cell_type": "code", 1113 | "execution_count": 49, 1114 | "metadata": {}, 1115 | "outputs": [ 1116 | { 1117 | "data": { 1118 | "text/plain": [ 1119 | "True" 1120 | ] 1121 | }, 1122 | "execution_count": 49, 1123 | "metadata": {}, 1124 | "output_type": "execute_result" 1125 | } 1126 | ], 1127 | "source": [ 1128 | "np.allclose(np.dot(A, x), B)" 1129 | ] 1130 | }, 1131 | { 1132 | "cell_type": "code", 1133 | "execution_count": 50, 1134 | "metadata": {}, 1135 | "outputs": [ 1136 | { 1137 | "data": { 1138 | "text/plain": [ 1139 | "array([[ 1., 1.],\n", 1140 | " [ 2., 1.],\n", 1141 | " [ 3., 1.],\n", 1142 | " [ 4., 1.],\n", 1143 | " [ 5., 1.],\n", 1144 | " [ 6., 1.],\n", 1145 | " [ 7., 1.],\n", 1146 | " [ 8., 1.],\n", 1147 | " [ 9., 1.],\n", 1148 | " [10., 1.]])" 1149 | ] 1150 | }, 1151 | "execution_count": 50, 1152 | "metadata": {}, 1153 | "output_type": "execute_result" 1154 | } 1155 | ], 1156 | "source": [ 1157 | "from numpy import arange,array,ones,linalg\n", 1158 | "from pylab import plot,show\n", 1159 | "x = np.arange(1,11) \n", 1160 | "A = np.vstack([x, np.ones(len(x))]).T\n", 1161 | "A" 1162 | ] 1163 | }, 1164 | { 1165 | "cell_type": "code", 1166 | "execution_count": 51, 1167 | "metadata": {}, 1168 | "outputs": [ 1169 | { 1170 | "name": "stderr", 1171 | "output_type": "stream", 1172 | "text": [ 1173 | "c:\\users\\mert_cuhadaroglu\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\ipykernel_launcher.py:2: 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", 1174 | "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", 1175 | " \n" 1176 | ] 1177 | }, 1178 | { 1179 | "data": { 1180 | "text/plain": [ 1181 | "array([1.05575758, 3.29333333])" 1182 | ] 1183 | }, 1184 | "execution_count": 51, 1185 | "metadata": {}, 1186 | "output_type": "execute_result" 1187 | } 1188 | ], 1189 | "source": [ 1190 | "y = [5, 6, 6.5, 7, 8,9.5, 10, 10.4,13.1,15.5] \n", 1191 | "w = linalg.lstsq(A,y)[0] \n", 1192 | "w" 1193 | ] 1194 | }, 1195 | { 1196 | "cell_type": "code", 1197 | "execution_count": 52, 1198 | "metadata": {}, 1199 | "outputs": [ 1200 | { 1201 | "data": { 1202 |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nrdl0ddTxRq2zo5izQGOgJ/dPcOwEpS9Ct1uqseG+4B7Ae0AHY2MzXtayAK9DowsyaEMB/t7uOiridCtWqdnEUWAgvdff1vbGMJAZ+NTgK+cPcyd68ExgFdIq4pHXxlZnsDVH9cmoqLKNBrycyMMEb6ibsPi7qeKKl18qbcfQmwwMzaVh/qCsyKsKQozQc6m9lO1X9nupKlD4g38wpwWfXnlwEvp+IijVPxojF1NHAJUGJmM6qP3e7ur0dYk6SPG4DRZrY98DlwRcT1RMLdPzKzscB0wsywYrJsxaiZjQGOB5qZ2UJgIDAEeMHMriT8o9czJdfWSlERkXjQkIuISEwo0EVEYkKBLiISEwp0EZGYUKCLiMSEAl1EJCYU6CIiMaFAFxGJif8HcTtJMHuUg+QAAAAASUVORK5CYII=\n", 1203 | "text/plain": [ 1204 | "
" 1205 | ] 1206 | }, 1207 | "metadata": {}, 1208 | "output_type": "display_data" 1209 | } 1210 | ], 1211 | "source": [ 1212 | "line = w[0]*x+w[1] \n", 1213 | "plot(x,line,'r-',x,y,'o') \n", 1214 | "show()" 1215 | ] 1216 | }, 1217 | { 1218 | "cell_type": "markdown", 1219 | "metadata": {}, 1220 | "source": [ 1221 | "# Computing Gradient" 1222 | ] 1223 | }, 1224 | { 1225 | "cell_type": "code", 1226 | "execution_count": 53, 1227 | "metadata": {}, 1228 | "outputs": [ 1229 | { 1230 | "data": { 1231 | "text/plain": [ 1232 | "array([2. , 2.5, 2. , 2.5, 3.5, 3. ])" 1233 | ] 1234 | }, 1235 | "execution_count": 53, 1236 | "metadata": {}, 1237 | "output_type": "execute_result" 1238 | } 1239 | ], 1240 | "source": [ 1241 | "a = np.array([1,3, 6, 7, 11, 14]) \n", 1242 | "gr = np.gradient(a) \n", 1243 | "gr" 1244 | ] 1245 | }, 1246 | { 1247 | "cell_type": "code", 1248 | "execution_count": 54, 1249 | "metadata": {}, 1250 | "outputs": [ 1251 | { 1252 | "data": { 1253 | "text/plain": [ 1254 | "[array([[6., 8., 8.],\n", 1255 | " [6., 8., 8.]]), array([[2. , 2.5, 3. ],\n", 1256 | " [4. , 3.5, 3. ]])]" 1257 | ] 1258 | }, 1259 | "execution_count": 54, 1260 | "metadata": {}, 1261 | "output_type": "execute_result" 1262 | } 1263 | ], 1264 | "source": [ 1265 | "a = np.array([1,3, 6, 7, 11, 14]). reshape(2,3) \n", 1266 | "gr = np.gradient(a) \n", 1267 | "gr" 1268 | ] 1269 | } 1270 | ], 1271 | "metadata": { 1272 | "kernelspec": { 1273 | "display_name": "Python 3", 1274 | "language": "python", 1275 | "name": "python3" 1276 | }, 1277 | "language_info": { 1278 | "codemirror_mode": { 1279 | "name": "ipython", 1280 | "version": 3 1281 | }, 1282 | "file_extension": ".py", 1283 | "mimetype": "text/x-python", 1284 | "name": "python", 1285 | "nbconvert_exporter": "python", 1286 | "pygments_lexer": "ipython3", 1287 | "version": "3.6.5" 1288 | } 1289 | }, 1290 | "nbformat": 4, 1291 | "nbformat_minor": 2 1292 | } 1293 | -------------------------------------------------------------------------------- /Chapter09/Chapter_09.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Performance Benchmarks" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Timer function" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 270, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import inspect\n", 24 | "import time\n", 25 | "from datetime import datetime\n", 26 | "\n", 27 | "def timer(*args, operation, n):\n", 28 | " \"\"\"\n", 29 | " Returns average time spent \n", 30 | " for given operation and arguments.\n", 31 | " \n", 32 | " Parameters\n", 33 | " ----------\n", 34 | " *args: list (of numpy.ndarray, numpy.matrixlib.defmatrix.matrix or both)\n", 35 | " one or more numpy vectors or matrices\n", 36 | " operation: function\n", 37 | " numpy or scipy operation to be applied to given arguments\n", 38 | " n: int \n", 39 | " number of iterations to apply given operation\n", 40 | "\n", 41 | " Returns\n", 42 | " -------\n", 43 | " avg_time_spent: double\n", 44 | " Average time spent to apply given operation\n", 45 | " std_time_spent: double\n", 46 | " Standard deviation of time spent to apply given operation\n", 47 | " \n", 48 | " Examples\n", 49 | " --------\n", 50 | " >>> import numpy as np\n", 51 | "\n", 52 | " >>> vec1 = np.array(np.random.rand(1000))\n", 53 | " >>> vec2 = np.array(np.random.rand(1000))\n", 54 | "\n", 55 | " >>> args = (vec1, vec2)\n", 56 | " \n", 57 | " >>> timer(*args, operation=np.dot, n=1000000)\n", 58 | " 8.942582607269287e-07\n", 59 | " \"\"\"\n", 60 | " \n", 61 | " # Following list will hold the\n", 62 | " # time spent value for each iteration\n", 63 | " time_spent = []\n", 64 | " \n", 65 | " # Configuration info\n", 66 | " print(\"\"\"\n", 67 | " -------------------------------------------\n", 68 | " \n", 69 | " ### {} Operation ###\n", 70 | " \n", 71 | " Arguments Info\n", 72 | " --------------\n", 73 | " args[0] Dimension: {},\n", 74 | " args[0] Shape: {},\n", 75 | " args[0] Length: {}\n", 76 | " \"\"\".format(operation.__name__,\n", 77 | " args[0].ndim,\n", 78 | " args[0].shape,\n", 79 | " len(args[0])))\n", 80 | " \n", 81 | " # If *args length is greater than 1, \n", 82 | " # print out the info for second argument\n", 83 | " args_len = 0\n", 84 | " for i, arg in enumerate(args):\n", 85 | " args_len += 1\n", 86 | " \n", 87 | " if args_len > 1:\n", 88 | " print(\"\"\"\n", 89 | " args[1] Dimension: {},\n", 90 | " args[1] Shape: {},\n", 91 | " args[1] Length: {}\n", 92 | " \"\"\".format(args[1].ndim,\n", 93 | " args[1].shape,\n", 94 | " len(args[1])))\n", 95 | " \n", 96 | " print(\"\"\"\n", 97 | " Operation Info\n", 98 | " --------------\n", 99 | " Name: {},\n", 100 | " Docstring: {}\n", 101 | " \n", 102 | " Iterations Info\n", 103 | " ---------------\n", 104 | " # of iterations: {}\"\"\".format(\n", 105 | " operation.__name__,\n", 106 | " operation.__doc__[:100] + \n", 107 | " \"... For more info type 'operation?'\",\n", 108 | " n))\n", 109 | " \n", 110 | " print(\"\"\"\n", 111 | " -> Starting {} of iterations at: {}\"\"\".format(n, datetime.now()))\n", 112 | " \n", 113 | " if args_len > 1:\n", 114 | " for i in range(n):\n", 115 | " start = time.time()\n", 116 | " operation(args[0], args[1])\n", 117 | " time_spent.append(time.time()-start)\n", 118 | " else:\n", 119 | " for i in range(n):\n", 120 | " start = time.time()\n", 121 | " operation(args[0])\n", 122 | " time_spent.append(time.time()-start)\n", 123 | " \n", 124 | " avg_time_spent = np.sum(time_spent) / n\n", 125 | " std_time_spent = np.std(time_spent)\n", 126 | " \n", 127 | " print(\"\"\"\n", 128 | " -> Average time spent: {} seconds,\n", 129 | " -> Std. deviation time spent: {} seconds\n", 130 | " \n", 131 | " -------------------------------------------\n", 132 | " \"\"\".format(avg_time_spent, std_time_spent))\n", 133 | " \n", 134 | " return avg_time_spent, std_time_spent" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "Docstring of timer function" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 249, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "\n", 154 | " Returns average time spent \n", 155 | " for given operation and arguments.\n", 156 | " \n", 157 | " Parameters\n", 158 | " ----------\n", 159 | " *args: list (of numpy.ndarray, numpy.matrixlib.defmatrix.matrix or both)\n", 160 | " one or more numpy vectors or matrices\n", 161 | " operation: function\n", 162 | " numpy or scipy operation to be applied to given arguments\n", 163 | " n: int \n", 164 | " number of iterations to apply given operation\n", 165 | "\n", 166 | " Returns\n", 167 | " -------\n", 168 | " avg_time_spent: double\n", 169 | " Average time spent to apply given operation\n", 170 | " std_time_spent: double\n", 171 | " Standard deviation of time spent to apply given operation\n", 172 | " \n", 173 | " Examples\n", 174 | " --------\n", 175 | " >>> import numpy as np\n", 176 | "\n", 177 | " >>> vec1 = np.array(np.random.rand(1000))\n", 178 | " >>> vec2 = np.array(np.random.rand(1000))\n", 179 | "\n", 180 | " >>> args = (vec1, vec2)\n", 181 | " \n", 182 | " >>> timer(*args, operation=np.dot, n=1000000)\n", 183 | " 8.942582607269287e-07\n", 184 | " \n" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "print(timer.__doc__)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "### Vector - Vector Product" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 250, 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [ 205 | "import numpy as np\n", 206 | "\n", 207 | "vec1 = np.array(np.random.rand(1000))\n", 208 | "vec2 = np.array(np.random.rand(1000))" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 271, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "\n", 221 | " -------------------------------------------\n", 222 | " \n", 223 | " ### dot Operation ###\n", 224 | " \n", 225 | " Arguments Info\n", 226 | " --------------\n", 227 | " args[0] Dimension: 1,\n", 228 | " args[0] Shape: (1000,),\n", 229 | " args[0] Length: 1000\n", 230 | " \n", 231 | "\n", 232 | " args[1] Dimension: 1,\n", 233 | " args[1] Shape: (1000,),\n", 234 | " args[1] Length: 1000\n", 235 | " \n", 236 | "\n", 237 | " Operation Info\n", 238 | " --------------\n", 239 | " Name: dot,\n", 240 | " Docstring: dot(a, b, out=None)\n", 241 | "\n", 242 | " Dot product of two arrays. Specifically,\n", 243 | "\n", 244 | " - If both `a` and `b` are 1-D... For more info type 'operation?'\n", 245 | " \n", 246 | " Iterations Info\n", 247 | " ---------------\n", 248 | " # of iterations: 1000000\n", 249 | "\n", 250 | " -> Starting 1000000 of iterations at: 2018-06-09 21:04:45.545944\n", 251 | "\n", 252 | " -> Average time spent: 1.0687923431396485e-06 seconds,\n", 253 | " -> Std. deviation time spent: 3.695899798414836e-06 seconds\n", 254 | " \n", 255 | " -------------------------------------------\n", 256 | " \n" 257 | ] 258 | }, 259 | { 260 | "data": { 261 | "text/plain": [ 262 | "(1.0687923431396485e-06, 3.695899798414836e-06)" 263 | ] 264 | }, 265 | "execution_count": 271, 266 | "metadata": {}, 267 | "output_type": "execute_result" 268 | } 269 | ], 270 | "source": [ 271 | "args = [vec1, vec2]\n", 272 | "timer(*args, operation=np.dot, n=1000000)" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "### Vector - Matrix Product" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 148, 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "name": "stdout", 289 | "output_type": "stream", 290 | "text": [ 291 | "\n", 292 | " Arguments Info\n", 293 | " --------------\n", 294 | " args[0] Dimension: 1,\n", 295 | " args[0] Shape: (1000,),\n", 296 | " args[0] Length: 1000\n", 297 | " \n", 298 | " args[1] Dimension: 2,\n", 299 | " args[1] Shape: (1000, 1000),\n", 300 | " args[1] Length: 1000\n", 301 | " \n", 302 | " Operation Info\n", 303 | " --------------\n", 304 | " Name: dot,\n", 305 | " Docstring: dot(a, b, out=None)\n", 306 | "\n", 307 | " Dot product of two arrays. Specifically,\n", 308 | "\n", 309 | " - If both `a` and `b` are 1-D... For more info type 'operation?'\n", 310 | " \n", 311 | " Iterations Info\n", 312 | " ---------------\n", 313 | " # of iterations: 1000000\n", 314 | "\n", 315 | " -> Starting 1000000 of iterations: 2018-06-09 19:13:07.013949 at \n", 316 | "\n", 317 | " -> Average time spent: 0.00020063393139839174 seconds,\n", 318 | " -> Std. deviation time spent: 9.579314466482879e-05 seconds\n", 319 | " \n" 320 | ] 321 | }, 322 | { 323 | "data": { 324 | "text/plain": [ 325 | "(0.00020063393139839174, 9.579314466482879e-05)" 326 | ] 327 | }, 328 | "execution_count": 148, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "mat1 = np.random.rand(1000,1000)\n", 335 | "\n", 336 | "args = [vec1, mat1]\n", 337 | "\n", 338 | "timer(*args, operation=np.dot, n=1000000)" 339 | ] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": {}, 344 | "source": [ 345 | "### Matrix - Matrix Product" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 272, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "name": "stdout", 355 | "output_type": "stream", 356 | "text": [ 357 | "\n", 358 | " -------------------------------------------\n", 359 | " \n", 360 | " ### dot Operation ###\n", 361 | " \n", 362 | " Arguments Info\n", 363 | " --------------\n", 364 | " args[0] Dimension: 2,\n", 365 | " args[0] Shape: (100, 100),\n", 366 | " args[0] Length: 100\n", 367 | " \n", 368 | "\n", 369 | " args[1] Dimension: 2,\n", 370 | " args[1] Shape: (100, 100),\n", 371 | " args[1] Length: 100\n", 372 | " \n", 373 | "\n", 374 | " Operation Info\n", 375 | " --------------\n", 376 | " Name: dot,\n", 377 | " Docstring: dot(a, b, out=None)\n", 378 | "\n", 379 | " Dot product of two arrays. Specifically,\n", 380 | "\n", 381 | " - If both `a` and `b` are 1-D... For more info type 'operation?'\n", 382 | " \n", 383 | " Iterations Info\n", 384 | " ---------------\n", 385 | " # of iterations: 1000000\n", 386 | "\n", 387 | " -> Starting 1000000 of iterations at: 2018-06-09 21:06:27.521641\n", 388 | "\n", 389 | " -> Average time spent: 7.155929303169251e-05 seconds,\n", 390 | " -> Std. deviation time spent: 1.8437869836742578e-05 seconds\n", 391 | " \n", 392 | " -------------------------------------------\n", 393 | " \n" 394 | ] 395 | }, 396 | { 397 | "data": { 398 | "text/plain": [ 399 | "(7.155929303169251e-05, 1.8437869836742578e-05)" 400 | ] 401 | }, 402 | "execution_count": 272, 403 | "metadata": {}, 404 | "output_type": "execute_result" 405 | } 406 | ], 407 | "source": [ 408 | "mat1 = np.random.rand(100,100)\n", 409 | "mat2 = np.random.rand(100,100)\n", 410 | "\n", 411 | "args = [mat1, mat2]\n", 412 | "\n", 413 | "timer(*args, operation=np.dot, n=1000000)" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "### Benchmark Script: linalg_benchmark.py" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 275, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "name": "stdout", 430 | "output_type": "stream", 431 | "text": [ 432 | "\n", 433 | " -------------------------------------------\n", 434 | " \n", 435 | " ### svd Operation ###\n", 436 | " \n", 437 | " Arguments Info\n", 438 | " --------------\n", 439 | " args[0] Dimension: 2,\n", 440 | " args[0] Shape: (10, 10),\n", 441 | " args[0] Length: 10\n", 442 | " \n", 443 | "\n", 444 | " Operation Info\n", 445 | " --------------\n", 446 | " Name: svd,\n", 447 | " Docstring: \n", 448 | " Singular Value Decomposition.\n", 449 | "\n", 450 | " When `a` is a 2D array, it is factorized as ``u @ np.diag(s)... For more info type 'operation?'\n", 451 | " \n", 452 | " Iterations Info\n", 453 | " ---------------\n", 454 | " # of iterations: 10\n", 455 | "\n", 456 | " -> Starting 10 of iterations at: 2018-06-09 21:27:12.911005\n", 457 | "\n", 458 | " -> Average time spent: 0.00016016960144042968 seconds,\n", 459 | " -> Std. deviation time spent: 0.00014555827400804429 seconds\n", 460 | " \n", 461 | " -------------------------------------------\n", 462 | " \n" 463 | ] 464 | } 465 | ], 466 | "source": [ 467 | "args = [m1]\n", 468 | "sv_dec = timer(*args, operation=np.linalg.svd, n=n)" 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": 279, 474 | "metadata": {}, 475 | "outputs": [ 476 | { 477 | "name": "stdout", 478 | "output_type": "stream", 479 | "text": [ 480 | "\n", 481 | " -------------------------------------------\n", 482 | " \n", 483 | " ### dot Operation ###\n", 484 | " \n", 485 | " Arguments Info\n", 486 | " --------------\n", 487 | " args[0] Dimension: 1,\n", 488 | " args[0] Shape: (100,),\n", 489 | " args[0] Length: 100\n", 490 | " \n", 491 | "\n", 492 | " args[1] Dimension: 1,\n", 493 | " args[1] Shape: (100,),\n", 494 | " args[1] Length: 100\n", 495 | " \n", 496 | "\n", 497 | " Operation Info\n", 498 | " --------------\n", 499 | " Name: dot,\n", 500 | " Docstring: dot(a, b, out=None)\n", 501 | "\n", 502 | " Dot product of two arrays. Specifically,\n", 503 | "\n", 504 | " - If both `a` and `b` are 1-D... For more info type 'operation?'\n", 505 | " \n", 506 | " Iterations Info\n", 507 | " ---------------\n", 508 | " # of iterations: 100\n", 509 | "\n", 510 | " -> Starting 100 of iterations at: 2018-06-09 21:30:10.204886\n", 511 | "\n", 512 | " -> Average time spent: 3.025531768798828e-06 seconds,\n", 513 | " -> Std. deviation time spent: 4.170580226605431e-06 seconds\n", 514 | " \n", 515 | " -------------------------------------------\n", 516 | " \n", 517 | "\n", 518 | " -------------------------------------------\n", 519 | " \n", 520 | " ### dot Operation ###\n", 521 | " \n", 522 | " Arguments Info\n", 523 | " --------------\n", 524 | " args[0] Dimension: 1,\n", 525 | " args[0] Shape: (100,),\n", 526 | " args[0] Length: 100\n", 527 | " \n", 528 | "\n", 529 | " args[1] Dimension: 2,\n", 530 | " args[1] Shape: (100, 100),\n", 531 | " args[1] Length: 100\n", 532 | " \n", 533 | "\n", 534 | " Operation Info\n", 535 | " --------------\n", 536 | " Name: dot,\n", 537 | " Docstring: dot(a, b, out=None)\n", 538 | "\n", 539 | " Dot product of two arrays. Specifically,\n", 540 | "\n", 541 | " - If both `a` and `b` are 1-D... For more info type 'operation?'\n", 542 | " \n", 543 | " Iterations Info\n", 544 | " ---------------\n", 545 | " # of iterations: 100\n", 546 | "\n", 547 | " -> Starting 100 of iterations at: 2018-06-09 21:30:10.206615\n", 548 | "\n", 549 | " -> Average time spent: 6.701946258544922e-06 seconds,\n", 550 | " -> Std. deviation time spent: 2.350765829031953e-05 seconds\n", 551 | " \n", 552 | " -------------------------------------------\n", 553 | " \n", 554 | "\n", 555 | " -------------------------------------------\n", 556 | " \n", 557 | " ### dot Operation ###\n", 558 | " \n", 559 | " Arguments Info\n", 560 | " --------------\n", 561 | " args[0] Dimension: 2,\n", 562 | " args[0] Shape: (100, 100),\n", 563 | " args[0] Length: 100\n", 564 | " \n", 565 | "\n", 566 | " args[1] Dimension: 2,\n", 567 | " args[1] Shape: (100, 100),\n", 568 | " args[1] Length: 100\n", 569 | " \n", 570 | "\n", 571 | " Operation Info\n", 572 | " --------------\n", 573 | " Name: dot,\n", 574 | " Docstring: dot(a, b, out=None)\n", 575 | "\n", 576 | " Dot product of two arrays. Specifically,\n", 577 | "\n", 578 | " - If both `a` and `b` are 1-D... For more info type 'operation?'\n", 579 | " \n", 580 | " Iterations Info\n", 581 | " ---------------\n", 582 | " # of iterations: 100\n", 583 | "\n", 584 | " -> Starting 100 of iterations at: 2018-06-09 21:30:10.207751\n", 585 | "\n", 586 | " -> Average time spent: 7.758855819702149e-05 seconds,\n", 587 | " -> Std. deviation time spent: 5.597527255858913e-05 seconds\n", 588 | " \n", 589 | " -------------------------------------------\n", 590 | " \n", 591 | "\n", 592 | " -------------------------------------------\n", 593 | " \n", 594 | " ### svd Operation ###\n", 595 | " \n", 596 | " Arguments Info\n", 597 | " --------------\n", 598 | " args[0] Dimension: 2,\n", 599 | " args[0] Shape: (100, 100),\n", 600 | " args[0] Length: 100\n", 601 | " \n", 602 | "\n", 603 | " Operation Info\n", 604 | " --------------\n", 605 | " Name: svd,\n", 606 | " Docstring: \n", 607 | " Singular Value Decomposition.\n", 608 | "\n", 609 | " When `a` is a 2D array, it is factorized as ``u @ np.diag(s)... For more info type 'operation?'\n", 610 | " \n", 611 | " Iterations Info\n", 612 | " ---------------\n", 613 | " # of iterations: 100\n", 614 | "\n", 615 | " -> Starting 100 of iterations at: 2018-06-09 21:30:10.216056\n", 616 | "\n", 617 | " -> Average time spent: 0.0029004621505737305 seconds,\n", 618 | " -> Std. deviation time spent: 0.00037338939600657325 seconds\n", 619 | " \n", 620 | " -------------------------------------------\n", 621 | " \n", 622 | "\n", 623 | " -------------------------------------------\n", 624 | " \n", 625 | " ### lu Operation ###\n", 626 | " \n", 627 | " Arguments Info\n", 628 | " --------------\n", 629 | " args[0] Dimension: 2,\n", 630 | " args[0] Shape: (100, 100),\n", 631 | " args[0] Length: 100\n", 632 | " \n", 633 | "\n", 634 | " Operation Info\n", 635 | " --------------\n", 636 | " Name: lu,\n", 637 | " Docstring: \n", 638 | " Compute pivoted LU decomposition of a matrix.\n", 639 | "\n", 640 | " The decomposition is::\n", 641 | "\n", 642 | " A = P L U\n", 643 | "\n", 644 | " ... For more info type 'operation?'\n", 645 | " \n", 646 | " Iterations Info\n", 647 | " ---------------\n", 648 | " # of iterations: 100\n", 649 | "\n", 650 | " -> Starting 100 of iterations at: 2018-06-09 21:30:10.506669\n", 651 | "\n", 652 | " -> Average time spent: 0.0005651497840881347 seconds,\n", 653 | " -> Std. deviation time spent: 0.000960517011055221 seconds\n", 654 | " \n", 655 | " -------------------------------------------\n", 656 | " \n", 657 | "\n", 658 | " -------------------------------------------\n", 659 | " \n", 660 | " ### qr Operation ###\n", 661 | " \n", 662 | " Arguments Info\n", 663 | " --------------\n", 664 | " args[0] Dimension: 2,\n", 665 | " args[0] Shape: (100, 100),\n", 666 | " args[0] Length: 100\n", 667 | " \n", 668 | "\n", 669 | " Operation Info\n", 670 | " --------------\n", 671 | " Name: qr,\n", 672 | " Docstring: \n", 673 | " Compute the qr factorization of a matrix.\n", 674 | "\n", 675 | " Factor the matrix `a` as *qr*, where `q` is orth... For more info type 'operation?'\n", 676 | " \n", 677 | " Iterations Info\n", 678 | " ---------------\n", 679 | " # of iterations: 100\n", 680 | "\n", 681 | " -> Starting 100 of iterations at: 2018-06-09 21:30:10.563713\n", 682 | "\n", 683 | " -> Average time spent: 0.00044762134552001954 seconds,\n", 684 | " -> Std. deviation time spent: 9.282425235122166e-05 seconds\n", 685 | " \n", 686 | " -------------------------------------------\n", 687 | " \n", 688 | "\n", 689 | " -------------------------------------------\n", 690 | " \n", 691 | " ### cholesky Operation ###\n", 692 | " \n", 693 | " Arguments Info\n", 694 | " --------------\n", 695 | " args[0] Dimension: 2,\n", 696 | " args[0] Shape: (3, 3),\n", 697 | " args[0] Length: 3\n", 698 | " \n", 699 | "\n", 700 | " Operation Info\n", 701 | " --------------\n", 702 | " Name: cholesky,\n", 703 | " Docstring: \n", 704 | " Compute the Cholesky decomposition of a matrix.\n", 705 | "\n", 706 | " Returns the Cholesky decomposition, :math:... For more info type 'operation?'\n", 707 | " \n", 708 | " Iterations Info\n", 709 | " ---------------\n", 710 | " # of iterations: 100\n", 711 | "\n", 712 | " -> Starting 100 of iterations at: 2018-06-09 21:30:10.609023\n", 713 | "\n", 714 | " -> Average time spent: 2.604961395263672e-05 seconds,\n", 715 | " -> Std. deviation time spent: 6.470560762880865e-06 seconds\n", 716 | " \n", 717 | " -------------------------------------------\n", 718 | " \n", 719 | "\n", 720 | " -------------------------------------------\n", 721 | " \n", 722 | " ### eig Operation ###\n", 723 | " \n", 724 | " Arguments Info\n", 725 | " --------------\n", 726 | " args[0] Dimension: 2,\n", 727 | " args[0] Shape: (100, 100),\n", 728 | " args[0] Length: 100\n", 729 | " \n", 730 | "\n", 731 | " Operation Info\n", 732 | " --------------\n", 733 | " Name: eig,\n", 734 | " Docstring: \n", 735 | " Compute the eigenvalues and right eigenvectors of a square array.\n", 736 | "\n", 737 | " Parameters\n", 738 | " ---------... For more info type 'operation?'\n", 739 | " \n", 740 | " Iterations Info\n", 741 | " ---------------\n", 742 | " # of iterations: 100\n", 743 | "\n", 744 | " -> Starting 100 of iterations at: 2018-06-09 21:30:10.612870\n", 745 | "\n", 746 | " -> Average time spent: 0.00851508617401123 seconds,\n", 747 | " -> Std. deviation time spent: 0.00043796216250974087 seconds\n", 748 | " \n", 749 | " -------------------------------------------\n", 750 | " \n", 751 | "\n", 752 | "V-V Product: (3.025531768798828e-06, 4.170580226605431e-06),\n", 753 | "V-M Product: (6.701946258544922e-06, 2.350765829031953e-05),\n", 754 | "M-M Product: (7.758855819702149e-05, 5.597527255858913e-05),\n", 755 | "SV Decomp.: (0.0029004621505737305, 0.00037338939600657325),\n", 756 | "LU Decomp.: (0.0005651497840881347, 0.000960517011055221),\n", 757 | "QR Decomp.: (0.00044762134552001954, 9.282425235122166e-05),\n", 758 | "Cholesky D.: (2.604961395263672e-05, 6.470560762880865e-06),\n", 759 | "Eigval Dec.: (0.00851508617401123, 0.00043796216250974087)\n", 760 | "\n", 761 | "\n", 762 | "NumPy Configuration:\n", 763 | "--------------------\n", 764 | "\n", 765 | "blas_mkl_info:\n", 766 | " NOT AVAILABLE\n", 767 | "blis_info:\n", 768 | " NOT AVAILABLE\n", 769 | "openblas_info:\n", 770 | " NOT AVAILABLE\n", 771 | "atlas_3_10_blas_threads_info:\n", 772 | " NOT AVAILABLE\n", 773 | "atlas_3_10_blas_info:\n", 774 | " NOT AVAILABLE\n", 775 | "atlas_blas_threads_info:\n", 776 | " NOT AVAILABLE\n", 777 | "atlas_blas_info:\n", 778 | " NOT AVAILABLE\n", 779 | "blas_opt_info:\n", 780 | " extra_compile_args = ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers']\n", 781 | " extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']\n", 782 | " define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]\n", 783 | "lapack_mkl_info:\n", 784 | " NOT AVAILABLE\n", 785 | "openblas_lapack_info:\n", 786 | " NOT AVAILABLE\n", 787 | "openblas_clapack_info:\n", 788 | " NOT AVAILABLE\n", 789 | "atlas_3_10_threads_info:\n", 790 | " NOT AVAILABLE\n", 791 | "atlas_3_10_info:\n", 792 | " NOT AVAILABLE\n", 793 | "atlas_threads_info:\n", 794 | " NOT AVAILABLE\n", 795 | "atlas_info:\n", 796 | " NOT AVAILABLE\n", 797 | "lapack_opt_info:\n", 798 | " extra_compile_args = ['-msse3']\n", 799 | " extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']\n", 800 | " define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]\n" 801 | ] 802 | } 803 | ], 804 | "source": [ 805 | "from __future__ import print_function\n", 806 | "\n", 807 | "import time\n", 808 | "from datetime import datetime\n", 809 | "\n", 810 | "\n", 811 | "import numpy as np\n", 812 | "from numpy.random import rand\n", 813 | "from numpy.linalg import qr\n", 814 | "from numpy.linalg import eig\n", 815 | "from scipy.linalg import lu\n", 816 | "from scipy.linalg import cholesky\n", 817 | "\n", 818 | "\n", 819 | "# Seed for reproducibility\n", 820 | "np.random.seed(8053)\n", 821 | "\n", 822 | "dim = 100\n", 823 | "n = 100\n", 824 | "\n", 825 | "v1, v2 = np.array(rand(dim)), np.array(rand(dim))\n", 826 | "m1, m2 = rand(dim, dim), rand(dim, dim)\n", 827 | "\n", 828 | "# Vector - Vector Product\n", 829 | "args = [v1, v2]\n", 830 | "vv_product = timer(*args, operation=np.dot, n=n)\n", 831 | "\n", 832 | "# Vector - Matrix Product\n", 833 | "args = [v1, m1]\n", 834 | "vm_product = timer(*args, operation=np.dot, n=n)\n", 835 | "\n", 836 | "# Matrix - Matrix Product\n", 837 | "args = [m1, m2]\n", 838 | "mm_product = timer(*args, operation=np.dot, n=n)\n", 839 | "\n", 840 | "# Singular-value Decomposition\n", 841 | "args = [m1]\n", 842 | "sv_dec = timer(*args, operation=np.linalg.svd, n=n)\n", 843 | "\n", 844 | "# LU Decomposition\n", 845 | "args = [m1]\n", 846 | "lu_dec = timer(*args, operation=lu, n=n)\n", 847 | "\n", 848 | "# QR Decomposition\n", 849 | "args = [m1]\n", 850 | "qr_dec = timer(*args, operation=qr, n=n)\n", 851 | "\n", 852 | "# Cholesky Decomposition\n", 853 | "M = np.array([[1, 3, 4], \n", 854 | " [2, 13, 15], \n", 855 | " [5, 31, 33]])\n", 856 | "args = [M]\n", 857 | "cholesky_dec = timer(*args, operation=cholesky, n=n)\n", 858 | "\n", 859 | "# Eigenvalue Decomposition\n", 860 | "args = [m1]\n", 861 | "eig_dec = timer(*args, operation=eig, n=n)\n", 862 | "\n", 863 | "print(\"\"\"\n", 864 | "V-V Product: {},\n", 865 | "V-M Product: {},\n", 866 | "M-M Product: {},\n", 867 | "SV Decomp.: {},\n", 868 | "LU Decomp.: {},\n", 869 | "QR Decomp.: {},\n", 870 | "Cholesky D.: {},\n", 871 | "Eigval Dec.: {}\n", 872 | "\"\"\".format(vv_product,\n", 873 | " vm_product,\n", 874 | " mm_product,\n", 875 | " sv_dec,\n", 876 | " lu_dec,\n", 877 | " qr_dec,\n", 878 | " cholesky_dec,\n", 879 | " eig_dec))\n", 880 | "\n", 881 | "print(\"\"\"\n", 882 | "NumPy Configuration:\n", 883 | "--------------------\n", 884 | "\"\"\")\n", 885 | "np.__config__.show()" 886 | ] 887 | }, 888 | { 889 | "cell_type": "code", 890 | "execution_count": 263, 891 | "metadata": {}, 892 | "outputs": [ 893 | { 894 | "name": "stdout", 895 | "output_type": "stream", 896 | "text": [ 897 | "\n", 898 | "NumPy Configuration:\n", 899 | "--------------------\n", 900 | "\n", 901 | "blas_mkl_info:\n", 902 | " NOT AVAILABLE\n", 903 | "blis_info:\n", 904 | " NOT AVAILABLE\n", 905 | "openblas_info:\n", 906 | " NOT AVAILABLE\n", 907 | "atlas_3_10_blas_threads_info:\n", 908 | " NOT AVAILABLE\n", 909 | "atlas_3_10_blas_info:\n", 910 | " NOT AVAILABLE\n", 911 | "atlas_blas_threads_info:\n", 912 | " NOT AVAILABLE\n", 913 | "atlas_blas_info:\n", 914 | " NOT AVAILABLE\n", 915 | "blas_opt_info:\n", 916 | " extra_compile_args = ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers']\n", 917 | " extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']\n", 918 | " define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]\n", 919 | "lapack_mkl_info:\n", 920 | " NOT AVAILABLE\n", 921 | "openblas_lapack_info:\n", 922 | " NOT AVAILABLE\n", 923 | "openblas_clapack_info:\n", 924 | " NOT AVAILABLE\n", 925 | "atlas_3_10_threads_info:\n", 926 | " NOT AVAILABLE\n", 927 | "atlas_3_10_info:\n", 928 | " NOT AVAILABLE\n", 929 | "atlas_threads_info:\n", 930 | " NOT AVAILABLE\n", 931 | "atlas_info:\n", 932 | " NOT AVAILABLE\n", 933 | "lapack_opt_info:\n", 934 | " extra_compile_args = ['-msse3']\n", 935 | " extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']\n", 936 | " define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]\n" 937 | ] 938 | } 939 | ], 940 | "source": [] 941 | }, 942 | { 943 | "cell_type": "markdown", 944 | "metadata": {}, 945 | "source": [ 946 | "### Singular-value Decomposition" 947 | ] 948 | }, 949 | { 950 | "cell_type": "code", 951 | "execution_count": 61, 952 | "metadata": {}, 953 | "outputs": [], 954 | "source": [ 955 | "import numpy as np\n", 956 | "\n", 957 | "M = np.random.randint(low=0, high=20, size=20).reshape(4,5)" 958 | ] 959 | }, 960 | { 961 | "cell_type": "code", 962 | "execution_count": 62, 963 | "metadata": {}, 964 | "outputs": [ 965 | { 966 | "name": "stdout", 967 | "output_type": "stream", 968 | "text": [ 969 | "[[18 15 11 13 19]\n", 970 | " [ 1 6 8 13 18]\n", 971 | " [ 9 7 15 13 10]\n", 972 | " [17 15 12 14 12]]\n" 973 | ] 974 | } 975 | ], 976 | "source": [ 977 | "print(M)" 978 | ] 979 | }, 980 | { 981 | "cell_type": "code", 982 | "execution_count": 63, 983 | "metadata": {}, 984 | "outputs": [], 985 | "source": [ 986 | "U, d, VT = np.linalg.svd(M)" 987 | ] 988 | }, 989 | { 990 | "cell_type": "code", 991 | "execution_count": 64, 992 | "metadata": {}, 993 | "outputs": [ 994 | { 995 | "name": "stdout", 996 | "output_type": "stream", 997 | "text": [ 998 | "U:\n", 999 | " [[-0.60773852 -0.22318957 0.5276743 -0.54990921]\n", 1000 | " [-0.38123886 0.86738201 0.19333528 0.25480749]\n", 1001 | " [-0.42657252 0.10181457 -0.82343563 -0.36003255]\n", 1002 | " [-0.55076919 -0.43297652 -0.07832665 0.70925987]]\n", 1003 | "\n", 1004 | "d:\n", 1005 | " [56.31276456 13.15721839 8.08763849 2.51997135]\n", 1006 | "\n", 1007 | "VT:\n", 1008 | " [[-0.43547429 -0.40223663 -0.40386674 -0.46371223 -0.52002929]\n", 1009 | " [-0.72920427 -0.29835313 0.06197899 0.27638212 0.54682545]\n", 1010 | " [ 0.11733943 0.26412864 -0.73449806 -0.30022507 0.53557916]\n", 1011 | " [-0.32795351 0.55511623 -0.3571117 0.56067806 -0.3773643 ]\n", 1012 | " [-0.39661218 0.60932187 0.40747282 -0.55144258 0.03609177]]\n" 1013 | ] 1014 | } 1015 | ], 1016 | "source": [ 1017 | "print(\"U:\\n {}\\n\".format(U))\n", 1018 | "print(\"d:\\n {}\\n\".format(d))\n", 1019 | "print(\"VT:\\n {}\".format(VT))" 1020 | ] 1021 | }, 1022 | { 1023 | "cell_type": "code", 1024 | "execution_count": 65, 1025 | "metadata": {}, 1026 | "outputs": [ 1027 | { 1028 | "name": "stdout", 1029 | "output_type": "stream", 1030 | "text": [ 1031 | "U:\n", 1032 | " [[-0.60773852 -0.22318957 0.5276743 -0.54990921]\n", 1033 | " [-0.38123886 0.86738201 0.19333528 0.25480749]\n", 1034 | " [-0.42657252 0.10181457 -0.82343563 -0.36003255]\n", 1035 | " [-0.55076919 -0.43297652 -0.07832665 0.70925987]]\n", 1036 | "\n", 1037 | "d:\n", 1038 | " [56.31276456 13.15721839 8.08763849 2.51997135]\n", 1039 | "\n", 1040 | "VT:\n", 1041 | " [[-0.43547429 -0.40223663 -0.40386674 -0.46371223 -0.52002929]\n", 1042 | " [-0.72920427 -0.29835313 0.06197899 0.27638212 0.54682545]\n", 1043 | " [ 0.11733943 0.26412864 -0.73449806 -0.30022507 0.53557916]\n", 1044 | " [-0.32795351 0.55511623 -0.3571117 0.56067806 -0.3773643 ]]\n" 1045 | ] 1046 | } 1047 | ], 1048 | "source": [ 1049 | "# Setting full_matrices to false gives you reduced form where small values close to zero are excluded\n", 1050 | "U, d, VT = np.linalg.svd(M, full_matrices=False)\n", 1051 | "\n", 1052 | "print(\"U:\\n {}\\n\".format(U))\n", 1053 | "print(\"d:\\n {}\\n\".format(d))\n", 1054 | "print(\"VT:\\n {}\".format(VT))" 1055 | ] 1056 | }, 1057 | { 1058 | "cell_type": "markdown", 1059 | "metadata": {}, 1060 | "source": [ 1061 | "## LU decomposition" 1062 | ] 1063 | }, 1064 | { 1065 | "cell_type": "code", 1066 | "execution_count": 66, 1067 | "metadata": {}, 1068 | "outputs": [ 1069 | { 1070 | "name": "stdout", 1071 | "output_type": "stream", 1072 | "text": [ 1073 | "[[18 12 14 15 2]\n", 1074 | " [ 4 2 12 18 3]\n", 1075 | " [ 9 19 5 16 8]\n", 1076 | " [15 19 6 16 11]\n", 1077 | " [ 1 19 2 18 17]]\n" 1078 | ] 1079 | } 1080 | ], 1081 | "source": [ 1082 | "from numpy import array\n", 1083 | "from scipy.linalg import lu\n", 1084 | "\n", 1085 | "M = np.random.randint(low=0, high=20, size=25).reshape(5,5)\n", 1086 | "print(M)" 1087 | ] 1088 | }, 1089 | { 1090 | "cell_type": "code", 1091 | "execution_count": 67, 1092 | "metadata": {}, 1093 | "outputs": [ 1094 | { 1095 | "name": "stdout", 1096 | "output_type": "stream", 1097 | "text": [ 1098 | "P:\n", 1099 | " [[1. 0. 0. 0. 0.]\n", 1100 | " [0. 0. 1. 0. 0.]\n", 1101 | " [0. 0. 0. 0. 1.]\n", 1102 | " [0. 0. 0. 1. 0.]\n", 1103 | " [0. 1. 0. 0. 0.]]\n", 1104 | "\n", 1105 | "L:\n", 1106 | " [[ 1. 0. 0. 0. 0. ]\n", 1107 | " [ 0.05555556 1. 0. 0. 0. ]\n", 1108 | " [ 0.22222222 -0.03636364 1. 0. 0. ]\n", 1109 | " [ 0.83333333 0.49090909 -0.70149254 1. 0. ]\n", 1110 | " [ 0.5 0.70909091 -0.32089552 0.21279832 1. ]]\n", 1111 | "\n", 1112 | "U:\n", 1113 | " [[18. 12. 14. 15. 2. ]\n", 1114 | " [ 0. 18.33333333 1.22222222 17.16666667 16.88888889]\n", 1115 | " [ 0. 0. 8.93333333 15.29090909 3.16969697]\n", 1116 | " [ 0. 0. 0. 5.79918589 3.26594301]\n", 1117 | " [ 0. 0. 0. 0. -4.65360318]]\n" 1118 | ] 1119 | } 1120 | ], 1121 | "source": [ 1122 | "P, L, U = lu(M)\n", 1123 | "\n", 1124 | "print(\"P:\\n {}\\n\".format(P))\n", 1125 | "print(\"L:\\n {}\\n\".format(L))\n", 1126 | "print(\"U:\\n {}\".format(U))" 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "code", 1131 | "execution_count": 68, 1132 | "metadata": {}, 1133 | "outputs": [ 1134 | { 1135 | "data": { 1136 | "text/plain": [ 1137 | "array([[18., 12., 14., 15., 2.],\n", 1138 | " [ 4., 2., 12., 18., 3.],\n", 1139 | " [ 9., 19., 5., 16., 8.],\n", 1140 | " [15., 19., 6., 16., 11.],\n", 1141 | " [ 1., 19., 2., 18., 17.]])" 1142 | ] 1143 | }, 1144 | "execution_count": 68, 1145 | "metadata": {}, 1146 | "output_type": "execute_result" 1147 | } 1148 | ], 1149 | "source": [ 1150 | "P.dot(L).dot(U)" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "markdown", 1155 | "metadata": {}, 1156 | "source": [ 1157 | "## QR decomposition" 1158 | ] 1159 | }, 1160 | { 1161 | "cell_type": "code", 1162 | "execution_count": 172, 1163 | "metadata": {}, 1164 | "outputs": [], 1165 | "source": [ 1166 | "from numpy.linalg import qr" 1167 | ] 1168 | }, 1169 | { 1170 | "cell_type": "code", 1171 | "execution_count": 69, 1172 | "metadata": {}, 1173 | "outputs": [ 1174 | { 1175 | "name": "stdout", 1176 | "output_type": "stream", 1177 | "text": [ 1178 | "[[14 6 0 19 3]\n", 1179 | " [ 9 6 17 8 8]\n", 1180 | " [ 4 13 17 4 4]\n", 1181 | " [ 0 0 2 7 11]]\n" 1182 | ] 1183 | } 1184 | ], 1185 | "source": [ 1186 | "from numpy import array\n", 1187 | "from numpy.linalg import qr\n", 1188 | "\n", 1189 | "M = np.random.randint(low=0, high=20, size=20).reshape(4,5)\n", 1190 | "print(M)" 1191 | ] 1192 | }, 1193 | { 1194 | "cell_type": "code", 1195 | "execution_count": 70, 1196 | "metadata": {}, 1197 | "outputs": [ 1198 | { 1199 | "name": "stdout", 1200 | "output_type": "stream", 1201 | "text": [ 1202 | "Q:\n", 1203 | " [[-0.81788873 0.28364908 -0.49345895 0.08425845]\n", 1204 | " [-0.52578561 -0.01509441 0.83834961 -0.14314877]\n", 1205 | " [-0.2336825 -0.95880935 -0.15918031 0.02718015]\n", 1206 | " [-0. -0. 0.16831464 0.98573332]]\n", 1207 | "\n", 1208 | "R:\n", 1209 | " [[-17.11724277 -11.09991852 -12.91095786 -20.68090082 -7.59468109]\n", 1210 | " [ 0. -10.85319349 -16.5563638 1.43333978 -3.10504542]\n", 1211 | " [ 0. 0. 11.88250752 -2.12744187 6.4411599 ]\n", 1212 | " [ 0. 0. 0. 7.4645743 10.05937231]]\n" 1213 | ] 1214 | } 1215 | ], 1216 | "source": [ 1217 | "Q, R = qr(M, 'complete')\n", 1218 | "\n", 1219 | "print(\"Q:\\n {}\\n\".format(Q))\n", 1220 | "print(\"R:\\n {}\".format(R))" 1221 | ] 1222 | }, 1223 | { 1224 | "cell_type": "code", 1225 | "execution_count": 71, 1226 | "metadata": {}, 1227 | "outputs": [ 1228 | { 1229 | "data": { 1230 | "text/plain": [ 1231 | "array([[1.40000000e+01, 6.00000000e+00, 1.77635684e-15, 1.90000000e+01,\n", 1232 | " 3.00000000e+00],\n", 1233 | " [9.00000000e+00, 6.00000000e+00, 1.70000000e+01, 8.00000000e+00,\n", 1234 | " 8.00000000e+00],\n", 1235 | " [4.00000000e+00, 1.30000000e+01, 1.70000000e+01, 4.00000000e+00,\n", 1236 | " 4.00000000e+00],\n", 1237 | " [0.00000000e+00, 0.00000000e+00, 2.00000000e+00, 7.00000000e+00,\n", 1238 | " 1.10000000e+01]])" 1239 | ] 1240 | }, 1241 | "execution_count": 71, 1242 | "metadata": {}, 1243 | "output_type": "execute_result" 1244 | } 1245 | ], 1246 | "source": [ 1247 | "Q.dot(R)" 1248 | ] 1249 | }, 1250 | { 1251 | "cell_type": "markdown", 1252 | "metadata": {}, 1253 | "source": [ 1254 | "## Cholesky decomposition" 1255 | ] 1256 | }, 1257 | { 1258 | "cell_type": "code", 1259 | "execution_count": 202, 1260 | "metadata": {}, 1261 | "outputs": [ 1262 | { 1263 | "name": "stdout", 1264 | "output_type": "stream", 1265 | "text": [ 1266 | "[[ 1 3 4]\n", 1267 | " [ 2 13 15]\n", 1268 | " [ 5 31 33]]\n" 1269 | ] 1270 | } 1271 | ], 1272 | "source": [ 1273 | "from numpy import array\n", 1274 | "from scipy.linalg import cholesky\n", 1275 | "\n", 1276 | "M = np.array([[1, 3, 4], \n", 1277 | " [2, 13, 15], \n", 1278 | " [5, 31, 33]])\n", 1279 | "print(M)" 1280 | ] 1281 | }, 1282 | { 1283 | "cell_type": "code", 1284 | "execution_count": 203, 1285 | "metadata": {}, 1286 | "outputs": [ 1287 | { 1288 | "name": "stdout", 1289 | "output_type": "stream", 1290 | "text": [ 1291 | "[[1. 3. 4. ]\n", 1292 | " [0. 2. 1.5 ]\n", 1293 | " [0. 0. 3.84057287]]\n" 1294 | ] 1295 | } 1296 | ], 1297 | "source": [ 1298 | "L = cholesky(M)\n", 1299 | "print(L)" 1300 | ] 1301 | }, 1302 | { 1303 | "cell_type": "code", 1304 | "execution_count": 204, 1305 | "metadata": {}, 1306 | "outputs": [ 1307 | { 1308 | "data": { 1309 | "text/plain": [ 1310 | "array([[ 1., 3., 4.],\n", 1311 | " [ 3., 13., 15.],\n", 1312 | " [ 4., 15., 33.]])" 1313 | ] 1314 | }, 1315 | "execution_count": 204, 1316 | "metadata": {}, 1317 | "output_type": "execute_result" 1318 | } 1319 | ], 1320 | "source": [ 1321 | "L.T.dot(L)" 1322 | ] 1323 | }, 1324 | { 1325 | "cell_type": "markdown", 1326 | "metadata": {}, 1327 | "source": [ 1328 | "## Eigenvalue decomposition" 1329 | ] 1330 | }, 1331 | { 1332 | "cell_type": "code", 1333 | "execution_count": 72, 1334 | "metadata": {}, 1335 | "outputs": [ 1336 | { 1337 | "name": "stdout", 1338 | "output_type": "stream", 1339 | "text": [ 1340 | "[[13 9 5 0 12]\n", 1341 | " [13 6 11 8 15]\n", 1342 | " [16 17 15 12 1]\n", 1343 | " [17 8 5 7 5]\n", 1344 | " [10 6 18 5 19]]\n" 1345 | ] 1346 | } 1347 | ], 1348 | "source": [ 1349 | "from numpy import array\n", 1350 | "from numpy.linalg import eig\n", 1351 | "\n", 1352 | "M = np.random.randint(low=0, high=20, size=25).reshape(5,5)\n", 1353 | "print(M)" 1354 | ] 1355 | }, 1356 | { 1357 | "cell_type": "code", 1358 | "execution_count": 76, 1359 | "metadata": {}, 1360 | "outputs": [ 1361 | { 1362 | "name": "stdout", 1363 | "output_type": "stream", 1364 | "text": [ 1365 | "Eigenvalues:\n", 1366 | " [50.79415691 +0.j 5.76076687+11.52079216j\n", 1367 | " 5.76076687-11.52079216j -1.15784533 +3.28961651j\n", 1368 | " -1.15784533 -3.28961651j]\n", 1369 | "\n", 1370 | "Eigenvectors:\n", 1371 | " [[ 0.34875973+0.j -0.36831427+0.21725348j -0.36831427-0.21725348j\n", 1372 | " -0.40737336-0.19752276j -0.40737336+0.19752276j]\n", 1373 | " [ 0.46629571+0.j -0.08027011-0.03330739j -0.08027011+0.03330739j\n", 1374 | " 0.58904402+0.j 0.58904402-0.j ]\n", 1375 | " [ 0.50628483+0.j 0.62334823+0.j 0.62334823-0.j\n", 1376 | " -0.27738359-0.22063552j -0.27738359+0.22063552j]\n", 1377 | " [ 0.33975886+0.j 0.14035596+0.39427693j 0.14035596-0.39427693j\n", 1378 | " 0.125282 +0.46663129j 0.125282 -0.46663129j]\n", 1379 | " [ 0.53774952+0.j -0.18591079-0.45968785j -0.18591079+0.45968785j\n", 1380 | " 0.20856874+0.21329768j 0.20856874-0.21329768j]]\n" 1381 | ] 1382 | } 1383 | ], 1384 | "source": [ 1385 | "V, Q = eig(M)\n", 1386 | "\n", 1387 | "print(\"Eigenvalues:\\n {}\\n\".format(V))\n", 1388 | "print(\"Eigenvectors:\\n {}\".format(Q))" 1389 | ] 1390 | }, 1391 | { 1392 | "cell_type": "code", 1393 | "execution_count": 77, 1394 | "metadata": {}, 1395 | "outputs": [ 1396 | { 1397 | "data": { 1398 | "text/plain": [ 1399 | "array([[1.30000000e+01-2.88657986e-15j, 9.00000000e+00-2.33146835e-15j,\n", 1400 | " 5.00000000e+00+2.38697950e-15j, 1.17683641e-14+1.77635684e-15j,\n", 1401 | " 1.20000000e+01-4.99600361e-16j],\n", 1402 | " [1.30000000e+01-4.32986980e-15j, 6.00000000e+00-3.99680289e-15j,\n", 1403 | " 1.10000000e+01+3.38618023e-15j, 8.00000000e+00+1.72084569e-15j,\n", 1404 | " 1.50000000e+01-2.77555756e-16j],\n", 1405 | " [1.60000000e+01-7.21644966e-15j, 1.70000000e+01-6.66133815e-15j,\n", 1406 | " 1.50000000e+01+5.71764858e-15j, 1.20000000e+01+2.99760217e-15j,\n", 1407 | " 1.00000000e+00-6.66133815e-16j],\n", 1408 | " [1.70000000e+01-5.27355937e-15j, 8.00000000e+00-3.10862447e-15j,\n", 1409 | " 5.00000000e+00+4.27435864e-15j, 7.00000000e+00+2.22044605e-15j,\n", 1410 | " 5.00000000e+00-1.22124533e-15j],\n", 1411 | " [1.00000000e+01-3.60822483e-15j, 6.00000000e+00-4.21884749e-15j,\n", 1412 | " 1.80000000e+01+2.27595720e-15j, 5.00000000e+00+1.55431223e-15j,\n", 1413 | " 1.90000000e+01+3.88578059e-16j]])" 1414 | ] 1415 | }, 1416 | "execution_count": 77, 1417 | "metadata": {}, 1418 | "output_type": "execute_result" 1419 | } 1420 | ], 1421 | "source": [ 1422 | "from numpy import diag\n", 1423 | "from numpy import dot\n", 1424 | "from numpy.linalg import inv\n", 1425 | "\n", 1426 | "Q.dot(diag(V)).dot(inv(Q))" 1427 | ] 1428 | }, 1429 | { 1430 | "cell_type": "code", 1431 | "execution_count": null, 1432 | "metadata": {}, 1433 | "outputs": [], 1434 | "source": [] 1435 | } 1436 | ], 1437 | "metadata": { 1438 | "kernelspec": { 1439 | "display_name": "Python 3", 1440 | "language": "python", 1441 | "name": "python3" 1442 | }, 1443 | "language_info": { 1444 | "codemirror_mode": { 1445 | "name": "ipython", 1446 | "version": 3 1447 | }, 1448 | "file_extension": ".py", 1449 | "mimetype": "text/x-python", 1450 | "name": "python", 1451 | "nbconvert_exporter": "python", 1452 | "pygments_lexer": "ipython3", 1453 | "version": "3.6.1" 1454 | } 1455 | }, 1456 | "nbformat": 4, 1457 | "nbformat_minor": 2 1458 | } 1459 | --------------------------------------------------------------------------------