Settingup your Python Environment From the MOOC Python

  • Slides: 16
Download presentation
Setting-up your Python Environment From the MOOC: Python 3 : des fondamentaux concepts avancés

Setting-up your Python Environment From the MOOC: Python 3 : des fondamentaux concepts avancés du langage, http: //bit. ly/2 PLhkr. L

Python for Data Science • Python for Data Science is a mix of various

Python for Data Science • Python for Data Science is a mix of various packages to install • Some of them (e. g. , numpy or matplotlib) cannot be installed with pip and can be very complex to install by hand • For numpy you need to compile C and Fortran 77 code! • Fortunately, there is conda 2

Conda • Conda is • A package manager that can install any Python package

Conda • Conda is • A package manager that can install any Python package seamlessly • A virtual environment manager • Cross platform (windows, OS X, Linux) • You can install it in two ways • Install Anaconda that is Python, conda, and 100+ packages • It can be very large and come with more than you need • Install Miniconda that is Python and conda only 3

Installing Conda • Go to • https: //conda. io/miniconda. html • Select the Python

Installing Conda • Go to • https: //conda. io/miniconda. html • Select the Python 3 (64 bits) installer • Make sure conda is in the latest version >conda update conda • Create a virtual environment named, e. g. , homl >conda create -n homl • Activate the environment >activate homl (Windows) >source activate homl (OS X, Linux) 4

Installing Conda • Deactivate the environment >deactivate homl (Windows) >source deactivate homl (OS X,

Installing Conda • Deactivate the environment >deactivate homl (Windows) >source deactivate homl (OS X, Linux) 5

Installing Conda • Install packages we need >activate homl (homl) > conda install python=3.

Installing Conda • Install packages we need >activate homl (homl) > conda install python=3. 7. 1 jupyter matplotlib numpy pandas scipy scikit-learn Or directly create the environment with all packages >conda create -n homl python=3. 7. 1 jupyter matplotlib numpy pandas scipy scikit-learn You are done! More information on conda https: //conda. io/docs/using/cheatsheet. html https: //conda. io/docs/get-started. html 6

i. Python • This is the regular CPython interpreter + some really cool command

i. Python • This is the regular CPython interpreter + some really cool command line options • Everybody is using i. Python because it is very convenient • Let’s start it (py 3) >ipython 7

>ipython Python 3. 6. 0 |Continuum Analytics, Inc. | (default, Dec 23 2016, 11:

>ipython Python 3. 6. 0 |Continuum Analytics, Inc. | (default, Dec 23 2016, 11: 57: 41) [MSC v. 1900 64 bit (AMD 64)] Type "copyright", "credits" or "license" for more information. IPython 5. 3. 0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object? ? ' for extra details. 8

Integrated Help • You can access help on any object by putting a ?

Integrated Help • You can access help on any object by putting a ? before our after the object name • Way more convenient that using help(obj) In [5]: str. split? … In [6]: ? str. title … • Just hit TAB and i. Python with propose code completion 9

Command History • i. Python keeps the command history even between different sessions •

Command History • i. Python keeps the command history even between different sessions • Try Crtl-R for reverse-search through command history 10

Run Shell Commands • You can run shell commands from the i. Python prompt

Run Shell Commands • You can run shell commands from the i. Python prompt by prepending ! In [1]: !ls ISL. pptx Scratch. Pad. ipynb '~$ISL. pptx' In [2]: !pwd /cygdrive/c/Backup/Edu/Data. Science/Cours. Data. Science 11

Magic Functions • All commands that start with % or %% are magic functions

Magic Functions • All commands that start with % or %% are magic functions • You know them all with In [10]: %magic 12

Advanced Paste Support with %paste • The biggest problem with Python is that cut

Advanced Paste Support with %paste • The biggest problem with Python is that cut and paste from various sources usually fails in a command line • i. Python has the magic command %paste that automatically reformat the code so that cut and paste just work • Copy text from anywhere, and write at the i. Python prompt %paste • It really looks magic, just try it! • Also, see %cpaste for multiple cut and paste 13

Run Python Script %run • To run a python script in the i. Python

Run Python Script %run • To run a python script in the i. Python interpreter and keep all script variables in the local interpreter In [1]: !cat test. py print("Hello! in test. py") x = "abcde 123" In [3]: %run test. py Hello! in test. py In [4]: x Out[4]: 'abcde 123' 14

Test Execution Time with %timeit • To test the execution time of an expression

Test Execution Time with %timeit • To test the execution time of an expression and automatically adapt the number of runs to have a meaningful runtime In [8]: %timeit list(range(10000)). sort() 1000 loops, best of 3: 386 µs per loop In [9]: %timeit list(range(100000)). sort() 100 loops, best of 3: 4. 36 ms per loop In [10]: %timeit list(range(10000000)). sort() 1 loop, best of 3: 483 ms per loop 15

Start a notebook • Run in your command prompt > jupyter notebook 16

Start a notebook • Run in your command prompt > jupyter notebook 16