Top Banner
Python for ML How to be the most efficient Abraham Starosta
43

Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Jun 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Python for MLHow to be the most efficient

Abraham Starosta

Page 2: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Topics:1. Running your code (3 options)2. Numpy3. Python Foundations

a. Data Structuresb. Functions

4. Matplotlib5. Python Classes

Page 3: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Running Python File on Command lineFor example: ‘python hellow_world.py’

● The file needs to be executing a function

Page 4: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Running on Pycharm IDE

1. Debugger2. Move around quickly3. Profile code performance

Page 5: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Pycharm InstallationInstallation:

1. https://www.jetbrains.com/pycharm/download/#section=mac

2. Get Community (free)

3. For Professional, you can get a free student license:a. https://www.jetbrains.com/student/

Page 6: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

(Optional) Create conda python environment● Create environment

○ Install conda: https://www.anaconda.com/distribution/

○ ‘conda create --name python3’

● Activate environment:○ ‘source activate python3’

Page 7: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Pycharm setup local environment

Page 8: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Pycharm Main Hotkeys to Know● Cmd + b: jump into function● Cmd + [ : go forward (kind of like browsers)● Cmd + ] : go backward

Page 9: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Jupyter Notebooks ● Notebooks are usually for experimentation● https://jupyter.readthedocs.io/en/latest/install.html

● Run command ‘jupyter notebook’

Page 10: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Debugging with Pdb● ‘import pdb; pdb.set_trace()’ to stop the program and break into the debugger● https://realpython.com/python-debugging-pdb/

Page 11: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Numpy● General advice: do not just transpose matrices until dimensions match for a

matrix multiplication, it will cause you more headaches than learning matrix multiplication :)

● Check matrix shapes using matrix.shape

● Using vectors of shape (n,) can cause errors. It’s better to reshape them to (n,1) or (1,n)

Page 12: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Numpy Arrays

Page 13: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Array Initialization

Page 14: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Array Indexing

Page 15: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Broadcasting● Replaces loops● Makes your code much much faster● Can cause hidden errors that are hard to debug (be careful at the beginning)

Page 16: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Broadcasting

Page 17: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Broadcasting

Page 18: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Broadcasting

X = np.array([1, 2, 3, 4])X + 1>> array([2, 3, 4, 5])

Page 19: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Broadcasting Error (remember to check shapes!)

Page 20: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Other Numpy Commands● np.reshape● np.multiply● np.random● np.linalg (norms, etc)

Page 21: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Python Foundations

Page 22: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Mathematical Operations

Page 23: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Logical Operations

Page 24: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Functions

Page 25: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Lists

Page 26: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

List Comprehensions

Page 27: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Dictionaries

Page 28: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Sets

Page 29: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Tuples

Page 30: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

MatplotlibFull tutorial:

https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py

Page 31: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Matplotlib Function Plot

Page 32: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Matplotlib Function Plot

Page 33: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Matplotlib Scatter Plot

Page 34: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Histograms

Page 35: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Matplotlib Subplots

Page 36: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Precision-Recall Curves

Page 37: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Other Libraries for Plotting (optional)● Seaborn● Plotly

Page 38: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

Python Classes

Page 39: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance
Page 40: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance
Page 41: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance
Page 42: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance
Page 43: Python for ML - Machine Learningcs229.stanford.edu/notes-spring2019/Python_for_CS229.pdf · Running on Pycharm IDE 1. Debugger 2. Move around quickly 3. Profile code performance

(Optional)