Home
Python Scientific lecture notes
Contents
1. 2 8 5 Cython Cython is both a Python like language for writing C extensions and an advanced compiler for this language The Cython language is a superset of Python which comes with additional constructs that allow you call C functions and annotate variables and class attributes with c types In this sense one could also call it a Python with types In addition to the basic use case of wrapping native code Cython supports an additional use case namely interac tive optimization Basically one starts out with a pure Python script and incrementally adds Cython types to the bottleneck code to optimize only those code paths that really matter In this sense it is quite similar to SWIG since the code can be autogenerated but in a sense it also quite similar to ctypes since the wrapping code can almost be written in Python While others solutions that autogenerate code can be quite difficult to debug for example SWIG Cython comes with an extension to the GNU debugger that helps debug Python Cython and C code Note The autogenerated C code uses the Python C Api 2 8 Interfacing with C 265 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Advantages e Python like language for writing C extensions e Autogenerated code e Supports incremental optimization e Includes a GNU debugger extension e Support for C Since version 0 13 Disadvantages e Must be compiled e Requires an additional libr
2. 3 4 Traits building interactive dialogs 349 3 4 Traits building interactive dialogs 350 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 def print_state self print Storage tRelease tInflows tSpillage senl format e join 7 26 fer i in range 4 print str_format format self storage self release self inflows self spillage prine i 79 Traits listeners ttHteeeee eee eeeeeeeeeeeeeeeeeeeeeaea sade ttt ttt HH def _release_changed self new When the release is higher than zero warn all the inhabitants of the valley mona if new gt 0 print Warning we are releasing hm3 of water format new if name main _ projectA Reservoir name Project A max_storage 30 max_release 100 0 hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 10 state release 90 state inflows 0 state print_state The static trait notification signatures can be e def _release_changed self pass e def _release_changed self new pass e def _release_changed self old new pass e def _release_changed self name old new pass Listening to all the changes To listen to all the changes on a HasTraits class the magic _any_trait_changed method can be implemented In many situations you do not know in advance what type of listeners need to be activated Traits offers th
3. gt gt gt C dot c eumceny I Sop san ea 3 Sen Sell Peet Oh eo ull Exercise Elementwise operations e Try simple arithmetic elementwise operations add even elements with odd elements e Time them against their pure python counterparts using st imeit e Generate 2xx0 2xxl 2xx2 2xx3 2xx4 a_j 2 3xj J Other operations Comparisons gt gt gt a nNp array ll 2 3 4AN gt gt gt b np array 4 2 2 4 gt gt gt a b array False True False True dtype bool gt gt gt a gt D 3 array False False True False dtype bool ee Tip Array wise comparisons gt gt gt a np array 1 2 3 41 gt gt gt D np array l4 2 2 41 gt gt gt c np array 1 2 3 4 gt gt gt np array_equal a b False gt gt gt np array_equal a c True Logical operations gt gt gt a np array 1 1 0 0 dtype bool gt gt gt b np array I1 0 1 O dtype bool gt gt gt np logicallimor a D array True True True False dtype bool gt gt gt np logical_and a b array True False False False dtype bool1 Transcendental functions gt gt gt a np arange 5 gt gt gt np sin a 1 3 NumPy creating and manipulating numerical data 56 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013
4. seaborn pairplot data vars WAGE AGE EDUCATION kind reg hue RACE plt suptitle Effect of race 1 Other 2 Hispanic 3 White seaborn pairplot data vars WAGE AGE EDUCATION kind reg hue UNION plt suptitle Effect of union 1 Union member O Not union member PEPPER EEREREREEEEREREEERER ERE EEEPE EERE PEERERE TERE REREE EERE TET EEE HEH EEE Plot a simple regression seaborn lmplot y WAGE x EDUCATION data data plt show Total running time of the example 6 58 seconds 0 minutes 6 58 seconds Air fares before and after 9 11 This is a business intelligence BI like application What is interesting here is that we may want to study fares as a function of the year paired accordingly to the trips or forgetting the year only as a function of the trip endpoints Using statsmodels linear models we find that both with an OLS ordinary least square and a robust fit the intercept and the slope are significantly non zero the air fares have decreased between 2000 and 2001 and their dependence on distance travelled has also decreased 3 1 Statistics in Python 309 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 os rrer eee eet Sec ea aaah ees Cee SE Sb Se a ek ere dint Tl PESEE Figure 3 8 Air fares before and after 9 11 page 309 6 54 04 52 62 53 63 54 04 5
5. Blurring smoothing Gaussian filter from scipy ndimage 2 6 Image manipulation and processing using Numpy and Scipy 227 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt from scipy import misc gt gt gt lena misc lena gt gt gt blurred_lena ndimage gaussian_filter lena sigma 3 gt gt gt very_blurred ndimage gaussian_filter lena sigma 5 Uniform filter gt gt gt local_mean ndimage uniform_filter lena size 11 Sharpening Sharpen a blurred image gt gt gt from scipy import misc gt gt gt lena misc lena gt gt gt blurred_l ndimage gaussian_filter lena 3 increase the weight of edges by adding an approximation of the Laplacian gt gt gt filter_blurred_l ndimage gaussian_filter blurred_l 1 gt gt gt alpha 30 gt gt gt sharpened blurred_l alpha x blurred_l filter_blurred_1 D e NN E E E EPS Denoising Noisy lena gt gt gt from scipy import misc gt gt gt 1 misc lena gt gt gt 1 2230 3110 2107350 gt gt gt noisy 1 0 4 1l std np random random 1l shape A Gaussian filter smoothes the noise out and the edges as well gt gt gt gauss_denoised ndimage gaussian_filter noisy 2 2 6 Image manipulation and processing using Numpy and Scipy 228 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Mos
6. q a i 4 2 a En X 2 PE zg z is I T t as TT T eS a E as f r ra mm p m TE Ca a y ee Pa ri m Ga r gt ra Pa tal length sepal length sepal_ width presang petal width Figure 3 3 Analysis of Iris petal and sepal sizes page 292 blue setosa green versicolor red virginica n o sepal width sepal_length gt petal length NW ff WOH N UR e N vv o petal_width on o uw u aN mM t NH ON uw fa wn wW ie 4 4 N N sepal_length petal_length sepal_width petal_width Script output 3 1 Statistics in Python 293 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 OLS Regression Results Dep Variable sepal_width R squared 0 478 Model OLS Adj R squared 0 468 Method Least Squares ESS EATI SVC aL 44 63 Date Mon 21 Sep 2015 Prob F statistic 1 58e 20 Time IMeOse3s Log Likelihood Zoer Ins 5 No Observations 150 AIC Sa ST Df Residuals 146 BIC 96 41 Df Model 3 coef std err ic PIE 95 03 Conf Tnt Intercept Aes 27a 0 099 29 JG 000 Vata ous SB name T versicolor L 48211 OLAT 8 190 0 000 Sil SA0 o name T virginica l 6635 Oe 256 6 502 0 000 2 159 1 1 petal_length 0 2983 oros 4 920 0 000 0178 0 418 Omnibus 2 868 Durbin Watson 1 753 Prob Omnibus 0 238 Jargque Bera JB 25 QiOo Skew O 082 Prob JB 0 236 kortosis 5 659 Conds No 545 0 Testing the differenc
7. Can we conclude that education benefits males more than females Take home messages e Hypothesis testing and p value give you the significance of an effect difference e Formulas with categorical variables enable you to express rich links in your data e Visualizing your data and simple model fits matters e Conditionning adding factors that can explain all or part of the variation is important modeling aspect that changes the interpretation 3 1 Statistics in Python 287 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 3 1 6 Full examples Examples Code examples Code examples for the statistics in Python tutorial 150 140 130 120 110 100 90 80 FSIQ PIQ Figure 3 1 Boxplots and paired differences page 288 Boxplots and paired differences Plot boxplots for FSIQ PIQ and the paired difference between the two while the spread error bars for FSIQ and PIQ are very large there is a systematic Common effect due to the subjects This effect is cancelled out in the difference and the spread of the difference paired by subject is much smaller than the spread of the individual measures 3 1 Statistics in Python 288 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 150 140 130 120 110 100 20 15 10 Ln 15 FSIQ PIQ Python source code plot_paired_boxplots py import pandas import matplotlib pyplot as plt da
8. 29 t p c g ZI oO g 2 2 3 1 PL 4w g o nononono A Oo N O FN M tH O H O Hh Q Tm N g O KN KN NN N m m o a a N N tal length sepal_length sepal_width petai eng petal_width gt gt gt data pandas read_csv examples iris csv gt gt gt model ols sepal_width name petal_length data fit gt gt gt print model summary OLS Regression Results Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Post hoc hypothesis testing analysis of variance ANOVA In the above iris example we wish to test if the petal length is different between versicolor and virginica after removing the effect of sepal width This can be formulated as testing the difference between the coefficient associated to versicolor and virginica in the linear model estimated above it is an Analysis of Variance ANOVA For this we write a vector of contrast on the parameters estimated we want to test name T versicolor name T virginica with an F test gt gt gt print model f_test 0 1 1 0 ID lt F test F array 3 24533535 p 0 07369059 df f_denom 146 df_num 1 gt Is this difference significant Exercice Going back to the brain size IQ data test if the VIQ of male and female are different after removing the effect of brain size height and weight 3 1 4 More visualization seaborn for statistical exploration Seaborn combines
9. ax plot_surface X Y Z rstride 1 cstride 1 cmap hot Click on figure for solution See also 3D plotting with Mayavi page 357 Text BP Borie tt 7 me Wy ENP Y v pg Hint Have a look at the matplotlib logo Try to do the same from scratch Click on figure for solution Quick read If you want to do a first quick pass through the Scipy lectures to learn the ecosystem you can directly skip to the next chapter Scipy high level scientific computing page 103 The remainder of this chapter is not necessary to follow the rest of the intro part But be sure to come back and finish this chapter later 1 4 5 Beyond this tutorial Matplotlib benefits from extensive documentation as well as a large community of users and developers Here are some links of interest Tutorials e Pyplot tutorial e Introduction e Controlling line properties e Working with multiple figures and axes 1 4 Matplotlib plotting 98 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e Working with text e Image tutorial e Startup commands e Importing image data into Numpy arrays e Plotting numpy arrays as images e Text tutorial e Text introduction e Basic text commands e Text properties and layout e Writing mathematical expressions e Text rendering With LaTeX e Annotating text e Artist tutorial e Introduction e Customizing your objects e Object container
10. gt gt gt Erosion removes objects smaller than the structure gt gt gt morphology binary_erosion a morphology diamond 2 astype np uints8 ld ld OLGO 2 amp O O OOO be SoS oS Oo eo amp A YS os O amp jJarray 0 0 0 0 0 O OJ 0 0 0 0 O O Ol 0 0 0 O O O Ol l0 07 07 0 0 0 Olz 0 0 0 0 0 O Olz 0 0 0 O O O Ol LO DO OU UU 0 0 0 dtype uint 3 Dilation maximum filter gt gt gt a np zeros 5 5 gt gt gt al 2 2 1 gt gt gt a euacay l OW Os Ole Oy om ic ee O O O O O Mea Alege Oa Oa Oe Orr Oty leg Welly EBO Wie Osy Oe Wei gt gt gt morphology binary_dilation a morphology diamond 1 astype np uint8 karrcay LO OG OG y Li a r y i oy fi 0 ah 0 1 0 Us r 0 lf Qe eG S amp S 0 O 0 0 See Te Uf 7 P dtype uint8 Opening erosion dilation gt gt gt a np zeros 5 5 dtype np int gt gt gt a l 4 1 4 1 a 4 4 1 gt gt gt a ciscay LLG Of Ol Ope By idy dy Oly O 1 1 1 0 O 1 1 1 0 LOP Or Or yp gt gt gt morphology binary_opening a morphology diamond 1 astype np uint8 array TOP O O OW Ol Oy Op 2p Up 0 ORS ee ales 1s ON POr Oy ae 0 0 OF Oe OOO Ni Gite joe Unemnites Opening removes small objects and smoothes corner
11. ig ype 0 133 feig 1 140 fsig 2 139 FSE Si ioe pig 32 110 pig 33 86 pire gt gt gt model ols iq type data long fit gt gt gt print model summary OLS Regression Results coef std err PJE 95 0 Coimit Int Intercept 113 4500 3 683 30 807 0 000 106 119 120 781 type T piq 2 4350 5 208 0 466 0 643 a 7 943 We can see that we retrieve the same values for t test and corresponding p values for the effect of the type of iq than the previous t test gt gt gt stats ttest_ind data FSIOQ data PIQ Cin 60 465638 7596885647 O 64AZTIA5S0 5 o 5 3 1 Statistics in Python Multiple Regression including multiple factors 40 7 30 J 20 10 7 Zz 0 a 20T 307 407 50 ANA ANU NY 4 N WYV aia i WY A KV y Y y V NA NA i A i i Consider a linear model explaining a variable z the dependent variable with 2 variables x and y Z 2 C ycoti e Such a model can be seen in 3D as fitting a plane to a cloud of x y z points 3 1 Statistics in Python 282 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Example the iris data examples iris csv Tip Sepal and petal size tend to be related bigger flowers are bigger But is there in addition a systematic effect of species blue setosa green versicolor red virginica n sepal_width sepal_length
12. 15 print setting the installed capacity breaks the link between turbine power print and the ianstalled_capacity trait reservoir installed_capacity 8 print turbine power reservoir installed_capacity from traits api import HasTraits Str Float Range PrototypedFrom Instance class Turbine HasTraits turbine_type Str power Float 1 0 desc Maximal power delivered by the turbine Mw class Reservoir HasTraits name Str max_storage Float le6 desc Maximal storage hm3 max_release Float 10 desc Maximal release m3 s Notification Traits implements a Listener pattern For each trait a list of static and dynamic listeners can be fed with callbacks When the trait does change all the listeners are called Static listeners are defined using the _XXX_changed magic methods from traits api import HasTraits Instance DelegatesTo Float Range from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage moon reservoir Instance Reservoir min_storage Float max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storage Range low min_storage high max_storage A COM enOUm saa ia holies inflows Float desc Inflows hm3 release Range low min_release high max_release spillage Float desc Spillage hm3
13. Example reading wav files The wav file header 2 2 Advanced Numpy 158 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 chunk_id RIFF chunk_size 4 byte unsigned little endian integer format WAVE fmt_id fmt 4 byte unsigned little endian integer audio_fmt 2 byte unsigned little endian integer num_channels 2 byte unsigned little endian integer sample_rate 4 byte unsigned little endian integer byte_rate 4 byte unsigned little endian integer block_align 2 byte unsigned little endian integer bits_per_sample 2 byte unsigned little endian integer data_id data data_size 4 byte unsigned little endian integer e 44 byte block of raw data in the beginning of the file e followed by data_size bytes of actual sound data The wav file header as a Numpy structured data type gt gt gt wav_header_dtype np dtype chunk_id bytes 4 flexible sized scalar type item size 4 tohunk sizes sate little endian unsigned 32 bit integer VEormat 7 Sa 4 byte string Wine pd MSAT fme size lt un Taudio Emi Y u21 num channels T lt u2 more of the same Ssample_rate lt u4 byte_rate dima 1 block_align lt u2 les per sample Tuz mn Ea idt Wisi 23 29 7 sub array Fuse for fun data sizce Muat the sound data itself cannot be represented here it does not have a
14. Excercise Train an svm SVC on the digits dataset Leave out the last 10 and test prediction performance on these observations Ground truth K means 3 clusters K means 8 clusters Application to Image Compression Clustering can be seen as a way of choosing a small number of observations from the information For instance this can be used to posterize an image conversion of a continuous gradation of tone to several regions of fewer tones Using kernels Classes are not always separable by a hyperplane so it would be desirable to have a decision function that is not linear but that may be for instance polynomial or exponential gt gt gt from scipy import misc 2 gt gt gt lena misc lena astype np float32 Linear kernel Polynomial kernel RBF kernel Radial Basis Func e a a a a a et eee tion gt gt gt k means cluster KMeans n_clusters 5 EER gt gt gt k_means fit X E KMeans 2 pa gt gt gt values k_means cluster_centers_ squeeze EES labels k means labela oes gt gt gt lena_compressed np choose labels values gt gt gt lena_compressed shape lena shape ha gt gt gt svc svm SVC kernel gt s2Reave svm SVC kernels patysyc svm SVC kernels rbf degree 3 gt gt gt gamma inverse of gize of gt gt gt degree polynomial deyre radial kernel Exercise Which of the kernels noted above has a b
15. EEn 10 15 Fi aquicabor fri a Figure 3 5 Test for an education gender interaction in wages page 297 Test for an education gender interaction in wages Wages depend mostly on education Here we investigate how this dependence is related to gender not only does gender create an offset in wages it also seems that wages increase more with education for males than females Does our data support this last hypothesis We will test this using statsmodels formulas http statsmodels sourceforge net stable example_formulas html 2 0 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 3 1 Statistics in Python 297 T y gender 5 e female e male 0 0 amp 0 5 0 3 10 15 20 education Script output OLS Regression Results Dep Variable wage R squared omis Model OLS Adj R squared Q 19YO Method Least Squares ES SESNE aL SEALS S 63 42 Date MOI ae SiS 2015 Prob F statistic 2 OiLe 25 Time 19 08 56 Log Likelihood 86 654 No Observations 534 ALC 167 53 Df Residuals S531 BIC 154 5 Df Model 2 coef std err E IPS te 95 03 Comet Ia Intercept 0 4053 0 046 Sn se 0 000 0 314 0 496 gender T male 0 1008 omone 5 029 0 000 0 066 OF S6 education 0 0334 U2 003 G7 66 0 000 C2027 0 040 Omnibus 4 675 Durbin Watson 1 792 Prob Omnibus 0 097 Jarque Bera JB 4 876 Skew Ondy Prob JB C20873 IIBUISTEO S al S 31369 Cond No Som OLS Regression Results
16. Our function then becomes vectorized gt def find_answers while True ans look _for_next_answer if ans is None break yield ans A plugin registration system This is a class decorator which doesn t modify the class but just puts it in a global registry It falls into the category of decorators returning the original object class WordProcessor object PLUGINS def process self text for plugin in seli PLUGINS text plugin cleanup text return text classmethod def plugin els plugin cls PLUGINS append plugin 2 1 Advanced Python Constructs 151 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 WordProcessor plugin class CleanMdashesExtension object def cleanup self text return text replace amp mdash u N em dash Here we use a decorator to decentralise the registration of plugins We call our decorator with a noun instead of a verb because we use it to declare that our class is a plugin for WordProcessor Method plugin simply appends the class to the list of plugins A word about the plugin itself it replaces HTML entity for em dash with a real Unicode em dash character It exploits the unicode literal notation to insert a character by using its name in the unicode database EM DASH If the Unicode character was inserted directly it would be impossible to distinguish it from an en dash in the source
17. Routines converting between different colorspaces RGB HSV LAB etc are available in skimage color color rgb2hsv color lab2rgb etc Check the docstring for the expected dtype and data range of input images 3D images Most functions of skimage can take 3D images as input arguments Check the docstring to know if a function can be used on 3D images for example MRI or CT images Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Non local filters Non local filters use a large region of the image or all the image to transform the value of one pixel gt gt gt from skimage import exposure gt gt gt camera data camera gt gt gt camera_equalized exposure equalize_hist camera Exercise Open a color image on your disk as a NumPy array Find a skimage function computing the histogram of an image and plot the histogram of each color channel Convert the image to grayscale and plot its histogram 3 3 3 Image preprocessing enhancement Goals denoising feature edges extraction Local filters Local filters replace the value of pixels by a function of the values of neighboring pixels The function can be linear or non linear Neighbourhood square choose size disk or more complicated structuring element maximal 19 1A 1 f 1 179 19 14 14 neighbors Example horizontal Sobel filter gt gt gt text data text j gt gt gt h
18. String Form lt function funcname at Oxeaa0f0 gt Namespace Interactive File lt ipython console gt Definition funcname params DOS Siteraamaeype Concise one line sentence describing the function Extended summary which can contain multiple paragraphs Note Docstring guidelines For the sake of standardization the Docstring Conventions webpage documents the semantics and conventions associated with Python docstrings Also the Numpy and Scipy modules have defined a precise standard for documenting scientific func tions that you may want to follow for your own functions with a Parameters section an Examples section etc See http projects scipy org numpy wiki CodingStyleGuidelines docstring standard and http projects scipy org numpy browser trunk doc example py L37 Functions are objects Functions are first class objects which means they can be e assigned to a variable e an item in a list or any collection e passed as an argument to another function In 38 va variable_args In 39 va three x 1 y 2 args is three kuamge iS fiw s 2 aee all Methods Methods are functions attached to objects You ve seen these in our examples on lists dictionaries strings etc Exercises Exercise Fibonacci sequence Write a function that displays the n first terms of the Fibonacci sequence defined by u_0 1 uls 1 e u_ nt2 u_ ntl un Exercise Quickso
19. dense matrix array sparse matrix shape tuple create empty matrix data ij tuple 2 5 Sparse Matrices in SciPy 213 2 5 Sparse Matrices in SciPy 214 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 data indices indptr tuple e many arithmetic operations considerably more efficient than CSR for sparse matrices with dense sub matrices e use like CSR vector valued finite element discretizations Examples e create empty BSR matrix with 1 1 block size like CSR Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt mtx indptr array I0 27 gt 6i dtype Intz create using data indices indptr tuple with 2 2 block size gt gt gt indptr np array 0 2 3 6 gt gt gt indices np array 0 2 2 0 1 2 gt gt gt data np array 1l 2 3 4 5 6 repeat 4 reshape 6 2 2 gt gt gt mtx sparse bsr_matrix data indices indptr shape 6 6 gt gt gt mtx todense macai Oi Oye 2 i y 0 0 ee O CO Oye ee ly OF 0p On OF Se 5 Ee ap Sr G al MAr S S Si gt gt gt data amray MET Le les 2 2 2 2i Lien iy Sy Sll 4 i 5 5 5p Ol Me ol 6 GI Summary Table 2 1 Summary of storage schemes for matrix get fancy set fancy solvers note mat ve
20. e Let s use the imshow function of pylab to display the image 1 3 NumPy creating and manipulating numerical data 77 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt import pylab as plt gt gt gt lena misc lena gt gt gt plt imshow lena lt matplotlib image AxesImage object at 0x gt e Lena is then displayed in false colors A colormap must be specified for her to be displayed in grey gt gt gt plt imshow lena cmap plt cm gray lt matplotlib image AxesImage object at Ox gt e Create an array of the image with a narrower centering for example remove 30 pixels from all the borders of the image To check the result display this new array with imshow gt gt gt Crop lena lenal O0 30 30530 e We will now frame Lena s face with a black locket For this we need to create a mask corresponding to the pixels we want to be black The mask is defined by this condition y 256 2 x 256 x 2 gt gt gt y X np ogrid Us512 0 512 x and y indices of pixels gt gt gt y shape x shape S5ai 2s iy C S2 gt gt gt centerx centery 256 256 center of the image e gt gt Mask y cCentery s 2 x centers 42 gt 230e 2 7 Circle then we assign the value 0 to the pixels of the image corresponding to the mask The syntax is extremely simple and intuitive gt gt gt lena mask 0 gt gt gt p
21. gt gt gt clean border segmentation clear_border mask Visualize binary result gt gt gt plt figure I matplot lib figure Figure ob ject at 0x lt gt gt gt gt plt imshow clean_border cmap gray lt matplotlib image AxesImage object at Ox gt Visualize contour gt gt gt plt figure lt matplotlib figure Figure object at 0x gt gt gt gt plc limshow coins cmap Gray lt matplotlib image AxesImage object at Ox gt gt gt gt Plte comueoum elean border Oro lt matplovlib contour OuadContoursct 2 22 Use skimage dedicated utility function 3 3 Scikit image image processing 335 3 3 Scikit image image processing 336 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 250 200 150 100 50 0 30 100 150 200 Image Viewer 28 160 231 3 3 Feature extraction for computer vision Geometric or textural descriptor can be extracted from images in order to e classify parts of the image e g sky vs buildings e match parts of different images e g for object detection e and many other applications of Computer Vision 3 3 Scikit image image processing 337 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt from skimage import feature Example detecting corners using Harris detector from skimage feature import corner_harris corner
22. gt gt gt x_diag as_strided x shape 3 strides 3 1 x itemsize gt gt gt x diag array Mame 9I dtype amie 2 Slice first to adjust the data pointer gt gt gt as_strided x 0 1 shape 2 strides 3 1 x itemsize array 2 6 dtype int32 gt gt gt as_strided x 1l 0 shape 2 strides 3 1 x itemsize array 4 8 dtype int32 Note Using np diag gt gt gt y np diag x k 1 gt gt gt y array 2 6 dtype int32 However arr yo tlags owndata lia Ilse Note This behavior has changed before numpy 1 9 np diag would make a copy See also stride diagonals py Challenge Compute the tensor trace gt gt gt x np arange 5 5 5x 5 reshape 5 5 5 5 gt gt gt s 0 gt gt gt for i in range 5 for j in range 5 S x j Ly Je i by striding and using sum on the result gt gt gt y as_strided x shape 5 5 strides TODO TODO gt gt gt S2 ves gt gt gt assert s s2 Solution as_strided x shape 5 5 strides 5 5 5 5 x itemsize 545 1 x seems a zie gt gt gt s2 y sum 2 2 Advanced Numpy 167 In 1 x np zeros 20000 In 2 y Mom Zecosa Z 00005617 n6 In 3 x shape y shape CAZ2000C7 7 X 200007 In 4 Stimeit x sum 100000 loops best of 3 0 180 ms per loop In 5 Stimeit y sum IOOCOO Loops best or 822534 ms per Hoop in
23. import matplotlib pyplot as plt pilt imshow abs 2 22 lt LOO extento I m a OG SL 4 Ie 4 plt gray plt show 2 2 Advanced Numpy 175 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 1 5 1 0 0 5 0 0 0 5 Note Most of the boilerplate could be automated by these Cython modules http wiki cython org MarkLodato CreatingUfuncs Several accepted input types E g supporting both single and double precision versions cdef void mandel_single_point double complex xz_in double complex xc_in double complex z_out nogil cdef void mandel_single_point_singleprec float complex z_in float complex c_in float complex z_out cdef PyUFuncGenericFunction loop_funcs 2 cdef char input_output_types 3 2 cdef void x elementwise_funcs 1 2 looomtunes 0 Ee yUbuncaD DED input_output_types 0 NPY_CDOUBLE input_output_types 1 NPY_CDOUBLE input_output_types 2 NPY_CDOUBLE elementwise_funcs 0 lt void gt mandel_single_point oopa zune Ss i Wyirtioe_ ile input_output_types 3 NPY_CFLOAT input_output_types 4 NPY_CFLOAT input_output_types 5 NPY_CFLOAT elementwise_funcs 1 lt void gt mandel_single_point_singleprec mandel PyUFunc_FromFuncAndData Iioxeyo _ iE Ine elementwise_funcs input_output_types 2 number of supported input types lt 2 number of input args 1 number of output args 0 identity elemen
24. ls PyObject sout array build cos_module c cos_module so setup py NpyIter xin_iter NpylIter xout_iter e build_ext is to build extension modules NpyIter_IterNextFunc in_iternext NpyIter_IterNextFunc xout_iternext e inplace will output the compiled extension module into the current directory x parse single numpy array argument if PyArg_ParseTuple args O amp PyArray_Type amp in_array In 1 import cos_module return NULL The file cos_module so contains the compiled extension which we can now load in the Python interpreter In 2 cos_modulel a Construct the output array like the input array 7 2 8 Interfacing with C 255 2 8 Interfacing with C 256 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 out_array PyArray_NewLikeArray in_array NPY_ANYORDER NULL 0 if out_array NULL return NULL Greace Cue Teeracors 47 in_iter NpyIter_New in_array NPY_ITER_READONLY NPY_KEEPORDER NEY NO CASTING NULL if in_iter NULL goto fail out_iter NpyIter_New PyArrayObject out_array NPY_ITER_READWRITE NPY _KFEEFORDER NPY _NO_ CASTING NULL if out iter NULL NpyIter_Deallocate in_iter goto fail in_iternext NpyIter_GetIterNext in_iter NULL out_iternext NpyIter_GetIterNext out_iter NULL if in_iternext NULL out_iternext
25. 0 4 y position m 0 2 0 0 0 0 05 10 15 20 25 30 35 40 Time s Another example with scipy integrate odeint will be a damped spring mass oscillator 2nd order oscillator The position of a mass attached to a spring obeys the 2nd order ODE y 2 eps wo y wo 2 y 0 with wo 2 k m with k the spring constant m the mass and eps c 2 m wo with c the damping coefficient For this example we choose the parameters as gt gt gt mass 0 5 kg gt gt gt kspring 4 N m zee viscous 0A N 7m so the system will be underdamped because gt gt gt eps cviscous 2 x mass np sgrt kspring mass gt gt gt eps lt 1 True 1 5 Scipy high level scientific computing 118 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 For the scipy integrate odeint solver the 2nd order equation needs to be transformed in a system of two first order equations for the vector Y y y It will be convenient to define nu 2 eps wo c mandom wo 2 k m j z gt gt gt nu_coef cviscous mass gt gt gt om_coef kspring mass Thus the function will calculate the velocity and acceleration by gt gt gt def calc_deri yvec time nuc omc return yvec 1 nuec yvec 1 omc x yvec 0 gt gt gt time_vec np linspace 0O 10 100 gt gt gt yarr odei
26. Common Methods all scipy sparse classes are subclasses of spmatrix default implementation of arithmetic operations always converts to CSR subclasses override for efficiency shape data type set get nonzero indices format conversion interaction with NumPy toarray todense attributes mtx A same as mtx toarray mtx T transpose same as mtx transpose mtx H Hermitian conjugate transpose mtx real real part of complex matrix mtx imag imaginary part of complex matrix mtx size the number of nonzeros same as self getnnz mtx shape the number of rows and columns tuple data usually stored in NumPy arrays Sparse Matrix Classes Diagonal Format DIA very simple scheme diagonals in dense NumPy array of shape n_diag length fixed length gt waste space a bit when far from main diagonal subclass of _data_matrix sparse matrix classes with data attribute offset for each diagonal Ois the main diagonal negative offset below positive offset above fast matrix vector sparsetools fast and easy item wise operations manipulate data array directly fast NumPy machinery constructor accepts dense matrix array sparse matrix shape tuple create empty matrix data offsets tuple no slicing no individual item access 2 5 Sparse Matrices in SciPy 206 use Python Scientific lecture notes Release 20
27. In 42 foodir in os listdir os curdir Out 42 False Delete a file 1 2 The Python language 35 In 70 In 71 In 72 In 731 C e ly sil In 74 Outra In 78 QuE TEI In 79 Qut 79 In 80 Qut e0 In 84 Out gd In 86 Qut les In 87 Owe ke 7 In 88 Out 88 In 92 Qut 92 Running In 8 basic_types rst demo py control ke Owe asi Cone CO lEmOlSiataSit TORAS E demo2 py miret steps rst OO Oe a Sits gt fo open Junk txt w fp close a os path abspath junk txt a Us ers cburns src scipy2Z000 scipy 20090 _turorial source junk txt os path split a fUsece churns Siec scipy2009 scipy 2009 tutorial source junk txt os path dirname a Users cburis src scipy2009 scipy 2009 tutorial scurce os path basename a naak Pee Y os path splitext os path basename a Cigna y eh ELE OS path exists Junk Exe True os path isfile Tunk ite cee 9 True OS paccm rsdir jin ExEN False os path expanduser local Users cbhurns local os path Jorn os path cexpanduser C Local Tean Users cburns local bam an external command os system ls DvieNOn lOCGer Om reusing_code rst functions rst python_language rst standard_library rst 1 2 The Python language 36 Python Scientific lecture notes Release 20
28. NULL NpyIter_Deallocate in_iter NpyIter_Deallocate out_iter goto fail double xx in_dataptr double NpyIter_GetDataPtrArray in_iter double xx out_dataptr double NpyIter_GetDataPtrArray out_iter iterate over the arrays do xxout_dataptr cos xxin_dataptr while in_iternext in_iter amp amp out_iternext out_iter clean up and return the result x NpyIter_Deallocate in_iter NpyIter_Deallocate out_iter Py_INCREF out_array return out_array x in case bad things happen fail Py_XDECREE OuUELarray gt return NULL define functions in module static PyMethodDef CosMethods YCOs_ Mine _ nis COS _ItuiMne iyo IME Isl WANRVAING S evaluate the cosine on a numpy array NU NUE 0 NUE module initialization PyMODINIT_FUNC initcos_module_np void void Py_InitModule cos_module_np CosMethods IMPORTANT this must be called impon aantay OF from distutils core import setup Extension import numpy define the extension module cos_module_np Extension cos_module_np sources cos_module_np c include_dirs numpy get_include run the setup setup ext_modules cos_module_np To convince ourselves if this does actually works we run the following test script import cos_module_np import numpy as np import pylab x np arange 0 2 x np pi 0 1 y cos_module_np cos_func_np x Py labi
29. aL IOF a Dilation maximum filter 2 6 Image manipulation and processing using Numpy and Scipy 230 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt a np zeros 5 5 gt gt gt a 2 2 1 gt gt gt a array Glin Oban Owe Olea OF Ome pO Oke Ora GOR Olea Oke One ill Ory O ih Oke One LOR Oma MOE IK OMe O Oer 0n OI gt gt gt ndimage binary_dilation a astype a dtype amna Ws Oy Ose Olay Om LL OR Oky aller Ole Oa Oey hey idly ele 0d Oise Ong yo Oe Oy Or Or O Oa o Also works for grey valued images gt gt gt np random seed 2 gt gt gt im np zeros 64 64 gt gt gt x y 63 np random random 2 8 astype np int gt gt gt im x y np arange 8 gt gt gt bigger_points ndimage grey_dilation im size 5 5 structure np ones 5 5 gt gt gt square np zeros 16 16 gt gt gt sgquare 4 4 4 4 1 gt gt gt dist ndimage distance_transform_bf square gt gt gt dilate_dist ndimage grey_dilation dist size 3 3 structure np ones 3 3 Opening erosion dilation Ul 1 Uf Uf LOr Or Or Op l gt gt gt Opening removes small objects gt gt gt ndimage binary_opening a structure np ones 3 3 astype np int amra CLO O O Ol 0 1 1 1 Ol 0 1 1 1 Ol MO dy Ep Le Ol O OI gt gt gt Opening can also smooth c
30. e If you would like to track performace of your code across time i e as you make new commits to your repository you could try vbench e If you need some interactive visualization why not try RunSnakeRun 2 5 Sparse Matrices in SciPy Author Robert Cimrman 2 5 1 Introduction dense matrix is e mathematical object e data structure for storing a 2D array of values important features e memory allocated once for all items usually a contiguous chunk think NumPy ndarray e fast access to individual items Why Sparse Matrices e the memory that grows like n 2 e small example double precision matrix gt gt gt import numpy as np gt gt gt import matplotlib pyplot as plt gt gt gt x np linspace 0 le6 10 e gt gt ple plots 6 0 xexZ 7 1e6 Lwe 5 lt matplotlib lines Line2D object at gt gt gt gt plt xlabel size n lt Matplotlib text iText object at 24 5 gt gt gt plt ylabel memory MB lt matplot lib text Text object at 22 2 2 5 Sparse Matrices in SciPy 203 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Sparse Matrices vs Sparse Matrix Storage Schemes e sparse matrix is a matrix which is almost empty e storing all the zeros is wasteful gt store only nonzero items e think compression e pros huge memory savings e cons depends on actual storage scheme usually does not hold Typical Applications soluti
31. gt gt gt bubbles_labels bubbles_nb ndimage label void gt gt gt bubbles_areas np bincount bubbles_labels ravel 1 gt gt gt mean_bubble_ size bubbles _areas mean gt gt gt median_bubble_size np median bubbles_areas gt gt gt mean_bubble_size median_bubble_size 1 5 Scipy high level scientific computing 135 1 5 Scipy high level scientific computing 136 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 16099 601537 05 0 1 6 Getting help and finding documentation Author Emmanuelle Gouillart Rather than knowing all functions in Numpy and Scipy it is important to find rapidly information throughout the documentation and the available help Here are some ways to get information e In Ipython help function opens the docstring of the function Only type the beginning of the func tion s name and use tab completion to display the matching functions In 204 help np v np vander np vdot np version ME vordo Np vStaCk NPr vices np vectorize np void MO WSyoul wae In 204 help np vander In Ipython it is not possible to open a separated window for help and documentation however one can always open Numpy and Scipy Documentation SciPy v0 8 dev Reference Guide DRAFT next modules index Release 0 8 dev Reference Next topic SciPy Tutorial This Page Show Source Resources Scipy org website Edit page Quick sea
32. resizable True Fire up the dialog Visualization configure_traits Tip Full code of the example examples mlab_dialog py 3 5 6 Putting it together Exercise Using the code from the magnetic field simulation create a dialog that enable to move the 2 coils change their parameters Hint to define a dialog entry for a vector of dimension 3 direction Array float value 0 0 1 cols 3 shape 3 You can look at the example_coil_application py to see a full blown application for coil design in 270 lines of code 3 6 scikit learn machine learning in Python Authors Fabian Pedregosa Gael Varoquaux machine learning in Python Prerequisites e Numpy Scipy e Python e matplotlib e scikit learn http scikit learn org 3 6 scikit learn machine learning in Python 369 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Chapters contents e Loading an example dataset page 370 Learning and Predicting page 371 e Classification page 371 k Nearest neighbors classifier page 371 Support vector machines SVMs for classification page 372 e Clustering grouping observations together page 373 K means clustering page 374 e Dimension Reduction with Principal Component Analysis page 375 e Putting it all together face recognition page 376 e Linear model from regression to sparsity page 377 Sparse models page 377 e Model selec
33. 1 2 The Python language 13 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt L red blue green black white gt gt gt L append pink gt gt gt L red blue green black whrte pink gt gt gt L pop removes and returns the last item UTouiLialy lt 4 gt gt gt L red bluet green black white gt gt gt L extend pink purple extend L in place gt gt gt di Tplue toreen Vnllack 7 Ywihate Ys pink Moatolke gt gt gt in lif s 2 d gt gt gt red blue green black white Reverse peers Te In eel gt gt gt r t white blacki Toreen Voile red gt gt gt r2 list L gt gt gt r2 I redt blue oreen 7 black white gt gt gt r2 reverse in place gt gt gt r2 whitte placki oreen bilwe red Concatenate and repeat lists gt gt gt F F L white black green blue red red blue green black white gt gt gt r x gt 2 T white bilack oqreen bluet red white placek Voqneen blue redr Tip Sort gt gt gt sorted r new object black blue green red white gt gt gt xr T white Tbitack 7 Toreen Volliet rec gt gt gt r sort in place gt gt gt r black blue green red white Methods and O
34. Oly 1 al a sabe er Oa ee O 2 eo a a le a Ly 0 3 1 2 1 e slicing and indexing 27 0 ile 27 3 1 aoe mee L cee IMMER eA 8 0 0 lt 2x4 Sparse Matrix Of type lt Mumpy inte4 gt a gt gt mtx Led with 4 Stored elements im Linked List format gt lt IxZ sparse Matrix Of Type lt numpy float67 gt gt gt gt mtx 2 todense with 1 stored elements in Dictionary Of Keys format gt mac cix MOm i 2 Ol gt gt gt mtx 1 1 3 todense O O 1 O merri IE Osp 2 So gt mezll 2 10 2 tocdense zee mez 2 1 leal ctocdense l maternis o E matee gt gt gt mtx todense OT marris 3p Op i O Up 07 07 AL Ieee Coordinate Format COO a e also known as the ijv or triplet format Dictionary of Keys Format DOK three NumPy arrays row col data subclass of Python dict data i is value at row i col i position keys are row column index tuples no duplicate entries allowed permits duplicate entries values are corresponding non zero values subclass of _data_matrix sparse matrix classes with data attribute efficient for constructing sparse matrices incrementally e fast format for constructing sparse matrices e constructor accepts e constructor accepts dense matrix array dense matrix array sparse matrix sparse matrix shape tuple create empty matrix shape tuple create empty matrix efficient O 1 access to individual eleme
35. Out Sls x rue y True This tells us that x amp y is True whenever x and y are both True If an expression cannot be true i e no values of its arguments can make the expression True it will return False In 14 satisfiable x amp x Out 14 False Exercises 1 Solve the system of equations x y 2 2 r7 y 0 2 Are there boolean values x y that make x y amp y x true 3 2 5 Linear Algebra Matrices Matrices are created as instances from the Matrix class gt gt gt from sympy import Matrix See Maccix IL 0 1071 14 Li Ol O 54 unlike a NumPy array you can also put Symbols in it gt gt gt x Symbol x gt gt gt y Symbol y See A M trix IIx Iy N gt gt gt A IL x y gt gt gt Ax x2 psy r l 2xx Lo 2y xxy 1 3 2 Sympy Symbolic Mathematics in Python 323 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Differential Equations SymPy is capable of solving some Ordinary Differential To solve differential equations use dsolve First create an undefined function by passing cls Function to the symbols function gt gt gt f 9 symbols 1f g cl Funcetion f and g are now undefined functions We can call f x and it will represent an unknown function gt gt gt f x gt gt gt QSO IVe ie gt AEE Seo Sb EE Ee EGI CRSA CA eos xX Keyword arguments can be giv
36. Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Yo sheen t name__ package__ ous rear Horie e In 8 demo demo builtins _ demo _ init_ demo _str__ demo class __ demo name__ demo __subclasshook__ demo _delattr _ demo _ new__ demo c demo dict demo _ package__ demo d demo __doc__ demo __reduce_ demo print_a demo file _ demo reduce _ex____ demo print_b demo format__ demo __ repr_ demo py demo __getattribute__ demo __setattr__ demo pyc demo __hash__ demo __sizeof__ Importing objects from modules into the main namespace In 9 from demo import print_a print_b In 10 whos Variable Type Data Info demo module lt module demo from demo py gt prinera function SEUNCELOMN priimta at OBIA 2 IS TELNE D function lt fUNnCEVoOn prine pD at Oxby4714e4 gt Iin iI print a a Warning Module caching Modules are cached if you modify demo py and re import it in the old session you will get the old one Solution In 10 reload demo __main_ and module loading Tip Sometimes we want code to be executed when a module is run directly but not when it is imported by another module if __name__ __main__ allows us to check whether the module is being run directly File demo2 py def print_b Erin eS Dr S print p def print_a e ieS a P
37. S python examples lobpcg_sakurai py Results by LOBPCG for n 2500 0 06250083 00 0625002 OO 06250007 See also Exact eigenvalues For more advanced image processing and image specific routines see the tutorial Scikit image image processing l 0060250005 O 0625002 0 06250044 page 324 dedicated to the sk image module Elapsed time 7 01 Image 2 D numerical array or 3 D CT MRI 2D time 4 D Eigenvalue distribution 1022 Here image Numpy array np array 10 Tools used in this tutorial 102 e numpy basic array manipulation 10 e scipy scipy ndimage submodule dedicated to image processing n dimensional images See 108 http docs scipy org doc scipy reference tutorial ndimage html 107 gt gt gt from scipy import ndimage 10 Common tasks in image processing lt 10 e Input Output displaying images 104 e Basic manipulations cropping flipping rotating 102 e Image filtering denoising sharpening 102 e Image segmentation labeling pixels corresponding to different objects l e Classification o e Feature extraction a e Registration 2 10 10 107 10 10 Number i 2 5 4 Other Interesting Packages e PyAMG algebraic multigrid solvers 2 5 Sparse Matrices in SciPy 221 2 6 Image manipulation and processing using Numpy and Scipy 222 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Chapters contents Opening and w
38. S1 gt gt gt wav_header shape 1 gt gt gt wav_header data_id shape lit aay 9 When accessing sub arrays the dimensions get added to the end Note There are existing modules such as wavfile audiolab etc for loading sound data Casting and re interpretation views casting e on assignment e on array construction e on arithmetic e etc e and manually astype dtype data re interpretation e manually view dtype Casting e Casting in arithmetic in nutshell only type not value of operands matters largest safe type able to represent both is picked scalars can lose to arrays in some situations e Casting in general copies data 2 2 Advanced Numpy 160 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt x np array 1 2 3 4 dtype np float gt gt gt x asrar O le 2s So 4 gt gt gt y x astype np int8 gt gt gt y array 1 2 3 4 dtype int8 ao y ool array l2 gt 4 5 dtype inte gt gt gt y t 256 anra 2S 25e 25S ee OOM a ayee S MES aoe y o 256 0 anran TPO Se 25955 aoe gt gt gt y np array 256 dtype np int32 aicicayy 2457 258 299 260 cheyjoeaine 2 e Casting on setitem dtype of the array is not changed on item assignment gt gt gt ylk l y a eS gt gt gt y array 2 3 4 5 dtype int8 Note Exact rules see documentation http
39. Th 1 x 10 In 2 lt UP gt In 2 x 10 Python supports so called magic functions by prefixing a command with the character For example the run and whos functions from the previous section are magic functions Note that the setting aut omagic which is enabled by default allows you to omit the preceding sign Thus you can just type the magic function and it will work Other useful magic functions are e cd to change the current directory In 2 eel tmp tmp e Stimeit allows you to time the execution of short snippets using the timeit module from the standard library In 3 timeit x 10 10000000 loops best of 3 39 ns per loop e Scpaste allows you to paste code especially code from websites which has been prefixed with the stan dard Python prompt e g gt gt gt or with an ipython prompt e g in 3 In 5 cpaste Pasting code enter alone on the line to stop or use Ctrl D sIn So teams x 10 OOO OOVOM oops Lest Or Sco Iams per loop In 6 cpaste Pasting code entent allone on the line to stop or use Ctr CD gt gt gt timeit x 10 10000000 loops best of 3 86 ns per loop e debug allows you to enter post mortem debugging That is to say if the code you try to execute raises an exception using debug will enter the debugger at the point where the exception was thrown in 7 10 File lt ipython inpuc o Ii2rd42lboize gt dane 1 x
40. astype np int points 1 astype np int 1 2 6 Image manipulation and processing using Numpy and Scipy 234 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt im ndimage gaussian_filter im sigma 1 4 n gt gt gt mask im gt im mean e Analysis of connected components Label connected components ndimage label Compute size mean_value etc of each region gt gt gt sizes ndimage sum mask label_im range nb_labels 1 gt gt gt mean_vals ndimage sum im label_im range 1l nb_labels 1 Clean up small connect components gt gt gt mask_size sizes lt 1000 gt gt gt remove_pixel mask_size label_im gt gt gt remove_pixel shape 256 256 gt gt gt label_im remove_pixel 0 gt gt gt plt imshow label_im lt matplotlib image AxesImage object at Ox gt Now reassign labels with np searchsorted gt gt gt labels np unique label_im gt gt gt label_im np searchsorted labels label_im Find region of interest enclosing object gt gt gt slice_x slice_y ndimage find_objects label_im 4 0 gt gt gt roi im slice_x slice_y gt gt gt plt imshow roi lt matplotlib image AxesImage object at Ox gt Other spatial measures ndimage center_of_mass ndimage maximum_position etc Can be used outside the limited scope of segmentation applications Example block mean gt gt gt
41. fill value OOO S99 e Masking versions of common functions See Mo ome Sopee iL 2 e maskedkarry darca mO deaiag es ao2s0 ee 15 mask False True False True fill value le 20 Note There are other useful array siblings page 181 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Good practices e Explicit variable names no need of a comment to explain what is in the variable e Style spaces after commas around etc A certain number of rules for writing beautiful code and more importantly using the same conven tions as everybody else are given in the Style Guide for Python Code and the Docstring Conventions page to manage help strings e Except some rare cases variable names and comments in English 1 3 4 Advanced operations Section contents e Polynomials page 72 e Loading data files page 74 While it is off topic in a chapter on numpy let s take a moment to recall good coding practice which really do pay off in the long run 1 3 NumPy creating and manipulating numerical data Polynomials Numpy also contains polynomials in different bases For example 3x 2x 1 o gt b tp poly ld i3 27 11 gt gt gt p 0 i gt gt gt p roots array Nime A Wessesssssi gt gt gt p order 2 gt gt gt x np linspace 0 1 20 gt gt gt y np cos x 0 3 np random rand 20 See jo io OOy ilel inj joo
42. gt gt gt class replacing_decorator_class object def init self arg this method is called in the decorator expression 1 o print in decorator init 3s arg self arg arg def seca Mae Selle Function this method is called to do the job print in decorator call ssis self arg self function function return self _wrapper def _wrapper self xargs xxkwargs print in the wrapper 3s 2s args kwargs R return self function args x xkwargs gt gt gt deco_instance replacing_decorator_class foo imn decorator Imit COO gt gt gt deco_instance def function xargs xxkwargs preine in function 28 28 2 args kwargs in decorator call foo Soo runet dor iy ee in the wrapper 11 12 3 La UME tieny Gi 12 17 A decorator like this can do pretty much anything since it can modify the original function object and mangle the arguments call the original function or not and afterwards mangle the return value Copying the docstring and other attributes of the original function When a new function is returned by the decorator to replace the original function an unfortunate consequence is that the original function name the original docstring the original argument list are lost Those attributes of the original function can partially be transplanted to the new function by setting __doc__ the docstring __ module __ and __name___ t
43. gt gt gt mtx lt 9 x gt sparse matrix or Cypa n iMNuUmnpy Int oAl i with 6 stored elements blocksize 1x1 in Block Sparse Row format gt gt gt gt mtx todense macci Lil 2 Op Oy 31 or 27 Olose gt gt gt mtx data array CMAR 20 Ny Sii radiy CSi LLOJIT ocs gt gt gt mtx indices array 0 2 4 O 1 2 chesjoeaiimcsZ 2 5 Sparse Matrices in SciPy 215 2 5 3 Linear System Solvers e sparse matrix eigenvalue problem solvers live in scipy sparse linalg 2 5 Sparse Matrices in SciPy 216 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e the submodules dsolve direct factorization methods for solving linear systems isolve iterative methods for solving linear systems eigen sparse eigenvalue problem solvers e all solvers are accessible from gt gt gt import scipy sparse linalg as spla gt gt gt Sola n all l rinearOperator Tester larpack Taslinearoperator picCg pnregsotabi Meo egs Mese matrixi esr matrixi Vclsolwe eigen eigen symmetric factorized gmres interface LES Ol vie Vee wieciscdicl vie ae omnes as deemSoOlvicl Miobpeg i Marn Uqquimces Ving Venue Vsjeies Ysjallul selu sosolwe swell Teese Cumi packi tuse solver Mutil es Uwarsmmne si Sparse Direct Solvers e default solver SuperLU 4 0 included in SciPy real and complex systems both single and d
44. high level scientific computing 134 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 140000 120000 100000 80000 60000 40000 20000 4 Using the histogram of the filtered image determine thresholds that allow to define masks for sand pixels 6 Use mathematical morphology to clean the different phases glass pixels and bubble pixels Other option homework write a function that determines automatically the thresholds from the minima of the histogram gt gt gt sand_op ndimage binary_opening sand iterations 2 gt gt gt void filtdat lt 50 7 Attribute labels to all bubbles and sand grains and remove from the sand mask grains that are smaller than e gand no logical and filtdar gt 50 filicdst lt 114 10 pixels To do so use ndimage sum or np bincount to compute the grain sizes gt gt gt glass filtdat gt 114 gt gt gt sand_labels sand_nb ndimage label sand_op 5 Display an image in which the three phases are colored with three different colors eS ene ee a a eee ie ee eye eae TO MY Ae Ean tee gt max 1 gt gt gt mask sand_areas gt 100 gt gt gt phases void astype np int 2 glass astype np int 3 sand astype np int gt gt gt remove_small_sand mask sand_labels ravel reshape sand_labels shape 8 Compute the mean size of bubbles
45. ilo AVIARPB X OY Ax gt ee OO 1 dash_capstyle Cap style for dashes AV a gt HE Xx yiz O o 90 i z2 AV4IPH X rix 0 O dash_joinstyle Join style for dashes p 0 Sse a ma A Wmamemiiiwvy A viti 7 LMA A a marker see Markers page 102 Markers markeredgewidth line width around the marker mew symbol S ae Ed aT markeredgecolor edge color if a marker is used 7 ee ee 2 a mec markerfacecolor face color if a marker is used mfc markersize ms size of the marker in points Colormaps All colormaps can be reversed by appending _ r For instance gray_tr is the reverse of gray If you want to know more about colormaps checks Documenting the matplotlib colormaps 1 4 Matplotlib plotting 1 4 Matplotlib plotting 102 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Authors Adrien Chauve Andre Espaze Emmanuelle Gouillart Ga l Varoquaux Ralf Gommers spectral ocean pink a ee prism N D Q Dy s zig Spectral Wistia YiGn pa gnup hot nipy_ rainbow seismic spectral MA a summer terrain PN winter gt fa lt 1 5 Scipy high level scientific computing Reds Set1 Set2 Set3 4 a coolwarm SS co m B B a B B B l B n a Led Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 scipy cluster Vector quantization Km
46. lt makplotlib lines Line2D obyecte at Ux gt gt gt gt plt xlabel Time s lt M wot texte Text mOlom OCtE at Oca gt gt gt plt ylabel Amplitude lt mMatplovlib text Text ob jece at Ux gt Amplitude 0 5 10 15 20 Time s numpy fft Numpy also has an implementation of FFT numpy fft However in general the scipy one should be preferred as it uses more efficient underlying implementations 1 5 Scipy high level scientific computing 107 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Worked example Crude periodicity finding 80 hare lynx 60 carrot 70 50 40 30 20 Population number 10 10 0 1900 1905 1910 1915 1920 Year 300 250 200 150 Power 10 100 50 0 5 10 15 20 Period 1 5 Scipy high level scientific computing 108 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Worked example Gaussian image blur Exercise Denoise moon landing image Convolution this exercise we aim to clean up the noise using the Fast Fourier Transform 2 Load the image using pylab imread 3 Find and use the 2 D FFT function in scipy fftpack and plot the spectrum Fourier transform of the image Do you have any trouble visualising the spectrum If so why 4 The spectrum consists of h
47. shape 3 4 gt gt gt x2 array CHILD 2p Sh AF as Soe S A li 2r gt AIl dtype int l6 gt gt gt y np array 5 6 7 dtype np int16 gt gt gt y2 as_strided y strides 1 2 0 shape 3 4 gt gt gt y2 array S 5S 3 a gt gt gt KZ YZ auacewy I 5S lO Ss 20 DP G12 18 24 7 14 21 28 dtype int16 seems somehow familiar gt gt gt x np array 1 2 3 4 dtype np intl16 gt gt gt y np array 5 6 7 dtype np intl16 gt gt gt x np newaxis x yl np newaxis array I S 10 IS 20 L6 12 Ior 24 7 14 21 28 dtype int16 e Internally array broadcasting is indeed implemented using 0 strides More tricks diagonals See also stride diagonals py Challenge e Pick diagonal entries of the matrix assume C memory order 2 2 Advanced Numpy 166 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 SSS 3 mo eueray il 2 Sl ay 5 Sl Ve e Ilie CGhleyyjoeSajo shoe SZ gt gt gt x_diag as_strided x shape 3 strides e Pick the first super diagonal entries 2 6 e And the sub diagonals Hint to the last two slicing first moves the point where striding starts from Solution Pick diagonals Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 CPU cache effects Memory layout can affect performance
48. 10 AN Synwtaxmr ror es telus yaniterclo 1 1 Scientific computing with tools and workflow 7 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 In 8 debug gt Jona IPython core compilerop py 87 ast_parse 86 and are passed to the built in compile function gt O return compile source filename symbol sel 88 ipdb gt lLocals T source u x 10 n symbel s exec ISe lt IPython core compilerop CachingCompiler instance at Ox2ad8ef0 gt tr lename lt ipython inpurt 6 l2rd4ZipSsize gt IPython help e The built in Python cheat sheet is accessible via the squickref magic function e A list of all available magic functions is shown when typing Smagic Furthermore Python ships with various aliases which emulate common UNIX command line tools such as 1s to list files cp to copy files and rm to remove files A list of aliases is shown when typing alias In 1 alias Total number of aliases 16 Owe aL s cat cat elear el ear j ep ep i Cemi die Le lt b o coler 31 erep S gt less less Uiliey VIS e o eolon sil cree Vilik Yls f 0 Ccolor z orep aL Hid Vis Sf color ile Yils Ccolloir Vise ils 2 0 Scollor sll oe
49. 16 or 32 bytes signed or unsigned Warning An important if questionable skimage convention float images are supposed to lie in 1 1 in order to have comparable contrast for all float images gt gt gt from skimage import img_as_float gt gt gt camera_float img_as_float camera gt gt gt camera max camera_float max 255 O Some image processing routines need to work with float arrays and may hence output an array with a different type and the data range from the input array gt gt gt try from skimage import filters except ImportError from skimage import filter as filters gt gt gt camera_sobel filters sobel camera gt gt gt camera_sobel max OS 90 S02 522 Warning In the example above we use the filters submodule of scikit image that has been renamed from filter to filters between versions 0 10 and 0 11 in order to avoid a collision with Python s built in name filter Utility functions are provided in sk image to convert both the dtype and the data range following skimage s conventions util img_as_float util img_as_ubyte etc See the user guide for more details Colorspaces Color images are of shape N M 3 or N M 4 when an alpha channel encodes transparency gt gt gt lena data lena gt gt gt lena shape 5127 S512 3 3 3 Scikit image image processing 328 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013
50. 2 0 e For each integer j starting from 2 cross out its higher multiples gt gt gt N_max int np sqrt len is_prime gt gt gt for j in range 2 N max is_prime 2xj j False e Skim through help np nonzero and print the prime numbers e Follow up Move the above code into a script file named prime_sieve py Run it to check it works Use the optimization suggested in the sieve of Eratosthenes 1 Skip j which are already known to not be primes 2 The first number to cross out is 7 Fancy indexing Tip Numpy arrays can be indexed with slices but also with boolean or integer arrays masks This method is called fancy indexing It creates copies not views gt gt gt a np arange 10 ere Lx panray iis ly koe Ti gt gt gt idx shape Using boolean masks gt gt gt np random seed 3 gt gt gt a np random random_integers 0 20 15 gt gt gt d arrean OLO OO Oy 220 I ee aa gt gt gt a gt 0 array False True False True False False False True False True True False True False False dtype bool1 gt S gt mask a 3 3 0 1 3 NumPy creating and manipulating numerical data 53 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt extract_from_a a mask 6p alae E gt gt gt extract_from_a extract a sub array with the mask annay le Si OF 9 6 O Zi Indexing with a mask ca
51. 3 Om Bins 2M By To understand this you need to learn more about the memory layout of a numpy array OF Wp 2 oly O Ly 2 Sii Adding a dimension Array shape manipulation Indexing with the np newaxis object allows us to add an axis to an array you have seen this already above in the broadcasting section Flattening ee 7 npsorray iy 2 31 gt gt gt Z gt gt gt Cl iio array il 2 Sly 4 oS Sl array i 27 31 gt gt gt a ravel array li 2 Sp 4 Sp 6I gt gt gt z np newaxis gt gt gt aE aricery IL LL array ii 41 IF l2 Sl IND lS 611 gt gt gt a T ravel gt gt gt z np newaxis array il 4 2 Sp Sp Gl aiciceny GE 2 381 Higher dimensions last dimensions ravel out first Dimension shuffling Reshaping gt gt gt a np arange 4 3 2 reshape 4 3 2 gt gt gt a shape The inverse operation to flattening 4 3 2 p gt gt gt a shape alo 2y ll 27 5 gt gt gt b a ravel gt gt gt b a transpose 1 2 0 gt gt gt b b reshape 2 3 gt gt gt b shape gt gt gt b Si 27 4 array Li 2p Sil gt gt gt 6 2 1 0 l4 5 ely 5 Or Also creates a view gt gt gt a reshape 2 1 s UNnSpeciried 1 value a inferred a ole al Ol acrcey lili 2 sis Pee eO 27 1 47 a ll i Warning ndarray reshape may return a view cf help np reshape or copy Resizing Tip Size of an array can be changed with ndarray re
52. 6 xsstrides y strides 187 4367 Smaller strides are faster cache block size e CPU pulls data from main memory to its cache in blocks e If many array items consecutively operated on fit in a single block small stride fewer transfers needed faster See also numexpr is designed to mitigate cache effects in array computing Example inplace operations caveat emptor e Sometimes gt gt gt a D is not the same as gt gt gt a b copy gt gt gt x np array IIl 21i Ts 4m0 gt gt gt x x transpose gt gt gt x array Ll 0 i Laz 011 2 2 Advanced Numpy 168 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt y mp array II 2m Ie MA a N y 1 copy gt gt gt y array 0 1 fae ONN e x and x transpose share data e x x transpose modifies the data element by element e because x and x transpose have different striding modified data re appears on the RHS Findings in dissection gAn data type array em scalar ha F ndarray e memory block may be shared base data e data type descriptor structured data sub arrays byte order casting viewing astype view e strided indexing strides C F order slicing w integers as_strided broadcasting stride tricks diag CPU cache coherence 2
53. Got Titol 15 But these global variables cannot be modified within the function unless declared global in the function 1 2 The Python language 24 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 This doesn t work In 117 def setx y a e S x y T prine x is d 3 x In 118 setx 10 x ig 10 in 120 x Ouel Oss 5 This works In 121 def setx y TEE global x ERER x y ee Gane Print Cx 16 20 3 x In 122 setx 10 lt x is Le In 123 Out l gt 10 x Variable number of parameters Special forms of parameters e xargs any number of positional arguments packed into a tuple e kwargs any number of keyword arguments packed into a dictionary In 35 def variable_args xargs xxkwargs eta Print args 15 args eee print kwargs 1s kwargs In 36 variable_args one two x l y 2 z 3 args is one M EWON kwargs is eran Sy eu celle a sean o Docstrings Documentation about what the function does and its parameters General convention In 67 def funcname params nA Concise one line sentence describing the function EE Extended summary which can contain multiple paragraphs moo Smack function body re pass In 68 funcname Type IE BVOC TE aL Oval Base Class type function gt 1 2 The Python language 25 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013
54. It s page 156 Block of memory page 157 Data types page 158 Indexing scheme strides page 163 Findings in dissection page 169 Author Pauli Virtanen e Universal functions page 169 What they are page 169 Exercise building an ufunc from scratch page 170 Solution building an ufunc from scratch page 174 Generalized ufuncs page 177 This section covers e Interoperability features page 178 Sharing multidimensional typed data page 178 The old buffer protocol page 178 e Universal functions what why and what to do if you want a new one The old buffer protocol page 179 Array interface protocol page 180 e Array siblings chararray maskedarray matrix page 181 chararray vectorized string operations page 181 e Recently added features and what s in them PEP 3118 buffers generalized ufuncs masked_array missing data page 181 2 2 Advanced Numpy Numpy is at the base of Python s scientific stack of tools Its purpose to implement efficient operations on many items in a block of memory Understanding how it works in detail helps in making efficient use of its flexibility taking useful shortcuts e Anatomy of Numpy arrays and its consequences Tips and tricks e Integration with other tools Numpy offers several ways to wrap any data in an ndarray without unnecessary copies recarray purely convenience page 184 matrix
55. NPY_UINT object PyUFunc_FromFuncAndData PyUFuncGenericFunctionx CUNG void x data NPY_LONG NPY_ULONG NPY_LONGLONG NPY_ULONGLONG NPY_FLOAT NPY_DOUBLE Chers types int ntypes int nin int nout 2 2 Advanced Numpy 173 2 2 Advanced Numpy 174 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 int identity char name Charx doc int c voud PyUrUnG DDID echares Npyoince Hpyoimtps vod Required module initialization import_array import_ufunc The actual ufunc declaration cdef PyUFuncGenericFunction loop_func 1 cdef char input_output_types 3 cdef void elementwise_funcs 1 loop_func 0 PyUFunc_DD_D input_output_types 0 NPY_CDOUBLE input_output_types 1 NPY_CDOUBLE input_output_types 2 NPY_CDOUBLE elementwise_funcs 0 lt void gt mandel_single_point mandel PyUFunc_FromFuncAndData Golo TE LINAS elementwise_funcs input_output_types 1 number of supported input types 2 number of input args 1 number of output args O identity element never mind this mandel function name mandel z gt computes iterated zsz c docsiring O unused mmm Plot Mandelbrot Plot the Mandelbrot ensemble moe import numpy as np import mandel x np linspace 1 7 0 6 1000 y np linspace 1 4 1 4 1000 e x None 1 jx y None Z mandel mandel c c
56. Note The autogenerated C code uses the Python C Api Advantages e Can automatically wrap entire libraries given the headers e Works nicely with C Disadvantages e Autogenerates enormous files e Hard to debug if something goes wrong 2 8 Interfacing with C 261 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e Steep learning curve Example Let s imagine that our cos function lives in a cos_module which has been written in c and consists of the source file cos_module c include lt math h gt double cos_func double arg return cos arg and the header file cos_module h double cos_func double arg And our goal is to expose the cos_func to Python To achieve this with SWIG we must write an interface file which contains the instructions for SWIG Example of wrapping cos function from math h using SWIG smodule cos_module x the resulting C file should be built as a python extension define SWIG_FILE_WITH_INIT x Includes the header in the wrapper code wince Weer moculer ia 3 x Parse the header file to generate wrappers Sinclude cos _module h As you can see not too much code is needed here For this simple example it is enough to simply include the header file in the interface file to expose the function to Python However SWIG does allow for more fine grained inclusion exclusion of functions found in header files check the documentation for detai
57. a True gt gt gt a np array Il 2 3 21 see lo apearen 2 2 3 21 gt gt gt c np array 6 4 4 5 gt gt gt a lt bD amp b lt c all True Statistics see c p aay is 3 lA gt ey ne sebreay e 2 Sly la Li gt gt gt x mean IRS gt gt gt np median x IS gt gt gt np median y axis 1 last axis array Ok 2er S21 gt gt gt x Sk full population standard dev 0 S291 5619756384995 and many more best to learn as you go Exercise Reductions e Given there is a sum what other function might you expect to see e What is the difference between sum and cumsum 1 3 NumPy creating and manipulating numerical data 59 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Worked Example data statistics Data in populations txt describes the populations of hares and lynxes and carrots in northern Canada during 20 years You can view the data in an editor or alternatively in Python both shell and notebook In 1 cat data populations txt First load the data into a Numpy array gt gt gt data np loadtxt data populations txt gt gt gt year hares lynxes carrots data T trick columns to variables Then plot it gt gt gt from matplotlib import pyplot as plt gt gt gt lit axes TOZ Osi O25 OW gt gt gt plt plot year hares year lynxes year carrots 22 gt
58. ccec view np chararray za o Scr au y charerean ONEEC Yeee Che WKS oa 1 eae x upper kehararray AT BBB Cee dtype Note view has a second meaning it can make an ndarray an instance of a specialized ndarray subclass masked_array missing data Masked arrays are arrays that may have missing or invalid entries For example suppose we have an array where the fourth entry is invalid Soe x op array il 2 3 99 51 One way to describe this is to create a masked array gt gt gt mx np ma masked_array x mask 0 07 0 1 O gt gt gt mx masiveditarisay Gatay 12 3 S mask False False False True False fillvalue 999999 Masked mean ignores masked data gt gt gt mx mean ees gt gt gt np mean mx as Warning Not all Numpy functions respect masks for instance np dot so check the return types The masked_array returns a view to the original array gt gt gt mx 1 9 gt gt gt x an rav il oF 3 99 S19 2 2 Advanced Numpy 181 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 The mask You can modify the mask by assigning gt gt gt mx 1 np ma masked gt gt gt mx masked array data TTIE T 5 mask False True False True False eiM yalue 99999 The mask is cleared on assignment gt gt gt mx 1 9 gt gt gt mx masked_arr
59. e Create different kinds of arrays with random numbers e Try setting the seed before creating an array with random values e Look at the function np empty What does it do When might this be useful 1 3 NumPy creating and manipulating numerical data 45 Basic data types You may have noticed that in some instances array elements are displayed with a trailing dot e g 2 vs 2 This is due to a difference in the data type used Ses 2 Npcarray Lily 2 31 gt gt gt a dtype dtype int64 gt gt gt b np array Il 27 3 1 gt gt gt b dtype dtype float64 Tip Different data types allow us to store data more compactly in memory but most of the time we simply work with floating point numbers Note that in the example above NumPy auto detects the data type from the input You can explicitly specify which data type you want gt gt gt C Np drray 1l 2 3 dtype float gt gt gt c dtype dtype float64 The default data type is floating point gt gt gt a np ones 3 3 gt gt gt a dtype drype Eloat 647 There are also other types Complex Se d mnp array TIS 23 Sarg Sros gt gt gt d dtype dtype complex128 Bool gt gt gt e np array True False False True gt gt gt e dtype dtype bool Strings gt gt gt Ap array BonJour Wsleil te Halko IN gt gt gt f dtype lt strings containing max 7 letters
60. from scipy import misc gt gt gt 1 misc lena os Sk By I shape gt gt gt X Y np ogrid 0ss6x 02sy gt gt gt regions sy 6 x X 4 Y 6 note that we use broadcasting gt gt gt block_mean ndimage mean l labels regions index np arange l 2 6 Image manipulation and processing using Numpy and Scipy 235 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt label_im nb_labels ndimage label mask gt gt gt nb_labels how many regions 16 gt gt gt plt imshow label_im lt matplotlib image AxesImage object at Ox gt 2 6 Image manipulation and processing using Numpy and Scipy 236 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 ae regions max 1 gt gt gt block_mean shape sx 4 sy 6 Correlation function Fourier wavelet spectrum etc When regions are regular blocks it is more efficient to use stride tricks Example fake dimensions with strides page 165 Non regularly spaced blocks radial mean One example with mathematical morphology granulometry http en wikipedia org wiki Granulometry_ 28morphology 29 gt gt gt def disk_structure n struc iNo wecos 2 im a ik 2 in a il xs W ino indices 2 e ia a Il 2 im ap il mask x n 2 y n 2 lt nxkx2 struct ma
61. gt 0 05 Python source code plot_wage_education_gender py import matplotlib pyplot as plt Heeeeeeeeeeeeeeeeeeeeeeeeeeeee settee eeeeeeeeeeteeeeaee eet dete ttae das dt tt ttt ttt tt plt show Load and massage the data eepost pendas Total running time of the example 0 35 seconds 0 minutes 0 35 seconds import urllib import os if not os path exists wages txt Download the file if it is not present urllib urlretrieve http lib stat cmu edu datasets CPS_85_Wages wages txt 40 4 EDUCATION Number of years of education 30 SEX 1 Female O Male 0 WAGE Wage dollars per hour 10 data pandas read_csv wages txt skiprows 27 skipfooter 6 sep None 7 0 header None names education gender wage 104 Aa 27 S7 204 370 Convert genders to strings this is particulary useful so that the 4 statsmodels formulas detects that gender is a categorical variable i import numpy as np 6 a data gender np choose data gender male female F Y Log transform the wages because they typically are increased with multiplicative factors data wage np log10 data wage Heeeeeeeteeeeeeeeeeeeeeeeee eee eee eeeeeeeeeeteeeeeeeaeee ee eetettta das dd tt ttt tt t simple plotting Figure 3 6 Multiple Regression page 300 import seaborn 7 PLOG 2 Jijear Lies Or mate ang tema e o sere an Impllot S a a a e a a Multiple Regression Calculate using statsmodels just the be
62. gt gt a np arange 10 0 n 1 aS gt gt gt a arca lO akhp 2p Sp ap De 6p Ty 8 SI gt gt gt b np arange l1 9 2 start end exclusive step gt gt gt D array i 3 S D e or by number of points gt gt gt c np linspace 0 1 6 start end num points gt gt gt c array I 0s y Ose O 4 Oar O58 1 gt gt gt d np linspace 0 1 5 endpoint False gt gt gt d array T Ws 4 O Wee Oho oN e Common arrays gt gt gt a np ones 3 3 reminder 3 3 is a tuple gt gt gt a eisai tags 1s 1l L ier Lop aly L tee ey EN gt gt gt b np zeros 2 2 gt gt gt b ankay OOn oly L Osp O21 gt gt gt c np eye 3 gt gt gt C asia I es ORE Orel Oct slg OR ise Orr N gt gt gt d np diag np array 1 2 3 4 gt gt gt d anran OPL O Oo LOr 2 0 ol LOOO oy Or Oe A 7 Li e np random random numbers Mersenne Twister PRNG gt gt gt a np random rand 4 A uni torm m TO qn gt gt gt a array 0 9579915l 014222247 Os08777354 0 5188 7998 gt gt gt b np random randn 4 Gaussian gt gt gt b array 0 37544699 0 11425369 0476165387 1 79604113 gt gt gt np random seed 1234 Setting the random seed Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercise Creating arrays using functions e Experiment with arange linspace ones zeros eye and diag
63. model f test 0 1 1 0 Script output pit show OLS Regression Results Total running time of the example 0 83 seconds 0 minutes 0 83 seconds Dep Variable y R squared 0 804 Model OLS Adj R squared 0 794 Method Least Squares PACC SVC ALES TA 03 l Date Mon 21 Sep 2015 Prob F statistic 8 56e 08 15 Time 19308 35 Log Likelihood 5 7 o Mais No Observations 20 AIG 120 0 10 Df Residuals 18 BLC 122 0 Df Model al 5 coef std err ie P gt t 95 07 Conf INnNE Intercept ogres ne 1036 75 342 0 000 AO pe ere 0 x 22 9969 0 341 8 604 0 000 2 220 3 654 gt 5 Omnibus DE TOO Durbin Watson 27 956 Prob Omnibus 0 951 Jarque Bera JB 0 322 10 Skew 0 053 Prob JB 0 851 l IIA Sal S 24390 Conadi No 3608 15 ANOVA results 70 GEE sum_sq mean_sq F PR gt F ae x 1 1588 873443 1588 873443 74 0293603 8 560649e 08 Residual 18 2606 329330 21 462741 NaN NaN 25 Python source code plot_regression py A Original author Thomas Haslwanter Figure 3 4 Simple Regression page 295 import numpy as np import matplotlib pyplot as plt import pandas Simple Regression Fit a simple linear regression using statsmodels compute corresponding p values ore For statistics Requires statsmodels 5 0 or more from statsmodels formula api import ols Analysis of Variance ANOVA on linear models from statsmodels stats anova import anova_lm 15 HEEREREEREREREREERERERE
64. or not e g custom error raising 1 2 9 Object oriented programming OOP Python supports object oriented programming OOP The goals of OOP are e to organize the code and e to re use code in similar contexts Here is a small example we create a Student class which is an object gathering several custom functions meth ods and variables attributes we will be able to use gt gt gt class Student object def init__ self name self name name def set_age self age self age age def set_major self major self major major gt gt gt anna Student anna 1 2 The Python language 41 not in range gt gt gt class MasterStudent Student Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt anna set_age 21 gt gt gt anna set_major physics In the previous example the Student class has __init__ set_age and set_major methods Its at tributes are name age and major We can call these methods and attributes with the following notation classinstance method or classinstance attribute The __init__ constructor is a special method we call with MyClass init parameters if any Now suppose we want to create a new class MasterStudent with the same methods and attributes as the previous one but with an additional internship attribute We won t copy the previous class but inherit from it internship mandatory from Ma
65. pl gca axes set_xlim 0 4 axes set_ylim 0 3 axes set_xticklabels axes set_yticklabels Click on figure for solution Multi Plots 1 4 Matplotlib plotting 96 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Hint You can use several subplots with different partition Starting from the code below try to reproduce the graphic on the right Ol stoolot 2 2 1 Dil SsubploulZ 2 3 TOMS Uo omen Ze a Click on figure for solution Polar Axis Hint You only need to modify the axes line Starting from the code below try to reproduce the graphic on the right pl axes 0 Oa le plop N 20 theta np earange 0 2 np pi 2 np pi 7 N radii 10 np random rand N width np pi 4 np random rand N bars pl bar theta radii width width bottom 0 0 for r bar in 7ip radii Bans bar set_facecolor cm jet r 10 bar set_alpha 0 5 Click on figure for solution 3D Plots Hint You need to use contourf Starting from the code below try to reproduce the graphic on the right 1 4 Matplotlib plotting 97 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 from mpl_toolkits mplot3d import Axes3D fig pl figure ax Axes3D fig np arange 4 4 0 25 np arange 4 4 0 25 X Y np meshgrid X Y R Mpesqre Wires Wez Z np sin R x K
66. you can use these to segment the regions 3 3 Scikit image image processing 333 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Watershed segmentation The Watershed skimage morphology watershed is a region growing approach that fills basins in the image gt gt gt from skimage morphology import watershed gt gt gt from skimage feature import peak_local_max SSS gt gt gt Generate an initial image with two overlapping circles gt gt gt xX y np indices 80 80 gt gt gt xl Vly x2 ye 28 28 MM SZ gt gt gt rl 2 l6 20 oe mask CIrelel x al 2s 2 a y y ss 2 lt ril s 2 Pee mas kieme le 2 x eA a a eye a a aa gt gt gt image np logical_or mask_circlel mask_circle2 gt gt gt Now we want to separate the two objects in image gt gt gt Generate the markers as local maxima of the distance gt gt gt to the background gt gt gt from scipy import ndimage gt gt gt distance ndimage distance_transform_edt image gt gt gt local_maxi peak_local_max distance indices False footprint np ones 3 3 labels image gt gt gt markers morphology label local_maxi gt gt gt labels_ws watershed distance markers mask image Random walker segmentation The random walker algorithm skimage segmentation random_walker 1is similar to the Watershed but with a more probabilistic approach It
67. 1 Bmax 0 4 Bmax opacity 0 5 Excercice Using the mlab pipeline interface generate a complete visualization with iso contours of the field magni tude and a vector cut plane click on the figure for a solution 3 5 4 Animating the data Tip To make movies or interactive application you may want to change the data represented on a given visualization If you have built a visualization using the mlab plotting functions or the mlab pipeline function we can update the data by assigning new values to the mlab_source attributes 3 5 3D plotting with Mayavi 366 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 lt p Wp 4 ino Oopeslelll 5s3 531007 Searl0Oi SSE Og Scalers np ein x by aa fie ky ee iso mlab contour3d scalars transparent True contours 0 5 for 2 in cange dy 20 Sealare No sins BK Soy eg fix eo ay ez iso mlab_source scalars scalars See also More details in the Mayavi documentation Event loops For the interaction with the user for instance changing the view with the mouse Mayavi needs some time to process these events The for loop above prevents this The Mayavi documentation details a workaround 3 5 5 Making interactive dialogs It is very simple to make interactive dialogs with Mayavi using the Traits library see the dedicated chapter Traits building interactive dialogs page 338 A simple dialog fro
68. 2 prints mone ii Welcome to pylab a matplotlib based Python environment For more information type help pylab Tip You can also download each of the examples and run it using regular python but you will loose interactive data manipulation F P Hint Documentation python exercice_l py e Customizing matplotlib You can get source for each step by clicking on the corresponding figure In the script below we ve instantiated and commented all the figure settings that influence the appearance of the plot Tip The settings have been explicitly set to their default values but now you can interactively play with the values to explore their affect see Line properties page 101 and Line styles page 102 below 1 4 Matplotlib plotting 83 1 4 Matplotlib plotting 84 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 import pylab as pl import numpy as np Create a figure of size 8x6 inches 80 dots per inch pl figure figsize 8 6 dpi 80 Create a new subplot from a grid of 1x1 jl Sulojoilote il i il X np linspace np pi np pi 256 endpoint True Cp S wo COS OS M Salina Ox Plot cosine with a blue continuous line of width 1 pixels pl plot X C color blue linewidth 1 0 linestyle Plot sine with a green continuous line of width 1 pixels pl plot X S color green linewidth 1 0 linestyle we Sele o I S
69. 8 8 am a e 3 1 Statistics in Python 291 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 150 140 130 120 110 100 PIQ 80 150 140 130 120 110 100 VIQ 80 70 140 120 110 FSIQ 100 80 FSBAaSRRmSRF BR SRARmMEHRR F amp FS GF RMA SG et eq ei eq eq eq eq eq eq ei eq ps am am an Pi an PIQ VIQ FSIQ Python source code plot_pandas py import pandas data pandas read_csv brain_size csv sep na_values Box plots of different columns for each gender groupby_gender data groupby Gender gGuoupbyagendenr boxploe columns Tms TOT Vlov TPTOTID from pandas tools import plotting Scatter matrices for different columns plotting scatter_matrix data Weight Height MRI_Count plotting scatter mene ieil lt Clereet ETO VWI CESTO import matplotlib pyplot as plt plt show Total running time of the example 0 96 seconds 0 minutes 0 96 seconds Analysis of Iris petal and sepal sizes Ilustrate an analysis on a real dataset e Visualizing the data to formulate intuitions e Fitting of a linear model e Hypothesis test of the effect of a categorical variable in the presence of a continuous confound 3 1 Statistics in Python 292 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 blue setosa green versicolor red virginica sapal width sapal length Ti ma
70. Are Ge hehe GR Ghee Ge Gare S 3 2 Sympy Symbolic Mathematics in Python J Scikitimag image PIOCESSING 2 avo yie cae ee eee sated 242 52 4 3 4 Traits building interactive dialogs dec oe he eee ee ES oe we eH eS 3 5 3D plotting with Mayavi to ctcnn Busse Hekede gtr Moke eos Kote oes aoe Gk BOR oe BOS 3 6 scikit learm machine learning in Python 305 004 45 04 oe 24 204 a o woe 48 Index Contents Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Part I One document to learn numerics science and data with Python CHAPTER 1 Getting started with Python for science This part of the Scipy lecture notes is a self contained introduction to everything that is needed to use Python for science from the language itself to numerical computing or plotting 1 1 Scientific computing with tools and workflow Authors Fernando Perez Emmanuelle Gouillart Ga l Varoquaux Valentin Haenel 1 1 1 Why Python The scientist s needs e Get data simulation experiment control e Manipulate and process data e Visualize results to understand what we are doing e Communicate results produce figures for reports or publications write presentations Specifications e Rich collection of already existing bricks corresponding to classical numerical methods or basic actions we don t want to re program the plotting of a curve a Fourier transform or a fitting algorithm
71. Cos 7 gt gt gt COS x Cos y sin Siny S10 G ssim y T COS x c08 7 Simplify Use simplify if you would like to transform an expression into a simpler form gt gt gt simplify x x y x y r ll Simplification is a somewhat vague term and more precises alternatives to simplify exists powsimp simplifica tion of exponents trigsimp for trigonometric expressions logcombine radsimp together Exercises 1 Calculate the expanded form of x y 2 Simplify the trigonometric expression sin x cos x 3 2 3 Calculus Limits Limits are easy to use in SymPy they follow the syntax limit function variable point so to compute the limit of f x as x 0 you would issue limit f x 0 3 2 Sympy Symbolic Mathematics in Python 320 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt damit Sim x x x 0 all you can also calculate the limit at infinity gt gt gt limit x x 00 OO gt gt gt limit lx 3 o0 0 cero haga rs o lt 0 il Differentiation You can differentiate any SymPy expression using di ff func var Examples Soe Cire Sin 2 gt x Costs gt gt gt diff sin 2 x x 2 xCOS 2xx gt gt gt dici tan x x 2 tan x 1 You can check that it is correct by gt gt gt limit tan x y tan x y y 0 2 wem x r l Higher derivatives can be calculated using the dif f fun
72. D a fdel SICWIMCIE LON 515 oS gt gt gt d D 24 varies this VS nO Ehe Same a TUnCEeILon Q D at ct H gt Q V V M Q M H an Il w deleting gt gt gt d a getting 1 i Properties are a bit of a stretch for the decorator syntax One of the premises of the decorator syntax that the name is not duplicated is violated but nothing better has been invented so far It is just good style to use the same name for the getter setter and deleter methods Some newer examples include e functools lru_cache memoizes an arbitrary function maintaining a limited cache of argu ments answer pairs Python 3 2 e functools total_ordering is a class decorator which fills in missing ordering methods __1t__ gt__ ___le__ based on a single available one Python 2 7 Deprecation of functions Let s say we want to print a deprecation warning on stderr on the first invocation of a function we don t like anymore If we don t want to modify the function we can use a decorator class deprecated object WHNPEIOE a deprecation warning Once on firs Use of Che Punceion gt gt gt deprecated doctest SKIP dem im js Somes pass SS Ge Adoctest SKIP f is deprecated def call self func self func func self count 0 return self _wrapper def _wrapper self xargs kwargs self count 1 if self count 1 print self func __name__ is deprecated return s
73. Format CSC column oriented three NumPy arrays indices indptr data indices is array of row indices data is array of corresponding nonzero values indptr points to column starts in indices and data x lengthisn_col 1 last item number of values length of both indices and data nonzero values of the i th column are data indptr i indptr i 1 with row indices indices indptr i indptr i l1 item i j can be accessed as data indptr j k where k is position of i in indices indptr j indptr j 1 subclass of _cs_matrix common CSR CSC functionality subclass of _data_matrix sparse matrix classes with data attribute e fast matrix vector products and other arithmetics sparsetools e constructor accepts dense matrix array sparse matrix shape tuple create empty matrix data ij tuple data indices indptr tuple e efficient column slicing column oriented operations e slow row slicing expensive changes to the sparsity structure e use actual computations most linear solvers support this format Examples e create empty CSC matrix Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt mtx sparse csc_matrix 3 4 dtype np int8 gt gt gt mtx todense meciab lt i Of OF Ol LOP OT Oro LOr Or O Ol cleyroe aanites e create using data ij tuple cee OW Apr array 0 O ly 2 42 Al gt gt gt CO mp arra
74. In the init of this object we populate the 3D scene with a curve Finally the configure_traits method creates the dialog and starts the event loop See also There are a few things to be aware of when doing dialogs with Mayavi Please read the Mayavi documentation Making it interactive We can combine the Traits events handler page 350 with the mlab_source to modify the visualization with the dialog We will enable the user to vary the n_t urns parameter in the definition of the curve For this we need e to define an n_turns attribute on our visualization object so that it can appear in the dialog We use a Range type e to wire modification of this attribute to a recomputation of the curve or this we use the on_traits_change decorator oO Edit properties HERR HREM Mm Fe a amp N turns 0 lt 30111 from traits api import Range on_trait_change class Visualization HasTraits n_turns Range 0O 30 11 scene Instance MlabSceneModel def __init_ self HasTraits __init__ self Xr Yr Z curve self n_turns self plot self scene mlab plot3d x y z on_trait_change n_turns def update_plot self X Y Zz curve self n_turns self plot mlab_source set x x y y Z Z view View Item scene height 300 show_label False 3 5 3D plotting with Mayavi 368 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 editor SceneEditor HGroup n_turns
75. Iterating with an index 1 2 The Python language 18 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt for i in range 4 print 1 CO Bo Ss But most often it is more readable to iterate over values gt gt gt for word any cool poveru Print Python is 3s 3 word Pyenon iS COO Python is powerful Python is readable while break continue Typical C style while loop Mandelbrot problem gt gt gt 2 pai gt gt gt while abs z lt 100 a 7 E gt gt gt Z 13443523 More advanced features break out of enclosing for while loop See A S il ae dy gt gt gt while abs z lt 100 if z imag 0 break Z Zxe x2 1 continue the next iteration of a loop gt gt gt a l 0 2 4 gt gt gt for element in a if element continue print 1 element aS amp S LS Sg ree ae Conditional Expressions if lt OBJECT gt Evaluates to False readable e any number equal to zero 0 0 0 0 0 e an empty container list tuple set dictionary e False None Evaluates to True e everything else a b Tests equality with logics 1 2 The Python language 19 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt True a is b Tests identity both sides are the same object gt gt gt 1 as 1 False gt gt gt a all gt gt gt D S
76. Loading and saving matlab files gt gt gt from scipy import io as spio gt gt gt a np ones 3 3 gt gt gt spio savemat file mat a a savemat expects a dictionary gt gt gt data spio loadmat file mat struct_as_record True gt gt gt datal a arkay II ibe il Ledy whey lt ts Le give oll e Reading images gt gt gt from scipy import misc gt gt gt misc imread array looo gt gt gt 7 Matplotlib also has a similar Function gt gt gt import matplotlib pyplot as plt gt gt gt plt imread GueTEAY 46 o fname png fname png See also e Load text files numpy loadtxt numpy savetxt e Clever loading of text csv files numpy genfromtxt numpy recfromcsv e Fast and efficient but numpy specific binary format numpy save numpy load 1 5 2 Special functions scipy special Special functions are transcendental functions The docstring of the scipy special module is well written so we won t list all functions here Frequently used ones are 1 5 Scipy high level scientific computing 104 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e Bessel function such as scipy special jn nth integer order Bessel function Elliptic function scipy special ellipy for the Jacobian elliptic function e Gamma function scipy special gamma also note scip
77. O lO 07 2 gt gt gt ali T i gt gt gt al2 1 10 third line second column gt gt gt a array ll Ol i Oy ty 0l 10 Zi gt gt gt all Note e In 2D the first dimension corresponds to rows the second to columns e for multidimensional a a 0 is interpreted by taking all elements in the unspecified dimensions Slicing Arrays like other Python sequences can also be sliced gt gt gt a np arange 10 gt gt gt a array TO ly 2 3 4 5S Ge Tn Se 21 gt gt gt a 2 9 3 start end step array 2m or 8 Note that the last index is not included a lt 4 array 0 id 2 3 All three slice components are not required by default start is 0 end is the last and step is 1 2 gt gt ales amran LiL 2 eo ey 2S 4 amran 0 2 4 6 S gt gt gt af 3 arn ray Qor AS Gy Te Sp S A small illustrated summary of Numpy indexing and slicing 1 3 NumPy creating and manipulating numerical data 50 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt a 0 3 5 array 3 4 Exercise Array creation Create the following arrays with correct data types EE a ae O y a y gt gt gt a 4 4 Oa array 44 45 ee a ee 54 55 Pee a Ok son Oor Onan Orel Oi WOR Gna wOls
78. OOOO 0 046 07050 nb_passengers T2 O25 sl e OL 0 000 3 5s 101 30 604 Omnibus 604 051 Durbin Watson 1 446 Prob Omnibus 0 000 Jargque Bera JB T40 T23 Skew O O Prob JB 1 42e 161 KUrEOS TSE 35338 Conc Nor 5 23e 03 Warnings 1 The condition number is large 5 23e 03 This might indicate that there are strong multicollinearity or other numerical problems Robust linear Model Regression Results Dep Variable fare No Observations 8352 Model RLM Df Residuals 8349 Method IRLS Df Model 2 Norm HuberT Scale Hist mad Cov Type H1 Date Mon Zi Sep 205 MME 19 08853 No Iterations LZ coef std err Z PS Z 95 02 Comer Tates Intercept 2 05 2 03 43 2 448 omm oS6 0mo00 210 28 219 B83 Gist 0 0460 001 46 166 C000 0 044 0 048 nb_passengers S5 2096 S Soleo 0 000 SLAG 7953 076 If the model instance has been used for another fit with different fit parameters then the fit options might not be the correct ones anymore OLS Regression Results Dep Variable fare_2001 R squared 0 159 Model OLS Adj R squared O11 58 Method Least Squares P SiC SUE aL Sic LCS TIL 7 Date Mona 2 Sep 20S Prob F statistic 1 20e 159 Time 19 083 Ss Log Likelihood 22640 No Observations 4176 AIC 4 528e 04 Df Residuals 4174 BIG 4 530e 04 Df Model 1 coef std err E P lel 95 0 Conty Ine Intercept 148 0279 1 673 88 480 0 000 WAA TAS TSL SOB chist 0 03E oomoo Zoe EG 0 0
79. OTOI 21 0m0 SL Onon i 5577054 L00 gt gt gt printe Hrror SS gt mtx x rhs Erron Oco OA OO O r OO both superlu and umfpack can be used if the latter is installed as follows prepare a linear system gt gt gt import numpy as np gt gt gt from scipy import sparse gt gt gt mtx sparse spdiags 1l gt gt gt mtx todense maw rax Naan r gt gt gt rhs dtype np float32 solve as single precision real money Solve a linear system Construct a 1000x1000 1il matrix and add some values to it convert it to CSR format and solve A x b for x and solve a linear system with a direct solver import numpy as np import scipy sparse as sps from matplotlib import pyplot as plt from scipy sparse linalg dsolve import linsolve rand np random rand mtx sps lil_matrix 1000 1000 dtype np float64 mex 0 2100 gt sand 100 wex OCS ZO0O ates lt 0 3 OO mtx setdiag rand 1000 2 5 Sparse Matrices in SciPy 217 piene kE plt spy mtx marker markersize 2 plt show mtx mtx tocsr rhs rand 1000 x linsolve spsolve mtx rhs peine rezidval Gr s nowlinalg norm mex lt chs 2 5 Sparse Matrices in SciPy 218 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 examples direct_solve py lterative Solvers the isolve module contains the following solvers
80. Ole a Aa OF 42 10 Set x ticks pl xticks np linspace 4 4 9 endpoint True Set y limits Ole yam 0 Set y ticks pl yticks np linspace 1l 1 5 endpoint True Save figure using 72 dots per inch savefig exercice_2 png dpi 72 Show result on screen pl show Changing colors and line widths 0 5 0 0 1 0 Hint Documentation e Controlling line properties e Line API Tip First step we want to have the cosine in blue and the sine in red and a slighty thicker line for both of them We ll also slightly alter the figure size to make it more horizontal pl figure figsize 10 6 dpi 80 pl plot X C color blue linewidth 2 5 linestyle 1 4 Matplotlib plotting 85 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 pl plot X S color red linewidth 2 5 linestyle Setting limits 0 5 Hint Documentation e xlim command e ylim command Tip Current limits of the figure are a bit too tight and we want to make some space in order to clearly see all data points foul N OX sialon e ilodl Ox aiilepe il iL pl ylim C min l Ce max x eee Setting ticks 3 142 1 571 0 000 1 571 3 142 Hint Documentation e xticks command e yticks command e Tick container e Tick locating and formatting Tip Current ticks are not ideal
81. a programming language as are C Fortran BASIC PHP etc Some specific features of Python are as follows e an interpreted as opposed to compiled language Contrary to e g C or Fortran one does not compile Python code before executing it In addition Python can be used interactively many Python interpreters are available from which commands and scripts can be executed a free software released under an open source license Python can be used and distributed free of charge even for building commercial software multi platform Python is available for all major operating systems Windows Linux Unix MacOS X most likely your mobile phone OS etc a very readable language with clear non verbose syntax a language for which a large variety of high quality packages are available for various applications from web frameworks to scientific computing e a language very easy to interface with other languages in particular C and C e Some other features of the language are illustrated just below For example Python is an object oriented language with dynamic typing the same variable can contain objects of different types during the course of a program See http www python org about for more information about distinguishing features of Python 1 2 1 First steps Start the Ipython shell an enhanced interactive Python shell e by typing ipython from a Linux Mac terminal or from the Windows cmd shell e or by starting th
82. activation LO ScCipy deveseipy org bias I d like to edit Numpy Scipy docstrings My account is XXXXX Cheers Nee Ne e Check the style guide http docs scipy org numpy Don t be intimidated to fix a small thing just fix it e Edit 2 Edit sources and send patches as for bugs 3 Complain on the mailing list Contributing features 0 Ask on mailing list if unsure where it should go 1 Write a patch add an enhancement ticket on the bug tracket 2 OR create a Git branch implementing the feature add enhancement ticket e Especially for big invasive additions e http projects scipy org numpy wiki GitMirror e http www spheredev org wiki Git_for_the_lazy Clone numpy repository git Clone origin svn http projects sciloy corg Gii numpy git mumoy cd numpy Create a feature branch git checkout b name of my feature branch svn trunk lt edit stuff gt Git Commi a e Create account on http github com or anywhere e Create a new repository Github e Push your work to github git remote add github git github YOURUSERNAME YOURREPOSITORYNAME git git push github name of my feature branch How to help in general e Bug fixes always welcome What irks you most 2 2 Advanced Numpy 185 2 2 Advanced Numpy 186 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Browse the tracker e Documentation work API docs improvements to docstrings K
83. and left ones to coordinate 0 in data space coordinates 1 4 Matplotlib plotting 87 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 ax pl gca gca stands for get current axis ax spines right set_color none ax spines top set_color none ax xaxis set_ticks_position bottom ax spines bottom set_position data 0 ax yaxis set_ticks_position left ax spines left set_position data 0 Adding a legend Hint Documentation e Legend guide e legend command e Legend API Tip Let s add a legend in the upper left corner This only requires adding the keyword argument label that will be used in the legend box to the plot commands pl plot X C color blue linewidth 2 5 linestyle label cosine pl plot X S color red linewidth 2 5 linestyle label sine pl legend loc upper left Annotate some points sin 22 Hint Documentation e Annotating axis e annotate command Tip Lets annotate some interesting points using the annotate command We chose the 27 3 value and we want 1 4 Matplotlib plotting 88 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 to annotate both the sine and the cosine We ll first draw a marker on the curve as well as a straight dotted line Then we ll use the annotate command to dis
84. brain_size csv sep na_values e First Steps with SymPy page 318 Using SymPy as a calculator page 318 Exercises page 319 Symbols page 319 model ols VIQ Gender MRI_Count Height data fit print model summary Here we don t need to define a contrast as we are testing a single e Algebraic manipulations page 320 COSEELEDENE Of Our Model and Not ay Combination of Coerricienrs Expand page 320 However defining a contrast which would then be a unit contrast Simplify page 320 will give us the same results e Calculus page 320 Print model i vest 0 1 0 0 Limits page 320 Differentiation page 321 Series expansion page 321 Integration page 322 Exercises page 322 e Equation solving page 322 Exercises page 323 e Linear Algebra page 323 from pandas tools import plotting Matrices page 323 import matplotlib pyplot as plt Differential Equations page 324 HEHEEAEEEEAEEEEEEREEAEEEEEEEEEAEEEEEEEEAEPEEEPH PEEP P EP EEEE HEE HEE TEETH EEE Here we plot a scatter matrix to get intuitions on our results This goes beyond what was asked in the exercise This plotting is useful to get an intuitions on the relationships between our different variables Fill in the missing values for Height for plotting data Height fillna method pad inplace True 3 2 1 First Steps with SymPy The parameter c is passed t
85. c lt some_threshold Do this computation by 1 Construct a grid of c x 1j y values in range 2 1 x 1 5 1 5 2 Do the iteration 3 Form the 2 d boolean mask indicating which points are in the set 4 Save the result to an image with gt gt gt import matplotlib pyplot as plt gt gt gt Oleeimshow mask il extent 27 leas oy eS lt matplotlib image AxesImage object at gt gt gt gt plt oray gt gt gt plt savefig mandelbrot png 1 3 NumPy creating and manipulating numerical data 80 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Solution Python source file Markov chain Pii Markov chain transition matrix P and probability distribution on the states p 1 0 lt P i jJ lt 1 probability to go from state i to state j 2 Transition rule prew P Pold 3 all sum P axis 1 1 p sum 1 normalization Write a script that works with 5 states and e Constructs a random matrix and normalizes each row so that it is a transition matrix e Starts from a random normalized probability distribution p and takes 50 steps gt p_50 e Computes the stationary distribution the eigenvector of P T with eigenvalue 1 numerically closest to 1 gt p_stationary Remember to normalize the eigenvector I didn t e Checks if p_50 and p_stationary are equal to tolerance le 5 Toolbox np random rand dot np linalg eig reductions abs argmin compari
86. cee mae INS g Oa y np conjugate np convolve Import conventions The recommended convention to import numpy is gt gt gt import numpy as np Creating arrays Manual construction of arrays e 1 D gt gt gt ada np array I0 1 2 31 gt gt gt a annava O aa gt gt gt a ndim il gt gt gt a shape 4 gt gt gt len a 4 e 2 D 3 D Sos bu Np array 0 1 25 ay 4 Sl Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 P 2x Ss array gt gt gt b array 0 il 2 Ly 4r al gt gt gt b ndim 2 gt gt gt b shape r 2 gt gt gt len b returns the size of the first dimension 2 ere cs wo aay INI 2 Wl Wa gt gt gt Cc array LET Lay eS ie clase gt gt gt c shape 25 T Exercise Simple arrays e Create a simple two dimensional array First redo the examples from above And then create your own how about odd numbers counting backwards on the first row and even numbers on the second e Use the functions len numpy shape on these arrays How do they relate to each other And to the ndim attribute of the arrays 1 3 NumPy creating and manipulating numerical data 43 1 3 NumPy creating and manipulating numerical data 44 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Functions for creating arrays Tip In practice we rarely enter items one by one e Evenly spaced gt
87. convenience page 184 e Summary page 184 e Numpy e Contributing to Numpy Scipy page 184 e Cython Why page 184 e Pillow Python imaging library used in a couple of examples Reporting bugs page 184 Contributing to documentation page 185 Contributing features page 186 How to help in general page 186 Prerequisites Tip In this section numpy will be imported as follows gt gt gt import numpy as np 2 2 1 Life of ndarray It s ndarray block of memory indexing scheme data type descriptor e raw data e how to locate an element e how to interpret an element 2 2 Advanced Numpy 155 2 2 Advanced Numpy 156 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 array scalar header ndarray typedef struct PyArrayObject PyObject_HEAD Block of memory char data Data type descriptor x PyArray_Descr xdescr Indexing scheme int nd npy_intp xdimensions Nnpy_intp strides f OLREr sturt PyObject base int flags PyObject xweakreflist PyArrayObject Block of memory gt gt gt x np array 1 2 3 dtype np int32 gt gt gt x data Loea AE eee gt gt gt str x data x01 x00 x00 x00 x02 x00 x00 x00 x03 x00 x00 x00 Memory address of the data gt gt gt xX __ array_interface_ data 0 64803824 The whole __ array_interface_ gt gt gt xX __array_interfac
88. csv sep na_values gt gt gt data Unnamed 0 Gender FSIQ VIQ PIQ Weight Height MRI_Count 0 1 Female Les se ae 118 64 5 S 1S2 il 2 Male 140 150 124 NaN TASS TOOTL2ZI 2 3 Male 139 123 150 143 Uses 1038437 s 4 Male 133 129 i128 L72 OE 9655355 4 5 Female KSA 132 isa 147 65 0 95545 Warning Missing values The weight of the second individual is missing in the CSV file If we don t specify the missing value NA not available marker we will not be able to do statistical analysis Creating from arrays A pandas DataFrame can also be seen as a dictionary of 1D series eg arrays or lists If we have 3 numpy arrays gt gt gt import numpy as np gt gt gt t np linspace 6 6 20 gt gt gt sin_t np sin t gt gt gt COS t MNP COSE We can expose them as a pandas DataFrame gt gt gt pandas Datanrame 1E t Tsini sin t COSl CoS CI COS sin 0 JGO WO2 279415 6 000000 0 S099 O VIZAT 5 206421 0 024S 0 9J9VOT 4 736842 Oso P0509 0 621291 4105263 M53603 02326021 32473684 U 959408 O0 299030 276427105 0 596979 O 802257 2 210526 O 003 L3L 0 999967 1 578947 W 983622 O 611882 02947368 ea Oy OT eS Oo Po FY S Other inputs pandas can input data from SQL excel files or other formats See the pandas documentation 3 1 Statistics in Python 273 Python Scientific lecture notes Release 2013 2 b
89. data and save again It turns out that PIL which knows next to nothing about Numpy happily shares the same data lt 8 8 l 454 img save test2 png 2 2 Advanced Numpy 179 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Array interface protocol Multidimensional buffers e Data type information present e Numpy specific approach slowly deprecated but not going away e Not integrated in Python otherwise See also Documentation http docs scipy org doc numpy reference arrays interface html gt gt gt x INO array IL Zi Ss m gt gt gt X __array_interface__ Melaitcal A769452 Falses memory address of data is readonly vdescer tS Cit Trao data type descriptor typestr lt i4 same in another form strides None strides or None if in C order USinsjoe s 2 2 yersion 3 2 2 Advanced Numpy 180 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt from PIL import Image gt gt gt img Image open data test png e ANG array interctace oatba Ae aser shape 2007 200 4 leypestre Yuk gt gt gt x np asarray img gt gt gt x shape 200 200 4 Note A more C friendly variant of the array interface is also defined 2 2 4 Array siblings chararray maskedarray matrix chararray vectorized string operations gt gt gt Xx np array ta bbb amp
90. data 42 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Numpy provides e extension package to Python for multi dimensional arrays e closer to hardware efficiency e designed for scientific computation convenience e Also known as array oriented computing gt gt gt import numpy as np gt gt gt a np array I0 1 2 31 gt gt gt a array IO ip 2p 31 Tip For example An array containing e values of an experiment simulation at discrete time steps e signal recorded by a measurement device e g sound wave e pixels of an image grey level or colour e 3 D data measured at different X Y Z positions e g MRI scan Why it is useful Memory efficient container that provides fast numerical operations In 1 L range 1000 In 2 stimerit abe 2 for aL in L 1000 loops best of 3 403 us per loop In 3 a np arange 1000 In 4 Stimeit axx2 OOOO Moors best On Ss 2a S per loop Numpy Reference documentation e On the web http docs scipy org e Interactive help In 5 np array String Porm OUr Im function array DOCS Eine array object dtype None copy True order None subok False ndmin 0 e Looking for something gt gt gt np lookfor create array Search results for create array numpy array Create an array numpy memmap Create a memory map to an array stored in a x binary file on disk In 6 np cons MOAI One
91. def 2 x a b return a x x 2 b np sin x Then we can use scipy optimize curve_fit to find a and b gt gt gt guess 2 2 gt gt gt params params_covariance optimize curve_fit f2 xdata ydata guess gt gt gt params array O S96673e6 WO T 7 soe 313 Now we have found the minima and roots of f and used curve fitting on it we put all those resuls together in a single plot 1 5 Scipy high level scientific computing 112 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 120 f x 100 Curve fit result e e Minima 80 vy v Roots 60 x q 40 20 0 20 10 5 0 5 10 X Note In Scipy gt 0 11 unified interfaces to all minimization and root finding algorithms are available scipy optimize minimize scipy optimize minimize_scalar and scipy optimize root They allow comparing various algorithms easily through the met hod keyword You can find algorithms with the same functionalities for multi dimensional problems in scipy optimize Exercise Curve fitting of temperature data The temperature extremes in Alaska for each month starting in January are given by in degrees Celcius max 17 19 TE Zee r237 ey ST Sira ele 23 19 lg eaS Se Ogee Op ee Oy o eye Oe re A M S e 1 Plot these temperature extremes 2 Define a function that can describe min and max temperatures Hint this function has to have a period of 1 year Hint inclu
92. following introductory paragraph If you re impatient and want to practice now please skip it and go directly to Loading and visualization page 130 Introduction Lidars systems are optical rangefinders that analyze property of scattered light to measure distances Most of them emit a short light impulsion towards a target and record the reflected signal This signal is then processed to extract the distance between the lidar system and the target Topographical lidar systems are such systems embedded in airborne platforms They measure distances between the platform and the Earth so as to deliver information on the Earth s topography see for more details l Mallet C and Bretar F Full Waveform Topographic Lidar State of the Art ISPRS Journal of Photogrammetry and Remote Sensing 64 1 pp 1 16 January 2009 http dx doi org 10 1016 j isprsjprs 2008 09 007 1 5 Scipy high level scientific computing 129 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 In this tutorial the goal is to analyze the waveform recorded by the lidar system Such a signal contains peaks whose center and amplitude permit to compute the position and some characteristics of the hit target When the footprint of the laser beam is around 1m on the Earth surface the beam can hit multiple targets during the two way propagation for example the ground and the top of a tree or building The sum of the contributions of each target h
93. for 2 4 Optimizing code 198 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 example to unmix multiple signals that have been recorded through multiple sensors Doing a PCA first and then an ICA can be useful if you have more sensors than signals For more information see the FastICA example from scikits learn To run it you also need to download the ica module In Python we can time the script in i 2 2run E demo py IPychonm CEU timings estimated User Ways 929 Ss System 0 256016 Sr and profile it In 2 Srun p demo py OMG runet ion Calls ain ASS Cll ssieiconels Ordered by internal time ncealls tottime percall cumtime percall filename lineno function 1 14 457 14 457 14 479 14 479 decomp py 849 svd il m OSA 0 054 0 094 0 054 method random_sample of mtrand Random tate object il Q017 Oe Olly 05021 0 021 function_base py 645 asarray_chkfinite 54 0011 0 000 omonmi 0 000 numpy core _dotblas dot 2 000S 0002 OOO 0 002 method Many of numpy ndarray objects 6 0 001 0 000 OZ 0101 O O00 tea pysloo gprame 6 0 001 0 000 ORCL O 000 tea py Lo2 Gg 14 0 001 0 000 OROlOm 0 000 numpy linalg lapack_lite dsyevd 19 000m 0 000 OROOm 0 000 twodim_base py 204 diag 1 0 00T 0 001 0 008 CO 008 aes pye SO sea jae 1 0 001 0 001 1a S51 14 551 execfile 107 0 000 0000 OOO 0 000 defmatrix py 239
94. get started about copies and views e Use flatten as an alternative to ravel What is the difference Hint check which one returns a Know how to create arrays array arange ones zeros view and which a copy Know the shape of the array with array shape then use slicing to obtain different views of the array e Experiment with transpose for dimension shuffling array 2 etc Adjust the shape of the array using reshape or flatten it with ravel e Obtain a subset of the elements of an array and or modify their values with masks gt gt gt alfa lt 0 0 Sorting data e Know miscellaneous operations on arrays such as finding the mean or max array max array mean No need to retain everything but have the reflex to search in the documentation online a npsarray lic Se 5l IL 2 iT docs help lookfor Sorting along an axis gt gt gt b np sort a axis 1 i a p f GR l e For advanced use master the indexing with arrays of integers as well as broadcasting Know more Numpy E E ky functions to handle various array operations D Quick read ly ene If you want to do a first quick pass through the Scipy lectures to learn the ecosystem you can directly skip to the next chapter Matploztlib plotting page 81 The remainder of this chapter is not necessary to follow the rest of the intro part But be sure to come back gt gt gt a sort axis 1 and finish this chapter a
95. gt 14 return libm cos arg ArgumentError argument 1 lt type exceptions TypeError gt wrong type 2 8 Interfacing with C 259 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Numpy Support Numpy contains some support for interfacing with ctypes In particular there is support for exporting certain attributes of a Numpy array as ctypes data types and there are functions to convert from C arrays to Numpy arrays and back For more information consult the corresponding section in the Numpy Cookbook and the API documentation for numpy ndarray ctypes and numpy ctypeslib For the following example let s consider a C function in a library that takes an input and an output array computes the cosine of the input array and stores the result in the output array The library consists of the following header file although this is not strictly needed for this example we list it for completeness void cos_doubles double in_array double out_array int size The function implementation resides in the following C source file include lt math h gt Compute the cosine of each element in in_array storing the result in e UE arrays void cos_doubles double in_array double out_array int size int i for i1 0 i lt size itt ouit tanray e Cos Gnmaricay aN And since the library is pure C we can t use distutils to compile it but must use a combination
96. gt center3 67 58 See centers 24 70 gt gt gt radiusi sadius2 radius radiusa 16 mM 157 14 gt gt gt circlel x centerl 0 2 y centerl 1 2 lt radiusl x2 gt gt gt circle2 x center2 0 2 y center2 1 2 lt radius2 x2 gt gt gt circles x centers 0 2 y centers 1 4sZ2 lt radius34 2 gt gt gt circle4 x center4 0 2 y center4 1 2 lt radius4 x2 gt gt gt 4 circles gt gt gt img circlel circle2 circle3 circle4 gt gt gt mask img astype bool gt gt gt img img astype float gt gt gt img 1 0 2 np random randn img shape gt gt gt Convert the image into a graph with the value of the gradient on gt gt gt the edges gt gt gt graph image img_to_graph img mask mask gt gt gt Take a decreasing function of the gradient we take it weakly gt gt gt dependant from the gradient the segmentation is close to a voronoi gt gt gt graph data np exp graph data graph data std gt gt gt labels spectral_clustering graph n_clusters 4 eigen_solver arpack gt gt gt label_im np ones mask shape gt gt gt label_im mask labels 2 6 6 Measuring objects properties ndimage measurements Synthetic data gt gt gt n 10 gt gt gt 1 256 gt gt gt im np zeros l 1 gt gt gt points lenp random random 2 m 2Z gt gt gt im points 0
97. gt nums __reversed_ lt reverseiterator object at gt gt gt gt it iter nums gt gt gt next it gt gt gt next it gt gt gt next it 3 gt gt gt next it Traceback most recent call last wle V lt erecliias line iL alin lt mnoctuls gt StopIteration Traceback most recent call last alle W lt esiechins n line i alin lt mTocule gt StLoplecrat ion When used in a loop StopIteration is swallowed and causes the loop to finish But with explicit invocation we can see that once the iterator is exhausted accessing it raises an exception Using the for in loop also uses the __iter__ method This allows us to transparently start the iteration over a sequence But if we already have the iterator we want to be able to use it in an for loop in the same way In order to achieve this iterators in addition to next are also required to have a method called ___it er___ which returns the iterator self Support for iteration is pervasive in Python all sequences and unordered containers in the standard library allow this The concept is also stretched to other things e g file objects support iteration over lines gt gt gt f open etc fstab gt gt gt f 1s sh iter Thue The file is an iterator itself and it s___itter___ method doesn t create a separate object only a single thread of sequential access is allowed 2 1 Advanced Python Co
98. i gt gt gt a np arange 10 e Reproduce the slices in the diagram above You may use the following expression to create the array re gt gt gt np arange 6 np arange 0 51 10 np newaxis erce 0 17 27 S7 4p 57 6r T 87 I1 2 i gt gt gt b af 2 arrcay TI 0 2 Se A S 4 i 10 Ie Nar Glas ety ees L207 C2 23 2A 25h a on 6 8 130 327 33 24 35 gt np may_share_memory a 40 12 43 WA 45 True 150 52 aa 54 55 gt e 0 22 gt gt gt b anra L2 2 A e SI gt gt gt a ae CH arrcay li2 lp 27 37 p 5 7 9J 1 3 NumPy creating and manipulating numerical data 51 gt gt gt a np arange 10 gt gt gt c a 2 copy force a copy gt gt gt c 0 12 gt gt gt a ema 0 I 27 S37 4 Sp Ge Te Se BI gt gt gt np may_share_memory a c False This behavior can be surprising at first sight but it allows to save both memory and time 1 3 NumPy creating and manipulating numerical data 52 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Worked example Prime number sieve 01234567 8 910111213 2 EX L MXM NM MD 3 KELLER ERE D 4 EALA EDEL VN Compute prime numbers in 0 99 with a sieve e Construct a shape 100 boolean array is_prime filled with True in the beginning gt gt gt is_prime np ones 100 dtype bool e Cross out 0 and which are not primes gt gt gt is prime
99. in a small number of lines of code outside framework or application code with short modify run fail cycles 1 Make it fail reliably Find a test case that makes the code fail every time 2 Divide and Conquer Once you have a failing test case isolate the failing code e Which module e Which function e Which line of code gt isolate a small reproducible failure a test case 3 Change one thing at a time and re run the failing test case 4 Use the debugger to understand what is going wrong 5 Take notes and be patient It may take a while Note Once you have gone through this process isolated a tight piece of code reproducing the bug and fix the bug using this piece of code add the corresponding code to your test suite 2 3 3 Using the Python debugger The python debugger pdb http docs python org library pdb html allows you to inspect your code interactively Specifically it allows you to e View the source code e Walk up and down the call stack e Inspect values of variables e Modify values of variables e Set breakpoints 2 3 Debugging code 190 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 print Yes print statements do work as a debugging tool However to inspect runtime it is often more efficient to use the debugger Invoking the debugger Ways to launch the debugger 1 Postmortem launch debugger after module errors 2 Launch the module with the deb
100. interface 2 the array interface 3 the new buffer interface PEP 3118 The old buffer protocol Only 1 D buffers e No data type information e C level interface PyBufferProcs tp_as_buffer in the type object e But it s integrated into Python e g strings support it Mini exercise using Pillow Python Imaging Library See also pilbuffer py 2 2 Advanced Numpy 178 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt from PIL import Image gt gt gt data np zeros 200 200 4 dtype np int8 gt gt gt idatals l 1255 0 0 2551 4 Rea gt gt gt In PIL RGBA images consist of 32 bit integers whose bytes are RR GG BB AA gt gt gt data data view np int32 squeeze gt gt gt img Image frombuffer RGBA 200 200 data raw RGBA 0 1 gt gt gt img save test png Check what happens if data is now modified and img saved again The old buffer protocol mMm From buffer Show how to exchange data between numpy and a library that only knows the buffer interface ULL import numpy as np import Image Let s make a sample image RGBA format x np zeros 200 200 4 dtype np ints8 x ko S 254 red 255 opaque X WW l data x view np int32 Check that you understand why this is OK img Image frombuffer RGBA 200 200 data img save test png Modify the original
101. is based on the idea of the diffusion of labels in the image gt gt gt from skimage import segmentation gt gt gt Transform markers image so that O valued pixels are to gt gt gt be labelled and 1 valued pixels represent background gt gt gt markers image 1 gt gt gt labels_rw segmentation random_walker image markers image distance map watershed segmentation random walker segmentation LJ hd O k Postprocessing label images skimage provides several utility functions that can be used on label images ie images where different discrete values identify different regions Functions names are often self explaining skimage segmentation clear_border skimage segmentation relabel_from_one skimage morphology remove_small_obje etc 3 3 Scikit image image processing 334 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercise gt gt gt coins_edges segmentation mark_boundaries coins clean_border astype np int e Load the coins image from the data submodule e Separate the coins from the background by testing several segmentation methods Otsu thresholding adaptive thresholding and watershed or random walker segmentation e If necessary use a postprocessing function to improve the coins background segmentation 5 lt f i y 3 3 5 Measuring regions proper
102. l_var local_var noisy_img size size ipdb gt m gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 3 35 denoised_img local_mean noisy_img size size gt 36 l1_var local_var noisy_img size size S7 for i in vsange 3 e Step a few lines and explore the local variables ipdb gt n gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filltering py 37 36 l_var local_var noisy_img size size gt 37 fori an range 3 38 res noisy_img denoised_img ipdb gt print ll var 586e 5379 53916 o o 5071 4799 5149 S013 363 437 i057 346 262 4355 e397 9 AIO 344 saap 392 604 39771 A35 S62 308 soap 275 198 1632 DAG T292 290 teu 248 209 1053 L 466 7869 V36 scop 1695 1725 1940 Toclo gt print l var mimg 0 Oh dear nothing but integers and 0 variation Here is our bug we are doing integer arithmetic 2 3 Debugging code 193 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Raising exception on numerical errors When we run the wiener_filtering py file the following warnings are raised In 2 run wiener_filtering py wiener_filtering py 40 RuntimeWarning divide by zero encountered in divide aOLse ews il morse il_werw We can turn these warnings in exception which enables us to do post mortem debugging on them and find our problem more quickl
103. lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 2 0 Effect of gender 1 Female 0O Male 2 0 1 5 1 0 WAGE 0 5 0 0 0 5 0 5 100 AGE AGE N O O EDUCATION EDUCATION n O 0 0 5 0 0 0 5 1 0 1 5 2 0 10 20 30 40 50 60 70 O 5 10 15 20 1055 0 0 0 5 1 0 1 5 2 0 10 20 30 40 50 60 70 O 3 10 15 20 WAGE AGE EDUCATION WAGE AGE EDUCATION 3 1 Statistics in Python 305 3 1 Statistics in Python 306 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 2 0 Effect of union 0 5 AGE EDUCATION 0 0 5 0 0 0 5 1 0 15 2 0 10 20 30 40 50 60 70 WAGE Union member 0 Not union member AGE 0 D 10 15 EDUCATION 20 3 1 Statistics in Python 307 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 2 0 i 1 5 e bs3che 2 E 8 2 1 0 e 2 gt T o 6 H a B a f i i d i 0 5 e ese ry a a 0 0 n 0 5 5 10 15 20 EDUCATION Script output loading seaborn Python source code plot_wage_data py Standard library imports import urllib import os import matplotlib pyplot as plt Hee eee eee eee estes eee eeeeeeeee tee ee eeeeeeeteeee tea teats tee t dt d te tet te teed te tHF Load the data import pandas if not os path exists wages txt Download the file if it is not present urllib urlre
104. look at how such typemaps work in practice We have the same cos_doubl1les function as in the ctypes example void cos_doubles double x in_array double out_array int size 2 8 Interfacing with C 263 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 include lt match N gt Compute the cosine of each element in in_array storing the result in aoue aray 7 void cos_doubles double in_array double out_array int size int i for i 0 i lt size itt out_array i cos in_array i This is wrapped as cos_doubles_func using the following SWIG interface file x Example of wrapping a C function that takes a C double array as input using numpy typemaps for SWIG Smodule cos doubles 6 x the resulting C file should be built as a python extension define SWIG_FILE_WITH_INIT x Includes the header in the wrapper code inelude cos doubles Mi o we include the numpy typemaps Sinclude numpy i x need this for correct module initialization gimit import_array ol x typemaps for the two arrays the second will be modified in place Sapply doublex IN_ARRAY1 int DIM1 double x in_array int size_in Sapply doublex INPLACE_ARRAY1 int DIM1 double out_array int size_out x Wrapper for cos_doubles that massages the types Sinline x takes as input two numpy arrays void cos_doubles_func double in_arr
105. lt setup gt try yield lt value gt finally lt cleanup gt The contextlib contextmanager helper takes a generator and turns it into a context manager The gen erator has to obey some rules which are enforced by the wrapper function most importantly it must yield exactly once The part before the yield is executed from __enter__ the block of code protected by the con text manager is executed when the generator is suspended in yield and the rest is executed in__ ex it__ If an exception is thrown the interpreter hands it to the wrapper through __exit__ arguments and the wrapper function then throws it at the point of the yield statement Through the use of generators the context manager is shorter and simpler Let s rewrite the closing example as a generator contextlib contextmanager def closing obj try yield obj finally obj close Let s rewrite the assert_raises example as a generator contextlib contextmanager def assert_raises type try yield except type return except Exception as value raise AssertionError wrong exception type else raise AssertionError exception expected 2 1 Advanced Python Constructs 154 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Chapter contents Here we use a decorator to turn generator functions into context managers e Life of ndarray page 156
106. ma masked_array data 1 gt gt gt year datal 0 bad_years year gt 1903 amp year lt 1 year gt 1917 amp year lt amp means and and means or populations bad_years 0 np ma masked populations bad_years 1 np ma masked gt gt gt populations mean axis 0 Masked _ array data 40472 7 2727272727 18627 272727272728 42400 0 mask False False False fill value le 20 gt gt gt populations std axis 0 masked_array data 21087 656489006717 15625 799814240254 3322 5062255844787 mask False False False fill value 1e 20 Note that Matplotlib knows about masked arrays gt gt gt pite pikot See Popullit ions To lt matplotlib lines Line2D object at gt 80000 70000 60000 50000 40000 30000 20000 10000 0 1900 1910 1915 1920 2 2 Advanced Numpy 183 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 recarray purely convenience eee arth Mpeerray i a aed Nb 2 deype l a ol Oy ame 1 gt gt gt arr2 arr view np recarray gt gt gt arr2 x echarannay kon mnp dtype S1 gt gt gt arr2 y array l 2 1 matrix convenience e always 2 D e x is the matrix product not the elementwise one See npemarci i Ol On Li Mpameeri lt a Ie e an macax e2 l y 4I 2 2 5 Summary e Anatomy of the ndarray data dtype str
107. mathematical theory that stems from set theory It characterizes and transforms geometrical structures Binary black and white images in particular can be transformed using this theory the sets to be transformed are the sets of neighboring non zero valued pixels The theory was also extended to gray valued images Erosion Dilation al er Elementary mathematical morphology operations use a structuring element in order to modify other geometrical structures Opening Closing Let us first generate a structuring element gt gt gt el ndimage generate_binary_structure 2 1 gt gt gt el array False True False lss Tr er True Truel False True False dtype bool gt gt gt el astype np int array CrO lp Ol Ly de Diy from scipy import misc gt gt gt lena misc lena gt gt gt import numpy as np gt gt gt noisy_lena np copy lena astype np float gt gt gt noisy_lena lena std 0 5 np random standard_normal lena shape gt gt gt blurred_lena ndimage gaussian_filter noisy_lena sigma 3 gt gt gt median_lena ndimage median_filter blurred_lena size 5 gt gt gt from scipy import signal gt gt gt wiener_lena signal wiener blurred_lena 5 5 Many other filters in scipy ndimage filters and scipy signal can be applied to images Exercise Compare histograms for the different filtered images 1 5 Scipy high level scien
108. mlab savefig foo png size 300 300 file Change the view mlab view azimuth 45 elevation 54 distance 1 Changing plot properties 3 5 3D plotting with Mayavi 360 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Tip In general many properties of the various objects on the figure can be changed If these visualization are created via mlab functions the easiest way to change them is to use the keyword arguments of these functions as described in the docstrings Example docstring mlab mesh Plots a surface using grid spaced data supplied as 2D arrays Function signatures MSS OX Wr Bp soc X y Zare 2D arrays all of the same shape giving the positions of the vertices of the surface The connectivity between these points is implied by the connectivity on the arrays For simple structures such as orthogonal grids prefer the surf function as it will create more efficient data structures Keyword arguments color the color of the vtk object Overides the colormap if any when specified This is specified as a triplet of float ranging from 0 to 1 eg 1 1 1 for white colormap type of colormap to use extent xmin xmax ymin ymax zmin zmax Default is the x y z arrays extents Use this to change the extent of the object created figure Figure to populate line_width The with of the lines if any used Must be
109. mode t A type as go spell checker like integration e In vim Use the pyflakes vim plugin 1 download the zip file from http www vim org scripts script php script_id 2441 2 extract the files in vim ftplugin python 3 make sure your vimrc has filetype plugin indent on Alternatively use the syntastic plugin This can be configured to use flake8 too and also handles on the fly checking for many other languages load data exercises j Z data pi data debug_file py at least two spaces before inline comment 2 3 Debugging code 189 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e In emacs Use the flymake mode with pyflakes documented on http www plope com Members chrism flymake mode add the following to your emacs file when load flymake t defun flymake pyflakes init let temp file flymake init create temp buffer copy flymake create temp inplace local file file relative name temp file file name directory buffer file name list pyflakes list local file add to list flymake allowed file name masks C spy flymake pyflakes init add hook find file hook flymake find file hook 2 3 2 Debugging workflow If you do have a non trivial bug this is when debugging strategies kick in There is no silver bullet Yet strategies help For debugging a given problem the favorable situation is when the problem is isolated
110. nb_ passengers 310 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 dist nb_passengers 0 0 O 100200300400500600 5000 50Q00DB02DG2bOHD00 0 51 01 52 02 53 03 54 04 5 fare dist Fare 2001 2000 300 250 200 150 100 50 0 nb_passengers 50 100 150 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 NB passengers 2001 2000 200 Lai 100 i 7 i r 4 0 al LI ce 7 100 b y 200 e 300 400 500 0 500 1000 1500 2000 2500 3000 dist Script output OLS Regression Results Dep Variable fare R squared 02m5 Model OLS Adj R squared OR2a7 5 Method Least Squares IP SiCSNC AL Giese L585 Date Mon 21 Sep 2015 PicOlo Cl Sic ete istic 0 00 Time ise Ns 3 bys Log Likelihood 45532 No Observations S352 ALC 9 107e 04 Df Residuals 8349 BIGE 9 109e 04 Df Model 2 coef std err E IPS ic 95 0 Commit Wine Intercept 211 2428 2 466 35 669 0 000 206 409 2G 206 3 1 Statistics in Python 311 3 1 Statistics in Python 312 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 import os HEPREEEEREREREREEREREREREEREREE REET ERE EEERER EERE REEEEEE EERE TEE EERE HEHE Load the data import pandas LE not os path exists aAlriares txt dist 0 0484 ORC Om AS LAS
111. not found eos doubles i1 24 Warning 490 Fragment NumPy Backward Compatibility not found cos_doubles i 24 Warning 490 Fragment NumPy_Backward_Compatibility not found creating build creating build temp linux x86_64 2 7 t geck pthrceadi Cno strict olmasang c O2 DNDEBUG o vrap OS Well SiWeicieikcEeSoueoic types fPIC geci pthread MnoO Suiclec ellicsiling o OA DNDEBUG o Enr apy OF Ma ME NS teet Pr OE Ta file included trom heme ese anaconda Jib pyenen2 Sire packages numpy cone inal p from home esc anaconda lib python2 7 site packages numpy core incl pf from home esc anaconda lib python2 7 site packages numpy core incl from ecos doubles iura e 20C home esc anaconda lib python2 7 site packages numpy core include numpy npy_deprecat gee pthread shared build teno linux x86 64 2 7 cos_doubles o pun la temp linux x3i _ otypes fPIC ude numpy nda ude numpy nda ude numpy arr d_api h 11 2 64 2 7 cos_dot SmiS Duana cos_doubles h cos_doubles py cos_doubles_wrap c setup py cos doubles Te eos doubles in Teos doubles sor MUME a test_cos_doubles py And as before we convince ourselves that it worked import numpy as np import pylab import cos_doubles x np arange 0 2 x np pi 0 1 y np empty_like x cos_doubles cos_doubles_func x y py lab plot y pylab show
112. notes Release 2013 2 beta euroscipy 2013 Brian Kernighan Everyone knows that debugging is twice as hard as writing a program in the first place So if you re as clever as you can be when you write it how will you ever debug it e We all write buggy code Accept it Deal with it e Write your code with testing and debugging in mind e Keep It Simple Stupid KISS What is the simplest thing that could possibly work e Don t Repeat Yourself DRY Every piece of knowledge must have a single unambiguous authoritative representation within a system Constants algorithms etc e Try to limit interdependencies of your code Loose Coupling e Give your variables functions and modules meaningful names not mathematics names pyflakes fast static analysis They are several static analysis tools in Python to name a few e pylint e pychecker e pyflakes e peps e flake Here we focus on pyflakes which is the simplest tool e Fast simple e Detects syntax errors missing imports typos on names Another good recommendation is the 1ake8 tool which is a combination of pyflakes and pep8 Thus in addition to the types of errors that pyflakes catches flake8 detects violations of the recommendation in PEP8 style guide Integrating pyflakes or flake8 in your editor or IDE is highly recommended it does yield productivity gains Running pyflakes on the current edited file You can bind a key to r
113. notes Release 2013 2 beta euroscipy 2013 reservoir Reservoir name Lac de Vouglans max_storage 605 Initialisation All the traits do have a default value that initialise the variables For example the basic python types do have the following trait equivalents Trait Python Type Built in Default Value Bool Boolean False Complex Complex number 0 0 Float Floating point number 0 0 Int Plain integer 0 Long Long integer OL Str String o Unicode Unicode u A number of other predefined trait type do exist Array Enum Range Event Dict List Color Set Expression Code Callable Type Tuple etc Custom default values can be defined in the code from traits api import HasTraits Str Float class Reservoir HasTraits name Str max_storage Float 100 reservoir Reservoir name Lac de Vouglans Complex initialisation When a complex initialisation is required for a trait a_XXX_default magic method can be implemented It will be lazily called when trying to access the XXX trait For example def _name_default self uur Complex initialisation of the reservoir name return Undefined Validation Every trait does validation when the user tries to set its content reservoir Reservoir name Lac de Vouglans max_storage 605 reservoir max_storage 230 TraitError Traceback most recent call last Users dpinte projects s
114. of a program See also More examples and reading e PEP 318 function and method decorator syntax PEP 3129 class decorator syntax http wiki python org moin PythonDecoratorLibrary http docs python org dev library functools html e http pypi python org pypi decorator e Bruce Eckel Decorators I Introduction to Python Decorators Python Decorators II Decorator Arguments Python Decorators III A Decorator Based Build System 2 1 3 Context managers A context manager is an object with _ enter __ and __exit__ methods which can be used in the with state ment with manager as var do_something var is in the simplest case equivalent to var manager _enter__ try do_something var finally manager __exit__ In other words the context manager protocol defined in PEP 343 permits the extraction of the boring part of a try except finally structure into a separate class leaving only the interesting do_something block 1 The __enter__ method is called first It can return a value which will be assigned to var The as part is optional if it isn t present the value returned by ___enter___1s simply ignored 2 The block of code underneath with is executed Just like with t ry clauses it can either execute success fully to the end or it can break continue or return or it can throw an exception Either way after the block is finished the __exit___ method is called If an ex
115. of make and gcc m PHONY clean libcos_ doubles so cos doubles o gcc shared Wl soname libcos_doubles so o libcos_doubles so cos_doubles o coe doubles O Cos doubles ce gec e EIC ceos doubles ec o cos doubles o clean rm vf libcos_doubles so cos_doubles o cos doubles pye We can then compile this on Linux into the shared library 1ibcos_doubles so S Is cos_doubles c cos_doubles h cos_doubles py makefile test_cos_doubles py make gecl cel reei cos doubles e One osmcdouloliasm eS gcc shared WI soname libcos_doubles so o libcos_doubles so cos doubles o SFS cos_doubles c Teos _doubles o libcos_doubles sox test_cos_doubles py cos_doubles h cos_doubles py makefile Now we can proceed to wrap this library via ctypes with direct support for certain kinds of Numpy arrays mun Example of wrapping a C library function that accepts a C double array as THOU Using Che MED a CE ISS zp u import numpy as np import numpy ctypeslib as npct from ctypes import c_int input type for the cos doubles function 2 8 Interfacing with C 260 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 must be a double array with single dimension that is contiguous array_ld_double npct ndpointer dtype np double ndim 1 flags CONTIGUOUS load the library using numpy mechanisms lobed npet load library libeces double T Ty setup the return types and ar
116. of the Hes sian 30 ovo 0 20 30 40 50 An ill conditionned quadratic function On a exactly quadratic function BFGS is not as fast as Newton s method but still very fast 10 0 20 40 60 80 100 120 An ill conditionned non quadratic function Here BFGS does better than Newton as its empirical estimate of the curvature is better than that given by the Hessian An ill conditionned very non quadratic function gt gt gt def f x The rosenbrock function DA return 5 Fee EE ON R TOE gt gt gt def fprime x return nip array G27 oe 0 T C E T a EO 2 e gt gt gt optimize fmin_bfgs f 2 2 fprime fprime Optimization terminated successfully Current function value 0 000000 Iterations 16 Function evaluations 24 Gradient evaluations 24 array 1 00000017 1 000000261 L BFGS Limited memory BFGS Sits between BFGS and conjugate gradient in very high dimensions gt 250 the Hessian matrix is too costly to compute and invert L BFGS keeps a low rank version In addition the scipy version scipy optimize fmin_l_bfgs_b includes box bounds gt gt gt def f x The rosenbrock function bes return ron K O ee2 a Cx Ol e222 22 gt gt gt def fprime x ig beaiel MO array C2 5 oe ll oO 4esx Ol ex Tl x Olee2 2x1 K 0 2 e gt gt gt optimize fmin_l_bfgs_b f 2 2
117. or C ordering may make a big difference on operations In 5 a np random rand 20 2 x18 In 6 b np random rand 20 2418 tna 7 timeit np adot B a T 1 loops best of 3 194 ms per loop In 8 c npcascontiquousarray a T in 9i Stimeil np dot b eE IOP UOops best ot e S4e7 ms sper Loop Note that copying the data to work around this effect may not be worth it In 10 timeit c np ascontiguousarray a T NOME GpSs bestor Nems per Koop Using numexpr can be useful to automatically optimize code for such effects e Use compiled code 2 4 Optimizing code 202 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 The last resort once you are sure that all the high level optimizations have been explored is to transfer the hot spots i e the few lines or functions in which most of the time is spent to compiled code For compiled code the preferred option is to use Cython it is easy to transform exiting Python code in compiled code and with a good use of the numpy support yields efficient code on numpy arrays for instance by unrolling loops Warning For all the above profile and time your choices Don t base your optimization on theoretical considerations Additional Links e If you need to profile memory usage you could try the memory_profiler e If you need to profile down into C extensions you could try using gperftools from Python with yep
118. poty pylab show And this should result in the following figure 1 0 2 8 3 Ctypes Ctypes is a foreign function library for Python It provides C compatible data types and allows calling functions in DLLs or shared libraries It can be used to wrap these libraries in pure Python Advantages e Part of the Python standard library e Does not need to be compiled e Wrapping code entirely in Python Disadvantages e Requires code to be wrapped to be available as a shared library roughly speaking d11 in Windows x So in Linux and dylib in Mac OSX e No good support for C Example As advertised the wrapper code is in pure Python To compile this we can use distutils again However we need to be sure to include the Numpy headers by using func numpy get_include money wt exame le Om wrapping COS LuUNnCEIOn From mach P using Caypes import ctypes from ctypes util import find_library find and load the library libm ctypes cdll LoadLibrary find_library m set the argument type libm cos argtypes ctypes c_double 2 8 Interfacing with C 257 2 8 Interfacing with C 258 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 set the return type libm cos restype ctypes c_double def cos_func arg IS SWiK Oem fon CoS From mach PEIE return libm cos arg e Finding and loading the library may vary depending on your operating sy
119. self release self inflows overflow new_storage self max_storage return max overflow 0 print statel selt print Storage tRelease tInflows tSpillage Str format NE onne 2E fon i in range 4 print str_format format self storage self release self inflows self spillage prine i R9 If name main projectA Reservoir name Project A max_storage 30 max_release 5 hydraulic_head 60r efficiency 0 ReservoirState reservoir projectA storage 25 release 4 inflows Printe state configure_traits Edit properties Name Project A Storage 21 0 Spillage Inflows 0 0 Release 0 0 5 0 4 0 Some use cases need the delegation mechanism to be broken by the user when setting the value of the trait The PrototypeFrom trait implements this behaviour head Float 10 desc Hydraulic head m efficiency Range 0 1 turbine Instance Turbine installed_capacity PrototypedFrom turbine power if name main turbine Turbine turbine_type typel power 5 0 reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 turbine turbine print installed capacity is initialised with turbine power print reservoir installed_capacity print 9 x15 print updating the turbine power updates the installed capacity turbine power 10 print reservoir installed_capacity prine
120. supported as well gt gt gt integrate exp x x 0 o l gt gt gt integrate exp xxx2 x 00 o Oi Exercises 3 2 4 Equation solving SymPy is able to solve algebraic equations in one and several variables Ia 7 solve x 4 1 x oveli Tr Le i As you can see it takes as first argument an expression that is supposed to be equaled to 0 It is able to solve a large part of polynomial equations and is also capable of solving multiple equations with respect to multiple variables giving a tuple as second argument 3 2 Sympy Symbolic Mathematics in Python 322 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 iit Se solve dx s Say 2 o 7 Gy IS lbs wv Owe lS 2 trys i se 3 It also has limited support for trascendental equations In 9 solve exp x 1 x Ome Ss iat Another alternative in the case of polynomial equations is factor factor returns the polynomial factorized into irreducible terms and is capable of computing the factorization over various domains iia TOs need Sioeee2 p il iia LiL s factorii Owe Lie il amp weee e l xeeZ iia 22 steececGe uechulus 5 Owe l2 s 2 a x a2 2 xx ee2 SymPy is also able to solve boolean equations that is to decide if a certain boolean expression is satisfiable or not For this we use the function satisfiable In 13 satisfiable x amp y
121. that the mere presence of this keyword completely changes the nature of the function this yield statement doesn t have to be invoked or even reachable but causes the function to be marked as a generator When a normal function is called the instructions contained in the body start to be executed When a generator is called the execution stops before the first instruction in the body An invocation of a generator function creates a generator object adhering to the iterator protocol As with normal function invocations concurrent and recursive invocations are allowed When next is called the function is executed until the first yield Each encountered yield statement gives a value becomes the return value of next After executing the yield statement the execution of this function is suspended gt gt gt def f yield 1 yield 2 SSS E lt generator object f at Ox gt gt gt gt gen f gt gt gt next gen 1 gt gt gt next gen 2 gt gt gt next gen Traceback most recent call last Raike lt lt sichne sine m min lt mocualke gt StopIteration 2 1 Advanced Python Constructs 141 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Let s go over the life of the single invocation of the generator function gt gt gt def f prine stark yield 3 Prin middle yield 4 TE Print finished gt gt gt gen gt gt gt nex
122. this image according to how the shape locally fits or misses the image Structuring element gt gt gt el ndimage generate_binary_structure 2 1 gt gt gt el array False True False True True True False True False dtype bool gt gt gt el astype np int 2 6 Image manipulation and processing using Numpy and Scipy 229 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Erosion minimum filter Replace the value of a pixel by the minimal value covered by the structuring element gt gt gt a np zeros 7 7 dtype np int gt gt gt all 6 225 il gt gt gt a array IIO 07 07 07 07 07 01 PO O Wee a ah Ole o POr Oe Teena ah Op 20 WO 0r Say Ma Sey o IC O 205 ye an aa o o LOr 07 Del Ty 0 Ol On 07 Op 0 0 0 7 0 gt gt gt gt ndimage binary_erosion a astype a dtype array ClO Oy GO OF 05 oi On Oy G7 O07 20 105 10 ORO awe Oe Cy On O Oe eo Oy el Ore WOee Oil Ons 07 Olly Oy WO Oe 0 0 07 Ol O 0 0 0 0 Or o gt gt gt Erosion removes objects smaller than the structure gt gt gt ndimage binary_erosion a structure np ones 5 5 astype a dtype array Oe 0 0 0 0 0 0l lOr 0 WO 0r 07 07 Ol Oz 07 07 07 07 07 Ol O 07 0 0 0 0 Oly Or 07 07 05 07 OF Wl lO 07 0 07 07 07 Ok lOr 07 0p 07 0 07 0l Erosion Dilation Opening Closing
123. which return the original function If they were to return a new function an extra level of nestedness would be required In the worst case three levels of nested functions gt gt gt def replacing_decorator_with_args arg print defining the decorator def decorator function in this inner function arg is available too prine doing decoration sr z arg def _wrapper xargs x kwargs print inside wrapper r r args kwargs return function x args xkwargs return _wrapper return _decorator gt gt gt replacing decorator _with_args abc def function xargs xxkwargs prine Viside TLunction sf gr args kwargs a return 14 defining the decorator doing decoration abc 22 gt uMCctakom CHIP inside wrapper 11 12 inside function lil 12 4 14 The _wrapper function is defined to accept all positional and keyword arguments In general we cannot know what arguments the decorated function is supposed to accept so the wrapper function just passes everything to the wrapped function One unfortunate consequence is that the apparent argument list is misleading Compared to decorators defined as functions complex decorators defined as classes are simpler When an object is created the __init__ method is only allowed to return None and the type of the created object cannot be changed This means that when a decorator is defined as a class it doesn t make
124. you can arrange plots in a regular grid You need to specify the number of rows and columns and the number of the plot Note that the gridspec command is a more powerful alternative Axes 1 subplot 2 1 1 subplot 2 2 1 subplot 2 2 2 subplot 1 2 1 subplot 1 2 2 Axes 2 Axes 3 subplot 2 1 2 subplot 2 2 3 subplot 2 2 4 Axes 4 Axes 5 Axes Axes are very similar to subplots but allow placement of plots at any location in the fig ure So if we want to put a smaller plot inside a bigger one we do so with axes A figure in matplotlib means the whole window in the user interface Within this figure there can be subplots Tip So far we have used implicit figure and axes creation This is handy for fast plots We can have more control over the display using figure subplot and axes explicitly While subplot positions the plots in a regular grid axes allows free placement within the figure Both can be useful depending on your intention We ve already worked with figures and subplots without explicitly calling them When we call plot matplotlib calls gca to get the 1 4 Matplotlib plotting 89 1 4 Matplotlib plotting Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 axes 0 1 0 1 8 8 axes 0 4 0 4 5 5 jaxes 0 2 0 2 3 3 axes 0 3 0 3 5 5
125. you cannot use IPython for instance to debug a script that wants to be called from the command line In this case you can call the script with python m pdb script py S python m pdb index_error py gt home varoquau dev scipy lecture notes advanced debugging_optimizing index_error pyf 1 lt module gt gt UN Temall snippet tO raser an IndexError 0 Pdb continue Traceback most recent call last File usr lib python2 6 pdb py line 1296 1n main pdb _runscript mainpyfile Bile usr lib python2 6 pdb py lane 1215 an vunseript self run statement bile uer lib python2 6 bdb py line 372 1m run exec cmd in globals locals File lt string gt line 1 in lt module gt File index_error py line 8 in lt module gt index_error File index_error py line 5 in index_error print lst len lst IndexError list index out of range Uncaught exception Entering post mortem debugging Running cont or step will restart the program gt home varoquau dev scipy lecture notes advanced debugging_optimizing index_error py 5 index_er gt prime sullen lst dls Step by step execution Situation You believe a bug exists in a module but are not sure where For instance we are trying to debug wiener_filtering py Indeed the code runs but the filtering does not work well e Run the script in Python with the debugger using Srun d wiener_filtering p In 1 run d wiener_filteri
126. 0 An ill conditionned very non quadratic function Error on f x 10 iterations 10 Ih function calls L L L 0 50 100 150 200 250 In scipy scipy optimize fmin implements the Nelder Mead approach gt gt gt def f x The rosenbrock function ae return orl ON 42 t x 1 ee ONE 2 gt gt gt Optimize cemin E I2 21 Optimization terminated successfully Current function value 0 000000 Iterations 46 Function evaluations 91 acca 0 999965667 O 99996682 2 7 Mathematical optimization finding minima of functions 247 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Global optimizers If your problem does not admit a unique local minimum which can be hard to test unless the function is convex and you do not have prior information to initialize the optimization close to the solution you may need a global optimizer Brute force a grid search scipy optimize brute evaluates the function on a given grid of parameters and returns the parameters corresponding to the minimum value The parameters are specified with ranges given to numpy mgrid By default 20 steps are taken in each direction gt gt gt def f x The rosenbrock function i return oes Oe xT1 x10 s42 2 gt gt gt Ope imize Druten CIPD 2 array O L O0001462 1 00001547 2 7 3 Practical guide to optimization with scipy Choosing a method 1
127. 0 10 5 2 107 e U c Q 2 0 10 Il conditioned Gaussian 2 IIl conditioned quadratic E 10 i a aa e dim 2 dim 32 Se SANSA o dim 8 e dim 128 Well conditioned quadratic 4 coGugate gradient Powell Nelder mead L BFGS BFGS Conjugate gradient BFGS L BFGS Newton w f w f w f w Hessian Without knowledge of the gradient e In general prefer BFGS scipy optimize fmin_bfgs or L BFGS scipy optimize fmin_l_bfgs_b even if you have to approximate nu merically gradients e On well conditioned problems Powell scipy optimize fmin_powell and Nelder Mead scipy optimize fmin both gradient free methods work well in high dimension but they collapse for ill conditioned problems With knowledge of the gradient e BFGS scipy optimize fmin_bfgs or L BFGS scipy optimize fmin_l_bfgs_b e Computational overhead of BFGS is larger than that L BFGS itself larger than that of conjugate gradient On the other side BFGS usually needs less function evaluations than CG Thus conjugate gradient method is better than BFGS at optimizing computationally cheap functions With the Hessian e If you can compute the Hessian prefer the Newton method scipy optimize fmin_ncg 2 7 Mathematical optimization finding minima of functions 248 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 If you have noisy measurements e Use Nelder Mead scipy optimize fmin or Powell scipy o
128. 0 plt subplots_adjust plt figure figsize 5 2 E seaborn boxplot data nb_passengers_2001 data nb_passengers_2000 4 DLE title NB passengers 2001 2000 plt subplots_adjust F HeeHHHHH HHH HHH HH HHH HHH HHH HHH HHH HHH Hea aaa HAAG EEF Statistical testing dependence of fare on distance and number of passengers import statsmodels formula api as sm result sm ols formula fare 1 dist nb_passengers data data_flat fit l print result summary FEE g 2 a 2 5 2 g i 4 72 amp P amp P Ss VQ F g B Haighi Using a repuse Fie oe result sm rlm formula fare 1 dist nb_passengers data data_flat fit print result summary Figure 3 9 Relating Gender and IQ page 315 HetHHHHH HHH HHH HHH HHH eH HHH HHH eee eee eae aaa EEE EEE EEF blue male green female Statistical testing regression of fare on distance 2001 2000 difference result sm ols formula fare_2001 fare_2000 1 dist data data fit print result summary Plot the corresponding regression oO data fare _difference data fare_2001 data fare 2000 gt seaborn Ilmplot x dist y fare_difference data data plt show ae 1050000 Total running time of the example 9 49 seconds 0 minutes 9 49 seconds 1000000 5 950000 Solutions to the exercises of the course 9 900000 cc a l 850000
129. 0 scr SU O88099970 sso 1a 32891964 5 8 28 Tip With a p value of 10 28 we can claim that the population mean for the IQ VIQ measure is not 0 2 sample t test testing for difference across populations We have seen above that the mean VIQ in the male and female populations were different To test if this is significant we do a 2 sample t test with scipy stats ttest_ind gt gt gt female_vig data data Gender Female VIQ gt gt gt male_vig data data Gender Male VIQ gt gt gt stats ttest_ind female_vigq male_vigq loos Q TIZOLOLI 232 265 U2444528 FOI 71S S38 222 3 1 Statistics in Python 277 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Paired tests repeated measurements on the same indivuals 150 140 130 120 110 100 90 80 70 FSIQ se PIQ VIQ and FSIQ give 3 measures of IQ Let us test if FISQ and PIQ are significantly different We can use a 2 sample test gt gt gt stats ttest_ind data FSIQ data PIQ een Os4Oobs459036 4 W 642 77250 2 8 The problem with this approach is that it forgets that there are links between observations FSIQ and PIQ are measured on the same individuals Thus the variance due to inter subject variability is confounding and can be removed using a paired test or repeated measures test gt gt gt stats ttest_rel data Halo data Emo socl JSAZOLI
130. 00 0 056 omorii Omnibus 136 558 Durbin Watson 1 544 Prob Omnibus 0 000 Jargque Bera JB 149 6074 Skew 0 462 Prob JB 3 23e 33 KUrECOSTS 2 MOPO Conca Nor 2 40e 03 Warnings 1 The condition number is large strong multicollinearity or other numerical problems 2 4e 03 This might indicate that there are Python source code plot_airfare py Standard library imports import urllib Download the file if it is not present urllib urlretrieve http www stat ufl edu winner data airg4 dat alrrares Exen As a seperator is a regular expression that means one of more space data pandas read_csv airfares txt sep header 0 neames city comity 2 i oop pop2 dist tare 2000 Tnb passengers 2000 fare_2001 nb_passengers_2001 we log transform the number of passengers import numpy as np data nb_passengers_2000 data nb_passengers_2001 np logl10 data nb_passengers_2000 np logl10 data nb_passengers_2001 PEPPER EPREREREREEREREREEEERERELE EEE EEE ERE TEER ERE TERRE ET EEE ERE HEHE Make a dataframe whith the year as an attribute instead of separate columns This involves a small danse in which we separate the dataframes in 2 one for year 2000 and one for 2001 before concatenating again Make an index of each flight data_flat data reset_index date 2000 data flati icity u eCity POPI POPA raisti rifare 2000
131. 1 in print_big_array small_array lt numpy ndarra TS 1630 Python ceval c No such file or directory If you have a segmentation fault you cannot debug it with pdb as it crashes the Python interpreter before it can a n Fe eon eevee a drop in the debugger Similarly if you have a bug in C code embedded in Python pdb is useless For this we turn aco to the gnu debugger gdb available on Linux pyframe segfault py 12 print_big_array Before we start with gdb let us add a few Python specific tools to it For this we add a few macros to our gbdinit The optimal choice of macro depends on your Python version and your gdb version I have added a simplified version in gdbinit but feel free to read Debugging WithGdb The corresponding code is 2 3 Debugging code 195 2 3 Debugging code 196 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 def make_big_array small_array big_array stride_tricks as_strided small_array shape 2e6 2e6 strides 32 32 return big_array def print_big_array small_array big_array make_big_array small_array Thus the segfault happens when printing big_array 10 The reason is simply that big_array has been allocated with its end outside the program memory Note For a list of Python specific commands defined in the gdbinit read the source of this file Wrap up exercise The following script is well documented and hopef
132. 1 Tnb passengers 20007 i Rename the columns data 2000 collumas Merey i euty2 gt gt popil pop dust iare nb_passengers Add a column with the year data_2000 year 2000 data 200 m Taatra silat ei yl eitye o e ose 2 dist Y tare 20011 Tanp passengers 20011 py Rename the columns datan 200i eco lumas i eici Veaheye ool Meco Velitsiey ranen nb_passengers Add a column with the year data_2001 year 2001 data_flat pandas concat data_2000 data_2001 PPEPFPRRFPPRRFPREFFRRRFRRAERRAFFPRRRPRRRFFRRRFRRRERREEERAREEREEEEEEEEEREEEEEEE H Plot scatter matrices highlighting different aspects import seaborn seaborn pairplot data flat vars fare dist nb_passengers kind reg markers A second plot to show the effect of the year ie the 9 11 effect seaborn pairplot data flat vars fare dist nb_passengers kind reg hue year markers 3 1 Statistics in Python 313 3 1 Statistics in Python 314 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Blue male green female Hee eee HHH HHH HHH eH AHR HHH RH HH aa RE HeaSeaH HHHE ER EHEH Plot the difference in fare import matplotlib pyplot as plt plt figure figsize 5 2 seaborn boxplot data fare_2001 data fare_2000 plE title Pare 2001 200
133. 13 2 beta euroscipy 2013 rather specialized solving PDEs by finite differences with an iterative solver Examples e create some DIA matrices gt gt gt data np array 1 2 3 4 repeat 3 axis 0 gt gt gt data array MHT 2 3 41 1 2 3 cul ld 2p Sy AND gt gt gt offsets np array 0 l 2 gt gt gt mtx sparse dia_matrix data offsets shape 4 4 gt gt gt mtx lt 4x4 Sparse matrix of type lt numpy int64 gt with 9 stored elements 3 diagonals in DIAgonal format gt gt gt gt mtx todense mac cix i O Ss Ol te 27 Oy 41 ROS 27 or O lOr 0r 37 41l gt gt gt data np arange 12 reshape 3 4 1 gt gt gt data arriay 1 2r 3 4 Sie e Ge cl 9 10 di 12 gt gt gt mtx sparse dia_matrix data offsets shape 4 4 gt gt gt mtx data arana b i 2 3 4 Eor Ses pro ler 9 10 ti 121 gt gt gt mtx offsets array I 0 17 2 dtype int 32 gt gt gt print mtx OF O il lips ele 2 CTE 3 3 3 4 1 Q 5 27 2 6 Sy 29 7 O2 Li ila S 12 gt gt gt mtx todense maw cass lp OW Lil Ol S 2r Oy Daly HOR Gr 37 0l OO Tye A e explanation with a scheme offset row Ae ls 1 Oso 1 SW o 2 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e matrix vector multiplication gt gt gt vec np ones 4 gt gt gt v
134. 13 2 beta euroscipy 2013 In 1 In Sie Gute Sa In 4 In 5 Cures eal a In 6 1a Out i 138 in 7 2 al In 8 a Cure Meo sae 27 3 In 9 id a out Tol 138611676 Same as in Out 6 yours will differ a b c Creates another object 1 2 3 Modifies object in place e the key concept here is mutable vs immutable mutable objects can be changed in place immutable objects cannot be modified once created See also A very good and detailed explanation of the above issues can be found in David M Beazley s article Types and Objects in Python 1 2 3 Control Flow Controls the order in which the code is executed if elif else l gt gt gt If 22 A print Obvious Obvious Blocks are delimited by indentation Tip Type the following lines in your Python interpreter and be careful to respect the indentation depth The Ipython shell automatically increases the indentation depth after a column sign to decrease the indentation depth go four spaces to the left with the Backspace key Press the Enter key twice to leave the logical block gt gt gt a 10 gt gt gt if a print 1 elif a 3 print 2 else sate print A lot A Not Indentation is compulsory in scripts as well As an exercise re type the previous lines with the same indentation in a script condition py and execute the script with run condition py in Ipython for range
135. 13 2 beta euroscipy 2013 Note Alternative toos system A noteworthy alternative to os system is the sh module Which provides much more convenient ways to obtain the output error stream and exit code of the external command Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 In 20 import sh In 20 com sh 1s In 21 print com basic_types rst exceptions rst OOP Enasit skaondardAlnibrary aS te controli iow cst Viuastmsteos est Ey thoni language mst demo2 py INGE AROMNG cele Site Dive non logon png demo py TORSE reusing_code rst In 22 print com exit_code 0 In 23 type com Out 23 sh RunningCommand Library Frameworks Python framework Versions 2 5 lib python2 5 In 16 os getenv PYTHONPATH tite MiG USeas ebur sre Utile Users churns renio lS Users cburns local lib python2 5 site packages usr local lib python2 5 site packages Library Frameworks Python framework Versions 2 5 lib python2 5 Walking a directory os path walk generates a list of filenames in a directory tree shutil high level file operations The shutil provides useful file operations e shutil rmtree Recursively delete a directory tree e shutil move Recursively move a file or directory to another location e shutil copy Copy files or directories glob Pattern matching on files The glob module provides convenient file pattern matching Find all files endi
136. 13 2 beta euroscipy 2013 Numpy internals If you are interested in the Numpy internals there is a good discussion in Advanced Numpy page 155 1 3 5 Some exercises Array manipulations 1 Form the 2 D array without typing it in explicitly 1 6 11 2 7 12 3 8 13 4 2 14 l5 107 1S and generate a new array containing its 2nd and 4th rows 2 Divide each column of the array gt gt gt import numpy as np gt gt gt a np arange 25 reshape 5 5 elementwise with the array b np array 1 5 10 15 20 Hint np newaxis 3 Harder one Generate a 10 x 3 array of random numbers in range 0 1 For each row pick the number closest to 0 5 e Use abs and argsort to find the column j closest for each row e Use fancy indexing to extract the numbers Hint a i j the array i must contain the row numbers corresponding to stuff in j Picture manipulation Framing Lena Lets do some manipulations on numpy arrays by starting with the famous image of Lena http www cs cmu edu chuck lennapg scipy provides a 2D array of this image with the scipy lena function gt gt gt from scipy import misc gt gt gt lena misc lena Note In older versions of scipy you will find lena under scipy lena Here are a few images we will be able to obtain with our manipulations use different colormaps crop the image change some parts of the image Vel
137. 2 Note that you should be familiar with the content of the previous chapter before reading the current one as basic operations such as masking and labeling are a prerequisite Chapters contents e Introduction and concepts page 325 scikit image and the SciPy ecosystem page 326 What s to be found in scikit image page 326 e Input output data types and colorspaces page 327 Data types page 328 Colorspaces page 328 e Image preprocessing enhancement page 329 Local filters page 329 Non local filters page 330 Mathematical morphology page 330 e Image segmentation page 332 Binary segmentation foreground background page 332 Marker based methods page 333 e Measuring regions properties page 335 e Data visualization and interaction page 335 Feature extraction for computer vision page 337 3 3 1 Introduction and concepts Images are NumPy s arrays np ndarray image np ndarray pixels array values a 2 3 channels array dimensions image encoding dtype np uint8 np uintl16 np float filters functions numpy skimage scipy gt gt gt import numpy as np gt gt gt check np zeros 9 9 So check s 2 222 1 gt gt gt check 1li 2 2 2 1 gt gt gt import matplotlib pyplot as plt gt gt gt plt imshow check cmap gray interpolation nearest 3 3 Scikit image image processing 325 Python Scientifi
138. 2 to find x 1 2 gt gt gt x flat byte_offset 6 gt gt gt x 1 2 6 e simple flexible C and Fortran order gt gt gt x array ly 2 al TE l 5 Clie y pep sine order Ci gt gt gt x strides Sp 2 gt gt gt str x data A OTN KOO x02 x00 x03 x00 x04 V0 1x05 x00 x06 x00 e Need to jump 6 bytes to find the next row e Need to jump 2 bytes to find the next column 2 2 Advanced Numpy 163 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt y np array x order F nee yo BC rLCES 2 4 22 gt str y data x01 x00 x04 x00 x02 x00 x05 x00 x03 x00 x06 x00 e Need to jump 2 bytes to find the next row e Need to jump 4 bytes to find the next column e Similarly to higher dimensions C last dimensions vary fastest smaller strides F first dimensions vary fastest shape d1 do dn strides s1 52 Sn sF dj i dj 2 dn x itemsize s d d dj 1 x itemsize Note Now we can understand the behavior of view gt gt gt y np array 1 3 2 4 dtype np uint8 transpose gt gt gt x y copy Transposition does not affect the memory layout of the data only strides gt gt gt x strides 2 1 nee yo ie Te Kel ets CEZ gt gt gt str x data AKOA KOAA O A OA gt gt gt str y data AOT x02 02 x04 e the results are different when interpreted as 2 of int16 e copy c
139. 2 2 Universal functions What they are e Ufunc performs and elementwise operation on all elements of an array Examples prada MPEP subtract Seip Specialia This function must compute the ufunc for many values at once in the way shown below char input_l char args 0 char input_2 char args 1 char output charx args 2 int i for Gi 10 ae lt damens rons Oli 41 xoutput elementwise_function input_l input_2 input_1l steps 0 input_2 steps 1 output steps 2 2 The Numpy part built by e Automatically support broadcasting casting e The author of an ufunc only has to supply the elementwise operation Numpy takes care of the rest e The elementwise operation needs to be implemented in C or e g Cython Parts of an Ufunc 1 Provided by user void ufunc_loop void args int xdimensions int steps void data int8 output elementwise_function int8 Input l int amp 8 input_2 2 2 Advanced Numpy 169 char types 3 types 0 NPY_BYTE je Cype Of First input arg 7 types 1 NPY_BYTE type of Second input arg types 2 NPY_BYTE 7 EIE Of Ehared Inpul arg 47 PyObject xpython_ufunc PyUFunc_FromFuncAndData urune loogo NULE types 1 ntypes 2 ff Man impulse 7 iy Ye MUM OC CUES a identity_element name docst ring unused e A ufunc can also support multiple different input output ty
140. 2 beta euroscipy 2013 E array 0 O 84147098 09092974S 0 14112001 O 7568025 axis 1 gt gt gt np log a 1 1 2 2 2 4 array n O OO 69Sl47ise 12096861229 12558629436 gt gt gt np exp a array Cl 1 2o leZel TsO 90S6l so 20 06555692 54 59815003 Shape mismatches gt gt gt a np arange 4 Sta Gl a Mo array iL 2I Traceback most recent call last alle Wesiechins line 1 aim lt mochile gt axis O ValueError operands could not be broadcast together with shapes 4 2 Traceback most recent call last alle W lt sechiias kine i ain lt moclhiile gt ValueError operands could not be broadcast together with shapes 4 2 3 3 Broadcasting We ll return to that later page 62 Sum by rows and by columns Transposition aa gt gt gt ada MO ceniu np ones Sr 38 I see help np triu Fe gt gt gt a array 1 1 Spear Iil Wa lees l a 21 P05 One iS e gt gt gt x sum axis 0 columns first dimension i ee Okey 205 14 array 3 3 gt gt gt ele I 2 gt gt XI Ole sine le sume array Ii Oe Oh Ol 3 3 fC hee AO LOA gt gt gt x sum axis 1 rows second dimension Dein cleat 20 alr array 2 41 ree A O a e ern O ll e ee 278 Warning The transposition is a view As a results the following code is wrong and will not make a matrix symmetric Tip Same idea in higher dimensions gt gt
141. 20062207 2002220 cmap ple sen gray lt matplotlib image AxesImage object at Ox gt gt gt gt plt imshow 1 200 220 200 220 cmap plt cm gray interpolation nearest lt matplotlib image AxesImage object at Ox gt 2 6 Image manipulation and processing using Numpy and Scipy 223 2 6 Image manipulation and processing using Numpy and Scipy 224 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt Slicing gt gt lena LOLS A0 AS amna CMSS 156 aS PES ieS SS Sr LSV LSS gt gt gt lena 100 120 25 gt gt gt eo da y Lena shape cee A A Mo Oea o O T 03 ily See mask X def 2 ee Dae a iy oy ee 2 a ey a A gt gt gt Masks gt gt gt lena mask 0 gt gt gt Fancy indexing gt gt gt lena range 400 range 400 255 3 D visualization Mayavi See 3D plotting with Mayavi page 357 e Image plane widgets e Isosurfaces Statistical information gt gt gt lena misc lena 2 6 3 Basic manipulations gt gt gt lena mean ZAR OAC SSAA AGG gt gt gt lena max lena min Images are arrays use the whole numpy machinery ae np histogram gt gt gt lena misc lena gt gt gt lena 0 40 166 2 6 Image manipulation and processing using Numpy and Scipy 225 2 6 Image manipulation and pr
142. 3 in rational arithmetic Symbols In contrast to other Computer Algebra Systems in SymPy you have to declare symbolic variables explicitly gt gt gt from sympy import x gt gt gt x Symbol x aes y symbol y Then you can manipulate them gt gt gt x yt tx y 2X 2 a o yea 2 Cx oy Symbols can now be manipulated using some of python operators arithmetic amp gt gt lt lt boolean 3 2 Sympy Symbolic Mathematics in Python 319 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Printing Here we use the following setting for printing gt gt gt sympy init_printing use_unicode False wrap_line True 3 2 2 Algebraic manipulations SymPy is capable of performing powerful algebraic manipulations We ll take a look into some of the most frequently used expand and simplify Expand Use this to expand an algebraic expression It will try to denest powers and multiplications gt gt gt expand x y x 3 3 2 2 ap Ske wy r Sy r Y e DEL ie sp Seis 2 ae nD r eeu 3 3 2 2 3 ar Sito iy ap Saxy Fy Further options can be given in form on keywords gt gt gt expand x y complex True 1S 56 ap see Wy Pesala se P ea yy gt gt gt I xim x I im y re x re y ia 5 lt cP eS 97 sr Meal se P I eatin yy gt gt gt expand cos x y trig True ae Son GC Siny r COSG
143. 5 p man man Cmkonkai Visnllecliise more more Um 5 9 Vinghy aL 5 remar ra 5 VY saiaaelatze Lastly we would like to mention the tab completion feature whose description we cite directly from the Python manual Tab completion especially for attributes is a convenient way to explore the structure of any object you re dealing with Simply type object_name lt TAB gt to view the object s attributes Besides Python objects and keywords tab completion also works on file and directory names In 1 x 10 In 2 x lt TAB gt x bit_length x conjugate x denominator x imag x numerator x real In 3 x real x real bit_length x real denominator x real numerator x real conjugate x real imag x real real In 4 x real 1 1 Scientific computing with tools and workflow 8 pillage PyCHeONLY AST 1 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 1 2 The Python language Authors Chris Burns Christophe Combelles Emmanuelle Gouillart Ga l Varoquaux Python for scientific computing We introduce here the Python language Only the bare minimum necessary for getting started with Numpy and Scipy is addressed here To learn more about the language consider going through the excellent tutorial http docs python org tutorial Dedicated books are also available such as http diveintopython net python Tip Python is
144. 690 lt Address 0x86c0690 out of bounds gt instrides 32 N 3 Debugger commands and interaction elsize 4 at numpy Ccore sre muliiarray ciors Cc 365 Lists the code at the current position 365 _FAST_MOVE Int32 Walk up the call stack gdb Walk down the call stack Execute the next line does not go down in new functions We get a segfault and gdb captures it for post mortem debugging in the C level stack not the Python call stack Execute the next statement goes down in new functions We can debug the C call stack using gdb s commands Print the call stack Gages a Print the local variables 00222222 O 1 0x004af4f5 in _copy_from_same_shape dest lt value optimized out gt command Execute the given Python command by opposition to pdb commands Src lt value Optimized cur myrunc 0x496 80 lt Ustrided byte COo swap 0 Warning Debugger commands are not Python code at numpy core src multiarray ctors c 748 You cannot name the variables the way you want For instance if in you cannot override the variables in the 748 myfunc dit gt dataptr dest gt strides maxaxis current frame with the same name use different names than your local variable when typing code in the debugger As you can see right now we are in the C code of numpy We would like to know what is the Python code that triggers this segfault so we go up the stack until we hit the Python execution loop gdb up Getting help when
145. AOs ccp 0 062 17265381S oa oO 10 15 PANA PRA This is equivalent to a 1 sample test on the difference gt gt gt stats ttest_lsamp data FSIQ data PIQ 0 sco MSAZOUIG4AOs cop 0 OSZILIZGSS 6 su T tests assume Gaussian errors We can use a Wilcoxon signed rank test that relaxes this assumption gt gt gt stats wilcoxon data FSIQO data PIQ 274 5 Oo lO65949277 565 Note The corresponding test in the non paired case is the Mann Whitney U test scipy stats mannwhitneyu 3 1 Statistics in Python 278 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercice e Test the difference between weights in males and females e Use non parametric statistics to test the difference between VIQ in males and females Conclusion we find that the data does not support the hypothesis that males and females have different VIQ gt gt gt from statsmodels formula api import ols gt gt gt model ols y x data fit Then we specify an OLS model and fit it We can inspect the various statistics derived from the fit gt gt gt print model summary OLS Regression Results 3 1 3 Linear models multiple factors and analysis of variance Dep Variable y R squared 0 804 Model OS Adj R squared 0 794 i 3 aoun n Method L
146. Al S E O Ve lenr e OL 8o Seea e Ol Vore Ero BFGS needs more function calls and gives a less precise result Note leastsq is interesting compared to BFGS only if the dimensionality of the output vector is large and larger than the number of parameters to optimize Warning If the function is linear this is a linear algebra problem and should be solved with scipy linalg lstsq 2 7 Mathematical optimization finding minima of functions 250 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Curve fitting 1 0 T T T 0 0 0 5 1 0 15 2 0 2 5 3 0 Least square problems occur often when fitting a non linear to data While it is possible to construct our optimization problem ourselves scipy provides a helper function for this purpose scipy optimize curve_fit Comment to make doctest pass gt gt gt np random seed 0 gt gt gt def E t omega PAi return np cos omega x t phi gt gt gt x np linspace 0 3 50 gt gt gt y f 1 5 1 2l enpscandom normal size 50 gt gt gt optimize curve_fit f Kp W array 1251854577 O292665541 0 CO0O56796 O0 00I123978 I array 0 00037994 0 00056796 Exercise Do the same with omega 3 What is the difficulty 2 7 5 Optimization with constraints Box bounds Box bounds correspond to limiting each of the individual parameters of the optimiz
147. Dep Variable wage R squared ORES Model Ole Adj R squared ORR Method Least Squares ES Stariistilek AS NA Date Mom cre Sep 205 Prob F statistic 2 94e 25 3 1 Statistics in Python 298 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Time 19s CS 36 Log Likelihood Se 903 joined model for male and female not separate models for male and No Observations 534 AIC CORO female The reason is that a single model enables statistical testing Df Residuals 520 BICE 151 9 result sm ols formula wage education gender data data fit Df Model 3 print result summary Cie wee Int The plots above highlight that there is not only a different offset in SS SS SSS SSS SS SSS SSS SS SSS SS SSS SSS SS L LLL wage but also a different slope Intercept Ole oie 0 441 gender T male 5 0 0193 0 457 We need to model this using an interaction education 3 0 051 0 052 result sm ols formula wage education gender education gender education gender T male A r x 027 0 000 data data fit print result summary Omnibus Durbin Watson Prob Omnibus Jarque Bera JB Skew Prob wb Looking at the p value of the interaction of gender and education the Kurtosis data does not support the hypothesis that education benefits males more than female p value
148. Don t reinvent the wheel e Easy to learn computer science is neither our job nor our education We want to be able to draw a curve smooth a signal do a Fourier transform in a few minutes e Easy communication with collaborators students customers to make the code live within a lab or a com pany the code should be as readable as a book Thus the language should contain as few syntax symbols or unneeded routines as possible that would divert the reader from the mathematical or scientific understanding of the code Efficient code that executes quickly but needless to say that a very fast code becomes useless if we spend too much time writing it So we need both a quick development time and a quick execution time e A single environment language for everything if possible to avoid learning a new software for each new problem Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Existing solutions Which solutions do scientists use to work Compiled languages C C Fortran etc e Advantages Very fast Very optimized compilers For heavy computations it s difficult to outperform these lan guages Some very optimized scientific libraries have been written for these languages Example BLAS vector matrix operations e Drawbacks Painful usage no interactivity during development mandatory compilation steps verbose syntax amp etc manual memory management tricky i
149. EA In 4 d 3 ee KeyE 23 eyError in iz 2 printcsorted 15 3 2 Pes E de ee 28 Le ey o in 13 prantsorted sel 1 3 2 In 6 1 4 set lil 2 3l IndexError list index out of range in 14 print sorted 1S2 In 71 L Foobar Bae Attributenrror list object has no attribute foobar a Raising exceptions As you can see there are different types of exceptions for different errors e Capturing and reraising an exception 1 2 The Python language 39 1 2 The Python language 40 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 In 15 def filter name name T try hee name name encode ascii nE except UnicodeError e ES if name Ga l peers print OK Ga l ern else artnet raise e aen return name In 16 filter_name Ga l OK Ga l Out 16 Ga xe3 xab1 In 17 filter _name St fan UnicodeDecodeError ascii codec can t decode byte Oxc3 in position 2 ordinal e Exceptions to pass messages between parts of the code In 17 def achilles_arrow x suet if abs x 1 lt le 3 peaches raise StopIteration cutee x 1 1 x 2 cece return x In 18 x 0 In 19 while True Seer try eee x achilles_arrow x dies except StopIteration Sweet break In 20 x One 20 2 0 9990234375 Use exceptions to notify certain conditions are met e g StopIteration
150. EEER AREER ETE EEER ERE RER EERE EERE EH EEE RET EHF 10 Generate and show the data xX np linspace 5 5 20 To get reproducable values provide a seed value np random seed 1 0 y 5 3 x 4 x np random normal size x shape gt 3 Plot the data plt figure figsize 5 4 10 picrpelot Ok We O e teeee eee ee eee ee ete eee eee eee eete eee eee eee eee eeee eee eee eeeeeeeeeeeeeet ee tte et 15 Multilinear regression model calculating fit P values confidence 2 IMEC VEILS oce 20 Convert the data into a Pandas DataFrame to use the formulas framework in statsmodels 5 data pandas DataFrame x x y y 6 pie the model 3 1 Statistics in Python 295 3 1 Statistics in Python 296 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 modell ols Uy x data Cit O Print the summary print model summary Peform analysis of variance on fitted linear model anova_results anova_lm model print nANOVA results print anova_results Heeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee tease ae tee ete tt Plot the fFicced model Retrieve the parameter estimates offset coef model _results params pier ploc Cenex COC Ol mS Cis plt xlabel x plt ylabel y plt show Total running time of the example 0 17 seconds 0 minutes 0 17 seconds 20 1 5 1 0 Seat wane e Ema 05 male
151. For classes the semantics are identical the original class definition is used as an argument to call the decorator and whatever is returned is assigned under the original name Before the decorator syntax was implemented PEP 318 it was possible to achieve the same effect by assigning the function or class object to a temporary variable and then invoking the decorator explicitly and then assigning the return value to the name of the function This sounds like more typing and it is and also the name of the decorated function doubling as a temporary variable must be used at least three times which is prone to errors Nevertheless the example above is equivalent to def function pass funetion decorator function Decorators can be stacked the order of application is bottom to top or inside out The semantics are such that the originally defined function is used as an argument for the first decorator whatever is returned by the first decorator is used as an argument for the second decorator and whatever is returned by the last decorator is attached under the name of the original function The decorator syntax was chosen for its readability Since the decorator is specified before the header of the function it is obvious that its is not a part of the function body and its clear that it can only operate on the whole function Because the expression is prefixed with is stands out and is hard to miss in your face
152. Mailing lists page 100 e Quick references page 100 Line properties page 101 Line styles page 102 Markers page 102 Colormaps page 102 1 4 1 Introduction Tip Matplotlib is probably the single most used Python package for 2D graphics It provides both a very quick way to visualize data from Python and publication quality figures in many formats We are going to explore matplotlib in interactive mode covering most common cases 1 4 Matplotlib plotting 82 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 IPython and the pylab mode Plotting with default settings Tip Python is an enhanced interactive Python shell that has lots of interesting features including named in 10 puts and outputs access to shell commands improved debugging and many more It is central to the scientific computing workflow in Python for its use in combination with Matplotlib z We start Python with the command line argument py lab py lab since IPython version 0 12 for interactive g matplotlib sessions with Matlab Mathematica like functionality pylab o Tip pylab provides a procedural interface to the matplotlib object oriented plotting library It is modeled closely after Matlab Therefore the majority of plotting commands in pylab have Matlab analogs with similar arguments Important comma
153. NPY_TYPES NPY_DOUBLE NPY_CDOUBLE NPY_LONG cdef extern from numpy ufuncobject h vorc Import wewa ctypedef void PyUFuncGenericFunction char object PyUFunc_FromFuncAndData PyUFuncGenericFunctionx func char types int ntypes int min int identity char name char doc List of pre defined loop functions ViOlGPVUEUnCm be le ASmGlmGl Ghiaiexx angs vole PyUPUNC Cedi Chars alge MPE im 1g Cligiewss margo MEy im void PyUFunc_f_ vol PyUPUNne IG Gqienhare args MEy sin Wolke Pyne it It Ns DID ehars eiecis void PyUFunc FP Echar args Npy in vold PyUPUneG DeD eharss args Ney in void PyUFunc_G_G char args npy_in VOLlGd PyURUNCEEEertoAsmdducdi ehiaisx args chark args mnpy in void yUbP une dvd chiar args mpy ont Charangas mpy aim void PyUFunc_FF_F_As_DD_D charx args char x args npy_in Wore Pyne isin Ja Chan mangs AP Him charkes args npy in Vordi EURUN ME void PyUFunc go 9 WOLiel IP WAUIE Ula _IDID _D void PyUFunc_GG_G Required module initialization import_array WDO E UNE The actual ufunc declaration cdef PyUFuncGenericFunction loop_func 1 cdef char input_output_types 3 woy MeEp npy_intp dimensions npy intp s npy_intp dimensions npy iNT S npy_intp dimensions HOS Eos S intp dimensions npy_intp s and 1000 but you don t really need to read this
154. Ora gt gt gt al 2 ae oe array 2 12 22 32 42 52 Par on course 3 statements for each Hint Individual array elements can be accessed similarly to a list e g a 1 ora 1 2 Hint Examine the docstring for diag gt gt gt Bi2tt2 232 array 20 22 24 40 42 44 Exercise Tiling for array creation Skim through the documentation for np t ile and use this function to construct the array You can also combine assignment and slicing i4 3 4 3 4 3j gt gt gt a np arange 10 o 2 1 2 1 2 ll gt gt gt a 5 10 4 3 4 3 4 3 2 Ay 22 A 2 gt gt gt a arca I 0 lon 2r y A LO LO LO 10 20 gt gt gt b np arange 5 gt gt gt als bles gt gt gt a Copies and views array 107 17 2 o Ar A ees L o0 A slicing operation creates a view on the original array which is just a way of accessing array data Thus the original array is not copied in memory You can use np may_share_memory to check if two arrays share Exercise Indexing and slicing the same memory block Note however that this uses heuristics and may give you false positives e Try the different flavours of slicing using start end and step starting from a linspace try to When modifying the view the original array is modified as well obtain odd numbers counting backwards and even numbers counting forwards i
155. Please enter a number a That was no valid number Try again Please enter a number 1 Exercise Write a program to search your PYTHONPATH for the module site py In 9 x Que Pos T path_site try finally 1 2 8 Exception handling in Python In 10 try It is likely that you have raised Exceptions if you have typed all the previous commands of the tutorial For e i x int eem ome Please enter e number 1 example you may have raised an exception if you entered a command with a typo 1 finally Caa print Thank you for your input Exceptions are raised by different kinds of errors arising when executing Python code In your own code you Les may also catch errors or define custom error types You may want to look at the descriptions of the the built in TE Exceptions when looking for the right exception type Please enter a mumeer a thank you for your anpuitc Exceptions Valuchrrors invalid literal for int O with base 10 a Exceptions are raised by errors in Python Important for resource management e g closing a file itn ai 170 Easier to ask for forgiveness than for permission ZeroDivisionError integer division or modulo by zero In 11 def print_sorted collection sere try REGE collection cork RES except AttributeError En 3 lt il 232 oE pass l na l AATE print collection In 2 Ll p re mpenn onr Unsupported opecand tyoc S tee amne and S
156. Project A Plot Editor 4 27 a o E z 2 be a 10 5 0 2 4 6 a 10 month A See also References e ETS repositories http github com enthought e Traits manual http docs enthought com traits traits_user_manual index html e Traits UI manual http docs enthought com traitsui traitsui_user_manual index html e Mailing list enthought dev enthought com 3 4 Traits building interactive dialogs 356 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 3 5 3D plotting with Mayavi Author Ga l Varoquaux Tip Mayavi is an interactive 3D plotting package matplotlib page 81 can also do simple 3D plotting but Mayavi relies on a more powerful engine VTK and is more suited to displaying large or complex data Chapters contents e Mlab the scripting interface page 357 3D plotting functions page 358 Points page 358 Lines page 358 Elevation surface page 359 Arbitrary regular mesh page 359 Volumetric data page 360 Figures and decorations page 360 x Figure management page 360 x Changing plot properties page 360 x Decorations page 362 Interactive work page 363 The pipeline dialog page 363 The script recording button page 364 Slicing and dicing data sources modules and filters page 364 An example inspecting magnetic fields page 364 Different views on data sources and modules page 364 Different sources scatters and fields pa
157. Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 EuroScipy tutorial team Editors Valentin Haenel Emmanuelle Gouillart Ga l Varoquaux http scipy lectures github com September 26 2015 2013 2 beta 328 g3cd80a5 One document to learn numerics science and data with Python Getting started with Python for science 1 1 Scientific computing with tools and workflow lt 2 64 44 4h eeee8 ee ea es 1 2 We Python language s os sos sos eek re ok ee Bek Hoh Pek Pe a He HS 1 3 NumPy creating and manipulating numerical data 2444544264 454 565 l4 Matplotlib plotting ss oe ke eee oe ee ee aee 1 5 Scipy high level scientific computing 2 22822 2 28882 oe e2o oe 44 1 6 Getting help and finding documentation 54564 444 eee eee eS EY HS Advanced topics 24 ACvanced Python Constructs s sos et eae eee eke Eee eer eee He 2 2 Advanced Numpy 9646 56 56 pow Bow oh Ow Hoa es ow a a wee 2 Debugging code parei he he feeb dade ded aaa dy dio ckuk amp Gr dod Gos 4 24 OMIA Code ses sos ie bg gr oe NA Oe os Bo ee aT t 23 sparse Matices I SOIPY e sos oie oe ee Be eR BREESE wee eee HS 2 6 Image manipulation and processing using Numpy and Scipy 2 7 Mathematical optimization finding minima of functions 2 8 Interfacing With C s pes ior hh Be Re aae kak Sak HD deh Hoh hed amp ha HG SSS Packages and applications Del Statistics GM Python gt c aioe che Becks ee Ry de a Ge rahe ke
158. Relating Gender and IQ Going back to the brain size IQ data test if the VIQ of male and female are different after removing the effect of brain size height and weight Notice that here Gender is a categorical value As it is a non float data type statsmodels is able to automatically infer this E g I O 8 S So o o o o oo o a hs W 2 Q N 4 ia 7am 8 468 8 amp 8 8 8 VIQ 2628 8 8 amp ae qe a Script output 3 1 Statistics in Python 315 3 1 Statistics in Python 316 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 OLS Regression Results c data Gender Female marker o SSS SS SS SS SS SSS SS SS SS SSS SS SS SS SS SS SSS SS SS SS SS SS SS SSS SS SS SS SSS SS SS SS SS SS SSS SS SSS alpha 1 cmap winter Dep Variable VIQ R squared 0 246 Model OLS Adj R squared omike LUG plte gcE Method Least Squares H SSE SNE IL SNC ALS 35 808 fig suptitle blue male green female size 13 Date Mon 21 Sep 2015 Prob iSite Sie sLe 2 004d Time Oeo a5 Log Likelihood I 4 plt show No Observations 39 ALC 852 7 a Df Residuals 35 BIC 359 3 Total running time of the example 0 62 seconds 0 minutes 0 62 seconds Df Model 3 O ASE o RS ao SNL N o a 3 2 Sympy Symbolic Mathematics in Python Intercept 166160258 Semena OWES 0 069 13 696 346 948 G
159. Scientific lecture notes Release 2013 2 beta euroscipy 2013 K means clustering The simplest clustering algorithm is k means This divides a set into k clusters assigning each observation to a cluster so as to minimize the distance of that observation in n dimensional space to the cluster s mean the means are then recomputed This operation is run iteratively until the clusters converge for a maximum for max_iter rounds An alternative implementation of k means is available in SciPy s cluster package The scikit learn implementation differs from that by offering an object API and several additional features including smart initial ization gt gt gt from sklearn import cluster datasets gt gt gt iris datasets load_iris gt gt gt k_means cluster KMeans n_clusters 3 gt gt gt k_means fit iris data KMeans z gt gt gt print k_means labels_ 10 gt gt gt from sklearn import svm i 1 oOo moo oe eT gt gt gt svc svm SVC kernel linear So Ste enacse te 0 gt SVC mle Oris data iris target 0000011111222 22 SWE 5 oo There are several support vector machine implementations in scikit learn The most commonly used ones are svm SVC svm NuSVC and svm LinearSVC SVC stands for Support Vector Classifier there also exist A y s SVMs for regression which are called SVR in scikit learn E By 2 amp 2 Versicolor Sy 5 g 3
160. __array_finalize_ 7 0 000 0 000 0 004 0 001 ica py 58 _sym_decorrelation 7 0 000 0 000 Oe 002 0 000 linalg py 841 eigh sls 0 000 0 000 0 000 0 000 isinstance 1 0 000 0 000 14 551 14 551 demo py 1 lt module gt 29 0 000 0 000 0 000 0 000 numeric py 180 asarray 35 0 000 0 000 0 000 0 000 defmatrix py 193 __new__ 35 0 000 0 000 oomoo 0 000 detmatrix 97 453 asmat ci al 0 000 0 0100 G2 001 2000 detmatrisipy 267 mui 41 0 000 0 000 CRGIOO 0 000 numpy core multiarray zeros 28 0 000 CROC CRC 0 000 method transpose of numpy ndarray objects 1 0 000 0 000 0 008 O200S Hea Pye ON CaS E wea Clearly the svd in decomp py is what takes most of our time a k a the bottleneck We have to find a way to make this step go faster or to avoid this step algorithmic optimization Spending time on the rest of the code is useless Profiling outside of IPython running cProfile Similar profiling can be done outside of Python simply calling the built in Python profilers cProfile and profile python m cProfile o demo prof demo py Using the o switch will output the profiler results to the file demo prof to view with an external tool This can be useful if you wish to process the profiler output with a visualization tool 2 4 Optimizing code 199 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Line profiler The profiler tells us which function takes most of the time but no
161. _module so co _module_wrap We can now load and execute the cos_module as we have done in the previous examples In 1 import cos_module ra 2 cos_module Type module String Form lt module cos_module from cos_module py gt File home esc git working scipy lecture notes advanced interfacing_with_c swig cos_module DoOcst aing lt me docstring gt In 3 dir cos_module Outi l Lepu lltins ae clove Y Stile name _ package__ eos module _ newclass _object w Swie GeCatti _ swig_property _Swig_repr UESWiGaset ait a swig_setattr_nondynamic GOR Funet f In 4 cos module cos runc 170 Out 4s 0 54030230596813 98 In Si cos module cos LUne OFO Outils 1 0 Ia 6 cos Moale cos rune Se 4S 9265359 Outil 1 0 Again we test for robustness and we see that we get a better error message although strictly speaking in Python there is no double type Iin 7 cos module cos Fune Troon TypeError Traceback most recent call last lt ipython input 7 llbee483665d gt in lt module gt gt i os_imocule cos_Eline 7 ROOY TypeError in method cos_func argument 1 of type double Numpy Support Numpy provides support for SWIG with the numpy i file This interface file defines various so called typemaps which support conversion between Numpy arrays and C Arrays In the following example we will take a quick
162. _subpix corner_peaks from skimage transform import warp AffineTransform tform AffineTransform scale 1 3 1 1 rotation 1 shear 0 7 translation 210 50 image warp data checkerboard tform inverse output_shape 350 350 coords corner_peaks corner_harris image min_distance 5 coords_subpix corner_subpix image coords window_size 13 this ex ample is taken from http scikit image org docs stable auto_examples plot_corner html Points of interest such as corners can then be used to match objects in different images as described in http scikit image org docs stable auto_examples plot_matching html 3 4 Traits building interactive dialogs Author Didrik Pinte The Traits project allows you to simply add validation initialization delegation notification and a graphical user interface to Python object attributes Tip In this tutorial we will explore the Traits toolset and learn how to dramatically reduce the amount of boilerplate code you write do rapid GUI application development and understand the ideas which underly other parts of the Enthought Tool Suite Traits and the Enthought Tool Suite are open source projects licensed under a BSD style license 3 4 Traits building interactive dialogs 338 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Intended Audience Intermediate to advanced Python programmers Requirements e Python 2 6 or 2 7 www p
163. a and in a slightly different approach Statistical approach The annual maxima are supposed to fit a normal probability density function However such function is not going to be estimated because it gives a probability from a wind speed maxima Finding the maximum wind speed occurring every 50 years requires the opposite approach the result needs to be found from a defined probability That is the quantile function role and the exercise goal will be to find it In the current model it is supposed that the maximum wind speed occurring every 50 years is defined as the upper 2 quantile By definition the quantile function is the inverse of the cumulative distribution function The latter describes the probability distribution of an annual maxima In the exercise the cumulative probability p_i for a given year i is defined as p_i i N 1 with N 21 the number of measured years Thus it will be possible to calculate the cumulative probability of every measured wind speed maxima From those experimental points the scipy interpolate module will be very useful for fitting the quantile function Finally the 50 years maxima is going to be evaluated from the cumulative probability of the 2 quantile 1 5 Scipy high level scientific computing 125 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Computing the cumulative probabilities The annual wind speeds maxima have already been computed and saved in the numpy forma
164. a false value and therefore the exception is rethrown after__e x it__ is finished The ability to catch exceptions opens interesting possibilities A classic example comes from unit tests we want to make sure that some code throws the right kind of exception 2 1 Advanced Python Constructs 153 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 class assert_raises object based on pytest and unittest TestCase def init__ self type self type type def enter self pass def __exit_ self type value traceback if type is None raise AssertionError exception expected if issubclass type self type return True swallow the expected exception raise AssertionError wrong exception type with assert_raises KeyError een Using generators to define context managers When discussing generators page 141 it was said that we prefer generators to iterators implemented as classes because they are shorter sweeter and the state is stored as local not instance variables On the other hand as described in Bidirectional communication page 142 the flow of data between the generator and its caller can be bidirectional This includes exceptions which can be thrown into the generator We would like to implement context managers as special generator functions In fact the generator protocol was designed to support this use case contextlib contextmanager def some_generator lt arguments gt
165. a float Default 2 0 Example mask boolean mask array to suppress some data points mask_points If supplied only one out of mask_points data point is displayed In 1 import numpy as np This option is usefull to reduce the number of points displayed on large datasets I 2 theta pea 0210 npr pin npr pei 0p Must be an integer or None n 2 r theta np mgrid np pi np pi 10j mode the mode of the glyphs Must be 2darrow or 2dcircle or 2dcross or f In 3 x np wcos theta 2ddash or 2ddiamond or 2dhooked_arrow or 2dsquare or 2dthick_arrow or 2dthick_cross or 2dtriangle or 2dvertex or arrow or cone or cube or In 4 y r np sin theta cylinder or point or sphere Default sphere name the name of the vtk object created in 5 2S psi i representation the representation type used for the surface Must be surface or wireframe or points or mesh or fancymesh Default surface In 6 from mayavi import mlab resolution The resolution of the glyph created For spheres for instance this is the number of divisions along theta and phi Must be an integer Default 8 el FAN os aie tele umes ky yy eC Meme ci eee i ees a Ea pe te Ue Out 7 lt mayavi modules surface Surface object at Oxde6f08c gt scalars optional scalar data scale_factor scale factor of the glyphs used to repr
166. according to the PEP When more than one decorator is applied each one is placed on a separate line in an easy to read way Replacing or tweaking the original object Decorators can either return the same function or class object or they can return a completely different object In the first case the decorator can exploit the fact that function and class objects are mutable and add attributes e g add a docstring to a class A decorator might do something useful even without modifying the object for example register the decorated class in a global registry In the second case virtually anything is possible when something different is substituted for the original function or class the new object can be completely different Nevertheless such behaviour is not the purpose of decorators they are intended to tweak the decorated object not do something unpredictable Therefore when a function is decorated by replacing it with a different function the new function usually calls the original function after doing some preparatory work Likewise when a class is decorated by replacing if with a new class the new class is usually derived from the original class When the purpose of the decorator is to do something every time like to log every call to a decorated function only the second type of decorators can be used On the other hand if the first type is sufficient it is better to use it because it is simpler Decorators i
167. age 42 The remainder of this chapter is not necessary to follow the rest of the intro part But be sure to come back and finish this chapter later 1 2 6 Input and Output To be exhaustive here are some information about input and output in Python Since we will use the Numpy methods to read and write files you may skip this chapter at first reading We write or read strings to from files other types must be converted to strings To write in a file gt gt gt f open workfile w opens the workfile file gt gt gt type f lt type file gt 55 gt t weite This 1s a test Nnand another test gt gt gt close To read from a file In 1 open workfile 1T In 2 s f read In 3 print s This is a test and another test In 4 f close See also For more details http docs python org tutorial inputoutput html Iterating over a file In 6 f open workfile r In 7 for line In f print line This is a test and another test In 8 f close File modes e Read only r e Write only w Note Create a new file or overwrite existing file e Append a file a e Read and Write r 1 2 The Python language 34 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e Binary mode b Note Use for binary files especially on Windows 1 2 7 Standard Library Note Reference document for this secti
168. algorithm based on ball trees to represent the samples it is trained on KNN k nearest neighbors classification example gt gt gt Create and fit a nearest neighbor classifier gt gt gt from sklearn import neighbors gt gt gt knn neighbors KNeighborsClassifier 22 gt k n aeei data IrIS Target KNerghbors lassifiier gt gt gt knn predice Os O52 W 3 OCA array LOI Training set and testing set When experimenting with learning algorithms it is important not to test the prediction of an estimator on the data used to fit the estimator Indeed with the KNN estimator we would always get perfect prediction on the training set gt gt gt perm np random permutation iris target size gt gt gt iris data iris data perm gt gt gt iris target iris target perm gt knn Citare data l 0 iris tanget 00 KNeighborsClassifier gt gt gt knn score iris data 100 iris target 100 0 SoS ee Bonus question why did we use a random permutation Support vector machines SVMs for classification Linear Support Vector Machines SVMs try to construct a hyperplane maximizing the margin between the two classes It selects a subset of the input called the support vectors which are the observations closest to the separating hyperplane 3 6 scikit learn machine learning in Python 372 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python
169. ary but only at build time at this problem can be overcome by shipping the generated C files Example The main Cython code for our cos_module is contained in the file cos_module pyx mnn Example of Wrapping COs FUNCEIOn trom mach nh wsing CyeNEn edef extern from math h double cos double arg def cos fune arg return cos arg Note the additional keywords such as cdef and extern Also the cos_func is then pure Python Again we can use the standard distutils module but this time we need some additional pieces from the Cython Distutils from distutils core import setup Extension from Cython Distutils import build_ext setup cmdclass build_ext build_ext ext_modules Extension cos_module cos_module pyx D k o o Compiling this cd advanced interfacing_with_c cython le cos_module pyx setup py python setup py build_ext inplace running build_ext cythoning cos_module pyx to cos_module c building cos_module extension creating build creating build temp linux x86_64 2 7 qee prhread iLne strict aliescing Gg OZ DNUDEBUG G itwrapy O3 Nani Wstrict prototypes P LC gece pthread shared buirld temoe linux x96 64 2 7 cos_module o a a gt lpython2 I 1s build cos_module c cos module pyx cos module sos setup py And running it In 1 import cos_module In 2 cos module Type module 2 8 Int
170. ation Note that some problems that are not originally written as box bounds can be rewritten as such via change of variables 0 e scipy optimize fminbound for 1D optimization 2 7 Mathematical optimization finding minima of functions 251 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e scipy optimize fmin_1l_bfgs_b a quasi Newton page 246 method with bound constraints gt gt gt def f x return np squr 010 2 ee E gt gt gt optimize fmin_l_bfgs_b f np array 0 0 approx_grad 1 bounds 1 5 m5 Gravi 25 Uss 7 LooelreestveAlegs se ty General constraints Equality and inequality constraints specified as functions f x 0 and g x lt 0 e scipy optimize fmin_slsqp Sequential least square programming equality and inequality con N straints gt gt gt def f x returninp sar e C 0 aE aa eTA gt gt gt def constraint x return np atleast_ld 1 5 np sum np abs x gt gt gt optimize fmin_slsqp f np array 0 0 ieqcons constraint Optimization terminated successfully Exit mode 0 Current function value 2 47487373504 Iterations 5 Function evaluations 20 Gradient evaluations 5 array t22 5004696 0224995204 e scipy optimize fmin_cobyla Constraints optimization by linear approximation inequality constraints only gt gt gt optimize fmin_cobyla
171. ations 24 Gradient evaluations 8 array 1 30644003 A possible issue with this approach is that if the function has local minima the algorithm may find these local minima instead of the global minimum depending on the initial point gt gt gt optimize fmin_bfgs f 3 disp 0 array I 3837466631 If we don t know the neighborhood of the global minimum to choose the initial point we need to resort to costlier global optimization To find the global minimum we use scipy optimize basinhopping which combines a local optimizer with stochastic sampling of starting points for the local optimizer New in version 0 12 0 basinhopping was added in version 0 12 0 of Scipy gt gt gt optimize basinhopping f 0 nfev 1725 minimization failures 0 fun 7 9458233 756152845 x array I I 30644001 j message requested number of basinhopping iterations completed success njev 575 makes LOQ 1 5 Scipy high level scientific computing 111 fully Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Another available but much less efficient global optimizer is scipy optimize brute brute force opti mization on a grid More efficient algorithms for different classes of global optimization problems exist but this is out of the scope of scipy Some useful packages for global optimization are OpenOpt IPOPT PyGMO and PyEvolve Note scipy used to contain the routine ann
172. axes 0 2 0 2 5 5 axes 0 1 0 1 8 8 Ticks Well formatted ticks are an important part of publishing ready figures Matplotlib provides a totally configurable system for ticks There are tick locators to specify where ticks should appear and tick formatters to give ticks the appearance you want Major and minor ticks can be located and formatted independently from each other Per default minor ticks are not shown i e there is only an empty list for them because it is as NullLocator see below Tick Locators Tick locators control the positions of the ticks They are set as follows pl gca ax xaxis set_major_locator eval locator NullLocator MultipleLocator 1 0 0 1 2 3 4 5 6 7 8 9 1C FixedLocator 0 2 8 9 101 0 2 8 9 1C IndexLocator 3 1 1 4 7 1C LinearLocator 5 0 2 5 5 0 7 5 10 LogLocator 2 1 0 0 5 1 0 2 0 4 0 8 0 AutoLocator There are several locators for different kind of requirements 2 i j 3 1c All of these locators derive from the base class matplotlib ticker Locator You can make your own locator deriving from it Handling dates as ticks can be especially tricky Therefore matplotlib provides special locators in matplotlib dates 1 4 4 Other Types of Plots examples and exercises ee Plot pI plot Scatter Plot pl scatter page 93 1 4 Matplotlib plotting 91 Python Scientific
173. ay int size_in double x out_array int size_out calls Ene Original funcion providing only che size of the first 7 cos_doubles in_array out_array size_in o e To use the Numpy typemaps we need include the numpy i file e Observe the call to import_array which we encountered already in the Numpy C API example e Since the type maps only support the signature ARRAY SIZE we need to wrap the cos_doubles as cos_doubles_func which takes two arrays including sizes as input e As opposed to the simple SWIG example we don t include the cos_doubles h header There is nothing there that we wish to expose to Python since we expose the functionality through cos_doubles_func And as before we can use distutils to wrap this from distutils core import setup Extension import numpy setup ext_modules Extension _cos_doubles sources cos doubles cos doubles 1i include_dirs numpy get_include As previously we need to use include_dirs to specify the location 2 8 Interfacing with C 264 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 cos_doubles c cos_doubles h cos_doubles i numpy i setup py test_cos_doubles py python setup py build ext i running build_ext building _cos_doubles extension Swigging cos _doubles i to cos doubles _wrap c swig python o cos_doubles_wrap c cos_doubles i cos_doubles i 24 Warning 490 Fragment NumPy_Backward_Compatibility
174. ay data 1 9 3 5 mask False False False True False fill_value 999999 The mask is also available directly gt gt gt mx mask j array False False False True False dtype bool1 The masked entries can be filled with a given value to get an usual array back gt gt gt x2 mx filled 1 gt gt gt x2 array i 1 9 3 1 5l The mask can also be cleared gt gt gt mx mask np ma nomask gt gt gt mx masked_array data 1 9 3 99 5 mask False False False False False fill_value 999999 Domain aware functions The masked array package also contains domain aware functions S222 np ma loc npr ansa il 2 25 3 SDD MmasikedRarway data OO MOM GoStay ASO S599 a a OO SIGH ZS SiG CoO SS ln False False True True False True 1le 20 mask fill value II Note Streamlined and more seamless support for dealing with missing data in arrays is making its way into Numpy 1 7 Stay tuned 2 2 Advanced Numpy 182 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Example Masked statistics Canadian rangers were distracted when counting hares and lynxes in 1903 1910 and 1917 1918 and got the numbers are wrong Carrot farmers stayed alert though Compute the mean populations over time ignoring the invalid numbers gt gt gt data np loadtxt data populations txt gt gt gt populations np
175. b as pl gt gt gt pl imshow digits images 0 cmap pl cm gray_r lt matplotlib image AxesImage object at gt To use this dataset with the scikit we transform each 8x8 image into a vector of length 64 gt gt gt data digits images reshape digits images shape 0 1 Learning and Predicting Now that we ve got some data we would like to learn from it and predict on new one In scikit learn we learn from existing data by creating an estimator and calling its fit X Y method gt gt gt from sklearn import svm gt gt gt clf svm LinearSVC gt gt gt Cli fib aris data iris target 7 tearm from the data LinearSVC Once we have learned from the data we can use our model to predict the most likely outcome on unseen data cee ulic predict i S20 S61 LoS O25 array 0 Note We can access the parameters of the model via its attributes ending with an underscore gt gt gt clf coef_ array Imon 3 6 2 Classification k Nearest neighbors classifier The simplest possible classifier is the nearest neighbor given a new observation take the label of the training samples closest to it in n dimensional space where n is the number of features in each sample 3 6 scikit learn machine learning in Python 371 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Sepal width Sepal length The k nearest neighbors classifier internally uses an
176. because they do not show the interesting values 7 77 2 for sine and cosine We ll change them such that they show only these values ol xtiekst nep ci ne 2 07 MPi np Di jou switskeks i aril 1 4 Matplotlib plotting 86 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Setting tick labels Hint Documentation e Working with text e xticks command e yticks command set_xticklabels set_yticklabels Tip Ticks are now properly placed but their label is not very explicit We could guess that 3 142 is m but it would be better to make it explicit When we set tick values we can also provide a corresponding label in the second argument list Note that we ll use latex to allow for nice rendering of the label Plexticks np pl np p172 U mMp pl72 me Pily bee EAN pie SENE Ze me OE A A E EN O lpr yticks I l 0 1 et eae ee Oats v S 1s Moving spines Hint Documentation e Spines e Axis container e Transformations tutorial Tip Spines are the lines connecting the axis tick marks and noting the boundaries of the data area They can be placed at arbitrary positions and until now they were on the border of the axis We ll change that since we want to have them in the middle Since there are four of them top bottom left right we ll discard the top and right by setting their color to none and we ll move the bottom
177. bicg BIConjugate Gradient bicgstab BIConjugate Gradient STABilized cg Conjugate Gradient symmetric positive definite matrices only cgs Conjugate Gradient Squared gmres Generalized Minimal RESidual minres MINimum RESidual qmr Quasi Minimal Residual Common Parameters mandatory A sparse matrix dense matrix LinearOperator The N by N matrix of the linear system b array matrix Right hand side of the linear system Has shape N or N 1 optional x0 array matrix Starting guess for the solution tol float Relative tolerance to achieve before terminating maxiter integer Maximum number of iterations Iteration will stop after maxiter steps even if the specified tolerance has not been achieved M sparse matrix dense matrix LinearOperator Preconditioner for A The preconditioner should ap proximate the inverse of A Effective preconditioning dramatically improves the rate of convergence which implies that fewer iterations are needed to reach a given error tolerance callback function User supplied function to call after each iteration It is called as callback xk where xk is the current solution vector LinearOperator Class from scipy sparse linalg interface import LinearOperator common interface for performing matrix vector products useful abstraction that enables using dense and sparse matrices within the solvers as well as matrix free solutions has sha
178. bject Oriented Programming The notation r method e g r append 3 and L pop is our first example of object oriented programming OOP Being a list the object r owns the method function that is called using the notation No further knowledge of OOP than understanding the notation is necessary for going through this tutorial 1 2 The Python language 14 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Discovering methods Reminder in Ipython tab completion press tab In 28 r lt TAB gt s ace elass Contains_ a delattr delitem__ delslice _ Taben ladd 6 i m l etater n setitem _ _setslice __ 5 Salve n Se ee __Subclasshook__ append Count extend index 3 AUNE A iter insert Pop removie __getattribute _reduce__ __getitem_ __ getslice__ eee gE hash _ reduce_ex 5 repr F reversed reverse SORE 1g 16 16 ig 1G 1G IE 5 aire 165 An 1g E E 16 H In Im lp Ini a im ay lms my e e ly lah tah Im Ip my da in Ip ip ey ley Io TAY TA py Ip I ley rmul Strings Different string syntaxes simple double or triple quotes s Hello how are you SIS Hive wie See to Eee tripling the quotes allows the how are you the string to span more than one line s Hi what ts upe wey Ta O THI whats up Bile lt ipython Console gt Gmel Wii
179. c var n method gt gt gt ditt sinx x 1 2 COS 2 x e CLfrt sin Qex r 2 4xsin 2 x 2 gt gt Qir Susan oe exe 9 o Cos Series expansion SymPy also knows how to compute the Taylor series of an expression at a point Use series expr var So gt Series cos x ix 2 4 x x i ON al ONE 2 24 gt gt gt series l cos x x 2 4 x 5xX VGN 1 4 BONE 2 24 3 2 Sympy Symbolic Mathematics in Python 321 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercises 1 Calculate lim _ 9 sin x 2 Calculate the derivative of log x for x Integration SymPy has support for indefinite and definite integration of transcendental elementary and special functions via integrate facility which uses powerful extended Risch Norman algorithm and some heuristics and pattern matching You can integrate elementary functions gt gt gt integrate 6 x 5 x 6 x gt gt gt integrate sin x x O S x gt gt gt integrate log x x xr log G x gt gt gt integrate 2xx sinh x x 2 AOSAN Also special functions are handled easily gt gt gt integrate exp xxx2 xerf x x 2 PL ert x 4 It is possible to compute definite integral gt gt gt integrate x 3 x 1 1 0 gt gt gt INtegqrate sin x x 0 pi 2Z 1 gt gt integrate cos x x 7 2 P72 2 Also improper integrals are
180. c lecture notes Release 2013 2 beta euroscipy 2013 scikit image and the SciPy ecosystem Recent versions of scikit image is packaged in most Scientific Python distributions such as Anaconda or Enthought Canopy It is also packaged for Ubuntu Debian gt gt gt import skimage gt gt gt from skimage import data most functions are in subpackages Most scikit image functions take NumPy ndarrays as arguments gt gt gt camera data camera gt gt gt camera dtype diype ruimte gt gt gt camera shape Co ASEA gt gt gt from skimage import restoration gt gt gt filtered_camera restoration denoise_bilateral camera gt gt gt type filtered_camera lt type numpy ndarray gt Other Python packages are available for image processing and work with NumPy arrays e scipy ndimage for nd arrays Basic filtering mathematical morphology regions properties e Mahotas Also powerful image processing libraries have Python bindings e OpenCV computer vision e ITK 3D images and registration e and many others but they are less Pythonic and NumPy friendly to a variable extent What s to be found in scikit image e Website http scikit image org 3 3 Scikit image image processing 326 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e Gallery of examples as in matplotlib or scikit learn http scikit image org docs stable auto_examples Different kinds of function
181. c using the trait Property The depends_on attribute ex presses the dependency between the property and other traits When the other traits gets changed the property is invalidated Again Traits uses magic method names for the property e _get_XXX for the getter of the XXX Property trait e _set_XXX for the setter of the XXX Property trait Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 from traits api import HasTraits Instance DelegatesTo Float Range from traits api import Property from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage For the simplicity of the example the release is considered in hm3 timestep and not in m3 s mony reservoir Instance Reservoir max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storage Property depends_on inflows release control attributes inflows Float desc Inflows hm3 release Range low min_release high max_release spillage Property desc Spillage hm3 depends_on storage inflows release Private traits eee Fete e ee eee eee eeeeeee eee eeeeteeeeeeeeae setae ttt tt _storage Float Traits property implementation t ee teeta teed d teats ttt ttt ttt tet def _get_storage self new_Storage self _storage self release se
182. ccording to the model gt gt gt import numpy as np o gt x np linspace 5 5 20 r a d a ea ree Categorical variables comparing groups or multiple categories gt gt gt normal distributed noise gt gt gt y 5 3 x 4 np random normal size x shape Let us go back the data on brain size p Create d Caca frame Concat mang All the veleveiie Varicdioles eee eee ee ee oe a E F gt gt gt data pandas DataFrame x x y y gt gt gt data pandas read_csv examples brain_size csv sep na_values We can write a comparison between IQ of male and female using a linear model 66 99 7 E __ formulas for statistics in Python ee ee ee ee See the statsmodels documentation gt gt gt print model summary 3 1 Statistics in Python 279 3 1 Statistics in Python 280 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 OLS Regression Results Dep Variable VIQ R squared G05 Model OLS Adj R squared ORO Method Least Squares IPS SIC SUC al Sic LS g 0 5969 Date Piolo E Statis CLE amp 0 445 Time Log Likelihood S82 542 No Observations 40 ATC SHGIS 5 s Df Residuals 38 BICE IZo 2 Df Model i coef std err E P gt IE 95 0 Come Int Intercept 109 4500 523086 2O G19 0000 98 704 120 16 Gender T Male 5 8000 ISA oS 0 445 9 397 20 997 Omnibus 2o IGG D rbin Wats
183. ccur if the closing step was performed before the opening Exercise For gray valued images eroding resp dilating amounts to replacing a pixel by the minimal resp maximal value among pixels covered by the structuring element centered on the pixel of interest gt gt gt a np zeros 7 7 dtype np int gt gt gt a l 6 1 6 3 mae ela al 2 eel ot w gt gt gt a array 10 0r O O o Ol Oy lt 8 2 Se Sip Se i Oi Soy le oy Sy JOG Wes Sr 37 3r Sr 3r Ula le Se ose Se 2 Se Olly he WSs se Se Se Ses Oly Or 0 Or Oe 07 0 oln gt gt gt ndimage grey_erosion a size 3 3 array CPO O O O GO O Ol hoy Uy OF Oy Oy OR 8 O 0r ee i ep o ONS lp MOF ae Ty Ly Oy Oy tn Os Sp 2 Be Uy Oly Ral MOG 07 0r On HO 0l O Oz 0r 0 0 Ope BON Measurements on images Let us first generate a nice synthetic binary image gt gt gt x y np indices 100 100 22 gt Sig mo cil 2p pli OO ap Sin 2eipepiay 502 ley 50 2a gt gt gt mask sig gt 1 Now we look for various information about the objects in the image gt gt gt labels nb ndimage label mask gt gt gt nb 8 gt gt gt areas ndimage sum mask labels range 1 labels max 1 gt gt gt areas emtay T 190 7 AS A24 2 lger 459a 190 549 5 A24 1 5 Scipy high level scientific computing 124 Python Scientific lecture notes Release 2013 2 beta euros
184. cela 5555 1D plotting gt gt gt x np linspace 0 3 20 gt gt gt y np linspace 0 9 20 gt gt gt plt plot x y line plot lt Mak plot lip lines Lane2D objecke ak 4 gt gt gt gt plt plot x y o dot plot lt matplotlib lines Line2D object at gt 1 3 NumPy creating and manipulating numerical data 47 1 3 NumPy creating and manipulating numerical data 48 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 See also More in the matplotlib chapter page 81 Exercise Simple visualizations e Plot some simple arrays a cosine as a function of time and a 2D matrix e Try using the gray colormap on the 2D matrix Indexing and slicing The items of an array can be accessed and assigned to the same way as other Python sequences e g lists gt gt gt a np arange 10 gt gt gt a Warning Indices begin at 0 like other Python sequences and C C In contrast in Fortran or Matlab indices begin at 1 The usual python idiom for reversing a sequence is supported gt gt gt a iss array 9r Sr T Or or A Spe 2 le Oy For multidimensional arrays indexes are tuples of integers 1 3 NumPy creating and manipulating numerical data 49 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt a np diag np arange 3 gt gt gt a eieceny L110 07 Ol 0 1
185. ception was thrown the information about the exception is passed to __e xit__ which is described below in the next subsection In the normal case exceptions can be ignored just like ina finally clause and will be rethrown after__ ex it ___is finished Let s say we want to make sure that a file is closed immediately after we are done writing to it 2 1 Advanced Python Constructs 152 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt class closing object def _ init self obj self obj obj def _enter_ self return self obj def _ exit_ self xargs a self obj close gt gt gt with closing open tmp file w as f f write the contents Nnn Here we have made sure that the f close is called when the with block is exited Since closing files is such a common operation the support for this is already present in the file class Ithasan__exit__ method which calls close and can be used as a context manager itself gt gt gt with open tmp file a as f Pewigiiaen Mmone Contents Nnt The common use for try finally is releasing resources Various different cases are implemented similarly in the __enter__ phase the resource is acquired in the __e xit___ phase it is released and the exception if thrown is propagated As with files there s often a natural operation to perform after the object has been used and it is most convenient to have the support bu
186. cipy 2013 gt gt gt maxima ndimage maximum sig labels range 1l labels max 1 gt gt gt maxima array becl23e238 1 L3S27605 52 SI Y5407 9 Zc ASG IS IS Go VLSVSEIG Le SO2ZS38233 io Vest 72u7 S SILG Sao 79 gt gt gt ndimage find_objects labels 4 slice 30L 48L None slice 30L 48L None gt gt gt sl ndimage find_objects labels 4 gt gt gt import pylab as pl gt gt gt pl imshow sig sl1 0 lt matplotlib image AxesImage object at gt sig mask labels See the summary exercise on Image processing application counting bubbles and unmolten grains page 133 for a more advanced example 1 5 11 Summary exercises on scientific computing The summary exercises use mainly Numpy Scipy and Matplotlib They provide some real life examples of scientific computing with Python Now that the basics of working with Numpy and Scipy have been introduced the interested user is invited to try these exercises Maximum wind speed prediction at the Sprog station The exercise goal is to predict the maximum wind speed occurring every 50 years even if no measure exists for such a period The available data are only measured over 21 years at the Sprog meteorological station located in Denmark First the statistical steps will be given and then illustrated with functions from the scipy interpolate module At the end the interested readers are invited to compute results from raw dat
187. cipy lecture notes advanced traits lt ipython input 7 979bdff9974a gt in lt modi gt 1 reservoir max_storage 230 Users dpinte projects ets traits traits trait_handlers pyc in error self object name value 1 66 wow O7 raise TraitError object name self full_info object name value gt L6 value 169 170 def arg_error self method arg_num object name value 3 4 Traits building interactive dialogs 342 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 TraitError The max_storage trait of a Reservoir instance must be a float but a B ue On 2 53 Documentation By essence all the traits do provide documentation about the model itself The declarative approach to the creation of classes makes it self descriptive from traits api import HasTraits Str Float class Reservoir HasTraits name Str max_storage Float 100 The desc metadata of the traits can be used to provide a more descriptive information about the trait from traits api import Haslraits Str Float class Reservoir HasTraits name Str max_storage Float 100 desc Maximal storage hm3 Let s now define the complete reservoir class from traits api import Haslraits Str Float Range class Reservoir HasTraits name Str max_storage Float le6 desc Maximal storage hm3 max_release Float 10 desc Maximal release m3 s head Float 10 desc Hydraul
188. crop_surface Property depends_on irrigated_areas surface def _get_total_crop_surface self return sum iarea surface for iarea in self irrigated_areas def energy_production self release Returns the energy production Wh for the given release m3 s LONI GST A power 1000 x 9 81 x self head x release x self efficiency return power 3600 3 4 Traits building interactive dialogs 354 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 traits view View Item name Item max_storage Item max_release Item head Item efficiency Item irrigated_areas Reem tobalocroplsurrace resizable True if name _ main__ upper_block IrrigationArea name Section C surface 2000 crop Wheat reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 irrigated_areas upper_block release 80 print Releasing m3 s produces kWh format release reservoir energy_production release The next example shows how the Array trait can be used to feed a specialised TraitsUI Item the ChacoPlotItem impo from from from from from clas rt numpy as np traits api import HasTraits Array Instance Float Property traits api import DelegatesTo traitsui api import View Item Group chaco chaco_plot_editor import ChacoPlotItem reservoir import Rese
189. ction Passing by value Tip Can you modify the value of a variable inside a function Most languages C Java distinguish passing by value and passing by reference In Python such a distinction is somewhat artificial and it is a bit subtle whether your variables are going to be modified or not Fortunately there exist clear rules Parameters to functions are references to objects which are passed by value When you pass a variable to a function python passes the reference to the object to which the variable refers the value Not the variable itself If the value passed in a function is immutable the function does not modify the caller s variable If the value is mutable the function may modify the caller s variable in place gt gt gt def try to modiliy lt y Z x 23 y append 42 z 99 new reference print x print y print z gt gt gt a 77 immutable variable gt gt gt b 99 mutable variable gt gt gt c 28 Tee Cey Co _mochiiry a Jo GC 23 So 42 291 gt gt gt print a TT gt gt gt print b 99 42 gt gt gt print c 234 Functions have a local variable table called a local namespace The variable x only exists within the function try_to_modify Global variables Variables declared outside the function can be referenced within the function In 114 x 5 In 115 def addx y james return x y In 116 addx 10
190. ctor item get item set DIA sparsetools itera has data array specialized LIL via CSR yes yes yes yes itera arithmetics via CSR tive incremental construction DOK python yes one axis yes yes itera O 1 item access incremental only tive construction COO sparsetools itera has data array facilitates fast tive conversion CSR sparsetools yes yes slow any has data array fast row wise ops CSC sparsetools yes yes slow any has data array fast column wise ops BSR sparsetools a special has data array specialized ized gt gt gt mtx sparse bsr_matrix 3 4 dtype np int8 gt gt gt mtx lt 2x4 sparse matrix of type Imumpy Inte gt with 0 stored elements blocksize 1x1 in Block Sparse Row format gt gt gt gt mtx todense macci OMO OOo 0 0 0 Ol O 0 O OJ dtype ints e create empty BSR matrix with 3 2 block size gt gt gt mtx sparse bsr_matrix 3 4 blocksize 3 2 dtype np int8 gt gt gt mtx lt Sy sparse matrix or Cypa n milion Into with 0 stored elements blocksize 3x2 in Block Sparse Row format gt gt gt gt mtx todense macci MOOO 0 0 O Ol 0 0 O O dtype ints a bug e create using data ij tuple with 1 1 block size like CSR gt gt gt GOW Mer array TO Orem a 2n gt gt gt col np array I0 2 2 OW 1 21 gt gt gt data np array Il 27 3 45 5 Gl gt gt gt mtx sparse bsr_matrix data row col shape 3 3
191. current axes and gca in turn calls gcf to get the current figure If there is none it calls figure to make one strictly speaking to make a subplot 111 Let s look at the details Figures Tip A figure is the windows in the GUI that has Figure as title Figures are numbered starting from 1 as opposed to the normal Python way starting from 0 This is clearly MATLAB style There are several parameters that determine what the figure looks like Argument Description num number of figure figsize figsize figure size in in inches width height dpi dpi resolution in dots per inch facecolor figure facecolor color of the drawing background edgecolor figure edgecolor color of edge around the drawing background draw figure frame or not frameon True Tip The defaults can be specified in the resource file and will be used most of the time Only the number of the figure is frequently changed As with other objects you can set figure properties also setp or with the set_something methods When you work with the GUI you can close a figure by clicking on the x in the upper right corner But you can close a figure programmatically by calling close Depending on the argument it closes 1 the current figure no argument 2 a specific figure figure number or figure instance as argument or 3 all figures al1 as argument pl close 1 Closes figure 1 Subplots Tip With subplot
192. d array with field names r g b a without copying data gt gt gt y gt gt gt assert y r gt gt gt assert y g gt gt gt assert y b gt gt gt assert y a Solution gt gt gt y x view O7 4 dtype np int8 A WONS o fy fh 2 2 Advanced Numpy 162 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Warning Another array taking exactly 4 bytes of memory gt gt gt y np array 1 3 2 4 dtype np uint8 transpose gt gt gt x y copy gt gt gt x array TLI 2 3 4 dtype uint8 gt gt gt y array Ill 217 3 4 dtype uint8 gt gt gt x view np intl16 aicieay SOLS 1027 dtype intl16 gt gt gt 0x0201 0x0403 Sis 1027 gt gt gt y view np int16 array 769 1026 dtype int16 e What happened e we need to look into what x 0 1 actually means gt gt gt Ox0S01 0x0402 Teo 1026 Indexing scheme strides Main point The question gt gt gt x MP array Il 2 S 47 S Sly ee 7 8 9 dtype np ints8 gt gt gt str x data PNSODT N02 x0S x04 x05 x06 x07 x08 Vt At which byte in x data does the item x 1 2 begin The answer in Numpy e strides the number of bytes to jump to find the next element e stride per dimension gt gt gt x strides 3 1 gt gt gt byte_offset 3 1 1
193. de a time offset Fit this function to the data with scipy optimize curve_fit Plot the result Is the fit reasonable If not why Is the time offset for min and max temperatures the same within the fit accuracy 1 5 Scipy high level scientific computing 113 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercise 2 D minimization Six hump Camelback function The six hump camelback function 4 has multiple global and local minima Find the global minima of this function Hints e Variables can be restricted to 2 lt x lt 2and 1 lt y lt 1 e Use numpy meshgrid and pylab imshow to find visually the regions e Use scipy optimize fmin_bfgs or another multi dimensional minimizer How many global minima are there and what is the function value at those points What happens for an initial guess of x y 0 0 See the summary exercise on Non linear least squares curve fitting application to point extraction in topograph ical lidar data page 129 for another more advanced example 1 5 6 Statistics and random numbers scipy stats The module scipy stats contains statistical tools and probabilistic descriptions of random processes Random number generators for various random process can be found in numpy random Histogram and probability density function Given observations of a random process their histogram is an estimator of the random process s PDF probabil
194. define the residuals any more 1 5 Scipy high level scientific computing 131 1 5 Scipy high level scientific computing 132 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Image processing application counting bubbles and unmolten grains Det Spot Mag MV 15 05 2009 1280 C Statement of the problem 1 Open the image file MV_HFV_012 jpg and display it Browse through the keyword arguments in the docstring of imshow to display the image with the right orientation origin in the bottom left corner and not the upper left corner as for standard arrays This Scanning Element Microscopy image shows a glass sample light gray matrix with some bubbles on black and unmolten sand grains dark gray We wish to determine the fraction of the sample covered by these three phases and to estimate the typical size of sand grains and bubbles their sizes etc 2 Crop the image to remove the lower panel with measure information 3 Slightly filter the image with a median filter in order to refine its histogram Check how the histogram changes 4 Using the histogram of the filtered image determine thresholds that allow to define masks for sand pixels glass pixels and bubble pixels Other option homework write a function that determines automatically the thresholds from the minima of the histogram 5 Display an image in which the three phases are colored with three different colors 6 Use mathematica
195. dge length to the proper value moo return self edgexx2 area setter def area self area self edge area xx 0 5 The way that this works is that the property decorator replaces the getter method with a property object This object in turn has three methods getter setter and deleter which can be used as decorators Their job is to set the getter setter and deleter of the property object stored as attributes fget fset and fdel The getter can be set like in the example above when creating the object When defining the setter we already have the property object under area and we add the setter to it by using the setter method All this happens when we are creating the class Afterwards when an instance of the class has been created the property object is special When the inter preter executes attribute access assignment or deletion the job is delegated to the methods of the property object To make everything crystal clear let s define a debug example gt gt gt class D object property def a self print getting 1 return 1 a setter def a self value print Setting r value a deleter def a self print deleting gt gt gt D a 2 1 Advanced Python Constructs 149 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 lt property object at Ox 7 gt gt gt gt D a fget LIS WINCGIE NOM oo o gt gt gt gt D a fset STACIE LOM 5 6 oS gt gt gt
196. dices i suchas start lt i lt stop i ranging from start to stop 1 Therefore 1 start stop has stop start elements Slicing syntax 1 start stop stride Tip All slicing parameters are optional gt gt gt redi Yolue gt toreen Tbpblacki iviitet gt gt gt lees a Moda h vaite gt gt gt l 3 red blue green gt gt gt 2 red green white Lists are mutable objects and can be modified gt gt gt 0 yellow gt gt gt 1 yellow blue green black white gt gt gt 2 4 gray purple gt gt gt yellow blue gray purple white Note The elements of a list may have different types Soo 1 9 2 2200 helio gt gt gt 1 Se 200 aeren gt gt gt ell all P2 A100 Neon Tip For collections of numerical data that all have the same type it is often more efficient to use the array type provided by the numpy module A NumPy array is a chunk of memory containing fixed sized items With NumPy arrays operations on elements can be faster because elements are regularly spaced in memory and more operations are performed through specialized C functions instead of Python loops Tip Python offers a large panel of functions to modify lists or query them Here are a few examples for more details see http docs python org tutorial datastructures html more on lists Add and remove elements
197. docs scipy org doc numpy reference ufuncs html casting rules Re interpretation viewing e Data block in memory 4 bytes 0x01 Il 0x02 Il 0x03 Il 0x04 4of uint8 OR 4 of int8 OR 2 of int16 OR 1 of int32 OR 1 of float32 OR How to switch from one to another Switch the dtype gt gt gt x np array 1 2 3 4 dtype np uints8 gt gt gt x Ckype lt a2 gt gt gt X array 513 1027 dtype int16 gt gt gt Ox0Z201 0x003 S13 1027 0x01 0x02 Il 0x03 0x04 Note little endian least significant byte is on the left in memory Create a new view gt gt gt y x view lt i4 gt gt gt y array 67305985 dtype int32 gt gt gt 0x04030201 67305995 Note e view makes views does not copy or alter the memory block e only changes the dtype and adjusts array shape gt gt gt x 1 5 gt gt gt y array SASI9S gt gt gt y base is x True Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 dtype int32 Mini exercise data re interpretation 2 2 Advanced Numpy 161 See also view colors py You have RGBA data in an array gt gt gt x np zeros 10 gt gt gt aler cr O S gt gt gt x3 lt 7 I 2 gt gt gt xls Gp 2al 3s gt gt gt X 3 4 where the last three dimensions are the R B and G and alpha channels How to make a 10 10 structure
198. dtype S7 Fh Fh Much more e int32 e int64 e uint32 1 3 NumPy creating and manipulating numerical data 46 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e uint64 ee ee eee 9 Basic visualization Now that we have our first data arrays we are going to visualize them 8 Start by launching IPython 7 ioychon Or the notebook 6 ipython notebook 5 Once IPython has started enable interactive plots gt gt gt matplotlib 4 Or from the notebook enable plots in the notebook 3 gt gt gt Smatplotlib inline 2 The inline is important for the notebook so that plots are displayed in the notebook and not in a new window Matplotlib is a 2D plotting package We can import its functions as below 1 gt gt gt import matplotlib pyplot as plt the tidy way 0 And then use note that you have to use show explicitly if you have not enabled interactive plots with 0 0 0 5 1 0 1 5 2 0 2 5 3 0 smatplotlib gt gt gt plt plot x y line plot e 2D arrays such as images gt gt gt plt show lt shows the plot not needed with interactive plots gt gt gt image np random rand 30 30 Or if you have enabled interactive plots with smatplotlib gt gt gt plt imshow image cmap plt cm hot gt gt gt Olen Colona N gt gt gt plot x y line plot lt matp lot Imb ecolorbar COlkormloeie amstan
199. dules must be located in the search path therefore you can e write your own modules within directories already defined in the search path e g SHOME local lib python2 7 dist packages You may use symbolic links on Linux to keep the code somewhere else e modify the environment variable PYTHONPATH to include the directories containing the user defined mod ules Tip On Linux Unix add the following line to a file read by the shell at startup e g etc profile profile export PYTHONPATH SPYTHONPATH home emma user_defined_modules On Windows http support microsoft com kb 3 10519 explains how to handle environment variables 1 2 The Python language 31 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e or modify the sys path variable itself within a Python script Tip import sys new_path home emma user_defined_modules if new_path not in sys path sys path append new_path This method is not very robust however because it makes the code less portable user dependent path and because you have to add the directory to your sys path each time you want to import from a module in this directory See also See http docs python org tutorial modules html for more information about modules Packages A directory that contains many modules is called a package A package is a module with submodules which can have submodules themselves etc A special file called ___ init__ py wh
200. e Interactive File usr lib python2 6 dist packages scipy ndimage morphology py Definition morphology binary_dilation input structure None iterations 1 mask None output None border_value 0 origin 0 brute_force False DOS Steen cis Multi dimensional binary dilation with the given structure An output array can optionally be provided The origin parameter controls the placement of the filter If no structuring element is provided an element is generated with a squared connectivity equal to one The dilation operation is repeated iterations times If iterations is less than 1 the dilation is repeated until the result does not change anymore If a mask is given only those elements with a true value at the corresponding mask element are modified at each iteration Good practices Use meaningful object names Indentation no choice Tip Indenting is compulsory in Python Every command block following a colon bears an additional indentation level with respect to the previous line with a colon One must therefore indent after def f or while At the end of such logical blocks one decreases the indentation depth and re increases it if a new block is entered etc Strict respect of indentation is the price to pay for getting rid of or characters that delineate logical blocks in other languages Improper indentation leads to errors such as IndentationErro
201. e 248 e Practical guide to optimization with scipy page 248 Choosing a method page 248 Making your optimizer faster page 249 Computing gradients page 249 Synthetic exercices page 249 e Special case non linear least squares page 250 Minimizing the norm of a vector function page 250 Curve fitting page 251 e Optimization with constraints page 251 Box bounds page 251 General constraints page 252 2 7 1 Knowing your problem Not all optimization problems are equal Knowing your problem enables you to choose the right tool 2 7 Mathematical optimization finding minima of functions 239 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Dimensionality of the problem The scale of an optimization problem is pretty much set by the dimensionality of the problem i e the number of scalar variables on which the search is performed Convex versus non convex optimization angent to f J A convex function A non convex function e f is above all its tangents e equivalently for two point A B f C lies below the segment f A f B if A lt C lt B Optimizing convex functions is easy Optimizing non convex functions can be very hard Note It can be proven that for a convex function a local minimum is also a global minimum Then in some sense the minimum is unique Smooth and non smooth problems A smooth function A non smoo
202. e ability to register listeners on the fly with the dynamic listeners from reservoir import Reservoir from reservoir_state_property import ReservoirState def wake_up_watchman_if_spillage new_value if new_value gt 0 print Wake up watchman Spilling hm3 format new_value if name main _ projectA Reservoir name Project A max_storage 30 max_release I OLOROF hydraulic_head 60 efficiency 0 8 3 4 Traits building interactive dialogs 351 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 state ReservoirState reservoir projectA storage 10 register the dynamic listener state on_trait_change wake_up_watchman_if_spillage name spillage state release 90 state inflows 0 state print_state print Forcing spillage state inflows 100 state release 0 print Why do we have two executions of the callback The dynamic trait notification signatures are not the same as the static ones e def wake_up_watchman pass e def wake_up_watchman new pass def wake_up_watchman name new pass e def wake_up_watchman object name new pass e def wake_up_watchman object name old new pass Removing a dynamic listener can be done by e calling the remove_trait_listener method on the trait with the listener method as argument e calling the on_trait_change method with listener method and the keyword remove True e deleting the instance that holds the listene
203. e between effect of versicolor and virginica lt lt test H array I S2245385385 PeF 0207369059 cdtodenom 146 disnum i gt Python source code plot_iris_analysis py import matplotlib pyplot as plt import pandas from pandas tools import plotting from statsmodels formula api import ols oad Ehe Gata data pandas read_csv iris csv Heese eee ee eee ee ee eee eeeeeaeeeeeeeeseeeeeeeeeeeeeeaeeeeaeeaet ead teat dad tee teats aE in IDOE E SCEE iene Pals Express the names as categories categories pandas Categorical data name The parameter c is passed to plt scatter and will control the color plotting scatter_matrix data c categories labels marker o jee plit gein fig suptitle blue setosa green versicolor red virginica size 13 HHHHEEFHHEEH HEPA H EEA HAE A EEE AEA H EEA H HEA H EEA HEE PEE EEE EH ERE Statistical analysis Let us try to explain the sepal length as a function of the petal width and the category of iris model ols sepal_width name petal_length data fit print model summary Now formulate a contrast to test if the offset for versicolor and virginica are identical print Testing the difference between effect of versicolor and virginica 3 1 Statistics in Python 294 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 print
204. e http docs python org tutorial datastructures html dictionaries for more information A dictionary can have keys resp values with different types gt gt gt d anal Uw 2 Se eine ko gt gt gt d uate ee Sos hellot TPIS 25h More container types Tuples Tuples are basically immutable lists The elements of a tuple are written between parentheses or just separated by commas gt gt gt t 12345 54321 hello gt gt gt t 0 12345 gt gt gt t 12345 54321 hellol gt gt gt u 0 2 Sets unordered unique items S335 sen a oo Verte Fal gt gt gt S set tal Sew DN gt gt gt s difference a b serler Assignment operator Tip Python library reference says Assignment statements are used to re bind names to values and to modify attributes or items of mutable objects In short it works as follows simple assignment 1 an expression on the right hand side is evaluated the corresponding object is created obtained 2 aname on the left hand side is assigned or bound to the r h s object Things to note e a single object can have several names bound to it En ii a Wile 2 In 2 b a In 3 a Choe eal ee 2 sh In 4 b Ome ae Wel 27 53 In Si a IS p ouelo True ITa fees eS Thil In 7 a Gre a S e to change a list in place use indexing slices 1 2 The Python language 17 Python Scientific lecture notes Release 20
205. e much shorter than the definition of a class with the required next and__iter__ methods What is more important it is easier for the author of the generator to understand the state which is kept in local variables as opposed to instance attributes which have to be used to pass data between consecutive invocations of next on an iterator object A broader question is why are iterators useful When an iterator is used to power a loop the loop becomes very simple The code to initialise the state to decide if the loop is finished and to find the next value is extracted into a separate place This highlights the body of the loop the interesting part In addition it is possible to reuse the iterator code in other places Bidirectional communication Each yield statement causes a value to be passed to the caller This is the reason for the introduction of generators by PEP 255 implemented in Python 2 2 But communication in the reverse direction is also useful One obvious way would be some external state either a global variable or a shared mutable object Direct communication is possible thanks to PEP 342 implemented in 2 5 It is achieved by turning the previously boring yield statement into an expression When the generator resumes execution after a yield statement the caller can call a method on the generator object to either pass a value into the generator which then is returned by the yield statement or a different method to inject an e
206. e program from a menu e g in the Python x y or EPD menu if you have installed one of these scientific Python suites Tip If you don t have Ipython installed on your computer other Python shells are available such as the plain Python shell started by typing python in a terminal or the Idle interpreter However we advise to use the Ipython shell because of its enhanced features especially for interactive scientific computing Once you have started the interpreter type 1 2 The Python language 9 int a 3 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt print Hello world Hello world Tip The message Hello world is then displayed You just executed your first Python instruction congratula tions To get yourself started type the following stack of instructions gt gt gt a 3 gt gt gt D 2a gt gt gt type b cy pe aint gt gt gt print b 6 gt gt gt axb 18 gt gt gt b hello gt gt gt type b lt type str gt gt gt gt bes hellohello gt gt gt 2xb hellore llo Tip Two variables a and b have been defined above Note that one does not declare the type of an variable before assigning its value In C conversely one should write In addition the type of a variable may change in the sense that at one point in time it can be equal to a value of a certain type and a s
207. e__ Ieeiea 2 G98209207 Kalse tdesecr te MO TA shaper 4 strides None TEyPSSEr t KIMI Ivers ront o Reminder two ndarrays may share the same memory gt gt gt x np array 1l 2 3 4 2S gt ya 2 2 Advanced Numpy 157 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Memory does not need to be owned by an ndarray gt gt gt x b 1234 The b is for bytes necessary in Python 3 x is a string in Python 3 a bytes we can represent its data as an array of ints gt gt gt y np frombuffer x dtype np int8 gt gt gt y data Sooo aa gt gt gt y base is x TEUS gt gt gt y flags C_CONTIGUOUS True F_CONTIGUOUS True OWNDATA False WRITEABLE False ALIGNED True UPDATEIFCOPY False The owndata and writeable flags indicate status of the memory block Data types The descriptor dt ype describes a single item in the array type scalar type of the data one of int8 int16 float64 et al fixed size str unicode void flexible size itemsize size of the data block byte byte order big endian gt little endian lt not applicable order fields sub dtypes if it s a structured data type shape shape of the array if it s a sub array gt gt gt np dtype int type lt type numpy int64 gt gt gt gt np dtype int itemsize 8 gt gt gt np dtype int byteorder v
208. eal it has been deprecated since SciPy 0 14 0 and removed in SciPy 0 16 0 To find the local minimum lets constraint the variable to the interval 0 10 using scipy optimize fminbound gt gt gt xmin_local optimize fminbound f 0 10 gt gt gt xmin_local 3 8374671 6 Note Finding minima of function is discussed in more details in the advanced chapter Mathematical optimiza tion finding minima of functions page 239 Finding the roots of a scalar function To find a root i e a point where f x 0 of the function f above we can use for example scipy optimize fsolve gt gt gt root optimize fsolve f 1 our initial guess is 1 gt gt gt TOOLE array Ol Note that only one root is found Inspecting the plot of f reveals that there is a second root around 2 5 We find the exact value of it by adjusting our initial guess gt gt gt root2 optimize fsolve f 2 5 Sees ICC array 2 47948183 Curve fitting Suppose we have data sampled from f with some noise Comment to make doctest pass gt gt gt np random seed 42 gt gt gt xdata np linspace 10 10 num 20 gt gt gt ydata f xdata np random randn xdata size Now if we know the functional form of the function from which the samples were drawn x 2 sin x in this case but not the amplitudes of the terms we can find those by least squares curve fitting First we have to define the function to fit gt gt gt
209. eans scipy constants Physical and mathematical constants s ipy fftpack Fourier transform scipy integrate Integration routines scipy interpolate Interpolation scipy io Data input and output scipy linalg Linear algebra routines scipy ndimage n dimensional image package scipy odr Orthogonal distance regression scipy optimize Optimization scipy signal Signal processing scipy sparse Sparse matrices scipy spatial Spatial data structures and algorithms scipy special Any special mathematical functions scipy stats Statistics Tip They all depend on numpy but are mostly independent of each other The standard way of importing Numpy and these Scipy modules is gt gt gt import numpy as np gt gt gt from scipy import stats same for other sub modules Scipy The scipy package contains various toolboxes dedicated to common issues in scientific computing Its different submodules correspond to different applications such as interpolation integration optimization image processing statistics special functions etc scipy can be compared to other standard scientific computing libraries such as the GSL GNU Scientific Library for C and C or Matlab s toolboxes scipy is the core package for scientific routines in Python it is meant to operate efficiently on numpy arrays so that numpy and scipy work hand in hand Before implementing a routine it is worth checking if the desired data processing is not already imp
210. eans that help function will display a useless argument list which will be confusing for the user of the function An effective but ugly way around this problem is to create the wrapper dynamically using eval This can be automated by using the external decorator module It provides support for the decorator decorator which takes a wrapper and turns it into a decorator which preserves the function signature To sum things up decorators should always use functools update_wrapper or some other means of copying function attributes Examples in the standard library First it should be mentioned that there s a number of useful decorators available in the standard library There are three decorators which really form a part of the language e classmethod causes a method to become a class method which means that it can be invoked without creating an instance of the class When a normal method is invoked the interpreter inserts the instance object as the first positional parameter sel1 f When a class method is invoked the class itself is given as the first parameter often called cls Class methods are still accessible through the class namespace so they don t pollute the module s names pace Class methods can be used to provide alternative constructors class Array object def init__ self data self data data classmethod def fromfile cls file data numpy load file return cls data This is cleaner then
211. east Squares F statistic 74 03 formulas to specify statistical models in Python ees ay He Ng eee re ye Bele Time Pee Log Likelihood 5 7 IGE A simple linear regression No Observations 20 AIC 120 0 Df Residuals 18 BICE 12240 Df Model i 15 coef std err ie IPS e 95 03 Conf init 10 5 Intercept 5 75s 5 IROLE ZSC OA 0 000 F 5 VIC 35 39 1 x 229309 0 341 8 604 OF 000 2o ZAA 236 54 0 Omnibus OO LOO Durbin Watson 2956 a 5 Prob Omnibus 0 951 Jarque Bera JB 0 322 10 Skew OOS IPieOlo WJls omes IGUAL COS aL iS 2a 390 Conds Nee 3503 15 20 i 5 5 gt 4 6 Terminology x Given two set of observations x and y we want to test the Statsmodel uses a statistical terminology the y variable in statsmodel is called endogenous while the x variable is called exogenous This is discussed in more detail here y x coef intercept e To simplify y endogenous is the value you are trying to predict while x exogenous represents the features you are using to make the prediction hypothesis that y is a linear function of x In other terms where e is observation noise We will use the statmodels module to 1 Fit a linear model We will use the simplest strategy ordinary least squares OLS 2 Test that coef is non zero Exercise Retrieve the estimated parameters from the model above Hint use tab completion to find the relevent attribute First we generate simulated data a
212. ec array CE a ieee Lal gt gt gt mtx vec array I 1257 1957 On gt gt gt mtx toarray vec aimcay is Omer ES Oo Pl Sore Tee Oey 2s I kes Over Sip omir Oks ORF tae A I List of Lists Format LIL row based linked list each row is a Python list sorted of column indices of non zero elements rows stored in a NumPy array dt ype np object non zero values data stored analogously efficient for constructing sparse matrices incrementally constructor accepts dense matrix array sparse matrix shape tuple create empty matrix flexible slicing changing sparsity structure 1s efficient slow arithmetics slow column slicing due to being row based use when sparsity pattern is not known apriori or changes example reading a sparse matrix from a text file Examples e create an empty LIL matrix gt gt gt mtx sparse lil_matrix 4 5 prepare random data gt gt gt from numpy random import rand gt gt gt data np round rand 2 3 gt gt gt data array LL lo beter Aly pies Ore lel assign the data using fancy indexing gt gt gt MEXXI 2 M 2 si ceca gt gt gt mtx lt x Sparse matr ix Or tyoe lt 255 numpy tloarte4 gt with 5 stored elements in Linked List format gt 2 5 Sparse Matrices in SciPy 207 Sparse Matrices in SciPy 208 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Pyt
213. econd point in time it can be equal to a value of a different type b was first equal to an integer but it became equal to a string when it was assigned the value hello Operations on integers b 2 xa are coded natively in Python and so are some operations on strings such as additions and multiplications which amount respectively to concatenation and repetition 1 2 2 Basic types Numerical types Tip Python supports the following numerical scalar types Integer 7 gt gt gt T m iL 2 gt gt gt a 4 gt gt gt type a lt Eype ani gt Floats gt gt gt c 2 1 gt gt gt type c lt type float gt Complex 2a a oa Ue Sy gt gt gt a real 1 2 The Python language 10 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 15 gt gt gt a imag 0 5 gt gt gt type 1l 07 lt type complex gt Booleans gt gt gt SF gt 4 False gt gt gt test 3 gt 4 See TSS False gt gt gt type test lt type bool gt Tip A Python shell can therefore replace your pocket calculator with the basic arithmetic operations x modulo natively implemented gt gt gt he gt 3 2mo gt gt gt 2xx10 1024 gt gt gt 8 s 3 2 Type conversion casting gt gt gt float 1 LO 1 2 The Python language 11 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Warning Integer division In P
214. eds the UnivariateSpline will be used because a spline of degree 3 seems to correctly fit the data gt gt gt from scipy interpolate import UnivariateSpline gt gt gt quantile_func UnivariateSpline cprob sorted_max_speeds The quantile function is now going to be evaluated from the full range of probabilities gt gt gt nprob np linspace 0 1 1e2 gt gt gt fitted_max_speeds gquantile_func nprob In the current model the maximum wind speed occurring every 50 years is defined as the upper 2 quantile As a result the cumulative probability value will be 22 gt Litly prob k 0202 So the storm wind speed occurring every 50 years can be guessed by gt gt gt fifty_wind quantile _func fifty_prob ero hitry wind array 5220 Jo9eZ oes The results are now gathered on a Matplotlib figure Exercise with the Gumbell distribution The interested readers are now invited to make an exercise by using the wind speeds measured over 21 years The measurement period is around 90 minutes the original period was around 10 minutes but the file size has been reduced for making the exercise setup easier The data are stored in numpy format inside the file examples sprog windspeeds npy Do not look at the source code for the plots until you have completed the exercise 1 5 Scipy high level scientific computing 126 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scient
215. ehaviour is the state of the storage that depends on controlled and uncontrolled parameters storagez41 storage inflows release spillage irrigation Warning The data used in this tutorial are not real and might even not have sense in the reality 3 4 3 What are Traits A trait is a type definition that can be used for normal Python object attributes giving the attributes some additional characteristics e Standardization Initialization Validation Deferral e Notification e Visualization e Documentation A class can freely mix trait based attributes with normal Python attributes or can opt to allow the use of only a fixed or open set of trait attributes within the class Trait attributes defined by a class are automatically inherited by any subclass derived from the class The common way of creating a traits class is by extending from the HasTraits base class and defining class traits from traits api import HasTraits Str Float class Reservoir HasTraits name Str max_storage Float Warning For Traits 3 x users If using Traits 3 x you need to adapt the namespace of the traits packages e traits api should be enthought traits api e traitsui api should be enthought traits ui api Using a traits class like that is as simple as any other Python class Note that the trait value are passed using keyword arguments 3 4 Traits building interactive dialogs 341 Python Scientific lecture
216. eight g 3 8 amp amp 8 8 a 3 1 Statistics in Python 275 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt plotting scatter_matrix data PIQ VIO oO 1 Two populations The IQ metrics are bimodal as if there are 2 sub populations 150 140 130 120 110 100 90 80 PIQ 150 140 130 120 110 100 90 80 70 140 VIQ 130 120 110 FSIQ 100 90 80 140 Exercise Plot the scatter matrix for males only and for females only Do you think that the 2 sub populations corre spond to gender 3 1 2 Hypothesis testing comparing two groups For simple statistical tests we will use the sci py stats sub module of scipy gt gt gt from scipy import stats See also Scipy is a vast library For a quick summary to the whole library see the scipy page 103 chapter 3 1 Statistics in Python 276 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Student s t test the simplest statistical test 1 sample t test testing the value of a population mean 0 4 2 5 Ms 0 3 0 2 0 1 0 0 scipy stats ttest_lsamp tests if the population mean of data is likely to be equal to a given value technically if observations are drawn from a Gaussian distributions of given population mean It returns the T statistic and the p value see the function s help gt gt gt stats ttest_lsamp data VIQ
217. elf func args xxkwargs It can also be implemented as a function def deprecated func nuppi ne a Geprecalion warning Once On first use of Lhe Tunclion gt gt gt deprecated doctest SKIP def pass 2 1 Advanced Python Constructs 150 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Sie doctest SKIP f is deprecated count 0 def wrapper args xxkwargs ecount 0 1 LE count 0 1 print func __name_ is deprecated return func xargs xxkwargs return wrapper A while loop removing decorator Let s say we have function which returns a lists of things and this list created by running a loop If we don t know how many objects will be needed the standard way to do this is something like def find_answers answers while True ans look _for_next_answer if ans is None break answers append ans return answers This is fine as long as the body of the loop is fairly compact Once it becomes more complicated as often happens in real code this becomes pretty unreadable We could simplify this by using yield statements but then the user would have to explicitly call List find_answers We can define a decorator which constructs the list for us def vectorized generator_func def wrapper xargs xkwargs return list generator_func args xkwargs return functools update_wrapper wrapper generator_func
218. emory for a Therefore the reshape operation needs to make a copy here Example fake dimensions with strides Stride manipulation gt gt gt from numpy 1lib stride_tricks import as_strided gt gt gt help as_strided as_strided x shape None strides None Make an ndarray from the given array with the given shape and strides Warning as_strided does not check that you stay inside the memory block bounds gt gt gt x np array 1 2 3 4 dtype np intl16 gt gt gt as_strided x strides 2 2 shape 2 array 1 3 dtype intl16 gt gt gt X 2 array a dtype int16 See also stride fakedims py Exercise array Iir 27 gt F datype Np intg iy By Sp 4 ily 2p 3 a GleviseSiajo meS 2 2 Advanced Numpy 165 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 using only as_strided Hint byte_offset stride 0 index 0 stride 1 index 1 Spoiler Stride can also be 0 gt gt gt x np array 1 2 3 4 dtype np int8 gt gt gt y as_strided x strides 0 1 shape 3 4 gt gt gt y array ile 27 3 A pe Hp Sy A 1 2 3 4 dtype ints gt gt gt y base base is x True Broadcasting e Doing something useful with it outer product of 1 2 3 4 and 5 6 7 gt gt gt x np array 1 2 3 4 dtype np ant ic gt gt gt x2 as_strided x strides 0 1 2
219. en to this function in order to help if find the best possible resolution system For example if you know that it is a separable equations you can use keyword hint separable to force dsolve to resolve it as a separable equation gt gt gt dsolve sin x xcos f x cos x xsin f x f x diff x f x hint separable eal Cal EO a s Bae t e pirn E asin i men i pi Da7 2 I 2 YE cos x AA eos x X je Gil Gl F x asin el EE asinna rl fI la 2 eee 2 Z cos x iE V7 Cos x Exercises 1 Solve the Bernoulli differential equation eT fa fle 0 2 Solve the same equation using hint Bernoulli What do you observe 3 3 Scikit image image processing Author Emmanuelle Gouillart scikit image is a Python package dedicated to image processing and using natively NumPy arrays as image objects This chapter describes how to use scikit image on various image processing tasks and insists on the link with other scientific Python modules such as NumPy and SciPy See also For basic image manipulation such as image cropping or simple filtering a large number of simple operations can be realized with NumPy and SciPy only See Image manipulation and processing using Numpy and Scipy 3 3 Scikit image image processing 324 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 page 22
220. ender T Male 8 8524 OR LO 0 827 0 414 2 890 307995 Author Fabian Pedregosa MRI_Count 0 0002 6 46e 05 2 5 OLS OR Oaks 3 ce US 0 000 Height 3 0837 IRIS f AIL OmOvar 5 6 4 0 494 SS a eS a ee Objectives Omnibus JR SES Durbin Watson 23 LOY E Prob Omaira 0 025 Jarque Bera JB 2 252 1 Evaluate expressions with arbitrary precision Skew 0 006 Prob JB 0 a2 2 Perform algebraic manipulations on symbolic expressions Kirtoeiss 1 023 Cond Nos 2 40e 07 3 Perform basic calculus tasks limits differentiation and integration with symbolic expressions SSS SSS S555 555 55555555555 SSSSSSS55 5 4 Solve polynomial and transcendental equations 5 Solve some differential equations Warnings 1 The condition number is large 2 4e 07 This might indicate that there are strong multicollinearity or other numerical problems What is SymPy SymPy is a Python library for symbolic mathematics It aims to be an alternative to systems lt P test P array 0 693919608 p LL 0 414067684 l di2denom 35 gt E num 1 gt such as Mathematica or Maple while keeping the code as simple as possible and easily extensible SymPy is written entirely in Python and does not require any external libraries po OM SOURCE COREE pote anes Sympy documentation and packages for installation can be found on http www sympy org import pandas from statsmodels formula api import ols Chapters contents data pandas read_csv
221. erfacing with C 266 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 String Form lt module cos_module from cos_module so gt File home esc git working scipy lecture notes advanced interfacing_with_c cython cos_modu DOGSELING lt mo docs E Ino In 3 dir cos module curio lk Joni Ineslings Y 1 doc file name_ package ain test i e i SOSE EUNE In 4 cos module cos runem 0 Owie e OF S20 0280 Sis GE L SSS in Si ecos moduler cos rune 0m0 Outs 1 0 In 6 cos module cos rune 3 14159265359 Qutli 1 0 And testing a little for robustness we can see that we get good error messages In 7 7 coc module Cos Fune Too TypeError Traceback most recent call last lt ipython input 7 llbee483665d gt in lt module gt eos Moduler S OS FUNe TEO home esc git working scipy lecture notes advanced interfacing_with_c cython cos_modtile so in cos TypeError a float is required Additionally it is worth noting that Cython ships with complete declarations for the C math library which simplifies the code above to become UUs Simpler example of wrapping Cos Tuncel on from macn a Using Cython Our from libc math cimport cos def cos_func arg return cos arg In this case the cimport statement is used to import the cos function Numpy Support Cython has support for Numpy via the numpy pyx file which allows you to add t
222. es Release 2013 2 beta euroscipy 2013 1 0 0 5 0 0 0 5 1 0 The Chebyshev polynomials have some advantages in interpolation Loading data files Text files Example populations txt year hare lynx carrot 1900 30e3 4e3 48300 1901 47 2e3 6 1e3 48200 1902 70 2e3 9 8e3 41500 1903 77 4e3 35 2e3 38200 gt gt gt data np loadtxt data populations txt gt gt gt data arcreay II L20027 500005 A000 mee 00n I9015 47200 CLOG 4820021 fk LOO 0200r 93 100 AL500 2 gt gt Np Savetxt poe tock 7 data gt gt gt data2 np loadtxt pop2 txt Note If you have a complicated text file what you can try are e np genfromtxt e Using Python s I O functions and e g regexps for parsing Python is quite well suited for this 1 3 NumPy creating and manipulating numerical data 74 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Reminder Navigating the filesystem with Python In 1 pwd show current directory home user stuff 2011 numpy tutorial In 2 cd ex home user stuff 2011 numpy tutorial ex In 3 Is populations txt species txt Images Using Matplotlib gt gt gt img plt imread data elephant png gt gt gt img shape img dtype 200 300 3 dtyoe t loat32 E gt gt gt plt imshow img E lt matplotlib image Ax
223. es or features The data can than be seen as a 2D table or matrix with columns giving the different attributes of the data and rows the observations For instance the data contained in examples brain_size csv 271 un Gender E510 VIO P10 Weight Height MRI Count viem mema llet T33 B2 eae AU ilo Udo Sie cotey ill Gye 2 vm Malet IMALO 50r aya GU cgi eye OVO laLal 2411 vov Made 139 1237150 1149m M72 30 1038437 TAM Me lem T337 129 128 ie M690 965353 Une Meema len 1ST oil Syne A TAS 0555 The panda data frame Tip We will store and manipulate this data in a pandas DataFrame from the pandas module It is the Python equivalent of the spreadsheet table It is different from a 2D numpy array as it has named columns can contain a mixture of different data types by column and has elaborate selection and pivotal mechanisms Creating dataframes reading data files or converting arrays 3 1 Statistics in Python 272 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Separator It is a CSV file but the separator is Reading from a CSV file Using the above CSV file that gives observations of brain size and weight and IQ Willerman et al 1991 the data are a mixture of numerical and categorical values gt gt gt import pandas gt gt gt data pandas read_csv examples brain_size
224. esImage object at gt mmi gt gt gt ple saverio ploc png a inn iz E EE uN 0 10 20 30 40 gt gt gt plt imsave red_elephant img 0 cmap plt cm gray This saved only one channel of RGB gt gt gt plt imshow plt imread red_elephant png lt matplotlib image AxesImage object at gt Other libraries Numpy s own format gt gt gt from scipy misc import imsave gt gt gt imsave tiny_elephant png img 6 6 i i oe ple imohow ple tmread tinsel nat mat emtcrpolstien nearesn Numpy has its own binary format not portable but with efficient I O lt matplotlib image AxesImage object at gt gt gt gt data np ones 3 3 gt gt gt np save pop npy data gt gt gt datas np load pop npy Well known amp more obscure file formats HDF5 h5py PyTables e NetCDF scipy io netcdf_file netcdf4 python e Matlab scipy io loadmat scipy io savemat e MatrixMarket scipy io mmread scipy io mmwrite e IDL scipy io readsav If somebody uses it there s probably also a Python library for it Exercise Text data files Write a Python script that loads data from populations txt and drop the last column and the first 5 rows Save the smaller dataset to pop2 txt 1 3 NumPy creating and manipulating numerical data 75 1 3 NumPy creating and manipulating numerical data 76 Python Scientific lecture notes Release 20
225. esent the vertices in fancy_mesh mode Must be a float Default 0 05 scale_mode the scaling mode for the glyphs vector scalar or none bit 8 transparent make the opacity of the actor depend on the scalar tube_radius radius of the tubes used to represent the lines in mesh mode If None simple lines are used tube_sides number of sides of the tubes used to represent the lines Must be an integer Default 6 vmax vmax is used to scale the colormap If None the max of the data will be used Tip Different items can be added to the figure to carry extra information such as a colorbar or a title vmin vmin is used to scale the colormap If None the min of the data will be used In 8 mlab mesh x y 27 amp tent 0 1 0 1 0 1 representation wireframe line_width 1 color 0 5 0 5 0 5 lt mayavi modules surface Surface object at 0xdd6a71c gt Decorations In 9 mlab colorbar Out 7 orientation vertical Out Os k tytk elasses scalar bar actor ocCalarBarActor object at Uxdsil 7 rc In 10 mlab title polar mesh Out 10 lt enthought mayavi modules text Text object at Oxd8ed38c gt In 11 mlab outline Out 7 Out 11 lt enthought mayavi modules outline Outline object at Oxdd21b6c gt In 12 mlab axes Out 7 Out 12 lt enthought mayavi modules axes Axes object at Oxd2e4bcc gt 3 5 3D plotting with Mayavi 361 3 5 3D plotting with Mayavi 362 P
226. eta euroscipy 2013 CAERA Isa Oscsd An 3 20 Oni C maAu 1 2 O is dtype I Sensow_coce S4 Posi Eroni Y lt irs Ywellue Vir I Field access works by indexing with field names gt gt gt samples sensor_code array ALA BETAS TAU YALPA Alea TAU Iy dtype S4 gt gt gt samples value array i Oese Ocoilil OclS Wsas7 Osiil Osils gt gt gt samples 0 CATEAS SLs Oss 7 gt gt gt samples 0 sensor_code TAU gt gt gt samples 0 VAR le OF 0S7 Multiple fields at once gt gt gt samples position value Irra O sO IO gt Hole Oa o JOR eh SCI Ooi Co 10h iy 2r OSN y d eype I C posit ioni EEren Cva luei E E Fancy indexing works as usual gt gt gt samples samples sensor_code ALFA array C ALBEA LS 0SN VALPA 3 0 orn dtype I C sensor eode SAD C position EES value ESN Note There are a bunch of other syntaxes for constructing structured arrays see here and here maskedarray dealing with propagation of missing data e For floats one could use NaN s but masks work for all types gt gt gt x np ma array Il 2 3 4 mask 0 il WO I gt gt gt x masked_array data 1 3 mask False True False True fill_value 999999 gt gt gt y np ma array ll 2 3 4 mask 0 1 1 1 gt gt gt X y masked_array data 2 mask False True True True
227. eta euroscipy 2013 Manipulating data data isa pandas DataFrame that resembles R s dataframe gt gt gt data shape 40 rows and 8 columns 40 8 gt gt gt data columns It has columns indesx usUnmansd Olu Gendeia ul HSTO su Oly un PLO ue Welcihtic ee ANRT Count dt gt gt gt print data Gender Columns can be addressed by name 0 Female il Male 2 Male 3 Male 4 Female gt gt gt Simpler selector gt gt gt data data Gender Female VIQ mean 109 45 Note For a quick view on a large dataframe use its describe method pandas DataFrame describe groupby splitting a dataframe on values of categorical variables gt gt gt groupby_gender data groupby Gender gt gt gt for gender value in groupby_gender VIQ ee print gender value mean Female 109 45 Maler M5 25 groupby_gender is a powerful object that exposes many operations on the resulting group of dataframes gt gt gt groupby_gender mean Unnamed 0 SLO VIQ RIO Weight Height MRI_Count Gender Female 9265 Lilo 109 45 110 45 L37 200000 65765000 862654 6 Male 25 ao IS 0 tila 25 PGO T TEG MAAA Pilea sis 7s 954855 4 Tip Use tab completion on groupby_gender to find more Other common grouping functions are median count useful for checking to see the amount of missing values in different subsets or sum Groupby evaluation is lazy no work is done u
228. etter prediction performance on the digits dataset Raw image K means quantization 3 6 3 Clustering grouping observations together Given the iris dataset if we knew that there were 3 types of iris but did not have access to their labels we could try unsupervised learning we could cluster the observations into several groups by some criterion 3 6 scikit learn machine learning in Python 373 3 6 scikit learn machine learning in Python 374 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 3 6 4 Dimension Reduction with Principal Component Analysis 3 6 5 Putting it all together face recognition An example showcasing face recognition using Principal Component Analysis for dimension reduction and Sup port Vector Machines for classification The cloud of points spanned by the observations above is very flat in one direction so that one feature can almost be exactly computed using the 2 other PCA finds the directions in which the data is not flat and it can reduce the dimensionality of the data by projecting on a subspace gt gt gt from sklearn import decomposition gt gt gt pca decomposition PCA n_components 2 gt pea iiw anion data PCA copy True n_components 2 whiten False gt gt gt X pca transform iris data UELLE Stripped down version of the face recognition example by Oliv
229. example singular value decomposition SVD gt gt gt arr np arange 9 reshape 3 3 np diag 1 0 1 gt gt gt uarr spec vharr linalg svd arr The resulting array spectrum is 1 5 Scipy high level scientific computing 105 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt spec erre 145869825447 0 45294236 0 29654 967 The original matrix can be re composed by matrix multiplication of the outputs of svd with np dot gt gt gt sarr np diag spec gt gt gt svd_mat uarr dot sarr dot vharr gt gt gt np allclose svd_mat arr True SVD is commonly used in statistics and signal processing Many other standard decompositions QR LU Cholesky Schur as well as solvers for linear systems are available in scipy linalg 1 5 4 Fast Fourier transforms scipy fftpack The scipy fftpack module allows to compute fast Fourier transforms As an illustration a noisy input signal may look like gt gt gt time_step 0 02 22 PEro gt gt gt time_vec np arange 0 20 time_step gt gt gt sig np sin 2 np pi period L time vec O 5 np random randn time_vec size The observer doesn t know the signal frequency only the sampling time step of the signal sig The signal is supposed to come from a real function so the Fourier transform will be sym metric The scipy fftpack fftfreq function will generate the sampling frequencies and sci
230. f io erray lh Ly CTN 4 Which years any of the populations is above 50000 Hint comparisons and np any 5 The top 2 years for each species when they had the lowest populations Hint argsort fancy indexing 6 Compare plot the change in hare population see help np gradient and the number of lynxes Check correlation see help np corrcoef all without for loops Solution Python source file Crude integral approximations b Write a function f a b c that returns a c Form a 24x12x6 array containing its values in parameter ranges 0 1 x 0 1 x 0 1 Approximate the 3 d integral i pl pl f f f a c da db dc o Jo Jo over this volume with the mean The exact result is In 2 5 x 0 1931 what is your relative error Hints use elementwise operations and broadcasting You can make np ogrid give a number of points in given range with np ogrid 0 1 20 4 Reminder Python functions 1 3 NumPy creating and manipulating numerical data 79 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 def f a b OF return some_result Solution Python source file Mandelbrot set 1 5 1 0 0 5 1 5 2 0 1 5 1 0 0 5 0 0 0 5 1 0 Write a script that computes the Mandelbrot fractal The Mandelbrot iteration N_max 50 some_threshold 50 c x 1lj y for j in xrange N_max i wry a Point x y belongs to the Mandelbrot set if
231. f np array 0 0 cons constraint accay 1225009622 Ose2e990s76 Warning The above problem is known as the Lasso problem in statistics and there exists very efficient solvers for it for instance in scikit learn In general do not use generic solvers when specific ones exist Lagrange multipliers If you are ready to do a bit of math many constrained optimization problems can be converted to non constrained optimization problems using a mathematical trick known as Lagrange multipliers 2 8 Interfacing with C Author Valentin Haenel 2 8 Interfacing with C 252 L 5 L Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 This chapter contains an introduction to the many different routes for making your native code primarily C C available from Python a process commonly referred to wrapping The goal of this chapter is to give you a flavour of what technologies exist and what their respective merits and shortcomings are so that you can select the appropriate one for your specific needs In any case once you do start wrapping you almost certainly will want to consult the respective documentation for your selected technique Chapters contents Introduction page 253 Python C Api page 254 Ctypes page 258 SWIG page 261 Cython page 265 Summary page 269 Further Reading and References page 269 Exercises page 269 2 8 1 Introduction This chapter cover
232. f array Last but not least two small warnings e All of these techniques may crash segmentation fault the Python interpreter which is usually due to bugs in the C code e All the examples have been done on Linux they should be possible on other operating systems 2 8 Interfacing with C 253 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e You will need a C compiler for most of the examples 2 8 2 Python C Api The Python C API is the backbone of the standard Python interpreter a k a CPython Using this API it is possible to write Python extension module in C and C Obviously these extension modules can by virtue of language compatibility call any function written in C or C When using the Python C API one usually writes much boilerplate code first to parse the arguments that were given to a function and later to construct the return type Advantages e Requires no additional libraries e Lots of low level control e Entirely usable from C Disadvantages e May require a substantial amount of effort e Much overhead in the code e Must be compiled e High maintenance cost e No forward compatibility across Python versions as C API changes e Reference count bugs are easy to create and very hard to track down Note The Python C Api example here serves mainly for didactic reasons Many of the other techniques actually depend on this so it is good to have a high level understanding of how
233. f the form ay dt rhs yl Y2r eer ED was As an introduction let us solve the ODE dy dt 2y betweent 0 4 withthe initial condition y t 0 1 First the function computing the derivative of the position needs to be defined gt gt gt def calc_derivative ypos time counter_arr counter_arr 1 5 Scipy high level scientific computing 117 gt gt gt counter array 129 dtype uint16 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 return 2 x ypos An extra argument counter_arr has been added to illustrate that the function may be called several times for a single time step until solver convergence The counter array is defined as gt gt gt counter np zeros 1 dtype np uint16 The trajectory will now be computed gt gt gt from scipy integrate import odeint gt gt gt time_vec np linspace 0 4 40 gt gt gt yvec info odeint calc_derivative 1 time_vec args counter full_output True Thus the derivative function has been called more than 40 times which was the number of time steps and the cumulative number of iterations for each of the 10 first time steps can be obtained by gt gt gt Hol Vines l LO array Col 23 4o So Seo 59S 6 gt 6S5 S9 dtype InES2 Note that the solver requires more iterations for the first time step The solution yvec for the trajectory can now be plotted 1 0 0 8 0 6
234. f walkers gt gt gt C_inax 200 time during which we follow the walker We randomly choose all the steps 1 or 1 of the walk Seo tc nap arcangel max gt gt gt steps 2 x np random random_integers 0 1 n_stories t_max 1 gt gt gt np unique steps Verification all steps are 1 or 1 array T ii We build the walks by summing steps along the time gt gt gt positions np cumsum steps axis 1 axis 1 dimension of time gt gt gt sq distance positions 2 We get the mean in the axis of the stories gt gt gt mean_sq distance np mean sq _ distance axis 0 Plot the results gt gt gt plt figure figsize 4 3 lt Mmatplotlip figqgure Figure object at gt f2e jollic p kott mp Sect MSsin_ SCL distance Gov e mMe SCiee ee Wwe lt matplotlibs lines Line2D object at 2 2 5 lt matplotlib lines Line2D object at 22 ees pit xlabel 2 sro lt matplot hibri text text ob ject at gt gt gt gt plt ylabel r S sqgrt langle Adelta x 2 rangle amatplot lib text Text objecte at 21 2 16 14 12 10 N i 2 Nr x or N gating and manipulating numerical data 61 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Broadcasting e Basic operations on numpy arrays addition etc are elementwise e This works on arrays of the same size Nevertheless It s also possible
235. feature appeared in the language almost apologetically and with concern that it might not be that useful Bruce Eckel An Introduction to Python Decorators Since a function or a class are objects they can be passed around Since they are mutable objects they can be modified The act of altering a function or class object after it has been constructed but before is is bound to its name is called decorating There are two things hiding behind the name decorator one is the function which does the work of decorating i e performs the real work and the other one is the expression adhering to the decorator syntax i e an at symbol and the name of the decorating function Function can be decorated by using the decorator syntax for functions decorator def function pass e A function is defined in the standard way 2 1 Advanced Python Constructs 144 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e An expression starting with placed before the function definition is the decorator The part after must be a simple expression usually this is just the name of a function or class This part is evaluated first and after the function defined below is ready the decorator is called with the newly defined function object as the single argument The value returned by the decorator is attached to the original name of the function Decorators can be applied to functions and to classes
236. fine context managers page 154 139 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 2 1 1 lterators generator expressions and generators lterators Simplicity Duplication of effort is wasteful and replacing the various home grown approaches with a standard feature usually ends up making things more readable and interoperable as well Guido van Rossum Adding Optional Static Typing to Python An iterator is an object adhering to the iterator protocol basically this means that it has a next method which when called returns the next item in the sequence and when there s nothing to return raises the StopIteration exception An iterator object allows to loop just once It holds the state position of a single iteration or from the other side each loop over a sequence requires a single iterator object This means that we can iterate over the same sequence more than once concurrently Separating the iteration logic from the sequence allows us to have more than one way of iteration Calling the __iter__ method on a container to create an iterator object is the most straightforward way to get hold of an iterator The iter function does that for us saving a few keystrokes gt gt gt nums 1 2 3 note that varies these are different objects gt gt gt iter nums Koo Ee rat Or Ope ct Be 6655 gt gt gt nums _ iter_ Sooo eera On rODI SCA gt gt
237. fixed size E OO One i See also wavreader py gt gt gt wav_header_dtype format dtype S4 gt gt gt wav_header_dtype fields GUC iE MmOIE say Onloel konica elieyOSr Cu EMEC Ms OA i ormat GE VOC CSA D o adatan gt gt gt wav_header_dtype fields format dtype s4 8 e The first element is the sub dtype in the structured data corresponding to the name format e The second one is its offset in bytes from the beginning of the item 2 2 Advanced Numpy 159 dtype Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercise Mini exercise make a sparse dtype by using offsets and only some of the fields gt gt gt wav_header_dtype np dtype dict names format sample_rate data_id offsets offset_1 offset_2 offset_3 counted from start of structure iin b formats list of dtypes for each of the fields and use that to read the sample rate and data_id as sub array gt gt gt f open data test wav r gt gt gt wav_header np fromfile f dtype wav_header_dtype count 1 gt gt gt f close gt gt gt print wav_header MORTER I7yaOe2in Vindwav Yisate Lol il il L6OW00m S2000n 2 Ne 1 el gt gt gt gt wav_header sample_rate array 16000 dtype uint32 Let s try accessing the sub array gt gt gt wav_header data_id array Ti ds Sal Ree ert Ili dtype
238. fprime fprime emey I LA000C000S LeO000UOUS LPe44lfe7747s0lles9e 15 122 3 Note If you do not specify the gradient to the L BFGS solver you need to add approx_grad 1 Gradient less methods A shooting method the Powell algorithm Almost a gradient approach 2 7 Mathematical optimization finding minima of functions 246 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 An ill conditionned quadratic function Powell s method isn t too sensitive to local ill conditionning in low dimensions function calls 10 0 5 10 15 20 25 30 35 40 An ill conditionned very non quadratic function 0 0 50 100 150 200 250 Simplex method the Nelder Mead The Nelder Mead algorithms is a generalization of dichotomy approaches to high dimensional spaces The al gorithm works by refining a simplex the generalization of intervals and triangles to high dimensional spaces to bracket the minimum Strong points it is robust to noise as it does not rely on computing gradients Thus it can work on functions that are not locally smooth such as experimental data points as long as they display a large scale bell shape behavior However it is slower than gradient based methods on smooth non noisy functions An ill conditionned non quadratic function Error on f x a f iterations 107 l function calls 1 f 50 100 150 200 25
239. from sklearn import linear_model gt gt gt regr linear_model Lasso alpha 3 gt gt gt regr fit diabetes_X_train diabetes_y_train passom gt gt gt regr coef_ very sparse coefficients avatar i O 7 AS7 S407S682 199 17441034 0 7 m l 899291545 o r 430 9379595 4 OF gt gt gt regr score diabetes_X_test diabetes_y_test SSIOSS549366 being the score very similar to linear regression Least Squares gt gt gt lin linear_model LinearRegression gt gt gt lin fit diabetes_X_train diabetes_y_train LinearRegression gt gt gt lin score diabetes_X_test diabetes_y_test ORS eS 0755022 2 3 6 scikit learn machine learning in Python 377 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Different algorithms for a same problem Different algorithms can be used to solve the same mathematical problem For instance the Lasso object in the sklearn solves the lasso regression using a coordinate descent method that is efficient on large datasets However the sklearn also provides the LassoLARS object using the LARS which is very efficient for prob lems in which the weight vector estimated is very sparse that is problems with very few observations 3 6 7 Model selection choosing estimators and their parameters Grid search and cross validated estimators Grid search The scikit learn provides an object that given data computes the score d
240. g minima of functions 249 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercice A locally flat minimum Consider the function exp 1 1 x 2 yxx 2 This function admits a minimum in 0 0 Starting from an initialization at 1 1 try to get within le 8 of this minimum point 2 7 4 Special case non linear least squares Minimizing the norm of a vector function Least square problems minimizing the norm of a vector function have a specific structure that can be used in the Levenberg Marquardt algorithm implemented in scipy optimize leastsq Lets try to minimize the norm of the following vectorial function gt gt gt def f x return np arctan x np arctan np linspace 0 1 len x gt gt gt x0 np zeros 10 gt gt gt optimize leastsq f x0 lares I 0 OS LILLI O 22222222 O 33333333 0 44444444 0 55555555 O 6656665667 O 77777778 OMSISISSIS SISO ly 2 This took 67 function evaluations check it with full_output 1 What if we compute the norm ourselves and use a good generic optimizer BFGS gt gt gt def g x act return np sum f x 2 gt gt gt Optimize imim bigsig x0 Optimization terminated successfully Current function value 0 000000 Iterations 11 Function evaluations 144 Gradient evaluations 12 array mA 45 09 Ulee SS Ou 2 2 e O0l SEs a Oly AA 5 e011 Se orree
241. ge 365 x Transforming data filters page 366 x mlab pipeline the scripting layer page 366 Animating the data page 366 Making interactive dialogs page 367 A simple dialog page 367 Making it interactive page 368 Putting it together page 369 x x x x 3 5 1 Mlab the scripting interface The mayavi mlab module provides simple plotting functions to apply to numpy arrays similar to matplotlib or matlab s plotting interface Try using them in IPython by starting Python with the switch gui wx 3 5 3D plotting with Mayavi 357 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 3D plotting functions Points Hint Points in 3D represented with markers or glyphs and optionaly different sizes x Y Z value np random random 4 40 mlab points3d x y z value Lines plot3d Hint A line connecting points in 3D with optional thickness and varying color Imlab clf Clear the figure t np linspace 0 20 200 mabk plot odine alia ic mjo Cos Ce Wallet wt 3 5 3D plotting with Mayavi 358 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Elevation surface surf Hint A surface given by its elevation coded as a 2D array mlas clr x y np moridi 10 10 r00 0 10 roon E jal Sae A sp VA Z np sin r r mlab surf z warp_scale auto Arbitrary regular mesh mesh Hint A s
242. ght clicking on the nodes of the pipeline you can add new modules Quiz Why is it not possible to add a VectorCutPlane to the vectors created by mayavi mlab quiver3d Different sources scatters and fields Tip Data comes in different descriptions e A 3D block of regularly spaced value is structured it is easy to know how one measurement is related to another neighboring and how to continuously interpolate between these We can call such data a field borrowing from terminology used in physics as it is continuously defined in space e A set of data points measured at random positions in a random order gives rise to much more difficult and ill posed interpolation problems the data structure itself does not tell us what are the neighbors of a data point We call such data a scatter Unstructured and unconnected data a scatter Structured and connected data a field mlab points3d mlab quiver3d mlab contour3d Data sources corresponding to scatters can be created withmayavi mlab pipeline scalar_scatter or mayavi mlab pipeline vector_scatter field data sources can be created with mlab pipeline scalar_field ormlab pipeline vector_field Exercice 1 Create a contour for instance of the magnetic field norm by using one of those functions and adding the right module by clicking on the GUI dialog 2 Create the right source to apply a vector_cut_plane and reproduce the picture of the mag
243. ght com projects mayavi 1 1 Scientific computing with tools and workflow 5 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 1 1 3 The interactive workflow IPython and a text editor Interactive work to test and understand algorithms In this section we describe an interactive workflow with Python that is handy to explore and understand algorithms Python is a general purpose language As such there is not one blessed environment to work in and not only one way of using it Although this makes it harder for beginners to find their way it makes it possible for Python to be used to write programs in web servers or embedded devices Reference document for this section IPython user manual http ipython org ipython doc dev index html Command line interaction Start ipython In 1 print Hello world Hello world Getting help by using the operator after an object In 2 print Type builtin function or method Base Class Styoe builtins PMC ion Or MET String VOLNE ounle im Tuner vom print Namespace Pyihon Iie a Doest mimg print value sep end n file sys stdout Prints the values to a stream or to sys stdout by default Optional keyword arguments file a file like object stream defaults to the current sys stdout sep string inserted between values default a space end string appended after the last value default a newli
244. gque Bera JB Zo SSS Skew 07 082 Prob JB G26 aves tO Salis 35 O59 Conc Nor 54 0 3 1 Statistics in Python 283 3 1 Statistics in Python 284 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Look and feel and matplotlib settings Seaborn changes the default of matplotlib figures to achieve a more modern excel like look It does that upon import You can reset the default using gt gt gt from matplotlib import pyplot as plt See Plus receraults Tip To switch back to seaborn settings or understand better styling in seaborn see the relevent section of the seaborn documentation Implot plotting a univariate regression A regression capturing the relation between one variable and another eg wage and eduction can be plotted using seaborn lmplot gt gt gt seaborn 1mplot y WAGE x EDUCATION data data EDUCATION 2 0 0 0 5 0 0 0 5 1 0 15 2 0 10 20 30 40 50 60 70 0 5 10 15 20 WAGE AGE EDUCATION Categorical variables can be plotted as the ie hue gt gt gt seaborn pairplot data vars WAGE AGE EDUCATION kind reg hue SEX 1 0 WAGE 2 0 Effect of gender 1 Female 0 M le 0 5 0 0 0 5 10 15 20 EDUCATION Robust regression Tip Given that in the above plot there seems to be a couple of data points that are outs
245. gt a T 2 gt gt gt xX np random rand 2 2 2 gt gt gt x sum axis 2 0 1 a 1 1476 Note Linear algebra See x 0 1 2 emn 1 1476 The sub module numpy 1inalg implements basic linear algebra such as solving linear systems singular value decomposition etc However it is not guaranteed to be compiled using efficient routines and thus we recommend the use of scipy 1linalg as detailed in section Linear algebra operations scipy linalg page 105 Other reductions Exercise other operations works the same way and take axis e Look at the help for np allclose When might this be useful Extrema e Look at the help fornp triuandnp tril gt gt gt x MO array Il 3 7 21 gt gt gt x min ii Basic reductions aoa S Computing sums gt gt gt x argmin index of minimum 0 gt gt gt amp No array l 27 3 5 4 gt gt gt x argmax index of maximum gt gt gt np sum x il LO gt gt gt x sum Logical operations 10 gt gt gt np all True True False False 1 3 NumPy creating and manipulating numerical data 57 1 3 NumPy creating and manipulating numerical data 58 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt np any True True False True Note Can be used for array comparisons gt gt gt a np zeros 100 100 gt gt gt np any a 0 False gt gt gt np all a
246. gt gt gt a IS D True a in b For any collection b b contains a gt gt gt b lp 2 3 gt gt gt 2 IN D True gt gt gt 5 in b False If b is a dictionary this tests that a is a key of b Advanced iteration literate over any sequence You can iterate over any sequence string list keys in a dictionary lines in a file gt gt gt vowels aeiouy gt gt gt for i in powerful if i in vowels oon print i O e u gt gt gt message Hello how are you gt gt gt message split returns a list RELLO Jioni aret sou gt gt gt for word in message split print word Hello how are you Tip Few languages in particular languages for scientific computing allow to loop over anything but inte gers indices With Python it is possible to loop exactly over the objects of interest without bothering with indices you often don t care about This feature can often be used to make code more readable Warning Not safe to modify the sequence you are iterating over Keeping track of enumeration number Common task is to iterate over a sequence while keeping track of the item number 1 2 The Python language 20 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e Could use while loop with a counter as above Or a for loop gt gt gt words cool powerful readable gt gt gt for i in range 0 len words 5 pr
247. gument types libcd cos_doubles restype None libcd cos_doubles argtypes array_ld_double array_ld_double c_int def cos_doubles_func in_array out_array return libcd cos_doubles in_array out_array len in_array e Note the inherent limitation of contiguous single dimensional Numpy arrays since the C functions requires this kind of buffer e Also note that the output array must be preallocated for example with numpy zeros and the function will write into it s buffer e Although the original signature of the cos_doubles function is ARRAY ARRAY int the final cos_doubles_func takes only two Numpy arrays as arguments And as before we convince ourselves that it worked import numpy as np import pylab import cos_doubles x np arange 0 2 x np pi 0 1 y np empty_like x cos_doubles cos_doubles_func x y jonvllellsy oll Ox w pylab show 2 8 4 SWIG SWIG the Simplified Wrapper Interface Generator is a software development tool that connects programs written in C and C with a variety of high level programming languages including Python The important thing with SWIG is that it can autogenerate the wrapper code for you While this is an advantage in terms of development time it can also be a burden The generated file tend to be quite large and may not be too human readable and the multiple levels of indirection which are a result of the wrapping process may be a bit tricky to understand
248. guments of the function are given between parentheses followed by a colon e the function body e and return object for optionally returning values Parameters Mandatory parameters positional arguments In 81 def double_it x cette return x 2 In 82 double_it 3 inte bee 16 In 83 double_it Traceback most recent call last File lt stdin gt line 1 in lt module gt TypeError double_it takes exactly 1 argument 0 given Optional parameters keyword or named arguments In 84 def double_it x 2 TR return x 2 In 85 double_it put lesj 4 In 86 double_it 3 Outes 6 Keyword arguments allow you to specify default values 1 2 The Python language 22 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Warning Default values are evaluated when the function is defined not when it is called This can be problematic when using mutable types e g dictionary or list and modifying them in the function body since the modifications will be persistent across invocations of the function Using an immutable type in a keyword argument In 124 bigx 10 In 125 def double_it x bigx Persa ie return x gt 2 In 126 bigx le9 Now really big In 128 double_it O e 26 20 Using an mutable type in a keyword argument and modifying it inside the function body In 2 def add to dict args tat 1 TDi 2J for i in args key
249. he Numpy array type to your Cython code Le like specifying that variable i is of type int you can specify that variable a is of type numpy ndarray with a given dtype Also certain optimizations such as bounds checking are supported Look at the corresponding section in the Cython documentation In case you want to pass Numpy arrays as C arrays to your Cython wrapped C functions there is a section about this in the Cython wiki In the following example we will show how to wrap the familiar cos_doubles function using Cython void cos_doubles double x in_array double out_array int size inelude lt match R gt x Compute the cosine of each element in in_array storing the result in 2 8 Interfacing with C 267 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 a Oue arrayo void cos_doubles double x in_array double out_array int size int i for i1 0 i lt size itt out_array i cos in_array i This is wrapped as cos_doubles_func using the following Cython code net ESanple OF Wrapping aC funceion thal Cakes C double arrays as inpur using the Numpy declarations from Cython cimport the Cython declarations for numpy cimport numpy as np if you want to use the Numpy C API from Cython not strictly necessary for this example but good practice np import_array cdefine the signature of our c function cdef extern from cos _doubles h void cos_doub
250. he fact that the optimizer avoided the local minimum is a matter of luck gt gt gt from scipy import optimize gt gt gt def f x Fa return np exp x s2 gt gt gt x_min optimize brent f It actually converges in 9 iterations gt gt gt x min T 699999999 a gt gt gt X MN 2 1605 eTO Note Brent s method can also be used for optimization constrained to an interval using scipy optimize fminbound Note In scipy 0 11 scipy optimize minimize_scalar gives a generic interface to 1D scalar mini mization Gradient based methods Some intuitions about gradient descent Here we focus on intuitions not code Code will follow Gradient descent basically consists in taking small steps in the direction of the gradient that is the direction of the steepest descent Table 2 2 Fixed step gradient descent ar 0O 20 40 60 80 100 A well conditionned quadratic function 4 o 50 100 150 200 An ill conditionned quadratic function The core problem of gradient methods on ill conditioned problems is that the gradient tends not to point in the direction of the minimum We can see that very anisotropic ill conditionned functions are harder to optimize 2 7 Mathematical optimization finding minima of functions 242 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Take home message conditioning number and preconditio
251. he for loop Straightforward iteration mandel PyUFunc_FromFuncAndData UGC _ EWING f ka 1007 elementwise_funcs or in range i ic ile E ic 7 Z Z E eee If z realko z maag gt 1000 1 number of supported input types e TODO number of Input args TODO number of output args 0 identity element never mind this mandel function name mandellii e gt computes z z ec docst ring O unused Return the answer for this point z outlO z Boilerplate Cython definitions You don t really need to read this part it just pulls in stuff from the Numpy C headers Reminder some pre made Ufunc loops PyUfunc_f_f float elementwise_func float input_1 PyUfunc_ff ffloat elementwise_func float input_1l float input_2 cdef extern from numpy arrayobject h void import_array ctypedef int npy_intp cdef enum NPY_TYPES PyUfunc_d_d double elementwise_func double input_1 PyUfunc_dd Mouble elementwise_func double input_1 double input_2 PyUfunc_D elementwise_func complex_double xinput complex_doublex output NP Y_CDOUBLE PyUfunc_DD lementwise_func complex_double inl complex_double x in2 complex_doublex out cdef extern from numpy ufuncobject h VOLG IMpory urune Type codes i i ctypedef void PyUFuncGenericFunction Charx npy_intp npy_intp Vords NPY_BOOL NPY_BYTE NPY_UBYTE NPY_SHORT NPY_USHORT NPY_INT
252. he full name of the function and __ annotations___ extra information about arguments and the return value of the function available in Python 3 This can be done automatically by using functools update_wrapper Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 functools update_ wrapper wrapper wrapped Update a wrapper function to look like the wrapped function gt gt gt import functools gt gt gt def better_replacing_decorator_with_args arg print defining the decorator def _decorator function print doing decoracion sr s arg def _wrapper xargs xxkwargs print inside wrapper ar r s args kwargs return function xargs xkwargs return functools update_wrapper _wrapper function return _decorator gt gt gt better_replacing decorator_with_args abc def function extensive documentation print inside function 5 return 14 defining the decorator doimgi decoration kabei gt gt gt function lt FUNTE HOn EUnet onae Oxo 5 gt gt gt print function doe extensive documentation 2 1 Advanced Python Constructs 147 One important thing is missing from the list of attributes which can be copied to the replacement func tion the argument list The default values for arguments can be modified through the __defaults__ __kwdefaults__ attributes but unfortunately the argument list itself cannot be set as an attribute This m
253. hon Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt print mtx e can be efficiently converted to a coo_matrix once constructed a A ae e slow arithmetics for loops with dict iteritems Oy 2 tee e use Gj Se 7 ask G2 eo when sparsity pattern is not known apriori or changes gt gt gt mtx todense mercies 1 Oo dhe iiy Eor OI Daten Ba Wis allen Bll Examples O O O O O 1 e create a DOK matrix element by element Ghee Or Oh er Ose gt gt gt Mex toarray gt gt gt mtx sparse dok_matrix 5 5 dtype np float64 arraya Oy Weg he se 3Olee gt gt gt mtx L Oo ees Osp T Daly Go Kom sparse matrix or HE ViOCm Gene MMIND Ve e loar oane On Oy Oy cee Ord with 0 stored elements in Dictionary Of Keys format gt iv Olen ORP o Ore Gah gt gt gt for ir in range 5 for ic in range 5 e more slicing and indexing os mi e ne OP ite ae gt gt gt mtx 2 gt gt mex sparse lal matrix 0 1 2 Ol 13 0 wy Ol It 0 0 MD 5x5 Sparse Matrix Of type lt ac numpy tloate4 gt gt gt gt mtx todense with 20 stored elements in Dictionary Of Keys format gt matrix 0 1 2 O gt gt gt mtx todense 3 0 1 O Meee Ws Wer Ie Were dhe I DE Ot Oza AE Da ae Oem pile ins Aner all ells gt gt gt print mtx lle E Oo Sil whee
254. i I TEVS LAGo ESC 1 faile ion rst except rons 2S workflow 2st tindex rst And also In 4 from os import listdir Importing shorthands In 5 import numpy as np Warning from os import x This is called the star import and please Use it with caution e Makes the code harder to read and understand where do symbols come from Makes it impossible to guess the functionality by the context and the name hint os name is the name of the OS and to profit usefully from tab completion Restricts the variable names you can use os name might override name or vise versa Creates possible name clashes between modules Makes the code impossible to statically check for undefined symbols Tip Modules are thus a good way to organize code in a hierarchical way Actually all the scientific computing tools we are going to use are modules gt gt gt import numpy as np data arrays eer Np lanepace 07 10iy e array il Ula oy Als Oar Bop ONN gt gt gt import scipy scientific computing In Python x y Ipython x y executes the following imports at startup gt gt gt import numpy gt gt gt import numpy as np gt gt gt from pylab import x gt gt gt import scipy and it is not necessary to re import these modules 1 2 The Python language 28 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Creating modules Tip If we want to write larger a
255. i Scene IF ee Pipeline gt Scalar LUT Vector LUT ModuleManager PrINHRMHOHS v Mayavi Scene 1 wr Look Up Table Manager v GridSource v PolyDataNormals Lut mode V amp Colors and legends t Surface Number of colors 256 Outline Reverse lut 7 T Axes Edit LUT properties Suggestions y AUREA Launch LUT editor v PolyDataNormals Show legend Q n Surface eg s MW Title Number of labels 0 64 8 Shadow C Use default name 4 Data name e If you compute the norm of the vector field you can apply an isosurface to it Edit bar Title Edit bar Text Edit bar Actor Edit bar Widget e using mayavi mlab quiver3d you can plot vectors You can also use the masking options in the GUI to make the plot a bit less dense e Set the background of the figure in the Mayavi Scene node Different views on data sources and modules e Set the colormap in the Colors and legends node Tip As we see above it may be desirable to look at the same data in different ways e Right click on the node to add modules or filters modules Mayavi visualization are created by loading the data ina data source and then displayed on the screen using 3 5 3D plotting with Mayavi 363 3 5 3D plotting with Mayavi 364 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 This can be seen by looking at the pipeline view By ri
256. i Waca Cor A SyntaxEr rror inya lil syntax The newline character is n and the tab character is t Tip Strings are collections like lists Hence they can be indexed and sliced using the same syntax and rules Indexing gt gt gt a hello gt gt gt a l0 U al gt gt gt all UAN gt gt gt al n TOR Tip Remember that negative indices correspond to counting from the right end Slicing gt gt gt a hello world gt gt gt a 3 6 3rd to 6th excluded elements elements 3 4 5 lo 1 2 The Python language 15 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 eer ales Oe joymtaxs ae iebaris stops srep ALOT Oe gt gt gt a 3 every three characters from beginning to end oll e Tip Accents and special characters can also be handled in Unicode strings see http docs python org tutorial introduction html unicode strings A string is an immutable object and it is not possible to modify its contents One may however create new strings from the original one In 53 a hello world In 54 a 2 z Traceback most recent call last Maile nt stdin n lane iy aim module TypeError str object does not support item assignment In 55 a replace Iry zt 1 out losl hex le vVorcloln In 56 a replace 1L T2 Out Isl hezzo worzd Tip Strings have many useful methods such as a replace as see
257. ic head m efficiency Range 0 1 def energy_production self release Returns the energy production Wh for the given release m3 s bu ie i power 1000 x 9 81 self head release x self efficiency return power x 3600 if name main __ reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 release 80 print Releasing m s produces kWh format release reservoir energy_production release Visualization opening a dialog The Traits library is also aware of user interfaces and can pop up a default view for the Reservoir class 3 4 Traits building interactive dialogs 343 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 reservoirl Reservoir reservoirl edit_traits Fa Pome Pome Edit properties Efficiency 0 0 4 1 0 0 8 Head 10 Hydraulic head 60 Max release 100 0 Max storage 30 0 Name Project A OK Cancel TraitsUI simplifies the way user interfaces are created Every trait on a HasTraits class has a default editor that will manage the way the trait is rendered to the screen e g the Range trait is displayed as a slider etc In the very same vein as the Traits declarative way of creating classes TraitsUI provides a declarative interface to build user interfaces code from traits api import HasTraits Str Float Range from traitsui api i
258. ich may be empty tells Python that the directory is a Python package from which modules can be imported eo ks cluster io README txt stsei COMEC y LATEST txt setup py __svn_version__ py S Comipig Oye Lio setup pyc __svn_version__ pyc constants dain Lee setupscons py THANKS txt fftpack linsolve setupscons pyc TOCHANGE txt i ye maxentropy signal version py Sime eye misc sparse version pyc GN Sa Aine Kee ndimage spatial weave integrate odr special interpolate optimize stats cd ndimage a is doccer py fourier pyc imkerpoOlation pye morphology pyc Setup EYE doccer pyc info py interpolation pyc _nd_image so setupscons py Mittens Pye into PYE measurements py _ni_support py getu upscons pyc filters pyc __init__ py measurements pyc _ni_support pyc tests fourier py _ _init__ pyc morphology py setup py From Ipython In 1 import scipy In 2 scipy stile _ Out ee aus r labo pyle hon2 o direct packages scipy nmi ipe In 3 import scipy version In 4 scipy version version Cue Pa von 20 In 5 import scipy ndimage morphology In 6 from scipy ndimage import morphology In 17 morphology binary_dilationf Type FUneEe ron 1 2 The Python language 32 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Base Class Stype functeiron gt Sicieiine MO lt function binary dilation at 0x9bedd84 gt Namespac
259. ide of the main cloud to the right they might be outliers not representative of the population but driving the regression EDUCATION To compute a regression that is less sentive to outliers one must use a robust model This is done in seaborn Qh hcl a wee ae ee using robust True in the plotting functions or in statsmodels by replacing the use of the OLS by a 0 5 0 0 0 5 10 15 2 0 10 20 30 40 50 60 70 O 5 10 15 20 i x lt 5 WAGE AGE EDUCATION Robust Linear Model statsmodels formula api rlm 3 1 Statistics in Python 285 3 1 Statistics in Python 286 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 3 1 5 Testing for interactions 2 0 o D gender g e female e male 0 0 2 0 5 0 5 10 15 20 education Do wages increase more with education for males than females Tip The plot above is made of two different fits We need to formulate a single model that tests for a variance of slope across the to population This is done via an interaction gt gt gt result sm ols formula wage education gender education gender he data data fit gt gt gt print result summary coef std err ic P gt t 95 0 Confie Line Intercept 0 2998 On OYZ Acids OOOO ORES 9 Omran gender T male OR S10 0 09S 25 O2 0 005 0 09S 0 457 education ORO IES 0 005 ESAN O OOO 0 OST 0 052 education gender T male 0 014 0 007 1 919 CROs 0 027 0 000
260. ides e Universal functions elementwise operations how to make new ones e Ndarray subclasses e Various buffer interfaces for integration with other tools e Recent additions PEP 3118 generalized ufuncs 2 2 6 Contributing to Numpy Scipy Get this tutorial http www euroscipy org talk 882 Why There s a bug e I don t understand what this is supposed to do I have this fancy code Would you like to have it Td like to help What can I do Reporting bugs e Bug tracker prefer this http projects scipy org numpy http projects scipy org scipy Click the Register link to get an account e Mailing lists scipy org Mailing Lists If you re unsure No replies in a week or so Just file a bug ticket 2 2 Advanced Numpy 184 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Good bug report Title numpy random permutations fails for non integer arguments I m trying to generate random permutations using numpy random permutations When calling numpy random permutation with non integer arguments it fails with a cryptic error message gt gt gt Tip random permutation 12 array die os le ae te E Soy A eS COE Sa 2 o gt np random permutation 12 Traceback most recent call last Bilek stdin kine i ain lt noduike gt File mtrand pyx line 3311 in mtrand RandomState permutation File mtrand pyx line 3254 in mtrand Rand
261. ier Grisel Now we can visualize the transformed iris dataset http scikit learn org dev auto_examples applications face_recognition html gt gt gt import pylab as pl aoe Pl scatter hs 0 kis llr C iris target original shape of images 3O 7 lt matplot libr collect iOms n Co heklet ion mol ect at aa nun import numpy as np import pylab as pl from sklearn import cross_val datasets decomposition svm E ex A ou ead daca lfw_people datasets fetch_lfw_people min_faces_per_person 70 resize 0 4 perm np random permutation lfw_people target size lfw_people data lfw_people data perm lfw_people target lfw_people target perm faces np reshape lfw_people data lfw_people target shape 0 1 train test iter cross_val StratifiedKFold lfw_people target k 4 next X_train X_test faces train faces test y_train y_test lfw_people target train lfw_people target test dimension reduction PCA is not just useful for visualization of high dimensional datasets It can also be used as a preprocessing step pca decomposition RandomizedPCA n_components 150 whiten True to help speed up supervised methods that are not efficient with high dimensions Peart ity eran Xe pea OC Ame Salis Onaiin OGateteceiny X test pea pca transform X_test 3 6 scikit learn machine learning in Python 375 3 6 scikit learn machine learning in Python 376 Python Scientific lecture no
262. ific lecture notes Release 2013 2 beta euroscipy 2013 1 0 0 8 _ a gt pe a Z g 0 6 O w 3 Vv 2 4 4 Ww 5 0 4 v 5 i U 5 Z Z lt 0 2 Veg 32 98 m s 05 22 24 26 28 30 32 34 Annual wind speed maxima m s Year Figure 1 1 Solution Python source file Figure 1 2 Solution Python source file 1 5 Scipy high level scientific computing 127 1 5 Scipy high level scientific computing 128 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e The first step will be to find the annual maxima by using numpy and plot them as a matplotlib bar figure e The second step will be to use the Gumbell distribution on cumulative probabilities p_i defined as 1log log p_i for fitting a linear quantile function remember that you can define the degree of the UnivariateSpline Plotting the annual maxima versus the Gumbell distribution should give you the following figure Gumbell cumulative probability Veg 34 23 m s K 20 25 30 35 40 45 Annual wind speed maxima m s Figure 1 3 Solution Python source file e The last step will be to find 34 23 m s for the maximum wind speed occurring every 50 years Non linear least squares curve fitting application to point extraction in topographical lidar data The goal of this exercise is to fit a model to some data The data used in this tutorial are lidar data and are described in details in the
263. igh and low frequency components The noise is contained in the high frequency part of the spectrum so set some of those components to zero use array slicing 5 Apply the inverse Fourier transform to see the resulting image 1 5 5 Optimization and fit scipy optimize Optimization is the problem of finding a numerical solution to a minimization or equality The scipy optimize module provides useful algorithms for function minimization scalar or multi dimensional curve fitting and root finding gt gt gt from scipy import optimize Finding the minimum of a scalar function Let s define the following function gt gt gt def f x return x 2 10xnp sin x and plot it gt gt gt x np arange 10 10 O 1 Pee pli plot x gt gt gt plt show 1 5 Scipy high level scientific computing 109 1 5 Scipy high level scientific computing 110 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 120 100 80 60 40 20 20 10 5 0 5 10 This function has a global minimum around 1 3 and a local minimum around 3 8 The general and efficient way to find a minimum for this function is to conduct a gradient descent starting from a given initial point The BFGS algorithm is a good way of doing this gt gt gt Optimi zen Eminas e O Optimization terminated successfully Current function valus 7 9453823 Teerat lons TS Function evalu
264. ilt in With each release Python provides support in more places e all file like objects file automatically closed fileinput tempfile py gt 3 2 bz2 BZ2File gzip GzipFile tarfile TarFile zipfile ZipFile ftplib nntplib close connection py gt 3 2 or 3 3 locks multiprocessing RLock lock and unlock multiprocessing Semaphore memoryview automatically release py gt 3 2 and 2 7 e decimal localcontext modify precision of computations temporarily e winreg PyHKEY open and close hive key e warnings catch_warnings kill warnings temporarily e contextlib closing the same as the example above call close parallel programming concurrent futures ThreadPoolExecutor invoke in parallel then kill thread pool py gt 3 2 concurrent futures ProcessPoolExecutor invoke in parallel then kill process pool py gt 3 2 nogil solve the GIL problem temporarily cython only Catching exceptions When an exception is thrown in the with block it is passed as arguments to__e x it___ Three arguments are used the same as returned by sys exc_info type value traceback When no exception is thrown None is used for all three arguments The context manager can swallow the exception by returning a true value from ___exit__ Exceptions can be easily ignored because if __e xit___ doesn t use return and just falls of the end None is returned
265. ima of functions Authors Ga l Varoquaux Mathematical optimization deals with the problem of finding numerically minimums or maximums or zeros of a function In this context the function is called cost function or objective function or energy Here we are interested in using scipy optimize for black box optimization we do not rely on the mathe matical expression of the function that we are optimizing Note that this expression can often be used for more efficient non black box optimization Prerequisites e Numpy Scipy e Python e matplotlib References Mathematical optimization is very mathematical If you want performance it really pays to read the books e Convex Optimization by Boyd and Vandenberghe pdf available free online e Numerical Optimization by Nocedal and Wright Detailed reference on gradient descent methods e Practical Methods of Optimization by Fletcher good at hand waving explainations Chapters contents e Knowing your problem page 239 Convex versus non convex optimization page 240 Smooth and non smooth problems page 240 Noisy versus exact cost functions page 241 Constraints page 241 e A review of the different optimizers page 241 Getting started 1D optimization page 241 Gradient based methods page 242 Newton and quasi newton methods page 244 Gradient less methods page 246 Global optimizers pag
266. iment the richness of Python is an invaluable asset Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Contents e Data representation and interaction page 272 Data as a table page 272 The panda data frame page 272 e Hypothesis testing comparing two groups page 276 Student s t test the simplest statistical test page 277 Paired tests repeated measurements on the same indivuals page 278 e Linear models multiple factors and analysis of variance page 279 formulas to specify statistical models in Python page 279 Multiple Regression including multiple factors page 282 Post hoc hypothesis testing analysis of variance ANOVA page 284 e More visualization seaborn for statistical exploration page 284 Pairplot scatter matrices page 284 Implot plotting a univariate regression page 286 e Testing for interactions page 287 e Full examples page 288 Tip In this document the Python inputs are represented with the sign gt gt gt Disclaimer Gender questions Some of the examples of this tutorial are chosen around gender questions The reason is that on such questions controlling the truth of a claim actually matters to many people 3 1 1 Data representation and interaction Data as a table The setting that we consider for statistical analysis is that of multiple observations or samples described by a set of different attribut
267. in the debugger 8 Ox080ddd23 in call function f Frame 0x8537lec for file home varoquau usr lib python2 6 site packages numpy core arrayprin adti Byt hon ceva Me S 750 Type h or help to access the interactive help 3750 sa Python ec val c No such tile or direcuory in Python ceval c ipdb gt help gdb up D ted d help lt t RES S ocumented commands type help OpLe gt ko pono aynan FOR oe Arcee aee Aunp ace 7 ek Frame 0x85371ec for fie home varoquau usr lib python2 6 site packages numpy core arrayprin k at s Python ceval c12412 a continue exit l pdoc restart whatis i i P ete ON Lm Pyehon ceva lke alias El d h list pinto return unalias where dE amS clear debug help n pp run unt g b commands disable ignore next gq S Uiel ne Once we are in the Python execution loop we can use our special Python helper function For instance we can break condition down J Pp quit step up find the corresponding Python code Miscellaneous help topics gdb pyframe home varoquau usr lib python2 6 site packages numpy core arrayprint py 158 _leading_trailing exec pdb gdb Haa a omuaude This is numpy code we need to go up until we find code that we have written retval rv gdb up gdb up 2 3 4 Debugging segmentation faults using gdb PE a a eee eee Ue Frame 0x82f064c for file segfault py line 1
268. ing lt no docstring je Construct the cutpuc from cos from c double co python floac 7 return Py_BuildValue f answer In 3 Eee ene ae 2 Cl 2 u Owell adoece meu aa aen name i l package n Meos functi f d fine functions 2n module 7 static PyMethodDef CosMethods In 4 cos module cos fune 1 0 Gie e 0s S002 SiO sis Geass cos_func cos_func META VARARGS evaluate the cosine NULL NULL 0 NULL in isi cose module cos fune Om 0 7 Ouel Tro In 6 cos Module cos tune 6214159265359 a module sil Ite Mell tele Tom Guti 1 0 PyMODINIT_FUNC initcos_module void Now let s see how robust this is In 10 AS cos_module cos_func Foo void Py_InitModule cos_module CosMethods TypeError Traceback most recent call last lt ipython input 10 llbee483665d gt in lt module gt As you can see there is much boilerplate both to massage the arguments and return types into place and for gt 1 cos_module cos_func fo0o the module initialisation Although some of this is amortised as the extension grows the boilerplate required for each function s remains iv pekrror a float is reoguiredl The standard python build system distutils supports compiling C extensions from a setup py which is rather convenient Numpy Support from distutils core import setup Extension Analog to the Python C API Numpy which is itself i
269. int i words i 0 eool 1 powerful 2 readable e But Python provides a built in function enumerate for this gt gt gt for index item in enumerate words print index item 0a tewo 1 powerful 2 readable Looping over a dictionary Use items See dea f a s Wy esi tet 14 gt gt gt for key val in sorted d items print Key s has value s key val Key a has value 1 Key b has value 1 2 Key c has value 1j Note The ordering of a dictionary in random thus we use sorted which will sort on the keys List Comprehensions gt gt gt i 2 for i in range 4 HO ea Exercise Compute the decimals of Pi using the Wallis formula 4 me z 1 1 1 2 4 Defining functions Function definition In 56 def test AnA prine in test founcreion In 57 test in test function 1 2 The Python language 21 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Warning Function blocks must be indented as other control flow blocks Return statement Functions can optionally return values In 6 def disk_area radius return 3 14 radius radius In 8 disk_area 1 5 Cubs ls 7 0649999999990995 Note By default functions return None Note Note the syntax to define a function e the def keyword is followed by the function s name then e the ar
270. ir contour X 4 EO O o colors black linewidths 5 Click on figure for solution Imshow Hint You need to take care of the origin of the image in the imshow command and use a colorbar Starting from the code below try to reproduce the graphic on the right taking care of colormap image interpolation and origin Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Quiver Plots SANAAA 4 4 4 ow w W 8 4 4 A A wR k A A Z E E FY AASA eee EYAN ery YY YSN Hint You need to draw arrows twice Starting from the code above try to reproduce the graphic on the right taking care of colors and orientations i XC injec unepestel Oa 0 sin pl quiver X Y def f x y return 1 x 2 x 5 y 3 np exp x 2 y 2 n 10 x np linspace 3 3 4 n np l inspace gt gt 3 ia X Y np meshgrid x y OMe arn Sinai E Y KS Click on the figure for the solution Pie Charts Ww Wh Hint You need to modify Z Starting from the code below try to reproduce the graphic on the right taking care of colors and slices size Z np random uniform 0 1 20 pl pie Z Click on the figure for the solution 1 4 Matplotlib plotting 95 Click on figure for solution FI aaa aa aa aa aaa aA i Starting from the code below try to reproduce the graphic on the right taking care of line styles axes
271. istogram based method Otsu thresholding Tip The Otsu method is a simple heuristic to find a threshold to separate the foreground from the background Earlier scikit image versions skimage filters is called skimage filter in earlier versions of scikit image from skimage import filters 3 3 Scikit image image processing 332 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 7000 6000 5000 4000 3000 2000 1000 0 O 50 100 150 200 250 300 Labeling connected components of a discrete image Tip Once you have separated foreground objects it is use to separate them from each other For this we can assign a different integer labels to each one Synthetic data gt gt gt n 20 gt gt gt l 256 gt gt gt im np zeros 1 1 gt gt gt points 1 np random random 2 n x 2 gt gt gt im points 0 astype np int points 1 astype np int 1 gt gt gt im filters gaussian_filter im sigma l 4 n gt gt gt blobs im gt im mean Label all connected components gt gt gt from skimage import measure gt gt gt all_labels measure label blobs Label only foreground connected components gt gt gt blobs_labels measure label blobs background 0 See also scipy ndimage find_objects is useful to return slices on object in an image Marker based methods If you have markers inside a set of regions
272. it by the laser beam then produces a complex signal with multiple peaks each one containing information about one target One state of the art method to extract information from these data is to decompose them in a sum of Gaussian functions where each function represents the contribution of a target hit by the laser beam Therefore we use the scipy optimize module to fit a waveform to one or a sum of Gaussian functions Loading and visualization Load the first waveform using gt gt gt import numpy as np gt gt gt waveform_l np load data waveform_1l npy and visualize it gt gt gt import matplotlib pyplot as plt gt gt gt t np arange len waveform_1 gt gt gt plt plot t waveform_1 lt matplotlib lines Line2D object at gt gt gt gt plt show 30 25 20 15 Intensity bins 10 0 10 20 30 40 50 60 70 80 Time ns As you can notice this waveform is a 80 bin length signal with a single peak 2 The data used for this tutorial are part of the demonstration data available for the FullAnalyze software and were kindly provided by the GIS DRAIX 1 5 Scipy high level scientific computing 130 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Fitting a waveform with a simple Gaussian model Going further The signal is very simple and can be modeled as a single Gaussian f
273. it works In 99 of the use cases you will be better off using an alternative technique Note Since refernce counting bugs are easy to create and hard to track down anyone really needing to use the Python C API should read the section about objects types and reference counts from the official python documentation Additionally there is a tool by the name of cpychecker which can help discover common errors with reference counting Example The following C extension module make the cos function from the standard math library available to Python Z Example of wrapping cos function from math h with the Python C API va include lt Python h gt include lt math h gt x wrapped cosine function static PyObject cos_func PyObject self PyObject args double value double answer a parse the input from python float Co double 7 if PyArg_ParseTuple args d amp value return NULL x if the above function returns l an appropriate Python exception will x have been set and the function simply returns NULL e 2 8 Interfacing with C 254 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 T I call eos from libm 7 ype module String korm module ecos module rron cos moduler sol answer cos value File home esc git working scipy lecture notes advanced interfacing_with_c python_c_api co DOCSstr
274. itrorm 0 5 130 nm 2 l xX 7 fleacin np andom unirorm 0 5 pl plot x 1 color blue alpha 1 00 pl plot x Y 1 color blue alpha 1 00 pl bar X Y1 facecolor 9999ff edgecolor white pl bar X Y2 facecolor ff 9999 edgecolor white Click on the figure for solution for is in zip O v1 pl text x t 04 y F 0 057 12 2 z y ha center va botton Scatter Plots pl ylim 1 25 1 25 Click on figure for solution S d Contour Plots Skoog io oes ce x gt iy e b 0 ee SR PLPA Yj p cas peP Hint Color is given by angle of X Y Starting from the code below try to reproduce the graphic on the right taking care of marker size color and transparency n 1024 X mo random normal O Lym Hint You need to use the clabel command Y np random normal 0 1 n Starting from the code below try to reproduce the graphic on the right taking care of the colormap see Colormaps pl scatter X Y page 102 below Click on figure for solution def f x y return I ys e oe ee 8 Mp ela a ay ae a 256 np linspace 3 3 M npr Limspace Ss gt A Y np meshgrid x y xx XD II 1 4 Matplotlib plotting 93 1 4 Matplotlib plotting 94 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Pil contourE x Yr ENX Y 2 alpha 1 5 cmaps Jee C p
275. ity density function l gt gt gt a np random normal size 1000 gt gt gt bins np arange 4 5 gt gt gt bins 1 5 Scipy high level scientific computing 114 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 array tl 4 5 2 l 0 l 2 37 4l gt gt gt histogram np histogram a bins bins normed True 0 cee Pins Oe Comme kI PANS e gt gt gt bins array eee eS ale ope OS O Webs wey 25 gt gt gt from scipy import stats gt gt gt b stats norm pdf bins 7 norm 1s a dIsEeribuCcIroON oor Ple POLOT Corme histogram lt matplotlib lines Line2D object at gt gt gt gt Plte PpIot Lins p lt matplotlib lin s LineZzD object aE mma 0 40 0 35 0 30 0 25 0 20 0 15 0 10 0 05 0 00 6 2 If we know that the random process belongs to a given family of random processes such as normal processes we can do a maximum likelihood fit of the observations to estimate the parameters of the underlying distribution Here we fit a normal process to the observed data gt gt gt loc std stats norm fit a gt gt gt loc OR OSI SAS ow Omer gt gt gt std omone EO OOIE Exercise Probability distributions Generate 1000 random variates from a gamma distribution with a shape parameter of 1 then plot a histogram from those samples Can you plot the pdf on top it should match Extra the distributions ha
276. jugate gradient solves this problem by adding a friction term each step depends on the two last values of the gradient and sharp turns are reduced Table 2 4 Conjugate gradient descent An ill conditionned non quadratic function iterations function calls Error on f x O 20 40 60 80 100 120 An ill conditionned very non quadratic function iterations function calls Error on f x 20 40 60 80 100 120 Methods based on conjugate gradient are named with cg in scipy The simple conjugate gradient method to minimize a function is scipy optimize fmin_cg gt gt gt def f x The rosenbrock function return 5x 1 ON ae alae ON ee gt gt gt Optimize Malin Ce Ge 2 2N Optimization terminated successfully Current function value 0 000000 merat ions 1S Function evaluations 120 Gradient evaluations 30 array Wa999o3CGe O299997s55 These methods need the gradient of the function They can compute it but will perform better if you can pass them the gradient gt gt gt def fprime x gt gt gt optimize fmin_cg f 2 2 fprime fprime Optimization terminated successfully Current function value 0 000000 Iterations 13 Function evaluations 30 Gradient evaluations 30 array 0 99999199 0 99993356 return np arrcay A a Cll TON cee 10 J ee IL x O 2 2 x 1 x O 2 Note that the function ha
277. l morphology to clean the different phases 7 Attribute labels to all bubbles and sand grains and remove from the sand mask grains that are smaller than 10 pixels To do so use ndimage sumornp bincount to compute the grain sizes 8 Compute the mean size of bubbles Proposed solution gt gt gt import numpy as np gt gt gt import pylab as pl gt gt gt from scipy import ndimage 1 5 Scipy high level scientific computing 133 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Example of solution for the image processing exercise unmolten grains in glass Det Spot Mag 10 4 mm 15 0 kKV SSD 5 8 41x 3 25 mm MV 15 05 2009 1280 C 1 Open the image file MV_HFV_012 jpg and display it Browse through the keyword arguments in the docstring of imshow to display the image with the right orientation origin in the bottom left corner and not the upper left corner as for standard arrays gt gt gt dat pl imread data MV_HFV_012 jpg 2 Crop the image to remove the lower panel with measure information gt gt gt dat dat c0 3 Slightly filter the image with a median filter in order to refine its histogram Check how the histogram changes gt gt gt filtdat ndimage median_filter dat size 7 7 gt gt gt hi_dat np histogram dat bins np arange 256 gt gt gt hi_filtdat np histogram filtdat bins np arange 256 1 5 Scipy
278. ld measured_time measures kind cubic gt gt gt cubic_results cubic_interp computed_time The results are now gathered on the following Matplotlib figure 1 5 Scipy high level scientific computing 116 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e e measures linear interp cubic interp 0 0 0 2 0 4 0 6 0 8 1 0 scipy interpolate interp2d is similar to scipy interpolate interpld but for 2 D arrays Note that for the interp family the computed time must stay within the measured time range See the sum mary exercise on Maximum wind speed prediction at the Sprog station page 125 for a more advance spline interpolation example 1 5 8 Numerical integration scipy integrate The most generic integration routine is scipy integrate quad gt gt gt from scipy integrate import quad gt gt gt res err quead np sin 0 np pi 2 gt gt gt np allclose res 1 True gt gt gt np allclose err 1 res True Others integration schemes are available with fixed_quad quadrature romberg scipy integrate also features routines for integrating Ordinary Differential Equations ODE In particular scipy integrate odeint isa general purpose integrator using LSODA Livermore Solver for Ordinary Differential equations with Automatic method switching for stiff and non stiff problems see the ODEPACK Fortran library for more details odeint solves first order ODE systems o
279. le mented in Scipy As non professional programmers scientists often tend to re invent the wheel which leads to buggy non optimal difficult to share and unmaintainable code By contrast Scipy s routines are optimized and tested and should therefore be used when possible Chapters contents File input output scipy io page 104 Special functions scipy special page 104 Linear algebra operations scipy linalg page 105 Fast Fourier transforms scipy fftpack page 106 Optimization and fit scipy optimize page 110 Statistics and random numbers scipy stats page 114 Interpolation scipy interpolate page 116 Numerical integration scipy integrate page 117 Signal processing scipy signal page 119 Image processing scipy ndimage page 121 Summary exercises on scientific computing page 125 Warning This tutorial is far from an introduction to numerical computing As enumerating the different submodules and functions in scipy would be very boring we concentrate instead on a few examples to give a general idea of how to use scipy for scientific computing scipy is composed of task specific sub modules 1 5 Scipy high level scientific computing 103 The main scipy namespace mostly contains functions that are really numpy functions try scipy cos is np cos Those are exposed for historical reasons only there s usually no reason to use import scipy in your code 1 5 1 File input output scipy io e
280. lecture notes Release 2013 2 beta euroscipy 2013 Bar Plot oe Contour Plot plI contour page 93 page 94 w Ww BW 8 4 r r axa ws 444 amp SP lt gt lt gt lt gt gt 5 gt gt E E E egy ewes E BL on A a E E YN F F yyy page 94 page 95 Pie Chart pl pie Grid pl grid oo page 96 page 95 Multiplot pl subplot i Polar Axis Plot several plots at once page 96 page 96 3D plots Text pl text 7 z Plot 2D or 3D data Us as ym 1 M VU Gp ae BEspgo x E he a f ae di p T V Vp 4 uV v4 30 dt VF riled P Seha WERE e we V T 2 T page 97 page 97 page 98 1 4 Matplotlib plotting 92 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Regular Plots Bar Plots Hint You need to use the fill_between command Hint You need to take care of text alignment Starting from the code below try to reproduce the graphic on the right taking care of filled areas Starting from the code below try to reproduce the graphic on the right by adding labels for red bars n 256 n 12 Xx np linspace np pi np pi n endpoint True X np arange n Y np sin 2 X Y l x 7 loar amh np random un
281. les double in_array double x out_array int size create the wrapper code with numpy type annotations def cos_doubles_func np ndarray double ndim 1 mode c in_array not None np ndarray double ndim 1 mode c out_array not None cos_doubles lt double gt np PyArray_DATA in_array lt double gt np PyArray_DATA out_array in_array shape 0 And can be compiled using distutils from distutils core import setup Extension import numpy from Cython Distutils import build_ext setup cmdclass build_ext build_ext ext_modules Extension cos_doubles soumces eos doubles pyx coszdoubles ec include_dirs numpy get_include e As with the previous compiled Numpy examples we need the include_dirs option SEIS Cosmdoublesre cosmcoubles h cose doubles Eoy e etrupi ey trest COs doubles EY python setup py build_ext i running build_ext Cyehoning ecos doubles py co Leosudoubles te building cos_doubles extension creating build er ating build temp linux x86_64 2 7 geci Oenscad eS Eno Sit ere Solaris a O4 DNDEBUG o Enap a OS mV WiSittarCeepm orntypes fPIC In file included from home esc anaconda lib python2 7 site packages numpy core inclide numpy nda from home esc anaconda lib python2 7 site packages numpy core include numpy nda from home esc anaconda lib python2 7 site packages numpy core include numpy arr from COS me Ouloikes eA So ho
282. lf inflows return min new_storage self max_storage def _set_storage self storage_value self _storage storage_value def _get_spillage self new_Sstorage self _storage self release self inflows overflow new_storage self max_storage return max overflow 0 def print_state self print Storage tRelease tInflows tSpillage Str format Ve jou i 4s eet fon 1 an range MND print str_format format self storage self release self inflows self spillage print x 72 if name main Ve projectA Reservoir name Project A max_storage 30 max release 5 hydraulic_head lA 60 efficiency 0 8 state ReservoirState reservoir projectA storage 25 state release 4 state inflows 0 state print_state Note Caching property Heavy computation or long running computation might be a problem when accessing a property where the inputs have not changed The cached_property decorator can be used to cache the value and only recompute them once invalidated Let s extend the TraitsUI introduction with the ReservoirState example from traits api import HasTraits Instance DelegatesTo Float Range Property from traitsui api import View Item Group VGroup from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage For the simplicity of the example the release is considered in hm3 timestep and
283. linear correlation of two 1 dimensional sequences In 46 numpy lookfor remove module os Search results for remove os remove remove path os removedirs removedirs path ose rma rmdir path OS Unde mik unlink path os walk Directory tree generator e If everything listed above fails and Google doesn t have the answer don t despair Write to the mailing list suited to your problem you should have a quick answer if you describe your problem well Experts on scientific python often give very enlightening explanations on the mailing list Numpy discussion numpy discussion scipy org all about numpy arrays manipulating them in dexation questions etc SciPy Users List scipy user scipy org scientific computing with Python high level data process ing in particular with the scipy package matplotlib users lists sourceforge net for plotting with matplotlib 1 6 Getting help and finding documentation 138 CHAPTER 2 Advanced topics This part of the Scipy lecture notes is dedicated to advanced usage It strives to educate the proficient Python coder to be an expert and tackles various specific topics 2 1 Advanced Python Constructs Author Zbigniew Jedrzejewski Szmek This section covers some features of the Python language which can be considered advanced in the sense that not every language has them and also in the sense that they are more useful in more com
284. llhyiciic se Wy S gt gt gt t np linspace 0 1 200 Sere jullie OLO Ok We YO Ee jolie Y lt matplorlibs Hines mne2D opec ak s gt mat plot lib Pines line2D object ak fe gt 1 3 NumPy creating and manipulating numerical data 72 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 1 3 1 2 1 1 1 0 0 9 0 8 0 7 0 6 0 0 0 2 0 4 0 6 0 8 1 0 See http docs scipy org doc numpy reference routines polynomials poly1d html for more More polynomials with more bases Numpy also has a more sophisticated polynomial interface which supports e g the Chebyshev basis 3z Qe 1 gt gt gt p np polynomial Polynomial 1 2 3 coefs in different order gt gt gt p 0 1 10 gt gt gt p roots array da 7 Or33999291 gt gt gt p degree In general polynomials do not always expose order 2 Example using polynomials in Chebyshev basis for polynomials in range 1 1 gt gt gt x np linspace 1 1 2000 gt gt gt y np cos x O knp random rand 2000 gt gt gt p np polynomial Chebyshev fit x y 90 gt gt gt t np linspace 1 17 200 sea Tolle aen x Wey aaa lt matplotlib Aines Line2D object at mene gt gt gt plt plott PHE ae ns Amatplotlib lines Line2zD object at gt 1 3 NumPy creating and manipulating numerical data 73 Python Scientific lecture not
285. losing gt gt gt eroded_img ndimage binary_erosion binary_img gt gt gt reconstruct_img ndimage binary_propagation eroded_img mask binary_img gt gt gt tmp np logical_not reconstruct_img gt gt gt eroded_tmp ndimage binary_erosion tmp gt gt gt reconstruct_final np logical_not ndimage binary_propagation eroded_tmp mask t gt gt gt np abs mask close_img mean OROOT2AT 5 Ssmi gt gt gt np abs mask reconstruct_final mean 07000595025 ar Exercise Check how a first denoising step e g with a median filter modifies the histogram and check that the resulting histogram based segmentation is more accurate See also More advanced segmentation algorithms are found in the scikit image see Scikit image image processing page 324 See also Other Scientific Packages provide algorithms that can be useful for image processing In this example we use the spectral clustering function of the scikit learn in order to segment glued objects 2 6 Image manipulation and processing using Numpy and Scipy 233 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt from sklearn feature_extraction import image gt gt gt from sklearn cluster import spectral_clustering gt gt gt I Oo gt gt gt x y np indices l 1 gt gt gt center 28 24 gt gt gt center2 40 50 gt gt
286. ls Generating the compiled wrappers is a two stage process 1 Run the swig executable on the interface file to generate the files cos_module_wrap c which is the source file for the autogenerated Python C extension and cos_module py which is the autogenerated pure python module 2 Compile the cos_module_wrap c into the _cos_module so Luckily distutils knows how to handle SWIG interface files so that our setup py is simply from distutils core import setup Extension setup ext_modules Extension _cos_module sources cos_module c cos_module i ed advanced interfacing_with_c swig Sols cos_module c cos_module h cos_module i setup py python setup py build_ext inplace umaing Oui el oie building _cos_module extension Sswigging cos_module i to cos_module_wrap c swig python O cos_module_wrap c cos_module i creating build creatino build temp 1inux x86_64 2 7 p gee pthread fno strict aliasing o9 02 DNDEBUG 9 fwrapy OS Wall Wstriect proto types gece pthread rno stenice alhasi O2 DNDEBUG g rwr a py OS amna EENS Eae E PE OEO 2 8 Interfacing with C 262 types fPIC f fPIC Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gece pthread shar edi build temo linux x86 64 2 7 cos_module o build temp linux x86_64 2 7 cos_modi SaS build cos_module c cos_module h cos_module i cos_module py _cos
287. lt imshow lena lt matplotlib image AxesImage object at 0x gt Follow up copy all instructions of this exercise in a script called lena_locket py then execute this script in Python with Srun lena_locket py Change the circle to an ellipsoid Data statistics The data in populations txt describes the populations of hares and lynxes and carrots in northern Canada during 20 years gt gt gt data np loadtxt data populations txt gt gt gt year hares lynxes carrots data T trick columns to variables gt gt gt import matplotlib pyplot as plt See plt exes 0 2 Ws Oca W 8 lt Mmatplot libwaxes m Aes object Al s2 2 gt gt gt plt plot year hares year lynxes year carrots k matpilot ilo limestr hine Dko ect BG scah sao gt gt gt pilt legend Harel vn r Carrot loc 305 0S lt matplotlib legend Legend object at gt 1 3 NumPy creating and manipulating numerical data 78 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 80000 70000 60000 Hare 50000 Lynx Carrot 40000 30000 20000 10000 0 1900 1905 1910 1915 1920 Computes and print based on the data in populations txt 1 The mean and std of the populations of each species for the years in the period 2 Which year each species had the largest population 3 Which species has the largest population for each year Hint argsort amp fancy indexing o
288. m traits api import HasTraits Instance from traitsui api import View Item HGroup from mayavi core ui api import SceneEditor MlabSceneModel def curve n_turns The function creating the x y z coordinates needed to plot pia np linspace 0 Zenp pa 2000 return np cos phi 1 O 5 np cos n_turns phi wo Salina Gola il Os S ajo CoS a Carasa 0 5 Ssmo Sin ial CUTS ani class Visualization HasTraits The class that contains the dialog scene Instance MlabSceneModel def __init_ self HasTraits __init__ self X Y Z curve n_turns 2 Populating our plot self plot self scene mlab plot3d x y zZ Describe the dialog view View Item scene height 300 show_label False editor SceneEditor HGroup n_turns resizable True Fire up the dialog Visualization configure_traits O Tip Let us read a bit the code above examples mlab_dialog py First the curve function is used to compute the coordinate of the curve we want to plot Second the dialog is defined by an object inheriting from HasTraits as it is done with Traits page 338 The important point here is that a Mayavi scene is added as a specific Traits attribute Instance This is important 3 5 3D plotting with Mayavi 367 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 for embedding it in the dialog The view of this dialog is defined by the view attribute of the object
289. m3 s 3 4 Traits building interactive dialogs 353 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 power 1000 9 81 x self head x release x self efficiency return power x 3600 traits _view View Item name Item max_storage Item max_release Item head Item efficiency Item irrigated_areas resizable True ols name main UG upper_block IrrigationArea name Section C surface 2000 crop Wheat reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 irrigated_areas upper_block release 80 print Releasing m3 s produces kWh format release reservoir energy_production release Trait listeners can be used to listen to changes in the content of the list to e g keep track of the total crop surface on linked to a given reservoir from traits api import HasTraits Str Float Range Enum List Property from traitsui api import View Item class IrrigationArea HasTraits name Str surface Float desc Surface ha erop knum Allkalfa VininiSeic 5 ViC eccim class Reservoir HasTraits name Str max_storage Float le6 desc Maximal storage hm3 max_release Float 10 desc Maximal release m3 s head Float 10 desc Hydraulic head m efficiency Range 0 1 irrigated_areas List IrrigationArea total_
290. me esc anaconda lib python2 7 site packages numpy core include numpy npy_deprecat d_api h PLZ home esc anaconda lib python2 7 site packages numpy core include numpy __ufunc_api h 236 warnin gec prhrcadi rno eS aCe lS INC CO sD NDE UChr Oma aWiaclO Vl OS Ml VcWiSitrarCtemmomotypes fPIC gee pthread shared builds tenp linuix x66 64 2 1 cos doubles o buald tema linux xs6 64 2 7 cos_d Seeks 2 8 Interfacing with C 268 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 build cos doubles c cos doubles lt cos doubles h cos doubles pyx cos_doubles ox And as before we convince ourselves that it worked import numpy as np import pylab import cos_doubles x np arange 0 2 x np pi 0 1 y np empty_like x less doubles ese doubles mn Ge y Py labi pilot y pylab show 2 8 6 Summary In this section four different techniques for interfacing with native code have been presented The table below roughly summarizes some of the aspects of the techniques x Part of CPython Python C API Autogenerated False Numpy Support True True Ctypes True False True Swig Cython False True True False True True True Of all three presented techniques Cython is the most modern and advanced In particular the ability to optimize code incrementally by adding types to your Python code is unique 2 8 7 F
291. mo np demo prof pdf demo py demo pyc demo linalg demo prof demo prof png demo py lprof demo test In 2 Stimeit demo test ica py 65 RuntimeWarning invalid value encountered in sqrt Wo 2s no diag 1 O0 7no sqrt s w W FW Ws WoT Sf l72 WwW i legos best oe 32 175 5 s Per loose In 3 import demo_opt In 4 timeit demo_opt test iD loops best ore Z0Seins joer loop Real incomplete SVDs e g computing only the first 10 eigenvectors can be computed with arpack available in scipy sparse linalg eigsh Computational linear algebra For certain algorithms many of the bottlenecks will be linear algebra computations In this case using the right function to solve the right problem is key For instance an eigenvalue problem with a symmetric matrix is easier to solve than with a general matrix Also most often you can avoid inverting a matrix and use a less costly and more numerically stable operation Know your computational linear algebra When in doubt explore scipy linalg and use St imeit to try out different alternatives on your data 2 4 4 Writing faster numerical code A complete discussion on advanced use of numpy is found in chapter Advanced Numpy page 155 or in the article The NumPy array a structure for efficient numerical computation by van der Walt et al Here we discuss only some commonly encountered tricks to make code faster e Vectorizing f
292. mplemented as a C extension comes with the Numpy C define the extension module API This API can be used to create and manipulate Numpy arrays from C when writing a custom C extension cos_module Extension cos_module sources cos_module c See also ref advanced_numpy run the setup Be ne S ae nea Note If you do ever need to use the Numpy C API refer to the documentation about Arrays and Iterators The following example shows how to pass Numpy arrays as arguments to functions and how to iterate over Numpy arrays using the old Numpy C API It simply takes an array as argument applies the cosine function from the cd advanced interfacing_with_c python_c_api math h and returns a resulting new array This can be compiled ls ee Example of wrapping the cos function from math h using the Numpy C API cos_module c setup py include lt Python h gt python setup py build_ext inplace include lt numpy arrayobject h gt running build_ext include lt math h gt building cos_module extension creating build x wrapped cosine function creating build temp linux x86_64 2 7 static PyObject cos func no PyObject self PyObject args Gee penveaa Lio ctu riece aliacing 7 O2 DNDELUG g twrapy o gt Wall Werrier proeotypes fPIC gce pthread shared build temp linux x86_64 2 7 cos_module o L home esc anaconda lib lpython2 PyArrayObject in_array
293. mplemented as classes and as functions The only requirement on decorators is that they can be called with a single argument This means that decorators can be implemented as normal functions or as classes with a___ca11___ method or in theory even as lambda functions Let s compare the function and class approaches The decorator expression the part after can be either just a name or a call The bare name approach is nice less to type looks cleaner etc but is only possible when no arguments are needed to customise the decorator Decorators written as functions can be used in those two cases gt gt gt def simple _decorator function print doing decoration return function gt gt gt simple_ decorator def function Print inside function 2 1 Advanced Python Constructs 145 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 doing decoration gt gt gt function inside function gt gt gt def decorator_with_arguments arg print defining the decorator def decorator function in this inner function arg is available too print cong decoration 22 2 arg return function return _decorator gt gt gt decorator_with_arguments abc def function print inside function defining the decorator doing decoration abe gt gt gt fuNnckaon inside function The two trivial decorators above fall into the category of decorators
294. mport View class Reservoir HasTraits name Str max_storage Float le6 desc Maximal storage hm3 max_release Float 10 desc Maximal release m3 s head Float 10 desc Hydraulic head m efficiency Range 0 1 traits_view View name max_storage max_release head efficiency title Reservoir resizable True def energy_production self release Returns the energy production Wh for the given release m3 s power 1000 9 81 x self head x release self efficiency return power x 3600 if name _main__ reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 reservoir configure_traits 3 4 Traits building interactive dialogs 344 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Reservoir Name Project A Max storage 30 0 Max release 100 0 Head 60 0 Efficiency 0 0 1 0 0 8 Deferral Being able to defer the definition of a trait and its value to another object is a powerful feature of Traits Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 A special trait allows to manage events and trigger function calls using the magic _xxxx_fired method from traits api import HasTraits Instance DelegatesTo Float Range from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state gi
295. mpy examples 2 Modify the example such that the function only takes a single input array and modifies this in place 3 Try to fix the example to use the new Numpy iterator protocol If you manage to obtain a working solution please submit a pull request on github 4 You may have noticed that the Numpy C API example is the only Numpy example that does not wrap cos_doubles but instead applies the cos function directly to the elements of the Numpy array Does this have any advantages over the other techniques 5 Can you wrap cos_doub1les using only the Numpy C API You may need to ensure that the arrays have the correct type are one dimensional and contiguous in memory Ctypes 1 Modify the Numpy example such that cos_doubles_func handles the preallocation for you thus mak ing it more like the Numpy C API example SWIG 1 Look at the code that SWIG autogenerates how much of it do you understand 2 Modify the Numpy example such that cos_doubles_func handles the preallocation for you thus mak ing it more like the Numpy C API example 3 Modify the cos_doubles C function so that it returns an allocated array Can you wrap this using SWIG typemaps If not why not Is there a workaround for this specific situation Hint you know the size of the output array so it may be possible to construct a Numpy array from the returned double x Cython 1 Look at the code that Cython autogenerates Take a closer look at some of the commen
296. much sense to use the argument less form the final decorated object would just be an instance of the decorating class returned by the constructor call which is not very useful Therefore it s enough to discuss class based decorators where arguments are given in the decorator expression and the decorator ___init___ method is used for decorator construction gt gt gt class decorator_class object def init__ self arg this method is called in the decorator expression print in decorator init ss 2 arg self arg arg def call self function this method is called to do the job Q Print in decorator call 3s s sSelzt arg 2 1 Advanced Python Constructs 146 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Mens return function gt gt gt deco_instance decorator_class foo in decorator init OO gt gt gt deco_instance def function args kwargs Print in function 26 26 args kwargs in decorator call EOS gt gt gt UMC te LON in ume rvonm O 1 Contrary to normal rules PEP 8 decorators written as classes behave more like functions and therefore their name often starts with a lowercase letter In reality it doesn t make much sense to create a new class just to have a decorator which returns the original function Objects are supposed to hold state and such decorators are more useful when the decorator returns a new object
297. n C These are difficult languages for non com puter scientists Scripting languages Matlab e Advantages Very rich collection of libraries with numerous algorithms for many different domains Fast execution because these libraries are often written in a compiled language Pleasant development environment comprehensive and well organized help integrated editor etc Commercial support is available e Drawbacks Base language is quite poor and can become restrictive for advanced users Not free Other scripting languages Scilab Octave Igor R IDL etc e Advantages Open source free or at least cheaper than Matlab Some features can be very advanced statistics in R figures in Igor etc e Drawbacks Fewer available algorithms than in Matlab and the language is not more advanced Some software are dedicated to one domain Ex Gnuplot or xmgrace to draw curves These programs are very powerful but they are restricted to a single type of usage such as plotting What about Python e Advantages Very rich scientific computing libraries a bit less than Matlab though Well thought out language allowing to write very readable and well structured code we code what we think Many libraries for other tasks than scientific computing web server management serial port access etc Free and open source software widely spread with a vibrant community e Drawbacks less
298. n above Remember the a object oriented notation and use tab completion or help str to search for new methods See also Python offers advanced possibilities for manipulating strings looking for patterns or formatting The interested reader is referred to http docs python org library stdtypes html string methods and http docs python org library string html new string formatting String formatting gt gt gt AN Integer si a float or another string ss s Ip Orl Strang TAn ineegers lta moar Os l00O00 mamot her SE rino SEEM gt gt gt i 102 gt gt gt filename processing_of_dataset_td txt i gt gt gt filename YOLGOC CSIs 1s GMO MIC ale S S tem OZ eptescte Dictionaries Tip A dictionary is basically an efficient table that maps keys to values It is an unordered container gt gt gt tel emmanuelle 5752 sebastian 5578 gt gt gt tel francis 5915 gt gt gt tel sebastian 5578 francisi 5915 emmanuelle 5752 gt gt gt tel sebastian 55m8 gt gt gt tel keys sebastian francis emmanuelle gt gt gt tel values S578 SOLS S752 gt gt gt francisi In tel Iruo 1 2 The Python language 16 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Tip It can be used to conveniently store and retrieve values associated with a name a string for a date a name etc Se
299. n be very useful to assign a new value to a sub array gt gt gt ala 3 0 1 gt gt gt a anres D10 S er l IS lo Li Ll lO a a 20 Tr Nea Indexing with an array of integers gt gt gt a np arange 0 LOO nO gt gt gt a array I Oy 107 207 30 40 507 607 707 207 90 Indexing can be done with an array of integers where the same index is repeated several time Bss alle S 27 47 2 t noce 2 Of 2 4 Zi Ase Pyenon LISE anrcay 20 S30 20 40 20 New values can be assigned with this kind of indexing gt gt gt a l 9 7 100 gt gt gt a larray 0 10 20 30 40 50 60 100 980 100 Tip When a new array is created by indexing with an array of integers the new array has the same shape than the array of integers 2 2 gt gt gt a lidx array 3 4 The image below illustrates various fancy indexing applications gt gt gt al 0 1 2 3 4 1 2 3 4 5 array 1 12 23 34 45 gt gt gt al3 0 2 5 array 30 32 35 40 42 45 50 52 55 gt gt gt mask array 1 0 1 0 0 1 dtype bool gt gt gt a mask 2 array 2 22 52 1 3 NumPy creating and manipulating numerical data 54 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercise Fancy indexing e Again reproduce the fancy indexing shown in the diagram above e Use fancy indexing o
300. n the left and array creation on the right to assign values into an array for instance by setting parts of the array in the diagram above to zero 1 3 2 Numerical operations on arrays Section contents e Elementwise operations page 55 Basic reductions page 57 Broadcasting page 62 Array shape manipulation page 65 Sorting data page 67 Summary page 68 Elementwise operations Basic operations With scalars gt gt gt a np array l1 2 3 AD gt gt gt a 1 array 2r 3 4 519 gt gt gt 2xxa array I 27 4 er SI All arithmetic operates elementwise gt gt gt b np ones 4 1 gt gt gt a p array Viale Oey dap al gt gt gt a D array Ci Aap Gap emip gt gt gt j np arange 5 bar Re ag al a arrcay 2 3 6 LS 28l These operations are of course much faster than if you did them in pure python gt gt gt a np arange 10000 gt gt gt Stimeit a 1 HOOUOMRC OMicy7 bestor Om o BUSI OCs al ojo gt gt gt 1 range 10000 gt gt gt Stimeit itl for i in 1 NOVO Woops best On Sa scl wus per oop 1 3 NumPy creating and manipulating numerical data 55 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Warning Array multiplication is not matrix multiplication gt gt gt c np ones 3 3 2 gt Se eS NOT matrix multiplication array Oi Way ale dle Pda legs sila
301. ncommon to find simple changes like moving computation or memory allocation outside a for loop that bring in big gains Example of the SVD In both examples above the SVD Singular Value Decomposition is what takes most of the time Indeed the computational cost of this algorithm is roughly n in the size of the input matrix However in both of these example we are not using all the output of the SVD but only the first few rows of its first return argument If we use the svd implementation of scipy we can ask for an incomplete version of the SVD Note that implementations of linear algebra in scipy are richer then those in numpy and should be preferred 2 4 Optimizing code 200 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 in 3 ttimeit noalinalg svdi datra 1 loops best of 3 14 5 s per loop In 4 from scipy import linalg In 5 timeit linalg svd data I logos best oe 32 1452 Ss joeue Icio In 6 Stimeit linalg svd data full_matrices False i lo os best Oo 32 295 ms joeice loos In 7 timeit np linalg svd data full_matrices False i le os best oe 32 293 ms josie Ilocos We can then use this insight to optimize the previous code def test data np random random 5000 100 u S v linalg svd data full_matrices False pea npe dot als nOn data results fastica pca T whiten False In 1 import demo In 2 timeit demo demo fastica de
302. nd better organized programs compared to simple scripts where some objects are defined variables functions classes and that we want to reuse several times we have to create our own modules Let us create a module demo contained in the file demo py A demo module def print_b Eri NES DEM print b def print_a WE eves a t print a c 2 d 2 Tip In this file we defined two functions print_a and print_b Suppose we want to call the print_a function from the interpreter We could execute the file as a script but since we just want to have access to the function print_a we are rather going to import it as a module The syntax is as follows In 1 import demo In 2 deme lt print al a in 3 Enor print b b Importing the module gives access to its objects using the module object syntax Don t forget to put the module s name before the object s name otherwise Python won t recognize the instruction Introspection In 4 demo Type module Base Class lt type module gt Siewiline Woes lt module demo from demo py gt Namespace Interactive File home varoquau Projects Python_talks scipy_2009_tutorial source demo py DOCSEAIMOk A demo module In 5 who demo In 6 whos Variable Type Data Info demo module lt module demo from demo py gt in 7 arr demo ouc epui keins aami doc n 1 2 The Python language 29
303. nds are explained with interactive examples e plot tutorial Hint Documentation e plot command 1 4 2 Simple plot Tip Matplotlib comes with a set of default settings that allow customizing all kinds of properties You can control the defaults of almost every property in matplotlib figure size and dpi line width color and style axes Tip In this section we want to draw the cosine and sine functions on the same plot Starting from the default axis and grid properties text and font properties and so on settings we ll enrich the figure step by step to make it nicer First step is to get the data for the sine and cosine functions import pylab as pl import numpy as np ee ee np linspace np pi np pi 256 endpoint True X C S nee COS xX nP Sinx np linspace np pi np pi 256 endpoint True X Cy S Np cos x Moo SLm OL pel plot O C L plot O S X is now a numpy array with 256 values ranging from r to 7 included C is the cosine 256 values and S is Pa the sine 256 values pl show To run the example you can type them in an IPython interactive session ipython pylab Instantiating defaults This brings us to the Python prompt IPython 0 13 An enhanced Interactive Python 2 gt Introduction to IPython s features Smagic gt Information about IPython s magic functions j help gt Python s own help system object Details about objecti object aleo works
304. ne Elaboration of the algorithm in an editor Create a file my_file py in a text editor Under EPD Enthought Python Distribution you can use Scite available from the start menu Under Python x y you can use Spyder Under Ubuntu if you don t already have your favorite editor we would advise installing Stani s Python editor In the file add the following lines s Hello world print s Now you can run it in Python and explore the resulting variables In 1 srun my_file py Hello world In 2 s OQut 2 Hello world In 3 whos Variable Type Data Info S SET Hello world 1 1 Scientific computing with tools and workflow 6 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 From a script to functions While it is tempting to work only with scripts that is a file full of instructions following each other do plan to progressively evolve the script to a set of functions e A script is not reusable functions are e Thinking in terms of functions helps breaking the problem in small blocks IPython Tips and Tricks The Python user manual contains a wealth of information about using IPython but to get you started we want to give you a quick introduction to four useful features history magic functions aliases and tab completion Like a UNIX shell Python supports command history Type up and down to navigate previously typed commands
305. netic field shown previously Note that one of the difficulties is providing the data in the right form number of arrays shape to the functions This is often the case with real life data See also Sources are described in details in the Mayavi manual 3 5 3D plotting with Mayavi 365 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Transforming data filters If you create a vector field you may want to visualize the iso contours of its magnitude But the isosurface module can only be applied to scalar data and not vector data We can use a filter Ext ract VectorNorm to add this scalar value to the vector field Filters apply a transformation to data and can be added between sources and modules Excercice Using the GUI add the ExtractVectorNorm filter to display iso contours of the field magnitude mlab pipeline the scripting layer The mlab scripting layer builds pipelines for you You can reproduce these pipelines programmatically with the mlab pipeline interface each step has a corresponding mlab pipeline function simply convert the name of the step to lower case underscore separated ExtractVectorNorm gives extract_vector_norm This function takes as an argument the node that it applies to as well as optional parameters and returns the new node For example iso contours of the magnitude are coded as mlab pipeline iso_surface mlab pipeline extract_vector_norm field contours 0
306. ng below from mpl_toolkits mplot3d import Axes3D For statistics Requires statsmodels 5 0 or more from statsmodels formula api import ols Analysis of Variance ANOVA on linear models from statsmodels stats anova import anova_lm HHEEEEAEEEEAEEEEEEREEAEEEEEEEEEEEEEEEREEAEEEEEEEEEEPEEEEPEEAEPAEAEEEEEEEEEE EEE Generate and show the data x np linspace 5 5 20 We generate a 2D grid X Y np meshgrid x x To get reproducable values provide a seed value np random seed 1 Z is the elevation of this 2D grid Z 5 3 X 0 5 Y 8 np random normal size X shape Plot the data fig plt figure ax figsgca projyection 3d surf ax plot_surface X Y Z cmap plt cm coolwarm rstride l1 cstride 1 axes VaLewe nae 20 i210 ax set_xlabel X ax set_ylabel Y ax set_zlabel Z eee eee eee eee eee eee eee eee e eet eeee eee eee eet ee ee eee eee eeeeeeeeeeeeeetee tee A Multilinear regression model calculating fit P values confidence 2 mear rvala IEC Convert the data into a Pandas DataFrame to use the formulas framework in statsmodels OSH First we need to flatten the data it s 2D layout is not relevent X flatten Y flatten Z flatten N K XK Ss data pandas Datanramel Us 2 VS MC 8 s mh Fit the model model ols zZ yy data fit Print the summary print model summary print nRetrieving manually the parameter estimates pri
307. ng in t xt In 10 for dirpath dirnames filenames in os walk os curdir ioucers for fp in filenames Oe print os path abspath fp jUsers cburns sre scipy2009 7 scipy_2 000 tutorial source index rst swo Users cburns src scipy2009 scipy_2009_tutorial source view_array py swp Users courns src scipy2 0097 scipy_2009_tutorial source basic_types rst Users churns src seipy2009 scipy 2009 tutorial source comt py J Users cburms sre scipy20097 scipy 2009 tutorial source control flow cst In 18 import glob in 29 olobwolob EE Goetel holy oran texte fe UMS exe Mnewi iler tset Environment variables In 9 import os En 21 os environ keys cucine a tf TES lR TERM PROGRAM VERSION FSLREMOTECALL USER HOME PATH ipg SHELL EDITOR WORKON_HOME PYTHONPATH In 12 os environ PYTHONPATH Cele es Uisers e burns sre uti lse Users ebmnins sey mools Users cburns local lib python2 5 site packages usr local lib python2 5 site packages sys module system specific information System specific information related to the Python interpreter e Which version of python are you running and where is it installed in pii sys platrorm QUELI Tdarwin In 118 sys version ouelo 2 5 2 252 600M Feb 22 2009 Uy 2573 57 Nn CCGA OSI Apo le Computern Ine burkadi a sios a In 119 sys prefix Out 119 Library Framewo
308. ng py x x x Blank or comment x x x Blank or comment x Blank or comment Breakpoint 1 at home varoquau dev scipy lect NOTE Enter Ee Vat the aodo gt josompr Eo Star gt lt string gt 1 lt module gt ure notes advanced debugging_optimizing wiener_f ViOUemSICrant oer CELICI Set a break point at line 34 using b 34 ipdb gt m gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 4 3 l gt 4 import numpy as np SAMO Onsite seip aS SE ipdb gt b 34 Breakpoint 2 at home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_f 2 3 Debugging code 192 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e Continue execution to next breakpoint with c ont inue Wpada Cc gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 34 33 sumni 2e noisy img noisy img 35 denoised_img local_mean noisy_img size size e Step into code with n ext and s tep next jumps to the next statement in the current execution context while st ep will go across execution contexts i e enable exploring inside function calls ipdb gt s gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 35 2 34 noisy img noisy img gt 35 denoised_img local_mean noisy_img size size 36
309. ning preconditioning If you know natural scaling for your variables prescale them so that they behave similarly This is related to Also it clearly can be advantageous to take bigger steps This is done in gradient descent code using a line search Table 2 3 Adaptive step gradient descent A well conditionned quadratic function Error on f x iterations function calls O 20 40 60 80 100 120 140 An ill conditionned quadratic function Error on f x 10 iterations 4 function calls 10 O 200 400 600 800 100012001400 An ill conditionned non quadratic function Error on f x iterations function calls O 200 400 600 800 1000 1200 An ill conditionned very non quadratic function Error on f x 107 O 200 400 600 8001000200400600 The more a function looks like a quadratic function elliptic iso curves the easier it is to optimize Conjugate gradient descent The gradient descent algorithms above are toys not to be used on real problems As can be seen from the above experiments one of the problems of the simple gradient descent algorithms is that it tends to oscillate across a valley each time following the direction of the gradient that makes it cross the valley 2 7 Mathematical optimization finding minima of functions 243 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 The con
310. not in m3 s wan reservoir Instance Reservoir name DelegatesTo reservoir max_storage DelegatesTo reservoir max_release DelegatesTo reservoir min_release Float state attributes storage Property depends_on inflows release control attributes inflows Float desc Inflows hm3 release Range low min_release high max_release spillage Property desc Spillage hm3 depends_on storage inflows release H Traits VieCw TEEEAEEREEEEEEREREREPEEREREEEEREREEEERER REE EEE EEE HEH EHF traits_view View Group VGroup Item name Item storage Item spillage label State style readonly yr VGroup Item inflows Item release label Control Private traits te ete ete ete eeteeee eee ete et eeeeeeeeeeeaeteettetezt _storage Float Traits property implementation Feee tee ee ses eee tee tae tad te ttt at t F def _get_storage self new_storage self _storage self release self inflows return min new_storage self max_storage def _set_storage self storage_value 3 4 Traits building interactive dialogs 347 3 4 Traits building interactive dialogs 348 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 self _storage storage_value _get_spillage self new_storage self _storage
311. now some Scipy module well User guide Needs to be done eventually x Want to think Come up with a Table of Contents http scipy org Developer_Zone UG_Toc e Ask on communication channels numpy discussion list scipy dev list 2 3 Debugging code Author Ga l Varoquaux This section explores tools to understand better your code base debugging to find and fix bugs It is not specific to the scientific Python community but the strategies that we will employ are tailored to its needs Prerequisites e Numpy e Python e nosetests http readthedocs org docs nose en latest e pyflakes http pypi python org pypi pyflakes e gdb for the C debugging part Chapters contents e Avoiding bugs page 187 Coding best practices to avoid getting in trouble page 187 pyflakes fast static analysis page 188 Running pyflakes on the current edited file page 188 A type as go spell checker like integration page 189 e Debugging workflow page 190 e Using the Python debugger page 190 Invoking the debugger page 191 Postmortem page 191 Step by step execution page 192 Other ways of starting a debugger page 194 Debugger commands and interaction page 195 Getting help when in the debugger page 195 e Debugging segmentation faults using gdb page 195 2 3 1 Avoiding bugs Coding best practices to avoid getting in trouble 2 3 Debugging code 187 Python Scientific lecture
312. nstructs 140 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Generator expressions A second way in which iterator objects are created is through generator expressions the basis for list compre hensions To increase clarity a generator expression must always be enclosed in parentheses or an expression If round parentheses are used then a generator iterator is created If rectangular parentheses are used the process is short circuited and we geta list gt gt gt i for i in nums lt generator object lt genexpr gt at Ox gt gt gt gt i for i in nums i 2r Ol Soo IMSE EFOr M In n ms R The list comprehension syntax also extends to dictionary and set comprehensions A set is created when the generator expression is enclosed in curly braces A dict is created when the generator expression contains pairs of the form key value gt gt gt i for i in range 3 See OF ele gt gt gt i i 2 for i in range 3 HOS OW bg i 2s a One gotcha should be mentioned in old Pythons the index variable i would leak and in versions gt 3 this is fixed Generators Generators A generator is a function that produces a sequence of results instead of a single value David Beazley A Curious Course on Coroutines and Concurrency A third way to create iterator objects is to call a generator function A generator is a function containing the keyword yield It must be noted
313. nt model _results params 7 SHoula De array 4 99754526 00250049 C2 505149077 3 1 Statistics in Python 301 3 1 Statistics in Python 302 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Peform analysis of variance on fitted linear model anova_results anova_lm model print nANOVA results print anova_results plt show Total running time of the example 0 15 seconds 0 minutes 0 15 seconds 55 EHKATHOH S E m a 500 025 1615 26 l ww SS oo T i au LS at VACI Ail CISA THR Figure 3 7 Visualizing factors influencing wages page 303 Visualizing factors influencing wages This example uses seaborn to quickly plot various factors relating wages experience and eduction Seaborn http stanford edu mwaskom software seaborn is a library that combines visualization and statistical fits to show trends in data Note that importing seaborn changes the matplotlib style to have an excel like feeling This changes affect other matplotlib figures To restore defaults once this example is run we would need to call plt rcdefaults WAGE 0 5 AGE EDUCATION Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 0 0 5 0 0 0 5 10 1 5 2 0 10 20 30 40 50 60 70 O 5 10 15 20 WAGE AGE EDUCATION 3 1 Statistics in Python 303 3 1 Statistics in Python 304 Python Scientific
314. nt calc_deri 1 0 time_vec args nu_coef om_coef The final position and velocity are shown on the following Matplotlib figure scipy signal resample resample a signal to n points using FFT gt gt gt t np linspace 0 5 100 gt gt gt x DDP LIne gt gt gt plt plot t x linewidth 3 lt matplotlib lines Line2D object at gt gt gt gt plt olor Celie lias no nicelese sampler 50 kor lt matolotlib line s Lin e2D object at gt 1 5 0 2 4 6 8 10 1 0 There is no Partial Differential Equations PDE solver in Scipy Some Python packages for solving PDE s are 0 5 available such as fipy or SfePy 0 0 1 5 9 Signal processing scipy signal a 0 5 gt gt gt from scipy import signal e scipy signal detrend remove linear trend from signal 1 0 gt gt gt t np linspace 0 5 100 gt gt gt x t np random normal size 100 1 5 0 1 2 3 4 5 gt gt gt plt plot t x linewidth 3 lt matplotlib lines Line2D object at gt See ee ee ae Notice how on the side of the window the resampling is less accurate and has a rippling effect _ smatplotlib lines Line2D object at gt _ i e scipy signal has many window functions scipy signal hamming scipy signal bartlett scipy signal blackman e scipy signal has filtering median filter scipy signal medfilt Wiener scipy signal wiener b
315. ntil an aggregation function is applied 3 1 Statistics in Python 274 150 140 130 120 110 100 90 80 70 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Female Male T T T T T Eg 1 i EEE i d i 4 1 i fi t 1 T 1 fore T l pe i 1 1 1 1 l 1 T 1 1 j l 1 1 s i i i 4 ak 1 j siu i 1 _ i i i i FSIQ VIQ PI FSIQ VIQ PIQ Exercise e What is the mean value for VIQ for the full population e How many males females were included in this study Hint use tab completion to find out the methods that can be called instead of mean in the above example e What is the average value of MRI counts expressed in log units for males and females Note groupby_gender boxplot is used for the plots above see this example page 289 Plotting data Pandas comes with some plotting tools pandas tools plotting using matplotlib behind the scene to display statistics of the data in dataframes Scat gt gt gt from pandas tools import plotting gt gt gt ter matrices plotting scatter_matrix data Weight Height MRI_Count 180 160 As 140 120 76 74 o 72 70 T 68 I 66 64 62 1050000 E 1000000 5 950000 i 900000 850000 800000 23 2 23 23 8 amp sBBRARREK GS 8 8 8 8 8 ia in 7 Height S 8 8 8 8 8 W
316. nto np tloatsz eps 11920929 e 07 gt gt gt np finfo np float64 eps Forced casts gt gt gt da DNp array Il 7 1 27 1 61 2 2204460492503131e 16 gt gt gt b a astype int lt truncates to integer gt gt gt b gt gt gt np float32 le 8 np float32 1 1 aire Ll iL 1 True gt gt gt np float 4 le 8 np float64 1 1 Rounding False gt gt gt a noranta 1 es l an 1 Or 25y SL Al 5 Complex floating point numbers 22 lo Mo aroun a Sera ag ote Deen Oe Aege 2a complex128 two 64 bit floats gt gt gt Tp eround a astyoe int complex192 two 96 bit floats platform dependent cae complex256 two 128 bit floats platform dependent arcray Ip 4p 2 2 45 4 Smaller data types Different data type sizes If you don t know you need special data types then you probably don t Comparison on using f loat 32 instead of float64 e Half the size in memory and on disk Integers signed e Half the memory bandwidth required may be a bit faster in some operations int8 8 bits In 1 a np zeros le6 dtype np float64 int16 16 bits int32 32 bits same as int on 32 bit platform In 2 b np zeros le6 dtype np float32 int64 64 bits same as int on 64 bit platform In 3 timeit axa an ee oma a eS 1000 loops best of 3 1 78 ms per loop A E oe l deype ime 64 e
317. nts data ij tuple flexible slicing changing sparsity structure is efficient e very fast conversion to and from CSR CSC formats 2 5 Sparse Matrices in SciPy 209 2 5 Sparse Matrices in SciPy 210 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 fast matrix vector sparsetools fast and easy item wise operations manipulate data array directly fast NumPy machinery no slicing no arithmetics directly use facilitates fast conversion among sparse formats when converting to other format usually CSR or CSC duplicate entries are summed together x facilitates efficient construction of finite element matrices Examples create empty COO matrix gt gt gt mtx sparse coo_matrix 3 4 dtype np int8 gt gt gt mtx todense meaicicis lt 10 O OG O 1G OF Ol OF OOF ONM adtype rnEeE create using data ij tuple gt gt gt row np array 0 3 1 01 gt gt gt COl np darray 10 3 1 21 gt gt gt data np array 4 5 7 91 gt gt gt mtx sparse coo_matrix data row col shape 4 4 gt gt gt mtx lt 4x4 sparse matrix of type lt numpy int6o4 gt with 4 stored elements in COOrdinate format gt gt gt gt mtx todense macc 4 0 9 Ol OR 1 20 on Om Oe 202 Wie O 0 0 5 duplicates entries are summed together gt gt gt row ne array Or Or bh 3 1 on 2 gt gt COl me a
318. o gt Moa LaO ip LMES max 2 oy 2147483647 2147483647 In 4 Stimeit bxb OOOO OMS BESE Om So ITO ms per ooo Unsigned integers e But bigger rounding errors sometimes in surprising places i e don t use them unless you really need them uint8 8 bits uint16 16 bits uint32 32 bits uint64 64 bits Structured data types eee De LiPo mpe eime S 2 aia 242 ol l 4294967295 4294967295 sensor_code 4 character string position float value float Long integers gt gt gt samples np zeros 6 dtype sensor_code S4 Python 2 has a specific type for long integers that cannot overflow represented with an L at the end In vee l position float value float Python 3 all integers are long and thus cannot overflow 4 samplesendim gt gt gt NP MMEO np int64 max 2 63 gt 1 gt gt gt samples shape OP2ASS7ZA0SC68544 7 S507 AZ S37 A0SI6 S547 a8 Oly Gin gt gt gt samples dtype names sensor_code position value Floating point numbers gt gt gt samples ALFA I OS eee CBETA 1 0rd TAU alle OMSIT TATEA a 15 Oe Su ee ALEAT Sr oLD CTA 2 or gt gt gt samples aray CAE A OOBE OO EAU OO 1 3 NumPy creating and manipulating numerical data 69 1 3 NumPy creating and manipulating numerical data 70 Python Scientific lecture notes Release 2013 2 b
319. o reservoir min_release max_release state attributes storage Range low min_storage high max_storage control attributes inflows Float desc Inflows hm3 release Range low min_release high max_release spillage Float desc Spillage hm3 update_storage Event desc Updates the storage to the next time step def _update_storage_fired self update storage state new_storage self storage self release self inflows self storage min new_storage self max_storage overflow new_storage self max_storage self spillage max overflow 0 def print_state self print Storage tRelease tInflows tSpillage str format e 7oim 27 26 for 1 in range 4 print str_format format self storage self release self inflows self spillage prine u 79 IE name main projectA Reservoir name Project A max_storage 30 max_release 5 0 hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 15 state release 5 state inflows 0 release the maximum amount of water during 3 time steps state update_storage True state print_state state update_storage True state print_state state update_storage True state print_state 3 4 Traits building interactive dialogs 346 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Dependency between objects can be made automati
320. o plt scatter and will control the color The same holds for parameters marker alpha and cmap that control respectively the type of marker used their transparency and the colormap plotting scatter_matrix data VI Using SymPy as a calculator Q MRI Count Height SymPy defines three numerical types Real Rational and Integer 3 1 Statistics in Python 317 3 2 Sympy Symbolic Mathematics in Python 318 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 The Rational class represents a rational number as a pair of two Integers the numerator and the denominator so Rational 1 2 represents 1 2 Rational 5 2 5 2 and so on gt gt gt from sympy import x gt gt gt a Rational 1 2 gt gt gt a LZ gt gt gt ax2 dL SymPy uses mpmath in the background which makes it possible to perform computations using arbitrary precision arithmetic That way some special constants like e pi oo Infinity are treated as symbols and can be evaluated with arbitrary precision gt gt gt pixx 2 2 pa gt gt gt pi evalf 3o 1415 9265358979 gt gt gt pi exp 1 evalf Saciol s A467 043 64 as you see evalf evaluates the expression to a floating point number There is also a class representing mathematical infinity called oo gt gt gt 00 gt 99999 True gt gt gt 00 1 exe Exercises 1 Calculate 2 with 100 decimals 2 Calculate 1 2 1
321. ocessing using Numpy and Scipy 226 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Exercise Open as an array the scikit image logo http scikit image org _static img logo png or an im age that you have on your computer Crop a meaningful part of the image for example the python circle in the logo Display the image array using matplot1lib Change the interpolation method and zoom to see the difference Transform your image to greyscale Increase the contrast of the image by changing its minimum and maximum values Optional use scipy stats scoreatpercentile read the docstring to saturate 5 of the darkest pixels and 5 of the lightest pixels Save the array to two different file formats png jpg tiff scikits image image processing in python Geometrical transformations gt gt gt lena misc lena gt gt gt Ix ly lena shape gt gt gt Cropping gt gt gt crop lena lenallx 4 lx 7 4 ly 7 4 ly 7 4 gt gt gt up lt gt down flip gt gt gt flip_ud_lena np flipud lena gt gt gt rotation gt gt gt rotate_lena ndimage rotate lena 45 gt gt gt rotate_lena_noreshape ndimage rotate lena 45 reshape False 2 6 4 Image filtering Local filters replace the value of pixels by a function of the values of neighboring pixels Neighbourhood square choose size disk or more complicated structuring element
322. odel t coeffs So let s get our solution by calling scipy optimize leastsq with the following arguments e When we want to detect very small peaks in the signal or when the initial guess is too far from a good solution the result given by the algorithm is often not satisfying Adding constraints to the parameters of P a POE ORN ae the model enables to overcome such limitations An example of a priori knowledge we can add is the sign e an initial solution of our variables which are all positive e the additional arguments to pass to the function With the following initial solution gt gt gt from scipy optimize import leastsq gt gt gt x0 np array l3 50 20 1 dtype float gt gt gt x flag leastsq residuals x0 args waveform_l t gt gt gt print x compare the result of scipy optimize leastsq and what you can get with 2270263341 27 02020742 15 479245 62 323 05636228 scipy optimize fmin_slsqp when adding boundary constraints And visualize the solution gt gt gt plt plot t waveform_1l t model t x lt matplotilib lines Line2D object at gt lt matplotilib lines Line2D object at s 2 gt gt gt plt legend waveform model lt matplotlib legend Legend object at gt gt gt gt plt show Remark from scipy v0 8 and above you should rather use scipy optimize curve_fit which takes the model and the data as arguments so you don t need to
323. ofiling runs lasting around 10s 2 4 2 Profiling Python code No optimization without measuring e Measure profiling timing e You ll have surprises the fastest code is not always what you think Timeit In IPython use t imeit http docs python org library timeit html to time elementary operations In 1 import numpy as np In 2 a np arange 1000 masi seimeit Eel was 2 MUGDOVO Loos best Or S25 5273 Us per Toop In 4 Stimeit a x 2 1 1000 loops best of 3 154 us per loop In 5 Stimeit a x a MUGCOVOMCoP Ss OSsSt Om Seo on us per loop Use this to guide your choice between strategies Note For long running calls using St ime instead of St imeit it is less precise but faster Profiler Useful when you have a large program to profile for example the following file For this example to run you also need the ica py file import numpy as np from scipy import linalg from ica import fastica def test data np random random 5000 100 u s v linalg svd data pea np dot ul 107r data results fastica pca T whiten False if name _ main_ test Note This is a combination of two unsupervised learning techniques principal component analysis PCA and independent component analysis ICA PCA is a technique for dimensionality reduction i e an algorithm to explain the observed variance in your data using less dimensions ICA is a source seperation technique
324. olver A initial approximation to the K eigenvectors X scipy rand A shape 0 K preconditioner based on ml M ml aspreconditioner compute eigenvalues and eigenvectors with LOBPCG W V lobpcg A X M M tol le 8 largest False plot the eigenvectors import pylab pylab figure figsize 9 9 2 5 Sparse Matrices in SciPy 219 2 5 Sparse Matrices in SciPy 220 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 http code google com p pyamg for i in range K Py lobi subplot 3 3 alsril e Pysparse pylab title Higenvector sd s i oylab peolor iyi Ll reshape N N own sparse matrix classes pylab axis equal pey labka isio Ei pylab show http pysparse sourceforge net matrix and eigenvalue problem solvers examples pyamg_with_lobpcg py example by Nils Wagner 2 6 Image manipulation and processing using Numpy and Scipy examples lobpcg_sakurai py Authors Emmanuelle Gouillart Ga l Varoquaux e output This section addresses basic image manipulation and processing using the core scientific modules NumPy and SciPy Some of the operations covered by this tutorial may be useful for other kinds of multidimensional array processing than image processing In particular the submodule scipy ndimage provides functions operating on n dimensional NumPy arrays
325. omState shuffle TypeError len of unsized object This also happens with long arguments and so np random permutation X shape 0 where X is an array fails on 64 bit windows where shape is a tuple of longs It would be great if it could cast to integer or at least raise a proper error for non integer types I m using Numpy 2 4 1 burle trom the ortiucial tarball on Windows 64 with Visual studio 2008 on Python org 64 bit Python 0 What are you trying to do 1 Small code snippet reproducing the bug if possible e What actually happens e What you d expect 2 Platform Windows Linux OSX 32 64 bits x86 PPC 3 Version of Numpy Scipy gt gt gt print np __version__ erie Check that the following is what you expect gt gt gt print np file_ Teas In case you have old broken Numpy installations lying around If unsure try to remove existing Numpy installations and reinstall Contributing to documentation 1 Documentation editor e http docs scipy org numpy e Registration Register an account Subscribe to scipy dev mailing list subscribers only Problem with mailing lists you get mail x But you can turn mail delivery off Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 x change your subscription options at the bottom of http mail scipy org mailman listinfo scipy dev Send a mail scipy dev mailing list ask for
326. on e The Python Standard Library documentation http docs python org library index html e Python Essential Reference David Beazley Addison Wesley Professional os module operating system functionality A portable way of using operating system dependent functionality Directory and file manipulation Current directory Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 In 44 fp open junk txt w In 45 fp close in 46 Junk txk im os listidir os curdir Out 46 True In 47 os remove junk txt In 48 junk Ext an os listdir os curdir Out 48 False os path path manipulations os path provides common operations on pathnames in 17 os T getcwd Owe Ly Users 7 churns src Scipy2009 scipy_2Z009_tutorial source List a directory ra sil os Mistar os c rdir Ouecliol Mo index rst Swoon Pie noma language rst SWEET view_array py swp Staten _templates Mpasi e eypest SEn Conie PY y econtrol flow rst debugging rst Make a directory In 32 2 os smkaie junkaur in Wiss sunken in os lista os curdis Cue Sai True Rename the directory in 36 os rename junkdir ftoodir En Lavi qunkair An coe lised r Osr curdin Out 37 False In 38 iE mciiic in Os listdir OS uieel ic Out 33 2 True ra MI os rmalr aoe cate
327. on Lo VON Prob Omnibus 0 000 Jargque Bera JB Se LOS Skew 0 010 PrOD I2 omiS7 aula tO SaaS 1S Conc Nor 2s OL Tips on specifying model Forcing categorical the Gender is automatical detected as a categorical variable and thus each of its different values are treated as different entities An integer column can be forced to be treated as categorical using gt gt gt model ols VIO C Gender data fit Intercept We can remove the intercept using 1 in the formula or force the use of an intercept using L Tip By default statsmodel treats a categorical variable with K possible values as K 1 dummy boolean variables the last level being absorbed into the intercept term This is almost always a good default choice however it is possible to specify different encodings for categorical variables http statsmodels sourceforge net devel contrasts html Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Link to t tests between different FSIQ and PIQ To compare different type of IQ we need to create a long form table listing IQs where the type of IQ is indicated by a categorical variable gt gt gt data_fisq pandas DataFrame iq data FSIQ type fsiq gt gt gt data pig pandas Datarrame i q s datal PIO eype s pig gt gt gt data_long pandas concat data_fisg data_piq gt gt gt print data_long
328. on gt 0 12 required because of the complex vars cython mandel pyx python setup py build_ext i and Gry ie ouc With imn tais directory gt gt gt import mandel gt gt gt mandel mandel 0 1 2j Se OSE OSE OH SHE HEHEHE HEHEHE HE The elementwise function cdef void mandel_single_point double complex xz_in double complex c_in double complex z_out nogil The Mandelbrot iteration Some points of note It s NOT allowed to call any Python functions here The Ufunc loop runs with the Python Global Interpreter Lock released Hence the nogil And so all local variables must be declared with edern Note also that this function receives pointers to the data cdef double complex z z_in 0 cdef double complex c c_in 0 cdef int k the integer we use in the for loop TODO write the Mandelbrot iteration for one point here as you would write it in Python Se SH OSE SHE Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Say use 100 as the maximum number of iterations as the CukOri for 2 realee2 2 mag TODO mandelbrot iteration should go here Return the answer for this point z out O z Boilerplate Cython definitions The litany below is particularly long Se SHE SHE SHE SE cdef extern from numpy arrayobject h void import_array ctypedef int npy_intp eder enum
329. on of partial differential equations PDEs the finite element method mechanical engineering electrotechnics physics e graph theory nonzero at i j means that node i is connected to node j Prerequisites recent versions of e numpy e scipy e matplotlib optional e ipython the enhancements come handy Sparsity Structure Visualization e spy from matplotlib e example plots 2 5 Sparse Matrices in SciPy 204 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 2 5 2 Storage Schemes seven sparse matrix types in scipy sparse 1 csc_matrix Compressed Sparse Column format 2 csr_matrix Compressed Sparse Row format 3 bsr_matrix Block Sparse Row format 4 lil_matrix List of Lists format 5 dok_matrix Dictionary of Keys format 6 coo_matrix COOrdinate format aka IJV triplet format 7 dia_matrix DIAgonal format each suitable for some tasks many employ sparsetools C module by Nathan Bell assume the following is imported gt gt gt import numpy as np gt gt gt import scipy sparse as sps gt gt gt import matplotlib pyplot as plt warning for NumPy users the multiplication with is the matrix multiplication dot product not part of NumPy passing a Sparse matrix object to NumPy functions expecting ndarray matrix does not work 2 5 Sparse Matrices in SciPy 205 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013
330. onstant gt gt gt sy ndimage sobel im axis l mode constant gt gt gt sob np hypot sx sy square Sobel x direction Sobel filter Sobel for noisy image Histogram based segmentation no spatial information Segmentation gt gt gt n 10 gt gt gt 1 256 gt gt gt im np zeros l 1 gt gt gt np random seed 1 gt gt gt points lx xnp random random 2 nxx2 gt gt gt im points 0 astype np int points 1 astype np int 1 gt gt gt im ndimage gaussian_filter im sigma 1 4 n gt gt gt mask im gt im mean astype np float gt gt gt mask 0 1 im 2 6 Image manipulation and processing using Numpy and Scipy 232 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt img mask 0 2x np random randn xmask shape gt gt gt hist bin_edges np histogram img bins 60 gt gt gt bin_centers 0 5 bin_edges 1 bin_edges 1 gt gt gt binary_img img gt 0 5 histogram L L 1 0 0 5 0 0 0 5 1 0 L5 Use mathematical morphology to clean up the result gt gt gt Remove small white regions gt gt gt open_img ndimage binary_opening binary_img gt gt gt Remove small black hole gt gt gt close_img ndimage binary_closing open_img Exercise Check that reconstruction operations erosion propagation produce a better result than opening c
331. or loops Find tricks to avoid for loops using numpy arrays For this masks and indices arrays can be useful e Broadcasting 2 4 Optimizing code 201 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Use broadcasting page 62 to do operations on arrays as small as possible before combining them In place operations In 1 a np zeros 1le7 In 2 Stimeit global a a Oxa 1 looses bestoken iil ms josie loos In 3 Stimeit global a a x 0 10 loops best of 3 48 4 ms per loop note we need global a in the timeit so that it work as it is assigning to a and thus considers it as a local variable Be easy on the memory use views and not copies Copying big arrays is as costly as making simple numerical operations on them In 1 a np zeros 1e7 In 212 stimeit a copy 10 loops best of 3 124 ms per loop In 3 timeit a l OMUE CPS OCS Obes Suis PETER Beware of cache effects Memory access is cheaper when it is grouped accessing a big array in a continuous way is much faster than random access This implies amongst other things that smaller strides are faster see CPU cache effects page 168 In 1 c np zeros le4 1e4 order C In 2 timeit c sum axis 0 IMAC OPIS bestiok So Oro SSCs Oop In 3 timeit c sum axis 1 1 loops best of 3 188 ms per loop In 4 c strides out Tale 80000 8 This is the reason why Fortran ordering
332. orners gt gt gt ndimage binary_opening a astype np int gt gt gt a np zeros 5 5 dtype np int gt gt gt afl 4 1 4 1 a 4 4 1 gt gt gt a array TIO r OU O Ol O 1 1 1 01 O Ty ty Le 8 O 1 17 1 0 aiccay TIO OG O Ol l0 0 Iy OF Olly Op Ly ap le Ol Op es Le 0G 0c O Ol Application remove noise 2 6 Image manipulation and processing using Numpy and Scipy 231 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt square np zeros 32 32 gt gt gt Square FLO TO Ol COE gt gt gt np random seed 2 gt gt gt x y 32xnp random random 2 20 astype np int gt gt gt square x y 1 gt gt gt open_square ndimage binary_opening square gt gt gt eroded_square ndimage binary_erosion square gt gt gt reconstruction ndimage binary_propagation eroded_square mask square Closing dilation erosion Many other mathematical morphology operations hit and miss transform tophat etc 2 6 5 Feature extraction Edge detection Synthetic data gt gt gt im np zeros 256 256 gt gt gt am 64 64 6A GA S SSS gt gt gt im ndimage rotate im 15 mode constant gt gt gt im ndimage gaussian_filter im 8 Use a gradient operator Sobel to find high intensity variations gt gt gt sx ndimage sobel im axis 0 mode c
333. ot lena ww Evom Scipy amMport muse l misc lena misc imsave lena png 1 uses the Image module PIL importi matp sO ello m pyp l otkas PiE plt imshow 1 plt show data are read from the file and not loaded into memory Working on a list of image files gt gt gt for i in range 10 255 10000 reshape 100 100 im np random random_integers 0 iL alia ite misc imsave random_ 02d png gt gt gt from glob import glob gt gt gt filelist glob randomx png gt gt gt filelist sort Creating a numpy array from an image file 2 6 2 Displaying images Use matplotlib and imshow to display an image inside amatplotlib figure gt gt gt 1 misc lena gt gt gt import matplotlib pyplot as plt gt gt gt plt imshow l cmap plt cm gray lt matplotlib image AxesImage object at Ox gt Increase contrast by setting min and max values gt gt gt plt imshow l cmap plt cm gray vmin 30 vmax 200 lt matplotlib image AxesImage object at Ox gt gt gt gt Remove axes and ticks gt gt gt jOuLic oeb lt alis COFEE 025 S1 5 S125 O5 Draw contour lines gt gt gt plte concour lL 60 2m0 lt Matplotlib contour OuadContourset eee For fine inspection of intensity variations use interpolation nearest gt gt gt from scipy import misc gt gt gt lena misc lena gt gt gt pit imshow 1
334. ouble precision e optional umfpack real and complex systems double precision only recommended for performance wrappers now live in scikits umfpack check out the new scikits suitesparse by Nathaniel Smith Examples e import the whole module and see its docstring gt gt gt from scipy sparse linalg import dsolve gt gt gt help dsolve Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt mtxl mtx astype np float32 gt gt gt x dsolve spsolve mtxl rhs use_umfpack False gt gt gt print x 106 211s 5 9 S le al gt gt gt Print Error 251 gt ited x rhs Error is OF On OR om solve as double precision real gt gt gt mtx2 mtx astype np float64 gt gt gt x dsolve spsolve mtx2 rhs use_umfpack True gt gt gt print x l 106 e 5 i ss ileal gt gt gt print Error sa s mtx2 2 rhs Error Oe ON OF OR omi solve as single precision complex gt gt gt mtxl mtx astype np complex64 gt gt gt x dsolve spsolve mtxl rhs use_umfpack False gt gt gt print x MOG 00 73 2i1 0s0 5 De SrO S SrO L 00 3 gt gt gt Prine Error say s mexi k x rhs ERCON POO Cee a Oaa Waray OEO solve as double precision complex gt gt gt mtx2 mtx astype np complex128 gt gt gt x dsolve spsolve mtx2 rhs use_umfpack True gt gt gt print x lOe
335. ow new_storage self max_storage return max overflow 0 on_trait_change storage def print_state self print Storage tRelease tInflows tSpillage Stroirormer ANE join 137 22 fer i in range 4 1 print str_format format self storage self release self inflows self spillage prine x 79 if name__ _main__ projectA Reservoir name Project A max_storage 30 max_release 5 hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 25 state release 4 state inflows 0 The patterns supported by the on_trait_change method and decorator are powerful The reader should look at the docstring of HasTraits on_trait_change for the details Some more advanced traits The following example demonstrate the usage of the Enum and List traits from traits api import HasTraits Str Float Range Enum List from traitsui api import View Item class IrrigationArea HasTraits name Str surface Float desc Surface ha erop ENUM Alfalfa WinSee Comtem class Reservoir HasTraits name Str max_storage Float le6 desc Maximal storage hm3 max_release Float 10 desc Maximal release m3 s head Float 10 desc Hydraulic head m efficiency Range 0 1 irrigated_areas List IrrigationArea def energy_production self release Returns the energy production Wh for the given release
336. part it just pulls in stuff from the Numpy C headers woy imos voidx data npy_intp teps teps teps npy_intp teps teps teps fe ra teps teps teps _intp x steps vold func ces teps teps voids steps voidx void void steps void void void npy_intp steps void func voidx voidx voidx voidx voids void voidx func func func ie lave voidx func func FUNS func TUNC FUNS FUNS EUNE func FUNE 2 2 Advanced Numpy 171 2 2 Advanced Numpy 172 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 cdef void xelementwise_funcs 1 NPY _LONGDOUBLE NPY _CFLOAT NPY _CDOUBLE NPY_CLONGDOUBLE NPY DATETIME NPY _IITMEDELTA NPY _ OBJECT NPY _ STRING NPY_UNICODE NPY_ VOID Reminder some pre made Ufunc loops i Solution building an ufunc from scratch EOE if _i float elementwise_func float input_1 z i T T e ee A a l eee ce The elementwise function By Urune ri float elementwise_func float input_1 float input_2 4 e PyUtune d lt a double elementwise_func double input_1 PyUfunc_dd_d double elementwise_func double input_1 double input_2 ae teas aca TEN a p aie put_2 EON cdef void mandel_single_point double complex z_in EyURUNCEDHAD elementwi
337. pdb to drop in post mortem debugging on exceptions and nosetests pdb failure to inspect test failures using the debugger In addition you can use the Python interface for the debugger in nose by installing the nose plugin ipdb plugin You can than pass ipdb and ipdb failure options to nosetests e Calling the debugger explicitly Insert the following line where you want to drop in the debugger import pdb pdb set_trace Warning When running nosetests the output is captured and thus it seems that the debugger does not work Simply run the nosetests with the s flag 2 3 Debugging code 194 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 To debug with gdb the Python script segfault py we can run the script in gdb as follows Graphical debuggers and alternatives gdb python e For stepping through code and inspecting variables you might find it more convenient to use a graph ical debugger such as winpdb e Alternatively pudb is a good semi graphical debugger with a text user interface in the console e Also the pydbgr project is probably worth looking at gdb run segfault py Starting program usr bin python segr aule py Thread debugging using libthread_db enabled Program received signal SIGSEGV Segmentation fault _strided_byte_copy dst 0x8537478 360 343G outstrides 4 src Ox86c0
338. pe and mat vec some optional parameters example gt gt gt import numpy as np gt gt gt from scipy sparse linalg import LinearOperator gt gt gt def mv v return np array 2 v 0 3 v 1 gt gt gt A LinearOperator 2 2 matvec mv gt gt gt A lt 2x2 LinearOperator with unspecified dtype gt Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt A matvec np ones 2 array ll Zap Ss gt gt gt A x np ones 2 array 22 2 1 A Few Notes on Preconditioning e problem specific e often hard to develop if not sure try ILU available in dsolve as spilu Eigenvalue Problem Solvers The eigen module e arpack a collection of Fortran77 subroutines designed to solve large scale eigenvalue problems e lobpcg Locally Optimal Block Preconditioned Conjugate Gradient Method works very well in com bination with PyAMG example by Nathan Bell Compute eigenvectors and eigenvalues using a preconditioned eigensolver In this example Smoothed Aggregation SA is used to precondition the LOBPCG eigensolver on a two dimensional Poisson problem with Dir richlee boundary Condi erTons moon import scipy from scipy sparse linalg import lobpcg from pyamg import smoothed_aggregation_solver from pyamg gallery import poisson N 100 K 9 A poisson N N format csr create the AMG hierarchy ml smoothed_aggregation_s
339. pe combinations Making it easier 3 ufunc_loop is of very generic form and Numpy provides pre made ones PyUfunc_f_f float elementwise_func float input_1 PyUfunc_ff_ ffloat elementwise_func float input_l float input_2 PyUfunc_d_d double elementwise_func double input_ 1 Ufunc_dd_ Jddouble elementwise_func double input_1l double input_2 elementwise_func npy_cdouble input npy_cdoublex output PyUfunc_DD_ Delementwise_func npy_cdouble inl npy_cdouble in2 npy_cdouble out PyU Lune e Only elementwise_func needs to be supplied e except when your elementwise function is not in one of the above forms Exercise building an ufunc from scratch The Mandelbrot fractal is defined by the iteration zg e where c x ty is a complex number This iteration is repeated if z stays finite no matter how long the iteration runs c belongs to the Mandelbrot set 2 2 Advanced Numpy 170 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e Make ufunc called mandel z0 c that computes z 20 for k in range iterations A Awn a C say 100 iterations or until z real 2 z imag 2 gt 1000 Use it to determine which c are in the Mandelbrot set e Our function is a simple one so make use of the PyUFunc_ helpers e Write it in Cython See also mandel pyx mandelplot py Fix the parts marked by TODO Compile this file by Cyth
340. play some text with an arrow E 2 lt MpL 3 pl plot t t 0 np cos t color blue linewidth 2 5 linestyle Ole Seaeeer ler mT Memos te el s0 5 Oc oltor Mole Dl annotete m Ssin fract 2 pi 3t trac veqre se i2ie xy t np sin t xycoords data xytext 10 30 textcoords offset points fontsize 16 arrowprops dict arrowstyle gt connectionstyle arc3 rad 2 Ole olor Gey 2 10 ne sinti color red sine warcth Ar 5 linesty lest 1 pli scatter le mnp sinir I SO color red pl annotate lr Scos Erac 2Z oi 3 irac 1 i2is xy t np cos t xycoords data xytext 90 50 textcoords offset points fontsize 16 arrowprops dict arrowstyle gt connectionstyle arc3 rad 2 Devil is in the details Hint Documentation e Artists e BBox Tip The tick labels are now hardly visible because of the blue and red lines We can make them bigger and we can also adjust their properties such that they ll be rendered on a semi transparent white background This will allow us to see both the data and the labels for label in ax get_xticklabels ax get_yticklabels label set_fontsize 16 label set_bbox dict facecolor white edgecolor None alpha 0 65 1 4 3 Figures Subplots Axes and Ticks Python Scientific lecture notes Release 2013 2 beta euroscipy 2013
341. pleasant development environment than for example Matlab More geek oriented Not all the algorithms that can be found in more specialized software or toolboxes 1 1 Scientific computing with tools and workflow 4 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 1 1 2 Scientific Python building blocks Unlike Matlab Scilab or R Python does not come with a pre bundled set of modules for scientific computing Below are the basic building blocks that can be combined to obtain a scientific computing environment e Python a generic and modern computing language Python language data types st ring int flow control data collections lists dictionaries pat terns etc Modules of the standard library A large number of specialized modules or applications written in Python web protocols web frame work etc and scientific computing Development tools automatic testing documentation generation shell Konsole lt 2 gt faz e IPython an advanced Python shell http ipython org e Numpy provides powerful numerical arrays objects and routines to manipulate them http www numpy org e Scipy high level data processing routines Optimization regression interpolation etc http www scipy org 0 54 0 53 0 52 O0 51 e Matplotlib 2 D visualization publication ready plots http matplotlib org e Mayavi 3 D visualization http code enthou
342. plicated programs or libraries but not in the sense of being particularly specialized or particularly complicated It is important to underline that this chapter is purely about the language itself about features supported through special syntax complemented by functionality of the Python stdlib which could not be implemented through clever external modules The process of developing the Python programming language its syntax is very transparent proposed changes are evaluated from various angles and discussed via Python Enhancement Proposals PEPs As a result features described in this chapter were added after it was shown that they indeed solve real problems and that their use is as simple as possible Chapters contents e Iterators generator expressions and generators page 140 Iterators page 140 Generator expressions page 141 Generators page 141 Bidirectional communication page 142 Chaining generators page 144 e Decorators page 144 Replacing or tweaking the original object page 145 Decorators implemented as classes and as functions page 145 Copying the docstring and other attributes of the original function page 147 Examples in the standard library page 148 Deprecation of functions page 150 A while loop removing decorator page 151 A plugin registration system page 151 e Context managers page 152 Catching exceptions page 153 Using generators to de
343. plt legend Hare lynx Carrow i Loc 1 05 0S5 80000 70000 60000 Hare 50000 Lynx Carrot 40000 30000 20000 10000 0 1900 1905 1910 1915 1920 The mean populations over time 222 populations darak r 12 gt gt gt populations mean axis 0 array 34080 95238095 20166 06006060667 42400 The sample standard deviations gt gt gt populations std axis 0 array 20897 90645509 16254 59153691 3322 306225561 Which species has the highest population each year gt gt gt np argmax populations axis 1 array 2 OS OF OF I ly OS 27 ae 2 ay 2 OF 0 0 i 27 on Oe Zr Ze 1 3 NumPy creating and manipulating numerical data 60 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Worked Example diffusion using a random walk algorithm 1 2 1 2 1 O 1 d t Tip Let us consider a simple 1D random walk process at each time step a walker jumps right or left with equal probability We are interested in finding the typical distance from the origin of a random walker after t left or right jumps We are going to simulate many walkers to find this law and we are going to do so using array computing tricks we are going to create a 2D array with the stories each walker has a story in one direction and the time in the other shuffled jumps Position cumulated jumps sum time time gt gt gt n stories 1000 number o
344. ptimize fmin_powell Making your optimizer faster e Choose the right method see above do compute analytically the gradient and Hessian if you can e Use preconditionning when possible e Choose your initialization points wisely For instance if you are running many similar optimizations warm restart one with the results of another e Relax the tolerance if you don t need precision Computing gradients Computing gradients and even more Hessians is very tedious but worth the effort Symbolic computation with Sympy page 318 may come in handy Warning A very common source of optimization not converging well is human error in the computation of the gradient You can use scipy optimize check_grad to check that your gradient is correct It returns the norm of the different between the gradient given and a gradient computed numerically gt gt gt optimize check_grad f fprime 2 2 Z2 384185791015625e 07 See also scipy optimize approx_fprime to find your errors Synthetic exercices Exercice A simple quadratic function Optimize the following function using K 0 as a starting point inp random seed 0 IK np random normal size 100 100 def x return np sum np dot K x 1 2 np sum x 2 x2 Time your approach Find the fastest approach Why is BFGS not working well 2 7 Mathematical optimization findin
345. py fftpack fft will compute the fast Fourier transform gt gt gt from scipy import fftpack gt gt gt sample _ freq fftpack fftfreq sig size d time_step gt gt gt sig_fft fftpack fft sig Because the resulting power is symmetric only the positive part of the spectrum needs to be used for finding the frequency gt gt gt pidxs np where sample_freg gt 0 gt gt gt freqs sample_fregq pidxs gt gt gt power np abs sig_fft pidxs Peak frequency eee 0 050 100 150 200 250 300 350 400 45 0 5 10 15 20 25 Frequency Hz The signal frequency can be found by 1 5 Scipy high level scientific computing 106 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt freq freqs power argmax gt gt gt np allclose freg 1 period check that correct freg is found True Now the high frequency noise will be removed from the Fourier transformed signal gt gt gt sig_fft np abs sample_freq gt freq 0 The resulting filtered signal can be computed by the scipy fftpack ifft function 22 gt Mali cig fitpack Liru sig tie The result can be viewed with gt gt gt import pylab as plt gt gt gt plt figure mat potion t igure r ergue object ae Ox yee gt gt gt plt plot time_vec sig lt matplotilib lines Lline2D object at 0x gt gt gt gt plt plot time_vec main_sig linewidth 3
346. r Listeners can also be added to classes using the on_trait_change decorator from traits api import HasTraits Instance DelegatesTo Float Range from traits api import Property on_trait_change from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage For the simplicity of the example the release is considered in hm3 timestep and not in m3 s mow reservoir Instance Reservoir max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storage Property depends_on inflows release control attributes inflows Float desc Inflows hm3 release Range low min_release high max_release spillage Property desc Spillage hm3 depends_on storage inflows release Private traits te eee eee ete eet eeee eee ete eteeeeeeeeeeeeteeteet tt _storage Float 3 4 Traits building interactive dialogs 352 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Traits property implementation tee teeta teed st tated ttt at tte tt ttt def _get_storage self new_Storage self _storage self release self inflows return min new_storage self max_storage def _set_storage self storage_value self _storage storage_value def _get_spillage self new_storage self _storage self release self inflows overfl
347. r unexpected indent test py line 2 All this indentation business can be a bit confusing in the beginning However with the clear indentation and in the absence of extra characters the resulting code is very nice to read compared to other languages Indentation depth Inside your text editor you may choose to indent with any positive number of spaces 1 2 3 4 However it is considered good practice to indent with 4 spaces You may configure your editor to map the Tab key to a 4 space indentation In Python x y the editor is already configured this way Style guidelines Long lines you should not write very long lines that span over more than e g 80 characters Long lines can be broken with the character gt gt gt long_line Here is a very very long line that we break in two parts Spaces Write well spaced code put whitespaces after commas around arithmetic operators etc gt gt gt a 1 yes gt gt gt a l1 too cramped A certain number of rules for writing beautiful code and more importantly using the same conventions as anybody else are given in the Style Guide for Python Code 1 2 The Python language 33 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Quick read If you want to do a first quick pass through the Scipy lectures to learn the ecosystem you can directly skip to the next chapter NumPy creating and manipulating numerical data p
348. ractive 2D plots e Mayavi 3D visualization of scientific data based on VTK e Envisage application plugin framework for building scriptable and extensible applications In this tutorial we will focus on Traits 3 4 2 Example Throughout this tutorial we will use an example based on a water resource management simple case We will try to model a dam and reservoir system The reservoir and the dams do have a set of parameters e Name e Minimal and maximal capacity of the reservoir hm3 e Height and length of the dam m e Catchment area km2 e Hydraulic head m e Power of the turbines MW e Minimal and maximal release m3 s e Efficiency of the turbines 3 4 Traits building interactive dialogs 340 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 The reservoir has a known behaviour One part is related to the energy production based on the water released A simple formula for approximating electric power production at a hydroelectric plant is P phrgk where e P is Power in watts e pis the density of water 1000 kg m3 e his height in meters e ris flow rate in cubic meters per second e gis acceleration due to gravity of 9 8 m s2 e kis a coefficient of efficiency ranging from 0 to 1 Tip Annual electric energy production depends on the available water supply In some installations the water flow rate can vary by a factor of 10 1 over the course of a year The second part of the b
349. rch grate Enter search terms or a module class or function name a second Ipython shell just to display help and docstrings se ren sie Ciona une nate nc toni e Numpy s and Scipy s documentations can be browsed online on http docs scipy org doc The search button is quite useful inside the reference documentation of the two packages http docs scipy org doc numpy reference and http docs scipy org doc scipy reference Tutorials on various topics as well as the complete API with all docstrings are found on this website Sdpy documantation editor Back to Numpy Scipy documentation gt Numpy documentation editor scipy org editor Wiki Docstrings Changes Milestones Search Stats Patch Login scipy ndimage morphology binary_dilation ue value at the corresponding mask element are modified at ich the output is placed By default a new array is created amp Previous upNext Highlight ell C Match case Reached end of page continued from top e Numpy s and Scipy s documentation is enriched and updated on a regular basis by users on a wiki http docs scipy org numpy As a result some docstrings are clearer or more detailed on the wiki and you may want to read directly the documentation on the wiki instead of the official documentation website Note that anyone can create an account on the wiki and write better documentation this is an easy way to contribute to an open source project and imp
350. rch to June gt gt gt James MasterStudent james e Janes Internship mandatory from March to June gt gt gt james set_age 23 gt gt gt James age 23 The MasterStudent class inherited from the Student attributes and methods Thanks to classes and object oriented programming we can organize code with different classes corresponding to different objects we encounter an Experiment class an Image class a Flow class etc with their own methods and attributes Then we can use inheritance to consider variations around a base class and re use code Ex from a Flow base class we can create derived StokesFlow TurbulentFlow PotentialFlow etc 1 3 NumPy creating and manipulating numerical data Authors Emmanuelle Gouillart Didrik Pinte Ga l Varoquaux and Pauli Virtanen This chapter gives an overview of Numpy the core tool for performant numerical computing with Python 1 3 1 The Numpy array object Section contents e What are Numpy and Numpy arrays page 42 e Creating arrays page 44 e Basic data types page 46 e Basic visualization page 47 e Indexing and slicing page 49 e Copies and views page 52 e Fancy indexing page 53 What are Numpy and Numpy arrays Numpy arrays Python objects e high level number objects integers floating point e containers lists costless insertion and append dictionaries fast lookup 1 3 NumPy creating and manipulating numerical
351. re int input_2_strides_n steps 5 steps int input_2_strides_p steps 6 int output_strides_n steps 7 int output_strides_p steps 8 int m dimension 1 core dimensions are added after int n dimension 2 the main dimension order as in int p dimension 3 signature int l for i 0 i lt dimensions 0O i matmul_for_strided_matrices input_1l input_2 output strides for each array input_1l steps 0 input_2 steps 1 output steps 2 gt gt gt import numpy core umath_tests as ut gt gt gt ut matrix_multiply signature m n n p gt m p gt gt gt x np ones 10 2 4 gt gt gt y np ones 10 4 5 22 gt t matrixi muliaply x y Shape 10 27 5 e the last two dimensions became core dimensions and are modified as per the signature e otherwise the g ufunc operates elementwise e matrix multiplication this way could be useful for operating on many small matrices at once Generalized ufunc loop Matrix multiplication m n n p gt m p 2 2 Advanced Numpy 177 2 2 3 Interoperability features Sharing multidimensional typed data Suppose you 1 Write a library than handles multidimensional binary data 2 Want to make it easy to manipulate the data with Numpy or whatever other library 3 but would not like to have Numpy as a dependency Currently 3 solutions 1 the old buffer
352. re values to finish immediately It allows the generator __ de 1___ method to destroy objects holding the state of generator Let s define a generator which just prints what is passed in through send and throw gt gt gt import itertools gt gt gt def g print starc for i in itertools count Print yielding lt i z 1 try ans yield i except GeneratorExit print closing raise except Exception as e print yield raised r e else prine y1eld returned we se atic gt gt gt it g gt gt gt next it o Aaris yielding 0 0 gt gt gt it send 11 yield returned 11 y alleine tas al gt gt gt it throw IndexError yield raised IndexError S yielding A 2 gt gt gt it close Clos ine 2 1 Advanced Python Constructs 143 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 next or next In Python 2 x the iterator method to retrieve the next value is called next It is invoked implicitly through the global function next which means that it should be called __next__ Just like the global func tion iter calls ___iter__ This inconsistency is corrected in Python 3 x where it next becomes it __next__ For other generator methods send and throw the situation is more complicated because they are not called implicitly by the interpreter Nevertheless there s a proposed s
353. reates new arrays in the C order by default Slicing with integers e Everything can be represented by changing only shape strides and possibly adjusting the data pointer e Never makes copies of the data gt gt gt x mnp array lil 2 SB 8 So Gl ayp Np ine 3Z a gt yi x le l gt gt gt y array 6 5 4 3 2 1 dtype int32 gt gt gt y strides 4 gt gt gt y x 2 gt gt gt y array interrace tdara t Ol x array Interface dara 0 gt gt gt x np zeros 10 10 10 dtype np float gt gt gt x strides 2 2 Advanced Numpy 164 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 S007 13 0 e So xe no Str rLdES E00 2407 32 e Similarly transposes never make copies it just swaps strides gt gt gt x np zeros 10 10 10 dtype np float gt gt gt x strides S007 80 a gt gt gt x T strides CS E07 oO But not all reshaping operations can be represented by playing with strides gt gt gt a np arange 6 dtype np int8 reshape 3 2 gt gt gt b a T gt gt gt b strides Cle So far so good However gt gt gt str a data Xx O00 x01 x02 x03 x04 x05 gt gt gt b array IO 27 Ay 1 3 5 dtype int8 gt gt gt c b reshape 3 2 gt gt gt c euaceny 0 2 4 3 5 dtype inte Here there is no way to represent the array c given one stride and the block of m
354. rint a A Prine bp runs On import pringe o 1 2 The Python language 30 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 if name main _ print_a is only executed when the module is run directly print_a Importing it In 11 import demo2 b In 12 import demo2 Running it In 13 Srun demo2 b a Scripts or modules How to organize your code Note Rule of thumb e Sets of instructions that are called several times should be written inside functions for better code reusability e Functions or other bits of code that are called from several scripts should be written inside a module so that only the module is imported in the different scripts do not copy and paste your functions in the different scripts How modules are found and imported When the import mymodule statement is executed the module mymodule is searched in a given list of directories This list includes a list of installation dependent default path e g usr lib python as well as the list of directories specified by the environment variable PYTHONPATH The list of directories searched by Python is given by the sys path variable In 1 import sys in 2 sys path Outl2i Luy home varoquau local bin masr lib oython2 7 home varoquau local lib python2 7 site packages usr lib python2 7 dist packages musr local lib pythonzZ 7 dist packages l Mo
355. riting to image files page 223 Displaying images page 224 Basic manipulations page 225 Statistical information page 226 Geometrical transformations page 227 Image filtering page 227 Blurring smoothing page 227 Sharpening page 228 Denoising page 228 Mathematical morphology page 229 Feature extraction page 232 Edge detection page 232 Segmentation page 232 Measuring objects properties ndimage measurements page 234 2 6 1 Opening and writing to image files Writing an array to a file Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt misc imsave lena png lena First we need to create the PNG file gt gt gt lena misc imread lena png gt gt gt type lena Sooo MmUIMNIOW ndan ray e gt gt gt lena shape lena dtype alz el2 deype Uuime 8 dtype is uint8 for 8 bit images 0 255 Opening raw files camera 3 D images gt gt gt lena tofile lena raw Create raw file gt gt gt lena_from_raw np fromfile lena raw dtype np uint8 gt gt gt lena_from_raw shape COPIA gt gt gt lena_from_raw shape 512 512 Need to know the shape and dtype of the image how to separate data bytes For large data use np memmap for memory mapping gt gt gt lena_memmap np memmap lena raw dtype np uint8 shape 512 512 Displaying Lena Small example to pl
356. rks Python framework Versions 2 5 e List of command line arguments passed to a Python script In 100 sys argv Coe OOk I Users churns lecal bin ipython sys path is a list of strings that specifies the search path for modules Initialized from PYTHONPATH 1 2 The Python language 37 In 121 sys path Owe LLZ s may Users cburns local bin Users cburns local lib python2 5 site packages grin 1 1l py2 5 egg Users cburns local lib python2 5 site packages argparse 0 8 0 py2 5 egg Users cburns local lib python2 5 site packages urwid 0 9 7 1 py2 5 egg Users cburns local lib python2 5 site packages yolk 0 4 1 py2 5 egg Users cburns local lib python2 5 site packages virtualenv 1 2 py2 5 egg 1 2 The Python language 38 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 pickle easy persistence Catching exceptions Useful to store arbitrary objects to a file Not safe or fast try except In 1 import pickle In 10 while True In 2 11 None Stearn CS try EE x int raw_input Please enter a number in 3 picklevdump l tile test pki Tw Acree break en except ValueError In 4 pickle load file test pkl Aer ro prine That was no valid number Iry again Our li4 1 Nome Stan ates
357. rove the tools you are using e Scipy s cookbook http www scipy org Cookbook gives recipes on many common problems frequently en 1 6 Getting help and finding documentation 137 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 amp matplotlib l home search examples galery d modules index Click on any image to see full size image and source code SENN f ee e f ar ar or Ki ii matplotlib countered such as fitting data points solving ODE etc ad a LF e Matplotlib s website http matplotlib sourceforge net features a very nice gallery with a large number of plots each of them shows both the source code and the resulting plot This is very useful for learning by example More standard documentation is also available Finally two more technical possibilities are useful as well e In Ipython the magical function psearch search for objects matching patterns This is useful if for example one does not know the exact name of a function In 3 import numpy as np In 4 tpsearch np diag np diag np diagrtilat np diagonal e numpy lookfor looks for keywords inside the docstrings of specified modules In 45 numpy lookfor convolution Search results for convolution numpy convolve Returns the discrete linear convolution of two one dimensional sequences numpy bartlett Return the Bartlett window numpy correlate Discrete
358. row oriented operations e slow column slicing expensive changes to the sparsity structure use actual computations most linear solvers support this format Examples e create empty CSR matrix gt gt gt mtx sparse csr_matrix 3 4 dtype np int8 gt gt gt mtx todense matcix OF Or OF On LO O Or One O 0 Or Oll dtype ints create using data ij tuple cee MON NPr array 0 O ly 2 42 All gt gt gt COl np array 0r 2 2 OW 1 Al gt gt gt data p array il 2 37 4 5 SI gt gt gt mtx sparse csr_matrix data row col shape 3 3 gt gt gt mtx lt 3xS Sparse matrix Of type uk eee numpey MEGAN with 6 stored elements in Compressed Sparse Row format gt gt gt gt mtx todense matea i ils Of 2 LOr Oa AS gt gt gt mtx data 2 5 Sparse Matrices in SciPy 212 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 arcay liy 27r Sr 4r 5r Glor gt gt gt mtx indices array 10 2 2 Op ly Sly dtype int32 gt gt gt mtx indptr array T07 27 2 oll datrype Inte e create using data indices indptr tuple cee data np array I 2 3 A 5 6 tee IINCLOSS npara 0 2 2 O il All gt gt gt ndpter mp array I0 27 gt 37 6N gt gt gt mtx sparse csr_matrix data indices indptr shape 3 3 gt gt gt mtx todense mat cix O 21 DOy 0 el la Sr Si Compressed Sparse Column
359. rray MO 2 Ip 3S 1 o o gt gt gt data npserrey I 1 1 4 1 1 2 gt gt gt mtx sparse coo_matrix data row col shape 4 4 gt gt gt mtx todense mac crx OST O Lz Ol LOZ O Olle LOOO Olle Or Or Or Ty no slicing gt gt gt mtx 2 3 Traceback most recent call last TypeError coo_matrix object m Traceback most recent call last Typebrror coo matrix ob ject 2 5 Sparse Matrices in SciPy 211 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Compressed Sparse Row Format CSR e row oriented three NumPy arrays indices indptr data indices is array of column indices data is array of corresponding nonzero values x indptr points to row starts in indices and data x lengthis n_row 1 last item number of values length of both indices and data nonzero values of the i th row are data indptr i indptr it 1 with column indices indices indptr i indptr i l item i j can be accessed as data indptr i k where k is position of j in indices indptr i indptr it 1 subclass of _cs_matrix common CSR CSC functionality subclass of _data_matrix sparse matrix classes with data attribute e fast matrix vector products and other arithmetics sparsetools e constructor accepts dense matrix array Sparse matrix shape tuple create empty matrix data ij tuple data indices indptr tuple e efficient row slicing
360. rt Implement the quicksort algorithm as defined by wikipedia function quicksort array var list less greater if length array lt 2 return array select and remove a pivot value pivot from array for each x in array if x lt pivot 1 then append x to less else append x to greater return concatenate quicksort less pivot quicksort greater 1 2 The Python language 26 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 1 2 5 Reusing code scripts and modules For now we have typed all instructions in the interpreter For longer sets of instructions we need to change track and write the code in text files using a text editor that we will call either scripts or modules Use your favorite text editor provided it offers syntax highlighting for Python or the editor that comes with the Scientific Python Suite you may be using e g Scite with Python x y Scripts Tip Let us first write a script that is a file with a sequence of instructions that are executed each time the script is called Instructions may be e g copied and pasted from the interpreter but take care to respect indentation rules The extension for Python files is py Write or copy and paste the following lines in a file called test py message Hello how are you for word in message split print word Tip Let us now execute the script interactively that is inside the python interpreter This is maybe the most common u
361. rvoir s ReservoirEvolution HasTraits reservoir Instance Reservoir name DelegatesTo reservoir inflows Array dtype np float64 shape None releass Array dtype np float64 shape None initial_stock Float stock Property depends_on inflows releases initial_stock month Property depends_on stock Traits view teeeeeee ee eee teste eeeeeeeeeeeeeeee eee eed sseee sete ttttt traits view View Item name Group ChacoPlotItem month stock show_label False MF width 500 resizable True 3 4 Traits building interactive dialogs 355 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Traits properties ete ete eee eeteeee eee eee eteeeeeeeeeeee tee tte ttzt def _get_stock self U fixme should handle cases where we go over the max storage woe return self initial_stock self inflows self releases cumsum def _get_month self return np arange self stock size if name _main__ reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 initial_stock 10 imi LOWS es np array Go Gr 4p Bp I 2p Op O Sp I S 3S lasg Dy 3 5 3 Si 5 3 2 1 3 31 releases_ts np array view ReservoirEvolution reservoir reservoir inflows inflows_ts releases releases_ts view configure_traits Edit properties Name
362. s from skimage import data camera data camera val filters threshold_otsu camera mask Camera lt val Grayscale mathematical morphology Mathematical morphology operations are also available for non binary grayscale images int or float type Erosion and dilation correspond to minimum resp maximum filters Higher level mathematical morphology are available tophat skeletonization etc See also Basic mathematical morphology is also implemented in scipy ndimage morphology The 3 3 Scikit image image processing 331 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 scipy ndimage implementation works on arbitrary dimensional arrays Example of filters comparison image denoising gt gt gt from skimage morphology import disk gt gt gt coins data coins gt CONS Zoom eeOums ll OsoORm SOS 7 0i gt gt gt median_coins filters rank median coins_zoom disk 1 gt gt gt from skimage import restoration gt gt gt tv_coins restoration denoise_tv_chambolle coins_zoom weight 0 1 gt gt gt gaussian_coins filters gaussian_filter coins sigma 2 Gaussian filter Median filter TV filter 3 3 4 Image segmentation Image segmentation is the attribution of different labels to different regions of the image for example in order to extract the pixels of an object of interest Binary segmentation foreground background H
363. s args i 1 print args In s addio ree Cte 2 lt function main add to dict gt In 4 add to dict uel oer ars Blom ee oi Tn 5 adadi to AICE Marts 3r Mio Ay in 6 add to dict P sa E Soh Tip More involved example implementing python s slicing In 98 def slicer seq start None stop None step None A uume kement Dasi e PY non S TCin a E return seq start stop step In 10I thyme one fish two ish d fish blue fish splito In 102 rhyme Owel Oe pea tonet sais a Evolt ne Gavia RoC Atri a Ulla T rrsaY In 103 slicer rhyme put Me ee UE Vome stash ewer Shashi redr kivsing r baie TEISA In 104 slicer rhyme step 2 out IOA oene two red slue In 105 slicer rhyme 1 step 2 Cite Ge piesa sie a MSI a ies in In 106 slicer rhyme start 1 stop 4 step 2 Crete MOMS ase T Ershini eR The order of the keyword arguments does not matter 1 2 The Python language 23 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 In 107 slicer rhyme step 2 start 1 stop 4 Cue LOTI ats y at teasine but it is good practice to use the same ordering as the function s definition Keyword arguments are a very convenient feature for defining functions with a variable number of arguments especially when default values are to be used in most calls to the fun
364. s e Figure container e Axes container e Axis containers e Tick containers e Path tutorial e Introduction e B zier example e Compound paths e Transforms tutorial e Introduction e Data coordinates e Axes coordinates e Blended transformations e Using offset transforms to create a shadow effect e The transformation pipeline Matplotlib documentation e User guide e FAQ Installation Usage How To Troubleshooting Environment Variables e Screenshots Code documentation The code is well documented and you can quickly access a specific command from within a python session Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 argument allowing for multiple x x y pairs with an optional format string For example each of the following is legal plot y plot x and y using default line style and color alkor xa We loo plot x and y using blue circle markers PESE 7 plot vy Wsing x as index array ONN plenty rt ditto but with red plusses If x and or xyx is 2 dimensional then the corresponding columns will be plotted gt gt gt import pylab as pl gt gt gt help pl plot Help on runet lon plot im modules mato Lot llon py olloie plot sargs x kwargs Plot lines and or markers to the class matplotlib axes Axes xargs is a variable length 1 4 Matplotlib plotting 99 Galleries The matplotlib gallery i
365. s 5 5 dtype np int gt gt gt a 1l 4 1 4 1 a 4 4 1 gt gt gt a 0 Up o anmray M 0 igs li y 10 0 0 k i Ul l So 2a K r r LO O O TO gt gt gt Opening removes small objects gt gt gt ndimage binary_opening a structure np ones 3 3 astype np int aiscay OTOT O OOO 0 1 1 1 0 LO Ly Dy Ly Olly Op Ly Ty Ly iO Oe O Oe OR A gt gt gt Opening can also smooth corners gt gt gt ndimage binary_opening a astype np int array TTO O OG Ol Ole Gig sl 20 1 OF en Olly Lote Ol al O 40 Oe Oi On Oe Wi e Closing ndimage binary_closing Exercise Check that opening amounts to eroding then dilating An opening operation removes small structures while a closing operation fills small holes Such operations can therefore be used to clean an image 1 5 Scipy high level scientific computing 123 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt a np zeros 50 50 22 gt a Olo les il 1 gt gt gt a 0 25x np random standard_normal a shape gt gt gt mask a gt 0 5 gt gt gt opened_mask ndimage binary_opening mask gt gt gt closed_mask ndimage binary_closing opened_mask opened_mask closed_mask Check that the area of the reconstructed square is smaller than the area of the initial square The opposite would o
366. s from boilerplate utility functions to high level recent algorithms e Filters functions transforming images into other images NumPy machinery Common filtering algorithms e Data reduction functions computation of image histogram position of local maxima of corners etc e Other actions I O visualization etc 3 3 2 Input output data types and colorspaces T O skimage io gt gt gt from skimage import io Reading from files skimage io imread gt gt gt import os gt gt gt filename os path join skimage data_dir camera png gt gt gt camera io imread filename Works with all data formats supported by the Python Imaging Library or any other I O plugin provided to imread with the plugin keyword argument Also works with URL image paths gt gt gt logo io imread http scikit image org _static img logo png Saving to files gt gt gt io imsave local_logo png logo imsave also uses an external plugin such as PIL 3 3 Scikit image image processing 327 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Data types Image ndarrays can be represented either by integers signed or unsigned or floats Careful with overflows with integer data types gt gt gt camera data camera gt gt gt camera dtype dtype uint8 gt gt gt camera_multiply 3 camera Different integer sizes are possible 8
367. s also incredibly useful when you search how to render a given graphic Each example comes with its source A smaller gallery is also available here Mailing lists Finally there is a user mailing list where you can ask for help and a developers mailing list that is more technical 1 4 6 Quick references Here is a set of tables that show main properties and styles 1 4 Matplotlib plotting 100 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Line properties Line styles Property Description Appearance yr oo yw L OL PVA H KD ANMSt oC AH IOT alpha or a alpha transparency on 0 1 scale Pt tot tot tl E z fF Y ka a e a Fe Tn _ A a Aliased t gt e AVAPBH xXOyr4A lt gt 0A 0 antialiased True or False use antialised ase I rendering Anti aliased 3 7 AVIPHB xXeyrA lt gt 8B Q color or c matplotlib color arg I tT tt tt tt tot o AVIAPH xXOyr4 lt gt 868 o 0 I E a linestyle or Is see Line properties page 101 i e eAaVvdarpreu xeryri lt gt esese linewidth or lw float the line width in points 2 i e eavearpmgix eryrix gt roee e is a di a D a a solid_capstyle Cap style for solid lines a AVIPH ixeyix x gt ree _ 0 solid_joinstyle Join style for solid lines T
368. s only been evaluated 30 times compared to 120 without the gradient Newton and quasi newton methods Newton methods using the Hessian 2nd differential Newton methods use a local quadratic approximation to compute the jump direction For this purpose they rely on the 2 first derivative of the function the gradient and the Hessian 2 7 Mathematical optimization finding minima of functions 244 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 ite function calls o 1 2 3 E tot G65 5 iot 6 io i 9 10 An ill conditionned quadratic function ene Note that as the quadratic approximation is exact the Newton method is blazing fast An ill conditionned non quadratic function Sana Here we are optimizing a Gaussian which is always below its quadratic approximation As a result the Newton method overshoots and leads to oscillations An ill conditionned very non quadratic function In scipy the Newton method for optimization is implemented in scipy optimize fmin_ncg cg here refers to that fact that an inner operation the inversion of the Hessian is performed by conjugate gradient scipy optimize fmin_tnc can be use for constraint problems although it is less versatile gt gt gt def f x The rosenbrock function ee tecurn 4 Ss CL xO eweet a eli lt LO 2e2 waz gt gt gt def fprime x ae returni npr orca
369. s the following techniques e Python C Api e Ctypes e SWIG Simplified Wrapper and Interface Generator e Cython These four techniques are perhaps the most well known ones of which Cython is probably the most advanced one and the one you should consider using first The others are also important if you want to understand the wrapping problem from different angles Having said that there are other alternatives out there but having understood the basics of the ones above you will be in a position to evaluate the technique of your choice to see if it fits your needs The following criteria may be useful when evaluating a technology e Are additional libraries required e Is the code autogenerated e Does it need to be compiled e Is there good support for interacting with Numpy arrays e Does it support C Before you set out you should consider your use case When interfacing with native code there are usually two use cases that come up e Existing code in C C that needs to be leveraged either because it already exists or because it is faster e Python code too slow push inner loops to native code Each technology is demonstrated by wrapping the cos function from math h While this is a mostly a trivial example it should serve us well to demonstrate the basics of the wrapping solution Since each technique also includes some form of Numpy support this is also demonstrated using an example where the cosine is computed on some kind o
370. s well as to do some more exercices page 77 gt gt gt a In place sort arro io aoe S ly Up ee 1 3 3 More elaborate arrays Sorting with fancy indexing Poe a Npvaerrey 4 3 0 2 gt gt gt j np argsort a Section contents gt gt gt j array 2 3 i O gt gt gt alj array p 2 Sp Ap e More data types page 68 e Structured data types page 70 e maskedarray dealing with propagation of missing data page 71 Finding minima and maxima SS 2 np erray A 3 121 More data types gt gt gt jJ_max np argmax a gt gt gt jJ_min np argmin a Casting gt gt gt MMe jamal in 0 2 Bigger type wins in mixed type operations gt gt gt np array tilly 2 Si e s oe es eo ae Assignment never changes the type 1 3 NumPy creating and manipulating numerical data 67 1 3 NumPy creating and manipulating numerical data 68 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 float1l6 16 bits gt gt gt a np array 1 2 3 _ gt gt gt a dtype float32 32 bits dtyoe l int64 float64 64 bits same as float alo 1 9 A float as truncated fo integer float96 96 bits platform dependent same as np Longdoub1le gt gt gt a float128 128 bits platform dependent same as np Longdouble array dp 3 1 gt o gt Np ti
371. se of scripts in scientific computing Note in Ipython the syntax to execute a scriptis srun script py For example in A srun test py Hello how are you In 2 message Cite ais Helios how ace youn The script has been executed Moreover the variables defined in the script such as message are now available inside the interpreter s namespace Tip Other interpreters also offer the possibility to execute scripts e g execfile in the plain Python inter preter etc It is also possible In order to execute this script as a standalone program by executing the script inside a shell terminal Linux Mac console or cmd Windows console For example if we are in the same directory as the test py file we can execute this in a console S python test py Hello how are you Tip Standalone scripts may also take command line arguments In file py import sys print sys argv python file py test arguments tile py best targumentsi 1 2 The Python language 27 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Warning Don t implement option parsing yourself Use modules such as optparse argparse or docopt Importing objects from modules In 1 import os In 2 os oaae module Os trom uer lib oytnon2 G os pyc gt ie PS 2 Oss Litsiechiie 47 ont lole Meonk iEyA loas te types ESEU i Gomiticoll sellers ESELI ViEuUnG easOmnSetasitem python language st
372. se_func complex_double xinput complex_doublex complex_double si So AR i double complex c_in IPW AU IE ae _IDJD _ ID elementwise_func complex_double inl complex_double in2 complex_double x A 4 E _ a 2 double complex n7 Aout nOg 4 The Mandelbrot iteration The full list is above ee i Type codes Some points of note NPY _BOOL NPY_BYTE NPY_UBYTE NPY_SHORT NPY_USHORT NPY_INT NPY_UINT f E E E E a E E E Goa w u a NPY_LONG NPY _ULONG NPY _LONGLONG NPY _ULONGLONG NPY_FLOAT NPY _DOUBLE i Yry NPY_LONGDOUBLE NPY_CFLOAT NPY_CDOUBLE NPY_CLONGDOUBLE NPY_DATETIME i 4 ae T fhe Utune Leor runs Mico the Python Global Interoreter Lock released NPY _TIMEDELTA NPY _OBJECT NPY _STRING NPY_UNICODE NPY_VOID i Eia Hence the nogid And so all local variables must be declared with cdef LOCO Awe O saa MODOS Siilcalole PURUN eE ie ins 5 ee impit output eypes lO mma ODO l A pie prec ae 9 Note also that this function receives pointers to the data TODOR FNM rest Or MNPC OUt PUC VOCS i Hee the traditional solution to passing complex variables around This thing is passed as the data parameter for the generic i i PyUbPune loo Orc te know whieh iuncti on it sinouilc eal a ea lt void gt mandel_ single point ede h pd une comple a Sn E cdef double complex c c_in 0 de eae Nae E E cdef int k the integer we use in t
373. simple statistical fits with plotting on pandas dataframes Let us consider a data giving wages and many other personal information on 500 individuals Berndt ER The Practice of Econometrics 1991 NY Addison Wesley Tip The full code loading and plotting of the wages data is found in corresponding example page 303 gt gt gt print data EDUCATION SOUTH SEX EXPERIENCE UNION WAGE AGE RACE 0 8 0 1 Zl 0 0 707570 32 Z 1 9 0 1 42 O 0 694605 Sl 3 2 12 0 0 i 0 0 824126 19 3 S 12 0 0 4 OF 0602060 22 3 Pairplot scatter matrices We can easily have an intuition on the interactions between continuous variables using seaborn pairplot to display a scatter matrix gt gt gt import seaborn gt gt gt seaborn pairplot data vars WAGE AGE EDUCATION kind reg Dep Variable sepal_width R squared 0 478 Model OLS Adj R squared 0 468 Method Least Squares I SiC GUC ALS ALS 44 63 Date Probo F statistic 1 58e 20 Time Log Likelihood 50 L65 No Observations LSC ATC 84 37 Df Residuals 146 BIC 96 41 Df Model 3 coef std err IDS e 95 0 Comer lime Intercept 229813 0099 293 989 O S006 Z2u7e5 S217 name T versicolor 1 4321 mdg T 0 000 i 840 i 124 name T virginica 6635 G22 516 o 502 C000 Se L69 ell oi petal_length 02905 omoi 4 920 C000 omme 0 418 Omnibus 258618 Durbin Watson T 753 Pra lou minellouis ae Os 23K Jar
374. size gt gt gt b 0 0 99 gt gt gt a np arange 4 gt gt gt a gt gt gt a resize 8 array IG 2 Sly gt gt gt a 44 Soy e em Gay I0 L 27 3 0 0 07 01 Beware reshape may also return a copy However it must not be referred to somewhere else 1 3 NumPy creating and manipulating numerical data 65 1 3 NumPy creating and manipulating numerical data 66 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 eb a Exercise Sorting gt gt gt a resize 4 Traceback most recent call last ale V lt siechiasY line i ain lt mocule gt ValueError cannot resize an array that has been referenced or is e Try both in place and out of place sorting e Try creating arrays with different dtypes and sorting them e Use all or array_equal to check the results referencing another array im this way Use the resize function e Look at np random shuffle for a way to create sortable input quicker Traceback most recent call last e Combine ravel sort and reshape File lt stdin gt line 1 in lt module gt e Look at the axis keyword for sort and rewrite the previous exercise ValueError cannot resize an array that has been referenced or is Exercise Shape manipulations Summary e Look at the docstring for reshape especially the notes section which has some more information What do you need to know to
375. sk 1 return struct astype np bool a gt gt gt def granulometry data sizes None s max data shape if sizes None sizes range l s 2 2 granulo ndimage binary_opening data structure disk_structure n sum for n in sizes return granulo SSS gt gt gt np random seed 1 gt gt gt n 10 gt gt gt 1 256 gt gt gt im np zeros 1 1 gt gt gt points lx xnp random random 2 nxx2 gt gt gt im points 0 astype np int points 1 astype np int 1 gt gt gt im ndimage gaussian_filter im sigma 1 4 n SS gt gt gt mask im gt im mean SSS gt gt gt granulo granulometry mask sizes np arange 2 19 4 eee ex Sy L snape e a CS o oeiee 0 sy gt gt gt no hypot X sx 2 Y sy 2 gt gt gt rbin 20x r r max astype np int gt gt gt radial_mean ndimage mean l labels rbin index np arange l e Other measures rboin max A 4 6 8 10 12 14 16 18 See also More on image processing e The chapter on Scikit image page 324 e Other more powerful and complete modules OpenCV Python bindings CellProfiler ITK with Python bindings 2 6 Image manipulation and processing using Numpy and Scipy 237 2 6 Image manipulation and processing using Numpy and Scipy 238 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 2 7 Mathematical optimization finding min
376. sobel_text filters hsobel text Enhances contrast in large almost uniform regions Mathematical morphology See https en wikipedia org wiki Mathematical_morphology Probe an image with a simple shape a structuring element and modify this image according to how the shape locally fits or misses the image Default structuring element 4 connectivity of a pixel Uses the following linear kernel for computing horizontal gradients gt gt gt from skimage import morphology gt gt gt morphology diamond 1 array 0 1 OT iy ip Ae O 1 O dtype uints Erosion minimum filter Replace the value of a pixel by the minimal value covered by the structuring element gt gt gt a np zeros 7 7 dtype np int gt gt gt alko e225 il gt gt gt a array CIPOT Or O Of O Ol W or Ae Ao le IOs o O 205 ay a Op o Pa Oe tye i oe o 0l ele Or Iy dle Cally o LG Reh Oy 1y 17 1 Om Ql Or o O OR Ol Oe HOA gt gt gt morphology binary_erosion a morphology diamond 1 astype np uints8 3 3 Scikit image image processing 329 3 3 Scikit image image processing 330 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Ss larray I B bi OGO GOOO Ser ower eye r r r k r r r r r E F y i OF i F a O 0 0 07 0 07 Ol cleyne
377. sons all np linalg norm etc Solution Python source file 1 4 Matplotlib plotting Thanks Many thanks to Bill Wing and Christoph Deil for review and corrections Authors Nicolas Rougier Mike Miiller Ga l Varoquaux 1 4 Matplotlib plotting 81 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Chapters contents e Introduction page 82 Python and the pylab mode page 83 pylab page 83 e Simple plot page 83 Plotting with default settings page 84 Instantiating defaults page 84 Changing colors and line widths page 85 Setting limits page 86 Setting ticks page 86 Setting tick labels page 87 Moving spines page 87 Adding a legend page 88 Annotate some points page 88 Devil is in the details page 89 e Figures Subplots Axes and Ticks page 89 Figures page 90 Subplots page 90 Axes page 90 Ticks page 91 e Other Types of Plots examples and exercises page 91 Regular Plots page 93 Scatter Plots page 93 Bar Plots page 94 Contour Plots page 94 Imshow page 95 Pie Charts page 95 Quiver Plots page 96 Grids page 96 Multi Plots page 96 Polar Axis page 97 3D Plots page 97 Text page 98 e Beyond this tutorial page 98 Tutorials page 98 Matplotlib documentation page 99 Code documentation page 99 Galleries page 100
378. sso alpha_ Ou On a oye These estimators are called similarly to their counterparts with CV appended to their name Exercise On the diabetes dataset find the optimal regularization parameter alpha 3 6 scikit learn machine learning in Python 378 D diff 321 323 differentiation 321 dsolve 323 E equations algebraic 322 differential 323 integration 322 M Matrix 323 P Python Enhancement Proposals PEP 255 142 PEP 3118 178 PEP 3129 152 PEP 318 145 152 PEP 342 142 PEP 343 152 PEP 380 144 PEP 380 id13 144 PEP 8 147 S solve 322 Index 379
379. st fit or all the corresponding statistical parame ters HHEAAEAAAAAAAAAAAAABAABAABAAAAAABAAAAAABAABAABAAAAAABAAAAAAAAAAAAAAAAAAAAAAAEE Also shows how to make 3d plots statistical analysis import statsmodels formula api as sm Note that this model is not the plot displayed above it is one 3 1 Statistics in Python 299 3 1 Statistics in Python 300 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Script output Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Residual 438 27576 201607 62 959564 NaN NaN Python source code plot_regression_3d py OLS Regression Results Dep Variable R squared Model Adj R squared Method Least Squares i SiC SNC aL Sic Le Date Mon 21 Sep 2015 Frob F statistic Time 19 0836 Log Likelihood No Observations 441 AMG Df Residuals Ie Siete s Se oo i 0 Salles 95 03 Conte int Omnibus D rbin Watson Prob Omnibus 3 Jarque Bera JB Skew 3 BCoOBD JB Kurtosis Retrieving manually the parameter estimates 4 50523302 gt 3 117 S4237 0 3109122 6 ANOVA results df sum_sq mean_sq E PR gt F Lo B9204 SOLZI 29284 SOTII 625962199 27990230E C9 1 1055220089 LOSS 220089 16 760536 S20 SUE IIS U5 Original author Thomas Haslwanter import numpy as np import matplotlib pyplot as plt import pandas For 3d plots This import is necessary to have 3D plotti
380. stem check the documentation for details e This may be somewhat deceptive since the math library exists in compiled form on the system already If you were to wrap a in house library you would have to compile it first which may or may not require some additional effort We may now use this as before In 1 import cos_module RS In 2 cos_module Type module String Form lt module cos_module from cos_module py gt File home esc git working scipy lecture notes advanced interfacing_with_c ctypes cos_modu DOocString no docstring gt In 3 dir cos_module Owell Sl s LO owale ias v i clove 1 en Sale tare ae name__ package__ JS oos LONCI et ypesu CTAC liore y t vona In 4 cos_module cos_func 1 0 Cmte 0750502305862139 Insi coc moduler cos Fune 0M0 Chita ull 1 0 In 6 cos_module cos_func 3 14159265359 Qutlol 1 0 As with the previous example this code is somewhat robust although the error message is not quite as helpful since it does not tell us what the type should be Ene Ae COS mOcle Cos ETa heen ArgumentError Traceback most recent call last lt ipython input 7 llbee483665d gt in lt module gt il cos_mocule cos _iitine U ROOY home esc git working scipy lecture notes advanced interfacing_with_c ctypes cos_modtile py in cos 12 der eos Tune arg T3 UUU NES ror COS Trom mecan
381. t gen Start 3 gt gt gt next gen middle 4 gt gt gt next gen ifn sine Traceback most recent call last StopIteration Traceback most recent call last StopIteration Contrary to a normal function where executing f would immediately cause the first print to be executed gen is assigned without executing any statements in the function body Only when gen next is invoked by next the statements up to the first yield are executed The second next prints middle and execution halts on the second yield The third next prints finished and falls of the end of the function Since no yield was reached an exception is raised What happens with the function after a yield when the control passes to the caller The state of each generator is stored in the generator object From the point of view of the generator function is looks almost as if it was running in a separate thread but this is just an illusion execution is strictly single threaded but the interpreter keeps and restores the state in between the requests for the next value Why are generators useful As noted in the parts about iterators a generator function is just a different way to create an iterator object Everything that can be done with yield statements could also be done with next methods Nevertheless using a function and having the interpreter perform its magic to create an iterator has advantages A function can b
382. t gt distance am rav lalla Ole P Ie bee 2k She A ili Nie p teste esse 2 296000798 e L02216 4 123L0563 7 E Pp 2222006798 2 02RA 2 605591297 42547213595 ees A sel6227766 S3 00555128 4 242760069 S iy 4 452200563 4247203595 S 7 on OT6CS4 25 Or in color gt gt gt plt pcolor distance gt gt gt plte colorbar N m Remark the numpy ogrid function allows to directly create vectors x and y of the previous example with two significant dimensions Sree Me W nP ogr rdo sr Wes aoe on y amnas GI Ont Ll 2 laly KI atreay Tio 1 27 3 AD gt gt gt x shape y shape ar Aya lp S Poo is tanecee NPesqre Oa aT al 2 3 4 Tip So np ogrid is very useful as soon as we have to handle computations on a grid On the other hand np mgrid directly provides matrices full of indices for cases where we can t or don t want to benefit from broadcasting 1 3 NumPy creating and manipulating numerical data 64 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt x Y np mgridlO 4 024 gt gt gt a np zeros 3 2 gt gt gt x gt gt gt b a T reshape 3x2 array L100 O O Ol gt gt gt b 0 9 4 1 1 i j j gt gt gt a 2a 2 2r 2 array I Os oiy l3 37 gt z 211D Mon onay gt gt gt y On OR arca COn It 2
383. t never mind this mandel function name mandel z gt computes iterated gz cc docstring O unused mogul 2 2 Advanced Numpy 176 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Generalized ufuncs ufunc output elementwise_function input Both output and input can be a single array element only generalized ufunc output and input can be arrays with a fixed number of dimensions For example matrix trace sum of diag elements input shape n n output shape i e scalar a e i E Matrix product input_1 shape m n input_2 shape n p output shape m p m D y Col p DA m p e This is called the signature of the generalized ufunc e The dimensions on which the g ufunc acts are core dimensions Status in Numpy e g ufuncs are in Numpy already e new ones can be created with PyUFunc_FromFuncAndDataAndSignature e but we don t ship with public g ufuncs except for testing ATM Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 void gufunc_loop void xargs int dimensions int steps void data char xinput_l char args 0 x these are as previously char input_2 charx args 1 char output charx args 2 int input_l_stride_m steps 3 Ve SERLAGS for eha core GlLuMenisH Ons 7 int input_l_stride_n steps 4 are added after the non co
384. t in the file examples max speeds npy thus they will be loaded by using numpy gt gt gt import numpy as np gt gt gt max_speeds np load intro summary exercises examples max speeds npy gt gt gt years_nb max_speeds shape 0 Following the cumulative probability definition p_i from the previous section the corresponding values will be gt gt gt cprob np arange years_nb dtype np float32 1 years_nb 1 and they are assumed to fit the given wind speeds gt gt gt sorted_max_speeds np sort max_speeds Prediction with UnivariateSpline In this section the quantile function will be estimated by using the UnivariateSpline class which can represent a spline from points The default behavior is to build a spline of degree 3 and points can have different weights according to their reliability Variants are InterpolatedUnivariateSpline and LSQUnivariateSpline on which errors checking is going to change In case a 2D spline is wanted the BivariateSpline class family is provided All those classes for 1D and 2D splines use the FITPACK For tran subroutines that s why a lower library access is available through the spl rep and splev functions for respectively representing and evaluating a spline Moreover interpolation functions without the use of FITPACK parameters are also provided for simpler use see interpld interp2d barycentric_interpolate and so on For the Sprog maxima wind spe
385. t local linear isotropic filters blur the image ndimage uniform_filter A median filter preserves better the edges gt gt gt med_denoised ndimage median_filter noisy 3 noisy Gaussian filter Median filter AAA Median filter better result for straight boundaries low curvature gt gt gt im np zeros 20 20 gt gt gt iml 5s 5 Sol il gt gt gt im ndimage distance_transform_bf im gt gt gt im_ noise im 0 2 x np random randn im shape gt gt gt im med ndimage median_filter im_noise 3 r r Median filter Error Other rank filter ndimage maximum_filter ndimage percentile_filter Other local non linear filters Wiener scipy signal wiener etc Non local filters Exercise denoising e Create a binary image of Os and 1s with several objects circles ellipses squares or random shapes e Add some noise e g 20 of noise e Try two different denoising methods for denoising the image gaussian filtering and median filtering e Compare the histograms of the two different denoised images Which one is the closest to the his togram of the original noise free image See also More denoising filters are available in skimage denoising see the Scikit image image processing page 324 tutorial Mathematical morphology See http en wikipedia org wiki Mathematical_morphology Probe an image with a simple shape a structuring element and modify
386. t where it is called For this we use the line_profiler in the source file we decorate a few functions that we want to inspect with profile no need to import it profile def test data np random random 5000 100 u Ss v linalg svd data pea c npr dot ulk a ON data results fastica pca T whiten False Then we run the script using the kernprof py program with switches 1 line by line and v view to use the line by line profiler and view the results in addition to saving them kernprof py l v demo py Wrote profile results to demo py lprof Timer unit le 06 s File demo py Function test at line 5 Total time 14 2793 s Line Hits Time Per Hit Time Line Contents 5 profile 6 def test 7 dl 19015 19015 0 omi data np random random 5000 r00 8 il LAZAPILGS LAQALES 0 92 7 r Ss Y Linalligesve data 9 1 ROAS ZA 102782 0 omi pea moocoe ul sil 3 data 10 1 TIS TII9 O Os i results fastica pca T whiten False The SVD is taking all the time We need to optimise this line 2 4 3 Making code go faster Once we have identified the bottlenecks we need to make the corresponding code go faster Algorithmic optimization The first thing to look for is algorithmic optimization are there ways to compute less or better For a high level view of the problem a good understanding of the maths behind the algorithm helps However it is not u
387. ta pandas read_csv brain_size csv sep na_values Box plot of FSIQ and PIQ different measures od IQ plt figure figsize 4 3 data bDoxplot column I ESTOT PLO Boxplot of the difference plt figure figsize 4 3 pit boxoloe datel ES TON aata eE ON pler ticks PT CRS iO pit show Total running time of the example 0 29 seconds 0 minutes 0 29 seconds 3 1 Statistics in Python 289 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 mal 150 Female Male t40 130 120 110 100 0 FHIG WIC AQ Fan Wig PQ Figure 3 2 Plotting simple quantities of a pandas dataframe page 289 Plotting simple quantities of a pandas dataframe This example loads from a CSV file data with mixed nu merical and categorical entries and plots a few quantities separately for females and males thanks to the pandas integrated plotting tool that uses matplotlib behind the scene See http pandas pydata org pandas docs stable visualization html 3 1 Statistics in Python 290 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 150 Female Male 140 130 120 110 100 90 80 70 FSIQ VIQ PIQ 180 a o Weight 120 76 74 72 E o 70 T 68 I 66 64 62 1050000 1000000 J 950000 i 9300000 cc 850000 800000 q 3 2 o D 68 S FF FF 8 3 amp S 8 FS T a A T Height e Ss 8 8 8 8 Weight g S B amp R
388. tes Release 2013 2 beta euroscipy 2013 i sha fee Classi rr cacton clf svm SVC C 5 gamma 0 001 Siira mit OX Wicaulin jOo ai y eea predict on new images for i in range 10 print lfw_people target_names clf predict X_test_pca i 0 pl imshow X_test i reshape 50 37 cmap pl cm gray raw_input 3 6 6 Linear model from regression to sparsity Diabetes dataset The diabetes dataset consists of 10 physiological variables age sex weight blood pressure measure on 442 patients and an indication of disease progression after one year gt gt gt diabetes datasets load_diabetes gt gt gt diabetes _X train diabetes data 20 gt gt gt diabetes _X test diabetes data 20 gt gt gt diabetes_y_train diabetes target 20 gt gt gt diabetes_y_test diabetes target 20 The task at hand is to predict disease prediction from physiological variables Sparse models To improve the conditioning of the problem uninformative variables mitigate the curse of dimensionality as a feature selection preprocessing etc it would be interesting to select only the informative features and set non informative ones to 0 This penalization approach called Lasso can set some coefficients to zero Such methods are called sparse method and sparsity can be seen as an application of Occam s razor prefer simpler models to complex ones gt gt gt
389. th function The gradient is defined everywhere and is a continuous function 2 7 Mathematical optimization finding minima of functions 240 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Optimizing smooth functions is easier true in the context of black box optimization otherwise Linear Program ming is an example of methods which deal very efficiently with piece wise linear functions Noisy versus exact cost functions Noisy blue and non noisy green functions Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Noisy gradients Many optimization methods rely on gradients of the objective function If the gradient function is not given they are computed numerically which induces errors In such situation even if the objective function is not noisy Constraints Optimizations under constraints Here l lt a lt l l lt a lt l 2 2 A review of the different optimizers Getting started 1D optimization Use scipy optimize brent tominimize 1D functions It combines a bracketing strategy with a parabolic approximation 2 7 Mathematical optimization finding minima of functions 241 1 Brent s method on a quadratic function it converges 2 a gi in 3 iterations as the quadratic approximation is then exact 1 35 w i Brent s method on a non convex function note that 26 H t
390. the significance of this difference e the p value the probability of both processes being identical If it is close to 1 the two process are almost certainly identical The closer it is to zero the more likely it is that the processes have different means See also The chapter on statistics page 271 introduces much more elaborate tools for statistical testing and statistical data loading and visualization outside of scipy 1 5 7 Interpolation scipy interpolate The scipy interpolate is useful for fitting a function from experimental data and thus evaluating points where no measure exists The module is based on the FITPACK Fortran subroutines from the netlib project By imagining experimental data close to a sine function gt gt gt measured _time np linspace 0 ip tL gt gt gt noise np random random 10 2 1 le 1l gt gt gt measures np sin 2 np pi measured_time noise The scipy interpolate interpid class can build a linear interpolation function gt gt gt from scipy interpolate import interpld gt gt gt linear_interp interpld measured_time measures Then the scipy interpolate linear_interp instance needs to be evaluated at the time of interest gt gt gt computed_time np linspace 0 1 50 gt gt gt linear_results linear_interp computed_time A cubic interpolation can also be selected by providing the kind optional keyword argument gt gt gt cubic_interp interp
391. ties gt gt gt from skimage import measure Example compute the size and perimeter of the two segmented regions gt gt gt properties measure regionprops labels_rw gt gt gt prop area for prop in properties 70 0 liee 0l The ex S gt gt prop perimeter Eor proce In properties perimental scikit image viewer lL UOOS Oa een ol lee a skimage viewer matplotlib based canvas for displaying images experimental Qt based GUI toolkit See also gt gt gt from skimage import viewer for some properties functions are available as well in scipy ndimage measurements with a different API a list is returned gt gt gt new_viewer viewer ImageViewer coins gt gt gt new_viewer show i D Useful for displaying pixel values xercise continue For more interaction plugins can be added to the viewer e Use the binary image of the coins and background from the previous exercise e Compute an image of labels for the different coins gt gt gt new_viewer viewer ImageViewer coins e Compute the size and eccentricity of all coins ee a a r m eae n gt gt gt new_viewer show IL 3 3 6 Data visualization and interaction Meaningful visualizations are useful when testing a given processing pipeline Some image processing operations gt gt gt Coins data coins gt gt gt mask coins gt filters threshold_otsu coins
392. tific computing 121 e Erosion gt gt gt a np zeros 7 7 dtype np int gt gt gt all 6 2 5 1 gt gt gt a abray TTO P i Oe GO 07 We Ol eer OG Ie I Ike OF Ol or OF lle iy L OF Oe Lor 0 is L Ly 0 Oly Kor 0 ie L i 0 Oly Loy OF IS 1L i CF oln 1O Oy 0 0 07 07 Oi gt gt gt ndimage binary_erosion a astype a dtype aicicay Ll 10 Of O Or O O Ole Lop 0 0 O O O 0 O 0 0 1 0 0 oip 10 0 0 1 0 0 Oly 1 5 Scipy high level scientific computing 122 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 oe 0 0 iL 0 0 onp DO 07 0 Up Up 0 01 Oz 07 0 07 0 07 Oa gt gt gt Erosion removes objects smaller than the structure gt gt gt ndimage binary_erosion a structure np ones 5 5 astype a dtype amran OPEO OO O OU O O Ol Oy Gi Ce 405207 OF Ol POF Op Oly Op Gy 10 Oly OG yn he OG O Olly Ose Uy Oy WO seh 207 il LO 0 GO 0 GO 0 U1 Oe Oy 0 Oy Oe 205 Ola e Dilation gt gt gt a np zeros 5 5 gt gt gt a 2 2 1 gt gt gt a array IL Os Obs OR Oy OR Es SOs Oe Wags SCs ee YO aha 20ea Oe Ose Ose O Ose Ol E Our Wee SO Or Oy gt gt gt ndimage binary_dilation a astype a dtype array LL Os F OR 7 One Dee Og fs 10h o Gage dite dae Hee Oa Guys One Sees L055 Ole Ose Wag ler Gee 20a e Opening gt gt gt a np zero
393. tion choosing estimators and their parameters page 378 Grid search and cross validated estimators page 378 3 6 1 Loading an example dataset First we will load some data to play with The data we will use is a very simple flower database known as the Iris dataset We have 150 observations of the iris flower specifying some measurements sepal length sepal width petal length and petal width together with its subtype Iris setosa Iris versicolor Iris virginica To load the dataset into a Python object gt gt gt from sklearn import datasets gt gt gt iris datasets load_iris This data is stored in the data member whichis a n_samples n_features array gt gt gt iris data shape ARS OF a4 The class of each observation is stored in the t aarget attribute of the dataset This is an integer 1D array of length n_samples gt gt gt iris target shape 50r gt gt gt import numpy as np gt gt gt np unique iris target array OPED 3 6 scikit learn machine learning in Python 370 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 An example of reshaping data the digits dataset SN OU BP WN FF OO 0123 4 5 6 7 The digits dataset consists of 1797 images where each one is an 8x8 pixel image representing a hand written digit gt gt gt digits datasets load_digits gt gt gt digits images shape LVS oie E gt gt gt import pyla
394. to do operations on arrays of different sizes if Numpy can transform these arrays so that they all have the same size this conversion is called broadcasting The image below gives an example of broadcasting 8 8 8 2 XX SN ii 3 8 2 1i TD G E Let s verify gt gt gt a np tile np arange 0 40 10 3 1 T gt gt gt a array il 0 0 Oly LLO 10 10 20 20 20 30 S30 SONT gt gt b np array I0 17 21 gt gt gt a b array I 2il L0 Lis 12 207 2 24 O 31 32 We have already used broadcasting without knowing it gt gt gt a np ones 4 5 gt gt gt a 0 2 we assign an array of dimension 0 to an array of dimension 1 gt gt gt a N N N array L 2 ARAA al E l 1 Pog Fadly 1 3 NumPy creating and manipulating numerical data 62 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 day ea ey Ae Loly L ilopo lapo top lor telih An useful trick gt gt gt a np arange 0 40 10 gt gt gt a shape 4 gt gt gt a al np newaxis adds a new axis gt 2D array gt gt gt a shape 4 1 gt gt gt a array 0 10 20 L32011 gt gt gt a b acca Tio 2l Or He ey 20 21 22 3 Sil 32 I Tip Broadcasting seems a bit magical but it is actually quite natural
395. to use it when we want to solve a problem whose output data is an array with more dimensions than input data Worked Example Broadcasting Let s construct an array of distances in miles between cities of Route 66 Chicago Springfield Saint Louis Tulsa Oklahoma City Amarillo Santa Fe Albuquerque Flagstaff and Los Angeles See Mi leposts np array IOP Ls SOS 7SG Sil Milys yo SAA 1913 2448 gt gt gt distance_array np abs mileposts mileposts np newaxis gt gt gt distance_array ancay Uy 19e S03 736 STL Dilys 1475 154 1913 2448 i 193 Op MOS S388 672 Sl L277 2250 S097 M057 G 433 S68 872 1172 ZrAS 136 538 433 GO Iss 489 297 1712 evi Gee SGe Usa O 304 604 EST i75 877 Siz 4389 304 Gp 300 12731 Hazs5 1277 11727 9739 604 300 973 ls44 1346 2A e0 673 369 904 Wols LS LELO LEVY TSS 5351 2448 2250 214 0 A lot of grid based or network based problems can also use broadcasting For instance if we want to compute the 1 3 NumPy creating and manipulating numerical data 63 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 distance from the origin of points on a 10x10 grid we can do gt gt gt x y np arange 5 np arange 5 np newaxis Po distances Np asquy x 4 2 yee 7 gt g
396. trieve http lib stat cmu edu datasets CPS_85_Wages wages txt Give names to the columns names EDUCATION Number of years of education SOUTH 1 Person lives in South O Person lives elsewhere SEX 1 Female O Male EXPERIENCE Number of years of work experience UNION 1 Union member O Not union member WAGE Wage dollars per hour AGE years 3 1 Statistics in Python 308 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 RACE 1 Other 2 Hispanic 3 White OCCUPATION 1 Management 2 Sales 3 Clerical 4 Service 5 Professional 6 Other SECTOR O Orher L Mantitackuring 2 Consteruck ion MARR O Unmarried 1 Married short_names n split 0 for n in names data pandas read_csv wages txt skiprows 27 skipfooter 6 sep None header None data columns short_names Log transform the wages because they typically are increased with multiplicative factors import numpy as np data WAGE np log10 data WAGE HeeHHH HHH HHH HHH HHH HEH HHH HHH AHHH eee EEE a ERE EEE EEE H Plot scatter matrices highlighting different aspects import seaborn seaborn pairplot data vars WAGE AGE EDUCATION kind reg seaborn pairplot data vars WAGE AGE EDUCATION kind reg hue SEX plt suptitle Effect of gender 1 Female O Male
397. ts that Cython inserts What do you see 2 Look at the section Working with Numpy from the Cython documentation to learn how to incrementally optimize a pure python script that uses Numpy 3 Modify the Numpy example such that cos_doubles_func handles the preallocation for you thus mak ing it more like the Numpy C API example 2 8 Interfacing with C 270 CHAPTER 3 Packages and applications This part of the Scipy lecture notes is dedicated to various scientific packages useful for extended needs 3 1 Statistics in Python Author Ga l Varoquaux Requirements e Standard scientific Python environment numpy scipy matplotlib e Pandas e Statsmodels e Seaborn To install Python and these dependencies we recommend that you download Anaconda Python or Enthought Canopy or preferably use the package manager if you are under Ubuntu or other linux See also Bayesian statistics in Python This chapter does not cover tools for Bayesian statistics Of particular interest for Bayesian modelling is PyMC which implements a probabilistic programming language in Python Tip Why Python for statistics R is a language dedicated to statistics Python is a general purpose language with statistics modules R has more statistical analysis features than Python and specialized syntaxes However when it comes to building complex analysis pipelines that mix statistics with e g image analysis text mining or control of a physical exper
398. ugger 3 Call the debugger inside the module Postmortem Situation You re working in Python and you get a traceback Here we debug the file index_error py When running it an IndexError is raised Type debug and drop into the debugger In 1 run index_error py IndexError Traceback most recent call last home varogquau dev scipy lecture notes advanced debugging_optimizing index_error py in lt module gt 6 7 if _ name main __ gt B index_error 9 home varoquau dev scipy lecture notes advanced debugging_optimizing index_error py in index_erro 3 def index _error 4 let list foobar gt 5 print lst len 1st 6 7 if _name_ _main_ IndexError list index out of range In 2 debug gt home varoquau dev scipy lecture notes advanced debugging_optimizing index_error py 5 index_err 4 last list fioobar ea rae Pring Wise ten ise 6 iodo List HUMSmall ismippet to raise an IndexError Wi def index _error lst list Toobar print iste ien ISE gt f iit name main ig index _error We Co H Gil USS Ge I ipdb gt len lst IpabLb prine Tise lem sic i 2 3 Debugging code 191 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 a ipdb gt quit To S Post mortem debugging without Python In some situations
399. ully legible It seeks to answer a problem of actual interest for numerical computing but it does not work Can you debug it Python source code to_debug py 2 4 Optimizing code Donald Knuth Premature optimization is the root of all evil Author Ga l Varoquaux This chapter deals with strategies to make Python code go faster Prerequisites e line_profiler Chapters contents e Optimization workflow page 197 e Profiling Python code page 198 Timeit page 198 Profiler page 198 Line profiler page 200 e Making code go faster page 200 Algorithmic optimization page 200 Example of the SVD page 200 e Writing faster numerical code page 201 Additional Links page 203 2 4 1 Optimization workflow 1 Make it work write the code in a simple legible ways 2 Make it work reliably write automated test cases make really sure that your algorithm is right and that if you break it the tests will capture the breakage 2 4 Optimizing code 197 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 3 Optimize the code by profiling simple use cases to find the bottlenecks and speeding up these bottleneck finding a better algorithm or implementation Keep in mind that a trade off should be found between profiling on a realistic example and the simplicity and speed of execution of the code For efficient work it is best to work with pr
400. un pyflakes in the current buffer e In kate Menu settings gt configure kate In plugins enable external tools In external Tools add pyflakes kdialog title pyflakes filename msgbox S pyflakes filename In TextMate Menu TextMate gt Preferences gt Advanced gt Shell variables add a shell variable TM_PYCHECKER Library Frameworks Python framework Versions Current bin pyflakes Then Ctrl sShift V is binded to a pyflakes report 2 3 Debugging code 188 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 e In vim In your vimrc binds F5 to pyflakes autocmd FileType python let amp mp echo xxx running xxx pyflakes autocmd FileType tex mp rst python imap lt Esc gt 15 lt C O gt make M autocmd FileType tex mp rst python map lt Esc gt 15 make M autocmd FileType tex mp rst python set autowrite e In emacs In your emacs binds F5 to pyflakes defun pyflakes thisfile interactive compile format pyflakes s buffer file name define minor mode pyflakes mode Toggle pyflakes mode With no argument this command toggles the mode Non null prefix argument turns on the mode Null prefix argument turns off the mode The initial value nil The indicator for the mode line Pyflakes The minor mode bindings 5 pyflakes thisfile add hook python mode hook lambda pyflakes
401. unction and an offset corresponding to the e Try with a more complex waveform for instance data waveform_2 npy that contains three signif background noise To fit the signal with the function we must icant peaks You must adapt the model which is now a sum of Gaussian functions instead of only one e define the model Gaussian peak e propose an initial solution e call scipy optimize leastsgq 25 Model A Gaussian function defined by t u B aes 20 o can be defined in python by gt gt gt def model t coeffs 15 return coeffs 0 coeffs 1 np exp t coeffs 2 coeffs 3 2 5 E where T ab coeffs 0 is B noise 10 e coeffs 1 is A amplitude e coeffs 2 is u center e coeffs 3 iso width 5 Initial solution An approximative initial solution that we can find from looking at the graph is for instance gt gt gt xO np array 3 30 15 1 dtype float 10 20 30 40 50 60 70 80 Time ns Fit scipy optimize leastsgq minimizes the sum of squares of the function given as an argument Basi cally the function to minimize is the residuals the difference between the data and the model E o l a e In some cases writing an explicit function to compute the Jacobian is faster than letting least sq esti mate it numerically Create a function to compute the Jacobian of the residuals and use it as an input for leastsq gt gt gt def residuals coeffs y t return y m
402. urface mesh given by x y z positions of its node points nikelo Er phip theta ME mo midion pus li ORe ea ial xX np sin phi np cos theta y np sin phi np sin theta Z np cos phi mlab mesh x y zZ mlab mesh x y Zz representation wireframe color 0 0 0 Note A surface is defined by points connected to form triangles or polygones In mayavi mlab surf and mayavi mlab mesh the connectivity is implicity given by the layout of the arrays See also mayavi mlab triangular_mesh Our data is often more than points and values it needs some connectivity information 3 5 3D plotting with Mayavi 359 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Volumetric data contour3d _ liza Hint If your data is dense in 3D it is more difficult to display One option is to take iso contours of the data milab elg Xe Yr A NP mgr idh 5 5 64 5 964 S5 6A values x x 0 5 yxy 2 z 2 0 mlab contour3d values This function works with a regular orthogonal grid the value array is a 3D array that gives the shape of the grid Figures and decorations Figure management Tip Here is a list of functions useful to control the current figure Get the current figure mlab gcf Clear the current figure mlab clf Set the current figure mlab figure 1 bgcolor 1 1 1 fgcolor 0 5 0 5 0 5 Save figure to image
403. uring the fit of an estimator on a parameter grid and chooses the parameters to maximize the cross validation score This object takes an estimator during the construction and exposes an estimator API gt gt gt from sklearn import svm grid_search gt gt gt gammas np logspace 6 1 10 gt gt gt svc svm SVC gt gt gt clf grid_search GridSearchCV estimator svc param_grid dict gamma gammas n_jobs 1 gt gt gt Clm mae digits datal OO ON Fa daicahtse targe L000 GridSearchCV cv None gt gt gt clf best_score_ Ooa gt gt gt clf best_estimator_ gamma 0 00059948425031894088 By default the GridSearchCV uses a 3 fold cross validation However if it detects that a classifier is passed rather than a regressor it uses a stratified 3 fold Cross validated estimators Cross validation to set a parameter can be done more efficiently on an algorithm by algorithm basis This is why for certain estimators the scikit learn exposes CV estimators that set their parameter automatically by cross validation gt gt gt from sklearn import linear_model datasets gt gt gt lasso linear_model LassoCyV gt gt gt diabetes datasets load_diabetes gt gt gt X_ diabetes diabetes data gt gt gt y_diabetes diabetes target gt gt gt lasso fit X_diabetes y_diabetes LassoCV alphas None gt gt gt The estimator chose automatically its lambda gt gt gt la
404. urther Reading and References e Ga l Varoquaux s blog post about avoiding data copies provides some insight on how to handle memory management cleverly If you ever run into issues with large datasets this is a reference to come back to for some inspiration 2 8 8 Exercises Since this is a brand new section the exercises are considered more as pointers as to what to look at next so pick the ones that you find more interesting If you have good ideas for exercises please let us know 1 Download the source code for each example and compile and run them on your machine 2 Make trivial changes to each example and convince yourself that this works E g change cos for sin 3 Most of the examples especially the ones involving Numpy may still be fragile and respond badly to input errors Look for ways to crash the examples figure what the problem is and devise a potential solution Here are some ideas a Numerical overflow b Input and output arrays that have different lengths c Multidimensional array 2 8 Interfacing with C 269 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 setup py d Empty array e Arrays with non doub1e types 4 Use the st imeit Python magic to measure the execution time of the various solutions Python C API 1 Modify the Numpy example such that the function takes two input arguments where the second is the preallocated output array making it similar to the other Nu
405. using a multitude of flags to___ init__ e staticmethod is applied to methods to make them static i e basically a normal function but acces sible through the class namespace This can be useful when the function is only needed inside this class its 2 1 Advanced Python Constructs 148 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 name would then be prefixed with _ or when we want the user to think of the method as connected to the class despite an implementation which doesn t require this property is the pythonic answer to the problem of getters and setters A method decorated with property becomes a getter which is automatically called on attribute access gt gt gt class A object property def a self Wan Important attribute return a value gt gt gt A a property cbject at Uxa gt gt gt gt A a a value In this example A a is an read only attribute It is also documented help A includes the docstring for attribute a taken from the getter method Defining a as a property allows it to be a calculated on the fly and has the side effect of making it read only because no setter is defined To have a setter and a getter two methods are required obviously Since Python 2 6 the following syntax is preferred class Rectangle object def __init_ self edge self edge edge property def area self Computed area Setting this updates the e
406. ut we will discuss this in the image section 1 5 Scipy high level scientific computing 119 1 5 Scipy high level scientific computing 120 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 1 5 10 Image processing scipy ndimage The submodule dedicated to image processing in scipy is scipy ndimage gt gt gt from scipy import ndimage Image processing routines may be sorted according to the category of processing they perform Geometrical transformations on images Changing orientation resolution gt gt gt from scipy import misc gt gt gt lena misc lena gt gt gt shifted_lena ndimage shift lena 50 50 gt gt gt shifted_lena2 ndimage shift lena 50 50 mode nearest gt gt gt rotated_lena ndimage rotate lena 30 gt gt gt cropped_lena lena 50 50 50 50 gt gt gt zoomed_lena ndimage zoom lena 2 gt gt gt zoomed_lena shape OZA NOAA gt gt gt pit r subpko rE lt matplotiibrkaxes subplots AxesSubplot opject at Okinn gt gt gt plt imshow shifted_lena cmap plt cm gray lt matplotlib image AxesImage object at Ox gt a gt Plte axis COFEE 055 SHTS SLS 05 gt gt A crc Image filtering Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 noisy lena Gaussian filter Wiener filter PRS E 2 a ieee tee Mathematical morphology Mathematical morphology is a
407. ve a number of useful methods Explore them by reading the docstring or by using IPython tab completion Can you find the shape parameter of 1 back by using the fit method on your random variates Percentiles The median is the value with half of the observations below and half above gt gt gt np median a 0 0410117693932 25 It is also called the percentile 50 because 50 of the observation are below it 1 5 Scipy high level scientific computing 115 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt stats scoreatpercentile a 50 O 040417 6959 Similarly we can calculate the percentile 90 gt gt gt stats scoreatpercentile a 90 LSS S699 2 Oke The percentile is an estimator of the CDF cumulative distribution function Statistical tests A Statistical test is a decision indicator For instance if we have two sets of observations that we assume are gen erated from Gaussian processes we can use a T test to decide whether the two sets of observations are significantly different gt gt gt a np random normal 0O 1 size 100 gt gt gt b np random normal l 1 size 10 gt gt gt stats ttest_ind a b Garray S2l7 7574054 5 7 U2 00193 T0639 Tip The resulting output is composed of e The T statistic value it is a number the sign of which is proportional to the difference between the two random processes and the magnitude is related to
408. ven the initial storage wo reservoir Instance Reservoir min_storage Float max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storage Range low min_storage high max_storage CONELrOL aberibuces inflows Float desc Inflows hm3 release Range low min_release high max_release spillage Float desc Spillage hm3 def print_state self print Storage tRelease tInflows tSpillage Strurormerc Ve join i 137 26 fer i in range 4 print str_format format self storage self release self inflows self spillage prine x 79 if name main uy projectA Reservoir name Project A max_storage 30 max_release 100 0 hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 10 state release 90 state inflows 0 state print_state print How do we update the current storage 3 4 Traits building interactive dialogs 345 from traits api import HasTraits Instance DelegatesTo Float Range Event from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage For the simplicity of the example the release is considered in hm3 timestep and not in m3 s woe reservoir Instance Reservoir min_storage Float max_storage DelegatesTo reservoir Float DelegatesT
409. xception into the generator The first of the new methods is send value whichis similar to next but passes value into the generator to be used for the value of the yield expression In fact g next and g send None are equivalent 2 1 Advanced Python Constructs 142 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 The second of the new methods is throw type value None traceback None which is equivalent to raise type value traceback at the point of the yield statement Unlike raise which immediately raises an exception from the current execution point throw first resumes the generator and only then raises the exception The word throw was picked because it is suggestive of putting the exception in another location and is associated with exceptions in other languages What happens when an exception is raised inside the generator It can be either raised explicitly or when executing some statements or it can be injected at the point of a yield statement by means of the throw method In either case such an exception propagates in the standard manner it can be intercepted by an except or finally clause or otherwise it causes the execution of the generator function to be aborted and propagates in the caller For completeness sake it s worth mentioning that generator iterators also have a close method which can be used to force a generator that would otherwise be able to provide mo
410. y In 3 np seterr all raise ouelo Vivilde Ys print inya lirdi prime semen tprint emcee Sicinence In 4 run wiener_filtering py FloatingPointError Traceback most recent call last hnome esc anaconda lib python2 7 site packages IPython utils py3compat pyc in execf fname xw LYE else Lay filename fname gt 178 _builtin__ execfile filename where ering py 11 home esc physique cuso python 2013 scipy lecture notes advanced debugging wiener_fi 55 pl matshow noisy_lena cut cmap pil em gray 56 gt 57 denoised_lena iterated_wiener noisy_lena 58 pl matshow denoised_lena cut cmap pl cm gray 59 home esc physique cuso python 2013 scipy lecture notes advanced debugging wiener_filffering py ii Sis res noisy_img denoised_img 39 Mouse rss177 sunl res SLz2E ES s 40 nose level 1 movse va 41 noise level morse level lt 0 0 42 denoised_img noise_levelxres FloatingPointError divide by zero encountered in divide Other ways of starting a debugger e Raising an exception as a poor man break point If you find it tedious to note the line number to set a break point you can simply raise an exception at the point that you want to inspect and use IPython s debug Note that in this case you cannot step or continue the execution e Debugging test failures using nosetests You can run nosetests
411. y 0 2 27 OW 1 Al gt gt gt data n p array Il 2 3 47 5 Gl gt gt gt mtx sparse csc_matrix data row col shape 3 3 gt gt gt mtx lt 43 x gt Sparse Matrix Of Type lt i2 numpy intvod gt with 6 stored elements in Compressed Sparse Column format gt gt gt gt mtx todense matca i iils OO 2 LO Of Si 4 Sp 6 gt gt gt mtx data arkay GMP Ur Spe 2 br lls 6 3 gt gt gt mtx indices array TO 2 27 0 a aea dtype int32 gt gt gt mtx indptr array OF 27 gt 6i atype Int 32 e create using data indices indptr tuple gt gt gt data np array 1 AS 2 a E gt gt gt indices np array 0 2 2 0 1 2 gt gt gt nd oere nprarrcay 07a 27S oll gt gt gt mtx sparse csc_matrix data indices indptr shape 3 3 gt gt gt mtx todense meicies lt lip WO 2 Oe Of Sip lr 9 oO Block Compressed Row Format BSR e basically a CSR with dense sub matrices of fixed shape instead of scalar items block size R C must evenly divide the shape of the matrix M N three NumPy arrays indices indptr data indices is array of column indices for each block data is array of corresponding nonzero values of shape nnz R C x subclass of _cs_matrix common CSR CSC functionality subclass of _data_matrix sparse matrix classes with data attribute e fast matrix vector products and other arithmetics sparsetools e constructor accepts
412. y l 27 oe 0 eS ee ea TO ee 2 e e gt gt gt optimize fmin_ncg f 2 2 fprime fprime Optimization terminated successfully Current function value 0 000000 LeSsracioass Function evaluations 11 Gradient evaluations 51 Hessian evaluations 0 lee In Note that compared to a conjugate gradient above Newton s method has required less function evaluations but more gradient evaluations as it uses it to approximate the Hessian Let s compute the Hessian and pass it to the algorithm gt gt gt def hessian x Computed with sympy oe return npsarray Gl 4ex le l2ex Ol se2 Sse Ol 4ex 10 23 gt gt gt optimize fmin_ncg f 2 2 fprime fprime fhess hessian Optimization terminated successfully Current function value 0 000000 Iterations 9 Function evaluations 11 Gradient evaluations 19 Hessian evaluations 9 airway i le ETA Note At very high dimension the inversion of the Hessian can be costly and unstable large scale gt 250 Note Newton optimizers should not to be confused with Newton s root finding method based on the same principles scipy optimize newton 2 7 Mathematical optimization finding minima of functions 245 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Quasi Newton methods approximating the Hessian on the fly BFGS BFGS Broyden Fletcher Goldfarb Shanno algorithm refines at each step an approximation
413. y special gammaln which will give the log of Gamma to a higher numerical precision e Erf the area under a Gaussian curve scipy special erf 1 5 3 Linear algebra operations scipy linalg The scipy 1linalg module provides standard linear algebra operations relying on an underlying efficient implementation BLAS LAPACK e The scipy linalg det function computes the determinant of a square matrix gt gt gt from scipy import linalg gt gt gt arr np array 1 2 oe 3 4 1 gt gt gt linalg det arr 2 0 gt gt gt arr np array 3 2 1 E bey 19 gt gt gt linalg det arr 0 0 gt gt gt linalg det np ones 3 4 Traceback most recent call last ValueError expected square matrix Traceback most recent call last ValueError expected square matrix The scipy linalg inv function computes the inverse of a square matrix gt gt arr np array IIl 2 ae l3 all gt gt gt iarr linalg inv arr gt gt gt jarr aray I2 AAF EE D gt leo OS gt gt gt np allclose np dot arr iarr np eye 2 True Finally computing the inverse of a singular matrix its determinant is zero will raise LinAlgError see arn Mpcerray 13 21 e exe 6 4 gt gt gt linalg inv arr Traceback most recent call last vss NALOEE ror singular matrix Traceback most recent call last 2a RINA LGETror singular matrix e More advanced operations are available for
414. yntax extension to allow continue to take an argument which will be passed to send of the loop s iterator If this exten sion is accepted it s likely that gen send will become gen __send__ The last of generator methods close is pretty obviously named incorrectly because it is already invoked implicitly Chaining generators Note This is a preview of PEP 380 not yet implemented but accepted for Python 3 3 Let s say we are writing a generator and we want to yield a number of values generated by a second generator a subgenerator If yielding of values is the only concern this can be performed without much difficulty using a loop such as subgen some_other_generator for v in subgen yield v However if the subgenerator is to interact properly with the caller in the case of calls to send throw and close things become considerably more difficult The yield statement has to be guarded by a try except finally structure similar to the one defined in the previous section to debug the generator function Such code is provided in PEP 380 here it suffices to say that new syntax to properly yield from a subgenerator is being introduced in Python 3 3 yield from some_other_generator This behaves like the explicit loop above repeatedly yielding values from some_other_generator until it is exhausted but also forwards send throw and close to the subgenerator 2 1 2 Decorators Summary This amazing
415. ython 2 gt gt gt D gt I In Python 3 gt gt gt 0 gt Io To be safe use floats 2 gt gt 3 2 mS gt gt gt la 3 gt gt gt 2 B A rn Python 2 oO all gt gt gt a gt gt gt a float b 15 Future behavior to always get the behavior of Python3 gt gt gt from _ future_ import division S gt gt 3 2 i 5 Tip If you explicitly want integer division use e 50 77 2 1O Note The behaviour of the division operator has changed in Python 3 Containers Tip Python provides many efficient types of containers in which collections of objects can be stored Lists Tip A list is an ordered collection of objects that may have different types For example gt gt gt 1 red blue green black white gt gt gt type tl lt type list gt Indexing accessing individual objects contained in the list gt gt gt 1 2 green Counting from the end with negative indices 1 2 The Python language 12 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 gt gt gt ah Sil white gt gt gt 1 2 kacky Warning Indexing starts at 0 as in C not at 1 as in Fortran or Matlab Slicing obtaining sublists of regularly spaced elements gt gt gt 1 red blue green black white gt gt gt I es al Loreen 7 Vbolkack Warning Note that 1 start stop contains the elements with in
416. ython Scientific lecture notes Release 2013 2 beta euroscipy 2013 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 1 60e 16 The script recording button poar mesh To find out what code can be used to program these changes click on the red button as you modify those properties and it will generate the corresponding lines of code 0 694 1 00 T 3 5 3 Slicing and dicing data sources modules and filters 0 546 An example inspecting magnetic fields 0 399 Z Suppose we are simulating the magnetic field generated by Helmholtz coils The 0 251 examples compute_field py script does this computation and gives you a B array that is 3 x n where the first axis is the direction of the field Bx By Bz and the second axis the index number of the point a Arrays X Y and Z give the positions of these data points 1 900 TN 0 0442 aa S Excercise 0 192 N N Warning Visualize this field Your goal is to make sure that the simulation code is correct extent If we specified extents for a plotting object mlab outline don t get them by default and mlab axes 3 5 2 Interactive work Tip The quickest way to create beautiful visualization with Mayavi is probably to interactively tweak the various settings The pipeline dialog Click on the Mayavi button in the scene and you can control properties of objects with dialogs L Mayavi pipeline EEE FI OO G Mayav
417. ython org e Either wxPython http www wxpython org PyQt https riverbankcomputing com software pyqt intro or PySide https pyside github i0 docs pyside e Numpy and Scipy http www scipy org e Enthought Tool Suite 3 x or higher http code enthought com projects e All required software can be obtained by installing the EPD Free https store enthought com Tutorial content e Introduction page 339 e Example page 340 e What are Traits page 341 Initialisation page 342 Validation page 342 Documentation page 343 Visualization opening a dialog page 343 Deferral page 345 Notification page 350 Some more advanced traits page 353 3 4 1 Introduction Tip The Enthought Tool Suite enable the construction of sophisticated application frameworks for data analysis 2D plotting and 3D visualization These powerful reusable components are released under liberal BSD style licenses 3 4 Traits building interactive dialogs 339 Python Scientific lecture notes Release 2013 2 beta euroscipy 2013 Mayavi TVTK Traits VTK Chaco Enable Kiva th edb l ij Ai The main packages of the Enthought Tool Suite are e Traits component based approach to build our applications e Kiva 2D primitives supporting path based rendering affine transforms alpha blending and more e Enable object based 2D drawing canvas e Chaco plotting toolkit for building complex inte
Download Pdf Manuals
Related Search
Related Contents
HiT 単線かける君 取扱説明書 - エー・アンド・デイ Altronix OLS180 power supply unit 取扱説明書(PDF)はこちら MANUAL DE USUARIO OPTIMA RT9 1K-3K - xmart-ups InLine USB A/Micro USB B, 1 m Tucano USB Travel Format PDF - 4442Ko - Pavillon de l`Arsenal User's Guide - Simvolt.com.ua Home Decorators Collection DC5236ABZ Installation Guide Copyright © All rights reserved.
Failed to retrieve file