python for ml - machine learningcs229.stanford.edu/notes-spring2019/python_for_cs229.pdf · running...
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/6.jpg)
(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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/37.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/38.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/39.jpg)
![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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/40.jpg)
![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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/41.jpg)
![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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/42.jpg)
![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](https://reader033.vdocuments.net/reader033/viewer/2022060313/5f0b42ec7e708231d42fa467/html5/thumbnails/43.jpg)
(Optional)