Plotting & Visualization Maryam Tavakol Machine Learning Group Winter semester 2016/17 1
Motivation • Making plots and static or interactive visualizations is one of the most important tasks in data analysis • Helping identify outliers • Needed data transformations • Coming up with ideas for models • etc. 2
matplotlib • Desktop plotting package designed for creating publication-quality plots • Does have interactive features like zooming and panning • Supports many different GUI backends on all operating systems • Can export graphics to all of the common vector and raster graphics formats 3
Figures • Plots in matplotlib reside within a Figure object • Creates a new empty window 4
Subplots 5
Subplot Options 6
Plotting 7
Formatting • Line style, color, and marker 8
Formatting 9
Titles & Labels 10
Adding Legends 11
Example 12
Annotations 13
Annotations • Annotations and text can be added using the text, arrow, and annotate functions 14
Adding Shapes 15
Saving Options 16
Histogram 17
Histogram 18
Box Plot 19
Box Plot 20
Profiling & Optimization Maryam Tavakol Machine Learning Group Winter semester 2016/17 21
Introduction • Python is generally known (a bit unfairly) as a slow language, but it is possible to achieve very good performance with the right methods • Profile : To evaluate what makes a program slow • Optimize : To make the code more efficient 22
%timeit • The %timeit allow you to quickly evaluate the time taken by one or several Python statements in IPython 23
%timeit • The %timeit command accepts several optional parameters, e.g.: • Using -r or - n to set number of statement evaluations • Inside your script: timeit.timeit() 24
cProfile • Creates a detailed report of the execution time of our code, function by function • The total number of calls, total time, etc. 25
line_profiler • Analyzing of code performance by a line-by-line report • Need the package kernprof • We decorate the functions we wish to profile with @profile 26
Memory Usage • Memory is also a critical factor • Using memory_profiler • We decorate the functions we wish to profile with @profile 27
Optimization Tips • Choosing the right data structure • Using NumPy over native Python code • Avoiding unnecessary array copying • Using in-place operations instead of implicit copy • Parallel computing and much more… 28
Recommend
More recommend