NumPy 1.5 Beginner's Guide An actionpacked guide for the easytouse, high performance, Python based free open source NumPy mathematical library using realworld examples Ivan Idris.; 'J 'A,, v g I open source I community experience distilled PUBLISHING BIRMINGHAM MUMBAI
installing installing installing creating Preface 1 Chapter 1: NumPy Quick Start 9 Python 9 Python on different operating systems 10 Windows 10 installing NumPy on Windows 11 Linux 13 installing NumPy on Linux 13 Mac OS X 14 NumPy on Mac OS X with a GUI installer 14 NumPy with MacPorts or Fink 16 Building from source 16 Vectors 16 adding vectors 17 IPython an interactive shell 20 Online resources and help 23 Summary 24 Chapter 2: Beginning with NumPy Fundamentals 25 NumPy array object 26 creating a multidimensional array 27 Selecting elements 28 NumPy numerical types 28 Data type objects 30 Character codes 30 dtype constructors 31 dtype attributes 32 a record data type 32
splitting reading calculating doing analyzing calculating enveloping predicting Onedimensional slicing and indexing 33 slicing and indexing multidimensional arrays 34 manipulating array shapes 36 Stacking 38 stacking arrays 38 Splitting 41 arrays 41 Array attributes 43 converting arrays 46 Summary 47 Chapter 3: Get into Terms with Commonly Used Functions 49 File I/O 49 and writing files 50 Identity matrix creation 50 CSV files 50 loading from CSV files 51 Volume weighted average price 51 volume weighted average price 52 The mean function 52 Time weighted average price 52 Value range 53 finding highest and lowest values 53 Statistics 54 simple statistics 54 Stock returns 56 stock returns 57 Dates 58 dealing with dates 58 Weekly summary 61 Time for action summarizing data 61 Average true range 65 the average true range 65 Simple moving average 66 computing the simple moving average 67 Exponential moving average 68 calcu lating the exponential moving average 69 Bollinger bands 70 with Bollinger bands 71 Linear model 72 price with a linear model 73
drawing avoiding computing Trend lines 74 trend lines 75 Methods of ndarray 78 clipping and compressing arrays 78 Factorial 79 calculating the factorial 79 Summary 80 Chapter 4: Convenience Functions for Your Convenience 81 Correlation 82 trading correlated pairs 82 Polynomials 85 fitting to polynomials 85 Onbalance volume 88 balancing volume 88 The mode 90 determining the mode of stock returns 90 Simulation 93 loops with vectorize 93 Smoothing 95 smoothing with the hanning function 95 Summary 98 Chapter 5: Working with Matrices and ufuncs 99_ Matrices 99 creating matrices 100 Creating a matrix from other matrices 101 creating a matrix from other matrices 101 Universal functions 102 creating universal function 102 Universal function methods 103 applying the ufunc methods on add 104 Arithmetic functions 105 dividing arrays 106 Modulo operation 107 computing the modulo 107 Fibonacci numbers 108 Fibonacci numbers 108 Lissajous curves 109 drawing Lissajous curves 110 Square waves 111 drawing a square wave 111 Hill
decomposing computing shifting gambling simulating drawing drawing sorting sorting extracting Table ofcontents Sawtooth and triangle waves 112 drawing sawtooth and triangle waves 113 Bitwise and comparison functions 114 twiddling bits 114 Summary 116 Chapter 6: Move Further with NumPy Modules 117 Linear algebra 117 inverting matrices 117 Solving linear systems 119 solving a linear system 119 Finding eigenvalues and eigenvectors 120 determining eigenvalues and eigenvectors 120 Singular value decomposition 121 a matrix 122 Pseudo inverse 123 the pseudo inverse of a matrix 123 Determinants 124 calculating the determinant of a matrix 124 Fast Fourier transform 124 calculating the Fourier transform 125 Shifting 126 frequencies 126 Random numbers 127 with the binomial 127 Hypergeometric distribution 129 a game show 129 Continuous distributions 130 a normal distribution 130 Lognormal distribution 131 the lognormal distribution 132 Summary 133 Chapter 7: Peeking Into Special Routines 135 Sorting 135 lexically 136 Complex numbers 137 complex numbers 137 Searching 138 using searchsorted 138 Array elements extraction 139 elements from an array 139 llv]
determining determining plotting plotting asserting comparing Financial functions 139 future value 140 Present value 140 getting the present value 140 Net present value 141 calculating the net present value 141 Internal rate of return 141 determining the internal rate of return 142 Periodic payments 142 calculating the periodic payments 142 Number of payments 143 the number of periodic payments 143 Interest rate 143 figuring out the rate 143 Window functions 144 plotting the Bartlett window 144 Blackman window 145 Time for action smoothing stock prices with the Blackman window 145 Hamming window 146 the Hamming window 147 Kaiser window 148 the Kaiser window 148 Special mathematical functions 149 plotting the modified Bessel function 149 Sine 150 plotting the sine function 150 Summary 151 Chapter 8: Assure Quality with Testing 153 Assert functions 153 asserting almost equal 154 Approximately equal arrays 155 approximately equal 155 Almost equal arrays 156 asserting arrays almost equal 156 Equal arrays 157 comparing arrays 157 Ordering arrays 158 checking the array order 158 Objects comparison 159 objects 159
plotting saving detecting String comparison 160 comparing strings 160 Floating point comparisons 161 comparing with assert_array_almost_equal_nulp 161 Comparison of floats with more ULPs 162 comparing using maxulp of 2 162 Summary 163 Chapter 9: Plotting with Matplotlib 165 Simple plots 165 plotting a polynomial function 166 Plot format string 167 Time for action plotting a polynomial and its derivative 167 Subplots 168 a polynomial and its derivatives 168 Finance 170 plotting a year's worth of stock quotes 171 Histograms 172 charting stock price distributions 173 Logarithmic plots 174 plotting stock volume 174 Scatter plots 175 plotting price and volume returns with scatter plot 175 Fill between 176 shading plot regions based on a condition 176 Legend and annotations 178 using legend and annotations 178 Summary 180 Chapter 10: When NumPy is Not Enough: SciPy and Beyond 181 Matlab and Octave 181 and loading a.mat file 182 Statistics 183 analyzing random values 183 Samples comparison and SciKits 185 comparing stock log returns 185 Signal processing 187 a trend in QQQ 187 Fourier analysis 189
interpolating filtering a detrended signal 189 Optimization 191 fitting to a sine 191 Numerical integration 194 calculating the Gaussian integral 194 Interpolation 194 in one dimension 194 Image processing 196 manipulating Lena 196 Summary 197 Pop Quiz Answers 199 Chapter 1, NumPy Quick Start 199 Chapter 2, Beginning with NumPy Fundamentals 199 Chapter 3, Get into Terms with Commonly Used Functions 199 Chapter 4, Convenience Functions for Your Convenience 199 Chapter 5, Working with Matrices and ufuncs 200 Chapter 6, Move Further with NumPy Modules 200 Chapter 7, Peeking into Special Routines 200 Chapter 8, Assured Quality with Testing 200 Chapter 9, Plotting with Matplotlib 200 Chapter 10, When NumPy is not enough SciPy and Beyond 200 Index 201 Iviil